Single module modding

This commit is contained in:
2026-11-14 12:18:41 -03:00
parent 605008f584
commit 313098892e
134 changed files with 919 additions and 924 deletions
+49 -44
View File
@@ -24,11 +24,11 @@ A mod is built from up to three layers. Most mods only need the first one or two
## Status
**Built:** the mod loader and `.cubi` files (packing, loading from `server_mods/`, sending players a copy without the
server and worldgen scripts), delivery to clients (hashed downloads, the confirm screen for other sites, credits), the
terrain's data and the config), delivery to clients (hashed downloads, the confirm screen for other sites, credits), the
data layer (blocks, block models with variants, items, recipes including mods' own types, entities and entity models,
ores), server scripts (components, events, commands, timers, storage, recipes, containers, displayed items), menus like
NeoForge's (menu types, slots, data, buttons and `still_valid` on the server, menu screens on the client, and the
player's inventory as menu 0, which mods can add slots to), client scripts (item and recipe lookups, plain screens,
ores), the server phase (components, events, commands, timers, storage, recipes, containers, displayed items), menus
like NeoForge's (menu types, slots, data, buttons and `still_valid` on the server, menu screens on the client, and the
player's inventory as menu 0, which mods can add slots to), the client phase (item and recipe lookups, plain screens,
inventory overlays, `Graphics` in GUI pixels), mod channels, worldgen features and ores, the base game's block behavior
and screens as a mod, world time, and worn items (an item's `worn` model, `player.set_worn`, synced to every client and
drawn on other players and on the first person arm). See [Menus](modding/inventories/menus.md),
@@ -41,12 +41,17 @@ The code is in `common/mod_loader.ts`, `common/mod_data.ts`, `common/mod_check.t
**In the API types but throwing:** forms, keybinds, `g.slider`, `register_terrain`.
**Content scripts** are built, the first part of common scripts: `scripts.content` with `ctx.blocks.register`, see
[Content Scripts](#content-scripts-as-built). The code is in `common/content_scripts.ts`, `common/mod_api/content.ts`,
`server/content_worker.ts`, `server/load_mods.ts` and `tools/check_mods.ts`.
**One script per mod** is built: the manifest's `main` names it, and its exports are its phases (`content`, `shared`,
`server`, `client`, `worldgen`), like NeoForge's one jar with setup events by phase and by dist. It's bundled into one
module that goes everywhere, players included, and each side runs only its own phases through `run_phase` in
`common/mod_loader.ts`. See [Sides](modding/concepts/sides.md).
**Not built at all:** the rest of common scripts (items and models from code, reading other mods' content), the registry
freeze and hash, overrides.
**The content phase** is built, the first part of the common phase: `content` with `ctx.blocks.register`, see
[The Content Phase](#the-content-phase-as-built). The code is in `common/content_scripts.ts`,
`common/mod_api/content.ts`, `server/content_worker.ts`, `server/load_mods.ts` and `tools/check_mods.ts`.
**Not built at all:** the rest of the common phase (items and models from code, reading other mods' content), the
registry freeze and hash, overrides.
**Engine access** is built (phase 3): `cubi/engine`, `hook`, `engine_access` and its checks, and `physics` as the first
plain object mods can hook. See [Engine Access](modding/misc/engine.md). The code is in `common/hooks.ts`,
@@ -66,37 +71,37 @@ plain object mods can hook. See [Engine Access](modding/misc/engine.md). The cod
## Planned API
### Common Scripts
### The Common Phase
A fourth script, `scripts.common`, run on the server and on every client while content loads, right after the mod's own
JSON is registered. Data would then be shorthand for code: loading `blocks/copper_block.json` would do exactly what
A phase, `common`, run on the server and on every client while content loads, right after the mod's own JSON is
registered. Data would then be shorthand for code: loading `blocks/copper_block.json` would do exactly what
`ctx.blocks.register(...)` with the same object does, so content that's easier to generate (every color of wool) or
depends on other mods can be written as code.
#### Content Scripts, as Built
#### The Content Phase, as Built
The first part is built differently from the rest of this section: `scripts.content` with only `ctx.blocks.register`,
run **once, on the server**, while it loads mods, instead of on both sides. What it registers is added to the mod's data
as if it were files, so everything downstream needs no change:
The first part is built differently from the rest of this section: `content` with only `ctx.blocks.register`, run
**once, on the server**, while it loads mods, instead of on both sides. What it registers is added to the mod's data as
if it were files, so everything downstream needs no change:
- `check-mods` runs it from the mod's folder before checking references, so tags, recipes and worldgen files can name
its blocks. Packing leaves its blocks out of `data.json`.
- The server runs it on each `.cubi` before its reference check, in a worker without permissions, since the host that
loads mods has file access.
- Players get the blocks in the copy of `data.json` they download and never run the script, like Luanti's clients get
- Players get the blocks in the copy of `data.json` they download and never run the phase, like Luanti's clients get
node definitions from the server. The server and its players can't disagree, so this part needs no freeze or hash.
- Worldgen workers get the blocks in the worldgen bundle, which is built from the mods' data, and number them the same
way, like Luanti's mapgen threads get a copy of the finished registrations instead of running mods' code again.
Since it runs per mod, before any registry exists, it can't read other mods' content or the server's config yet. Reading
other mods' content would mean running content scripts in load order with a view of what came before.
other mods' content would mean running content phases in load order with a view of what came before.
```ts
import type { CommonContext } from "bworld/common";
const COLORS = ["white", "red", "green", "blue", "black"];
export function setup(ctx: CommonContext) {
export function common(ctx: CommonContext) {
for (const color of COLORS) {
ctx.blocks.register({
id: `wool:${color}_wool`,
@@ -140,10 +145,10 @@ interface RecipeRegistry {
```
- Definitions are checked exactly like JSON files, and a problem is a load error naming the mod.
- Common scripts only run during loading and can't reach the world or players. They get the same `ctx` on both sides, so
they can't accidentally register different things.
- The common phase only runs during loading and can't reach the world or players. They get the same `ctx` on both sides,
so they can't accidentally register different things.
- `register_type` would let `check-mods` and the loader validate mods' own recipe types, instead of each mod checking
its recipes in `setup`. `shaped` becomes short for `bworld:shaped`. Shared scripts can now register types with
its recipes itself. `shaped` becomes short for `bworld:shaped`. The shared phase can now register types with
`ctx.recipes.register_type`, and every one of the base game's and create's are.
### The Freeze and the Registry Hash
@@ -151,7 +156,7 @@ interface RecipeRegistry {
After content loads, the registries close: numeric ids, block items and recipe indexes are made then, and registering
later throws. Then each side hashes its registries (every id and its definition), and the client sends the hash with
`ready`. A mismatch turns the player away naming the first thing that differs, instead of letting them play with
mismatched blocks. This matters once common scripts exist, since code can register different things on each side.
mismatched blocks. This matters once the common phase exists, since code can register different things on each side.
### Overrides
@@ -192,10 +197,10 @@ asks for it in its manifest:
```
```ts
// server.ts: broken blocks drop twice
// broken blocks drop twice
import { engine, hook } from "cubi/engine";
export function setup() {
export function server() {
hook(engine.server.GameServer.prototype, "pop_item", {
after(game, [x, y, z, stack], entity) {
game.spawn_item(entity.x, entity.y, entity.z, stack.clone());
@@ -218,13 +223,13 @@ function hook<T, K extends keyof T>(target: T, method: K, handlers: {
}): () => void; // unhook
```
- `engine` holds the live modules on each side: `engine.common` everywhere, `engine.server` in server scripts,
`engine.client` in client scripts. The engine puts them on the global object at startup, and `cubi/engine` in a mod's
bundle is a shim that reads them, so a hook on a prototype changes every instance already made.
- `engine` holds the live modules on each side: `engine.common` everywhere, `engine.server` in the server phase,
`engine.client` in the client phase. The engine puts them on the global object at startup, and `cubi/engine` in a
mod's bundle is a shim that reads them, so a hook on a prototype changes every instance already made.
- Types are the engine's own source, so `check-mods` typechecks against exactly the engine the mod will run on.
- Several mods can hook one method: `before` hooks run in priority order, `replace` hooks nest, `after` hooks chain
results. A hook that throws is logged under its mod and skipped.
- `hook` only works while a mod's script loads or runs `setup`, which is how it knows the mod. Each side clears every
- `hook` only works while a mod's script loads or runs a phase, which is how it knows the mod. Each side clears every
hook before it loads mods, so tests and a client joining another server start clean.
- The wrapper is rebuilt when hooks change, with only the steps they use, and the last unhook puts the original back.
- Module exports can't be reassigned, so functions mods may want to change move onto plain objects the engine calls
@@ -345,23 +350,23 @@ How the server sends the world, like Minecraft Java since 1.20.2. The code is in
`WORLDGEN_WORKERS` on the dedicated server, 0 for none. Each tick the game asks them for what the nearest chunks each
player is waiting for are built from, nearest first, and only sends a chunk once they've made it; entities and players
in a chunk they haven't made yet wait for it, like Minecraft's in chunks that aren't loaded. Each worker gets the
worldgen bundle (`common/worldgen_bundle.ts`: the mods' data generation reads and their worldgen scripts), so it makes
the same chunks as the game. The host starts them, since the game has no permissions to (see
`server/worldgen_workers.ts`), and they talk to the game through ports. Whatever needs a chunk right away, like a mod
script reading a block somewhere far, still generates it on the game thread: `/tps` shows how many chunks that was in
the last minute. `tools/chunk_loading_bench.ts` measures a player arriving somewhere new.
worldgen bundle (`common/worldgen_bundle.ts`: the mods' data generation reads and their scripts, whose worldgen phase
it runs), so it makes the same chunks as the game. The host starts them, since the game has no permissions to (see
`server/worldgen_workers.ts`), and they talk to the game through ports. Whatever needs a chunk right away, like a
mod's server phase reading a block somewhere far, still generates it on the game thread: `/tps` shows how many chunks
that was in the last minute. `tools/chunk_loading_bench.ts` measures a player arriving somewhere new.
## Phases
Each phase ends with every test passing and the game playable.
**Phase 1: loading and common scripts.** Partly done: content scripts register blocks, see
[Content Scripts](#content-scripts-as-built). `scripts.common` bundled like the client script and run on both sides
between each mod's data and the next mod. `CommonContext`, with the JSON loader going through the same functions. The
freeze and the registry hash. Recipe types as ids with `register_type`. Check: a test mod registers content from code,
and the server and a client end up with equal registries and hashes.
**Phase 1: loading and the common phase.** Partly done: the content phase registers blocks, see
[The Content Phase](#the-content-phase-as-built). `common` run on both sides between each mod's data and the next mod.
`CommonContext`, with the JSON loader going through the same functions. The freeze and the registry hash. Recipe types
as ids with `register_type`. Check: a test mod registers content from code, and the server and a client end up with
equal registries and hashes.
**Phase 2: overrides.** `override` and `extend` in common scripts, `overrides/*.json`, `recipes.remove`, and the
**Phase 2: overrides.** `override` and `extend` in the common phase, `overrides/*.json`, `recipes.remove`, and the
`check-mods` rules. Check: a test mod adds a component to `bworld:grass_block`, changes `bworld:glass`'s toughness and
replaces the chest recipe, on both sides and in the hash.
@@ -377,9 +382,9 @@ and dropped, containers clicked, players moving between chunks, entities.
rules become something `mods/bworld` sets, with the engine asking it on both sides so clients still predict breaking.
Check: all tests pass unchanged, and old saves still load.
**Phase 6: world generation.** Done, as data instead of a worldgen script with `register_terrain`: noises, density
**Phase 6: world generation.** Done, as data instead of `register_terrain` in the worldgen phase: noises, density
functions, noise settings and biome sources in `mods/bworld/worldgen/` (see [Terrain](modding/worldgen/terrain.md)),
features that are code or data (bworld's trees and cacti are code, in its worldgen script), and generation in worldgen
features that are code or data (bworld's trees and cacti are code, in its worldgen phase), and generation in worldgen
workers (see [Chunk Streaming](#chunk-streaming)). The golden fixture checked every step made the same world block for
block, and the server refuses to start without the `bworld:overworld` generator.
@@ -404,8 +409,8 @@ saved before any of this plays exactly the same.
every biome border. A `minecraft:multi_noise` source could exist next to the rules, for mods and imported datapacks
that want it.
- **Should `register_terrain` stay?** It's still in the worldgen API, and only throws. Data covers bworld's terrain, but
a mod might want a generator that isn't density functions at all, like a flat or skyblock world. Keeping it means the
worldgen bundle also carries a terrain script, which workers can load the same way as features. Otherwise it goes.
a mod might want a generator that isn't density functions at all, like a flat or skyblock world. Keeping it costs
little, since workers already run each mod's worldgen phase, where it would be registered. Otherwise it goes.
- **Importing Minecraft's terrain.** Datapacks' noise settings assume a world from -64 to 320 and Minecraft's noises.
The formats are close enough that an importer could translate them, but the result wouldn't look like Minecraft's. Not
a goal for now.