Block registry api

This commit is contained in:
2026-11-12 20:41:13 -03:00
parent e7fe663746
commit 76802ed430
39 changed files with 499 additions and 100 deletions
+29 -5
View File
@@ -41,7 +41,12 @@ The code is in `common/mod_loader.ts`, `common/mod_data.ts`, `common/mod_check.t
**In the API types but throwing:** forms, HUD elements, keybinds, `g.slider`, `register_terrain`.
**Not built at all:** common scripts, the registry freeze and hash, overrides.
**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`.
**Not built at all:** the rest of common scripts (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`,
@@ -68,6 +73,24 @@ JSON is registered. Data would then be shorthand for code: loading `blocks/coppe
`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 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:
- `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
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.
```ts
import type { CommonContext } from "bworld/common";
@@ -331,10 +354,11 @@ How the server sends the world, like Minecraft Java since 1.20.2. The code is in
Each phase ends with every test passing and the game playable.
**Phase 1: loading and common scripts.** `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 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 2: overrides.** `override` and `extend` in common scripts, `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