10 KiB
Sides
bworld is always a client connected to a server, even when you play alone. The server runs the game and the client shows it. Knowing which of your code runs where, and what each side can see, is most of what keeps a mod correct.
The Server Is the Authority
Clients send what the player is trying to do: "break the block at x, y, z", "use the block I'm looking at", "click slot 3". The server checks it (reach, the item being held, container rules), runs mod logic, and sends back what 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 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 │◄├──────────┤► │
│ │ world, inventories, tile data │ │ protocol │ chunk workers (meshing, light) │
│ │ + mods' server scripts │ │ chunks │ + mods' shared scripts │
│ │ + mods' shared scripts │ │ └──────────────────────────────────┘
│ │ + mods' worldgen 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: 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 |
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. Content scripts make more of it.
Server Scripts
import type { ServerContext } from "cubi/server";
export function setup(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.
The game worker has no Deno permissions: no files, network or subprocesses. State that should last goes in the
world, through tile data, containers and
ctx.storage, which are saved with it.
Client Scripts
import type { ClientContext } from "cubi/client";
export function setup(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.
Client code is visible to players and can be changed by them. Never trust it for anything that matters. See Security.
Shared Scripts
import type { SharedContext } from "cubi/shared";
export function setup(ctx: SharedContext) {
ctx.components.register_block("copper_tools:pipe", {
state_for_placement(placement) {/* ... */},
on_neighbor_change(block, params, neighbor) {/* ... */},
on_interact(block, params, interaction) {/* ... */},
});
}
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
changes (on_interact, like a trapdoor opening). See Block Components. 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, which name and color a fluid by its data, and
recipes made in code, 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.
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.
Shared code is sent to players like client code, so it's for rules anyone may read, never anti-cheat or secrets.
Content Scripts
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,
and can't import cubi/engine.
Worldgen Scripts
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.
What Each Side Knows
| Data | Server | Client |
|---|---|---|
| Block, item, entity and recipe definitions | yes | yes, the same |
| Blocks and their states | yes | chunks near the player |
| Tile data | yes | never, show it through a screen instead |
A block's displayed item (item_display) |
yes | yes, near the player |
Item stacks and their data |
yes | in containers the player has open or owns |
| Entities | yes | near the player |
ctx.storage |
yes | no |
| Config | 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, put it in an item's data, or set a block state the block's model shows.