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
+4 -4
View File
@@ -1,6 +1,6 @@
# Events
Events let a server script react to what happens in the game, and stop some of it from happening. They're the
Events let the server phase react to what happens in the game, and stop some of it from happening. They're the
alternative to [components](../blocks/components.md) when the behavior isn't tied to one block or item: logging chat,
protecting spawn, or reacting to every block broken anywhere.
@@ -15,7 +15,7 @@ Every event is a signal you subscribe to. Events come in two kinds:
```ts
import type { ServerContext } from "cubi/server";
export function setup(ctx: ServerContext) {
export function server(ctx: ServerContext) {
// nobody breaks blocks within 16 blocks of the world's center
ctx.events.before.block_break.subscribe((event) => {
const { x, z } = event.block;
@@ -52,7 +52,7 @@ showed. These events fire for **every** player's actions, since they all go thro
| `player_death` | | ✓ | `player`, `cause`. They've dropped what they carried and respawn right after |
| `player_join` | | ✓ | `player` |
| `player_leave` | | ✓ | `player` |
| `server_start` | | ✓ | none. Every mod's `setup` has run |
| `server_start` | | ✓ | none. Every mod's server phase has run |
| `tick` | | ✓ | `dt`, in seconds. 20 times a second |
| `new_day` | | ✓ | `day`. At 06:00, after every block's `on_new_day` |
@@ -66,7 +66,7 @@ only when the entity was hurt, with the amount the before handlers left.
### Order
Handlers run in the order they subscribed, and mods' `setup` functions run in [load order](registries.md#loading). For
Handlers run in the order they subscribed, and mods' server phases run in [load order](registries.md#loading). For
`block_interact`, the before event runs first, then the block's own components' `on_interact`, rules before handlers,
unless the player is [sneaking](../entities/players.md#sneaking) with an item in hand. If no component handled the click
and the held item has no `on_use`, the held block is placed, which fires `block_place`. Cancelling it undoes what the
+7 -7
View File
@@ -32,18 +32,18 @@ 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), 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.
[content phase](sides.md#content) 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.** Every mod's shared phase runs, then its server phase on the server and its client phase on each client.
The worldgen phase runs wherever the server makes chunks.
Scripts read the registries but can't add to them. Content comes from data files, and from content scripts, which the
Scripts read the registries but can't add to them. Content comes from data files, and from content phases, 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. Content scripts only run on the server, and players get
> only data, since both load the same files in the same order. Content phases only run on the server, and players get
> what they made as data.
## Numeric Ids
+11 -9
View File
@@ -9,24 +9,26 @@ Anything a client sends can be forged by a player who edits their client. The en
reach and the held item for every break, place and interact, and container rules (slot filters, output-only slots) on
the server. A mod's own logic has to do the same:
- Decide on the server. Client scripts show things. Server scripts decide what happens.
- Check permissions on the server, in the server script, before acting for a player, like `requires` on a
- Decide on the server. The client phase shows things. The server phase decides what happens.
- Check permissions on the server, in the server phase, before acting for a player, like `requires` on a
[command](../misc/commands.md).
- Never use a value from a client as an item id, a count or a position without checking it.
Server script code and tile data are never sent to clients, so a mod can keep anti-cheat logic or secrets there. Only
the fields a script sets as [client data](../blocks/tiledata.md#client-data) are.
Tile data is never sent to clients, only the fields a script sets as [client data](../blocks/tiledata.md#client-data). A
mod's script is: players get all of it, its server phase too, and only run its shared and client phases. So a mod can
keep secrets in the world, in tile data or [`ctx.storage`](../datastorage/index.md), but never in its code. Anti-cheat
works by the server deciding, not by hiding how it decides.
## Server Scripts
## The Server Phase
Server scripts run in the game worker with **no Deno permissions**: they can't read files, open connections or run
The server phase runs in the game worker with **no Deno permissions**: they can't read files, open connections or run
programs. Everything they can do goes through the mod API. Installing a server mod still means trusting it with the game
world and everything players send.
## Client Scripts
## The Client Phase
Client scripts run in the game page with the page's full access. How much that matters depends on where the page came
from:
The client phase runs in the game page, like the rest of a mod's script there, with the page's full access. How much
that matters depends on where the page came from:
- **The page was served by the game server** (the default). That server already chose every line of JavaScript on the
page, so running its mods doesn't trust it any more than loading the page did.
+99 -60
View File
@@ -10,81 +10,122 @@ slot 3". The server checks it (reach, the item being held, container rules), run
happened.
For responsiveness, the client guesses the result of breaking, placing and right clicking blocks and shows it right
away, with the states the server will give them: it runs the same [shared scripts](#shared-scripts) the server does.
When the server disagrees, like when a before event cancels a placement, it sends that player the real block and the
guess is undone. Like Minecraft, each click is numbered and the server says when it's done with it: until then the
client keeps showing its guess, so the server's answer to an earlier click can't flicker a block a newer one changed,
like a trapdoor clicked twice. Blocks with `"interactive": true` tell the client not to guess a placement, since right
clicking them does something else.
away, with the states the server will give them: it runs the same [shared phase](#shared) the server does. When the
server disagrees, like when a before event cancels a placement, it sends that player the real block and the guess is
undone. Like Minecraft, each click is numbered and the server says when it's done with it: until then the client keeps
showing its guess, so the server's answer to an earlier click can't flicker a block a newer one changed, like a trapdoor
clicked twice. Blocks with `"interactive": true` tell the client not to guess a placement, since right clicking them
does something else.
```
server process browser
┌───────────────────────────────────────┐ ┌──────────────────────────────────┐
│ host: http, websocket, files │ │ game client (rendering, input) │
│ ┌───────────────────────────────────┐ │ ws │ + mods' client scripts │
│ │ game server worker │◄├──────────┤► │
│ ┌───────────────────────────────────┐ │ ws │ + mods' shared phases │
│ │ game server worker │◄├──────────┤► + mods' client phases │
│ │ world, inventories, tile data │ │ protocol │ chunk workers (meshing, light) │
│ │ + mods' server scripts │ │ chunks │ + mods' shared scripts │
│ │ + mods' shared scripts │ │ └──────────────────────────────────┘
│ │ + mods' worldgen scripts │ │
│ │ + mods' shared phases │ │ chunks └──────────────────────────────────┘
│ │ + mods' server phases │ │
│ └───────────────────────────────────┘ │
│ worldgen workers │
│ + mods' worldgen phases │
│ content worker, while mods load │
│ + mods' content phases │
└───────────────────────────────────────┘
```
## The Five Scripts
## One Script, Five Phases
| 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: screens, inventory overlays and the 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 |
A mod has one script, named by `main` in its manifest. Each of its exports is a **phase**, a function the game calls
with that phase's context where and when it runs:
```ts
import type { ContentContext } from "cubi/content";
import type { SharedContext } from "cubi/shared";
import type { ServerContext } from "cubi/server";
import type { ClientContext } from "cubi/client";
import type { WorldgenContext } from "cubi/worldgen";
export function content(ctx: ContentContext) {/* blocks made in code */}
export function shared(ctx: SharedContext) {/* block rules, fluid rules, recipe types */}
export function server(ctx: ServerContext) {/* components, events, commands */}
export function client(ctx: ClientContext) {/* screens, the HUD */}
export function worldgen(gen: WorldgenContext) {/* features */}
```
| Phase | Runs on | For |
| ---------- | -------------------------------- | ------------------------------------------------------------------- |
| `content` | the server, while it loads mods | blocks made in code, like one for every kind of rock |
| `shared` | the server and every browser | block rules: the states blocks are placed with, joining neighbors |
| `server` | the server, in the game worker | game logic: block and item components, events, commands, containers |
| `client` | every player's browser | presentation: screens, inventory overlays and the HUD |
| `worldgen` | the server, when it makes chunks | features that decorate terrain, deterministic |
Every export is optional: a mod leaves out the phases it doesn't need. A big mod can keep each phase in its own file and
export them all from its main script, like the base game does:
```ts
// scripts/main.ts
export { content } from "./content.ts";
export { shared } from "./shared.ts";
export { server } from "./server.ts";
export { client } from "./client.ts";
export { worldgen } from "./worldgen.ts";
```
Or it can keep a feature together, with its shared rules and its server handlers in one file, sharing what's at its top
level. A side imports the script once, so every phase that side runs gets the same module.
The script is bundled into one module that goes everywhere: the server, every player, and the server's workers. Each
only runs its own phases, but every one of them loads the whole module, so its top level must work anywhere. Keep what
only one side has, like the browser's `document`, inside that side's phase, and import a library only one side needs
with `await import()` inside its phase. Players can read all of it, the server phase too, so it's no place for secrets,
see [Security](security.md).
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). Content scripts make more of it.
on every client, before any phase runs. See [Registries](registries.md). The content phase makes more of it.
### Server Scripts
### Server
```ts
import type { ServerContext } from "cubi/server";
export function setup(ctx: ServerContext) {
export function server(ctx: ServerContext) {
// register components and commands, subscribe to events
}
```
`setup` runs once when the server starts, after every mod's data is registered. Components and commands can only be
registered during `setup`. Everything else on `ctx` works for as long as the server runs, from timers, events and
component handlers.
It runs once when the server starts, after every mod's data is registered and every mod's shared phase has run.
Components and commands can only be registered while it runs. Everything else on `ctx` works for as long as the server
runs, from timers, events and component handlers.
The game worker has **no Deno permissions**: no files, network or subprocesses. State that should last goes in the
world, through [tile data](../blocks/tiledata.md), [containers](../inventories/containers.md) and
[`ctx.storage`](../datastorage/index.md), which are saved with it.
### Client Scripts
### Client
```ts
import type { ClientContext } from "cubi/client";
export function setup(ctx: ClientContext) {
export function client(ctx: ClientContext) {
ctx.ui.add_inventory_overlay({ on_render(g, screen) {/* ... */} });
}
```
`setup` runs after the client has downloaded the server's mods and before the world appears. Client scripts are for
presentation. They can read what the client sees (`ctx.world.get_block`, `ctx.player.position`, items, recipes) but
can't change the world or inventories.
It runs after the client has downloaded the server's mods and before the world appears. The client phase is for
presentation. It can read what the client sees (`ctx.world.get_block`, `ctx.player.position`, items, recipes) but can't
change the world or inventories.
Client code is visible to players and can be changed by them. Never trust it for anything that matters. See
[Security](security.md).
### Shared Scripts
### Shared
```ts
import type { SharedContext } from "cubi/shared";
export function setup(ctx: SharedContext) {
export function shared(ctx: SharedContext) {
ctx.components.register_block("copper_tools:pipe", {
state_for_placement(placement) {/* ... */},
on_neighbor_change(block, params, neighbor) {/* ... */},
@@ -93,32 +134,30 @@ export function setup(ctx: SharedContext) {
}
```
Shared scripts hold a block component's rules, the code Minecraft runs on both sides: what states a block is placed with
(`state_for_placement`), how it changes with the blocks beside it (`on_neighbor_change`) and what right clicking it
The shared phase holds a block component's rules, the code Minecraft runs on both sides: what states a block is placed
with (`state_for_placement`), how it changes with the blocks beside it (`on_neighbor_change`) and what right clicking it
changes (`on_interact`, like a trapdoor opening). See [Block Components](../blocks/components.md#rules). The server
script registers the rest of the same component, like its `on_break` or an `on_interact` that opens a screen, under the
same id. They also hold [fluids' rules](../resources/fluids.md#rules), which name and color a fluid by its data, and
phase registers the rest of the same component, like its `on_break` or an `on_interact` that opens a screen, under the
same id. It also holds [fluids' rules](../resources/fluids.md#rules), which name and color a fluid by its data, and
[recipes made in code](../resources/recipes.md#recipes-from-scripts), which every side has without the server sending
them.
On the server they run on the real world. On a client they run on what that client shows, when its player places, breaks
or right clicks a block, so a fence joins its neighbors the moment it's placed. What they change there is a guess, and
the server's answer replaces it a moment later, so a shared script that gives a different answer on a client only costs
a flicker. `ctx.side` says which one it's running on.
On the server it runs on the real world. On a client it runs on what that client shows, when its player places, breaks
or right clicks a block, so a fence joins its neighbors the moment it's placed. What it changes there is a guess, and
the server's answer replaces it a moment later, so a rule that gives a different answer on a client only costs a
flicker. `ctx.side` says which one it's running on.
`setup` runs before the server or client script on each side, for every mod first, so their scripts can count on any
mod's rules being there. Rules can only be registered during `setup`, and `ctx.world` only works in them.
It runs before the server or client phase on each side, for every mod first, so their phases can count on any mod's
rules being there. Rules can only be registered while it runs, and `ctx.world` only works in them.
Shared code is sent to players like client code, so it's for rules anyone may read, never anti-cheat or secrets.
### Content Scripts
### Content
```ts
import type { ContentContext } from "cubi/content";
const ROCKS = ["granite", "basalt", "slate"];
export function setup(ctx: ContentContext) {
export function content(ctx: ContentContext) {
for (const rock of ROCKS) {
ctx.blocks.register({
id: `rocks:${rock}`,
@@ -130,25 +169,27 @@ export function setup(ctx: ContentContext) {
}
```
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:
The content phase makes 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 it 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.
- Players get them with the rest of the mod's data and never run the content phase, 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).
The server runs it 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 it from the mod's folder. Blocks can only be registered while it
runs. It doesn't see other mods' content or the server's [config](../misc/config.md), and only `engine.common` of
[`cubi/engine`](../misc/engine.md) is there.
### Worldgen Scripts
### Worldgen
Worldgen scripts register features that run on every chunk the server generates. They must be deterministic: the same
seed and chunk always give the same blocks. See [World Generation](../worldgen/index.md).
The worldgen phase registers features that run on every chunk the server generates. It runs in each of the server's
worldgen workers, and features must be deterministic: the same seed and chunk always give the same blocks. See
[World Generation](../worldgen/index.md).
## What Each Side Knows
@@ -162,9 +203,7 @@ seed and chunk always give the same blocks. See [World Generation](../worldgen/i
| Entities | yes | near the player |
| `ctx.storage` | yes | no |
| [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 |
| A mod's script | yes | yes, but only runs its shared and client |
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.