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
+3
View File
@@ -26,6 +26,9 @@ This registers the block and an item with the same id that places it. It's a ful
side, takes 5 seconds to break by hand, breaks faster with a pickaxe, and only drops itself when broken with a pickaxe
of tier 1 (stone or copper) or better.
A mod with many similar blocks, like every block of every kind of rock, can register them in code from a
[content script](../concepts/sides.md#content-scripts) instead, with the same object a file holds under `"block"`.
## Properties
### Looks
+8 -5
View File
@@ -31,17 +31,20 @@ The server and every client load mods the same way, in the same order:
1. **Order.** Dependencies come before the mods that need them, and otherwise mods load alphabetically. Mods that depend
on each other in a circle are an error. The server sends its order to clients, and they follow it.
2. **Data**, one mod at a time: its block models, blocks (and their items), items, entity models, entities, effects and
fluids. Then every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and ores, which
can use tags.
2. **Data**, one mod at a time: its block models, blocks (and their items), including the ones its
[content script](sides.md#content-scripts) registers, after its `blocks/` files, items, entity models, entities,
effects and fluids. Then every mod's [tags](../resources/tags.md), merged in load order, then each mod's recipes and
ores, which can use tags.
3. **Scripts.** Server scripts' `setup` runs on the server, and client scripts' on each client. Worldgen scripts'
`setup` runs on the server before it makes chunks.
Scripts read the registries but can't add to them. Content comes from data files.
Scripts read the registries but can't add to them. Content comes from data files, and from content scripts, which the
server runs when it loads mods, before anything else, and whose blocks go to players as data.
> [!IMPORTANT]
> The server and the client must end up with exactly the same blocks, items and recipes. They do as long as content is
> only data, since both load the same files in the same order.
> only data, since both load the same files in the same order. Content scripts only run on the server, and players get
> what they made as data.
## Numeric Ids
+44 -8
View File
@@ -31,17 +31,18 @@ clicking them does something else.
└───────────────────────────────────────┘
```
## The Four Scripts
## The Five Scripts
| Script | Runs on | Sent to players | For |
| ---------- | -------------------------------- | --------------- | ------------------------------------------------------------------- |
| `server` | the server, in the game worker | **never** | game logic: block and item components, events, commands, containers |
| `client` | every player's browser | yes | presentation: inventory overlays, and later custom screens and HUD |
| `shared` | the server and every browser | yes | block rules: the states blocks are placed with, joining neighbors |
| `worldgen` | the server, when it makes chunks | no | features that decorate terrain, deterministic |
| Script | Runs on | Sent to players | For |
| ---------- | -------------------------------- | ---------------------- | ------------------------------------------------------------------- |
| `server` | the server, in the game worker | **never** | game logic: block and item components, events, commands, containers |
| `client` | every player's browser | yes | presentation: inventory overlays, and later custom screens and HUD |
| `shared` | the server and every browser | yes | block rules: the states blocks are placed with, joining neighbors |
| `worldgen` | the server, when it makes chunks | no | features that decorate terrain, deterministic |
| `content` | the server, while it loads mods | no, only what it makes | blocks made in code, like one for every kind of rock |
Data (blocks, items, models, recipes, entities, textures) isn't a script: it's registered the same way on the server and
on every client, before any script runs. See [Registries](registries.md).
on every client, before any script runs. See [Registries](registries.md). Content scripts make more of it.
### Server Scripts
@@ -110,6 +111,40 @@ mod's rules being there. Rules can only be registered during `setup`, and `ctx.w
Shared code is sent to players like client code, so it's for rules anyone may read, never anti-cheat or secrets.
### Content Scripts
```ts
import type { ContentContext } from "cubi/content";
const ROCKS = ["granite", "basalt", "slate"];
export function setup(ctx: ContentContext) {
for (const rock of ROCKS) {
ctx.blocks.register({
id: `rocks:${rock}`,
textures: `rocks:${rock}`,
mining: { toughness: 3, tool: "pickaxe", requires_tool: true },
drops: `rocks:${rock}`,
});
}
}
```
Content scripts make data in code, for content that's easier to generate than to write out file by file.
`ctx.blocks.register` takes the same object a `blocks/` file holds under `"block"`. The blocks a content script
registers become part of the mod's data, the same as its files:
- They're registered right after the mod's `blocks/` files, in the order of the calls.
- They're checked the same way, so a mistake stops the mod loading and names the script. They have to be in the mod's
own namespace.
- The mod's files can name them: tags, recipes, drops, ores and the rest of worldgen.
- Players get them with the rest of the mod's data and never run the script, so the server and its players always agree.
The server runs content scripts when it loads mods, before anything else reads their data, in a worker with **no Deno
permissions**, like the game's. `check-mods`, the build and tests run them from the mod's folder. Blocks can only be
registered during `setup`. Content scripts don't see other mods' content or the server's [config](../misc/config.md),
and can't import [`cubi/engine`](../misc/engine.md).
### Worldgen Scripts
Worldgen scripts register features that run on every chunk the server generates. They must be deterministic: the same
@@ -129,6 +164,7 @@ seed and chunk always give the same blocks. See [World Generation](../worldgen/i
| [Config](../misc/config.md) | yes | no |
| Server script code | yes | **never** |
| Shared script code | yes | yes, and runs it |
| Content script code | yes | **never**, only the blocks it registers |
When a client needs to see something only the server has, the server has to send it: open a
[menu](../inventories/menus.md), put it in an item's `data`, or set a block state the block's model shows.
+11 -7
View File
@@ -1,6 +1,6 @@
# Mod Files
A mod is a folder with a `manifest.json`, some data files, textures, and up to four scripts. The build packs it into a
A mod is a folder with a `manifest.json`, some data files, textures, and up to five scripts. The build packs it into a
single `.cubi` file, which is what servers load.
## Layout
@@ -36,6 +36,7 @@ mods/
client.ts # sent to players
shared.ts # block rules, run on the server and sent to players
worldgen.ts # terrain features, never sent to players
content.ts # blocks made in code, run by the server while it loads mods, never sent to players
```
Data folders can have subfolders. Every data file holds one definition, wrapped with the format version:
@@ -66,7 +67,8 @@ matter, the `id` inside does, except for [tags](../resources/tags.md), whose id
"server": "scripts/server.ts",
"client": "scripts/client.ts",
"worldgen": "scripts/worldgen.ts",
"shared": "scripts/shared.ts"
"shared": "scripts/shared.ts",
"content": "scripts/content.ts"
},
"credits": "CREDITS.md"
}
@@ -83,7 +85,7 @@ matter, the `id` inside does, except for [tags](../resources/tags.md), whose id
| `game_version` | no | The bworld versions the mod works with, as a semver range. Only checked for mods with `engine_access`. |
| `engine_access` | no | `true` lets the scripts import `cubi/engine` and hook the engine. See [Engine Access](../misc/engine.md). |
| `dependencies` | no | Other mods, `{ "id", "version" }`. A missing dependency is an error, and dependencies load first. The version range isn't checked yet. |
| `scripts` | no | Paths to the `server`, `client`, `shared` and `worldgen` scripts. See [Sides](../concepts/sides.md). |
| `scripts` | no | Paths to the `server`, `client`, `shared`, `worldgen` and `content` scripts. See [Sides](../concepts/sides.md). |
| `credits` | no | A markdown file for licenses and thanks, shown on the Credits screen. Raw HTML in it is shown as text. |
List every mod whose ids you use in `dependencies`, including `bworld` for the base game's blocks and items. It
@@ -102,8 +104,8 @@ so they can import other files and packages, but code imported by several script
When a mod's folder has a `deno.json`, its scripts are bundled and typechecked with it, so the mod can have its own
imports and npm or JSR packages. Mods without one use the `deno.json` of the folder the build runs in, like the mods in
this repository. That config maps the API types, which scripts import as `cubi/server`, `cubi/client`, `cubi/shared` and
`cubi/worldgen`, and [`cubi/engine`](../misc/engine.md):
this repository. That config maps the API types, which scripts import as `cubi/server`, `cubi/client`, `cubi/shared`,
`cubi/worldgen` and `cubi/content`, and [`cubi/engine`](../misc/engine.md):
```ts
import type { ServerContext } from "cubi/server";
@@ -127,6 +129,7 @@ copper_tools.cubi
scripts/client.js
scripts/worldgen.js
scripts/shared.js
scripts/content.js
textures/<name>.png
textures/<name>.json # for animated textures
credits.md
@@ -136,5 +139,6 @@ copper_tools.cubi
- Packing the same mod twice gives the same bytes, so the file only changes when the mod does.
- `deno task build` leaves other `.cubi` files in `server_mods/` alone, so mods from elsewhere can sit next to the ones
it builds.
- Players get a copy **without** `scripts/server.js`, `scripts/worldgen.js` and the config. See
[Networking](../networking/index.md).
- `data.json` doesn't hold the blocks `scripts/content.js` registers. The server runs it when it loads the `.cubi`.
- Players get a copy **without** `scripts/server.js`, `scripts/worldgen.js`, `scripts/content.js` and the config, but
with the blocks the content script registered in its `data.json`. See [Networking](../networking/index.md).
+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