Block registry api
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user