Shared state between client and server

This commit is contained in:
2026-10-03 18:45:07 -03:00
parent 63e6248f9f
commit 7417bfd5f1
24 changed files with 656 additions and 193 deletions
+55 -29
View File
@@ -59,21 +59,45 @@ component.
## Handlers
| Handler | Called when | Returns |
| --------------------------------------------- | ---------------------------------------------------------------------- | -------------------------- |
| `on_create(block, params)` | The block is placed or set, by a player or a script. | |
| `on_break(block, params, player?)` | The block is broken or replaced. `player` when a player broke it. | |
| `on_click(block, params, player)` | A player starts hitting it. | |
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
| `on_second(block, params, dt)` | Every second, near a player. | |
| `on_new_day(block, params, day)` | Every morning at 06:00, anywhere in the world. | |
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
| `state_for_placement(placement, params)` | A player is about to place it. | States to place it with |
| Handler | Called when | Returns |
| ------------------------------------ | ----------------------------------------------------------------- | -------------------------- |
| `on_create(block, params)` | The block is placed or set, by a player or a script. | |
| `on_break(block, params, player?)` | The block is broken or replaced. `player` when a player broke it. | |
| `on_click(block, params, player)` | A player starts hitting it. | |
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
| `on_second(block, params, dt)` | Every second, near a player. | |
| `on_new_day(block, params, day)` | Every morning at 06:00, anywhere in the world. | |
`block` is `{ id, x, y, z, data }`, where `data` is the block's [tile data](tiledata.md). `params` is what the block's
JSON lists for this component, and `dt` is in seconds.
## Rules
A component's rules decide a block's states from where it's placed and what's around it. They're registered in the mod's
[shared script](../concepts/sides.md#shared-scripts) instead, under the same id, because clients run them too: that's
how a player sees a fence join its neighbors the moment they place it, instead of a moment later when the server
answers. A component can have rules, handlers, or both, and a block can list one that only has rules.
```ts
import type { SharedContext } from "bworld/shared";
export function setup(ctx: SharedContext) {
ctx.components.register_block("copper_tools:pipe", {
state_for_placement(placement, params) {/* ... */},
on_neighbor_change(block, params, neighbor) {/* ... */},
});
}
```
| Rule | Called when | Returns |
| --------------------------------------------- | ---------------------------------------------------------------------- | ----------------------- |
| `state_for_placement(placement, params)` | A player is about to place it. | States to place it with |
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. | |
Here `block` is `{ id, x, y, z }`, without tile data, which clients never have. `ctx.world` in a shared script has
`get_block`, `get_state`, `set_state` and `is_face_sturdy`, and only works inside rules.
### Neighbors
`on_neighbor_change` is how a block keeps up with what's around it, like Minecraft's `updateShape`: `neighbor` is
@@ -87,10 +111,11 @@ a slab's sides and plants aren't.
### Placing
`state_for_placement` picks the states a block is placed with, like Minecraft's `getStateForPlacement`. `placement` is
`{ x, y, z, player, face, hit }`: where it's going, the face of the block that was clicked, and where on that block the
click was, from 0 to 1 on each axis. It returns states by name, and the rest keep their defaults, except that a 2 bit
`facing` already faces the player. It runs before the server checks that no one is in the way, so that's checked against
the shape the block will really have. A trapdoor clicked onto the upper half of a block's side hangs in the top half:
`{ x, y, z, face, hit, facing }`: where it's going, the face of the block that was clicked, where on that block the
click was (from 0 to 1 on each axis), and the way the player looks along the ground. It returns states by name, and the
rest keep their defaults, except that a 2 bit `facing` already faces the player. It runs before the server checks that
no one is in the way, so that's checked against the shape the block will really have. A trapdoor clicked onto the upper
half of a block's side hangs in the top half:
```ts
state_for_placement(placement): Record<string, number> {
@@ -101,7 +126,8 @@ state_for_placement(placement): Record<string, number> {
},
```
Clients guess blocks are placed with their defaults, and the server corrects them right after.
A fence's placement looks at its neighbors, so it's joined from the start, and its `on_neighbor_change` keeps it joined
after. Both are in `mods/bworld/scripts/shared.ts`.
### Interacting
@@ -127,18 +153,18 @@ catch up. If it's further behind than that, it skips the rest and logs a warning
`mods/bworld/scripts/server.ts` registers these, and any mod that depends on `bworld` can use them:
| Component | Params | What it does |
| ----------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
| `bworld:water_source` | none | Refills the watering can. |
| `bworld:fishable` | none | Can be fished in with the fishing rod. |
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's. Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
| Component | Params | What it does |
| ----------------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
| `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. |
| `bworld:crop_top` | see [Crops](crops.md) | The top half of a two block crop, like rice. |
| `bworld:water_source` | none | Refills the watering can. |
| `bworld:fishable` | none | Can be fished in with the fishing rod. |
| `bworld:trapdoor` | none | Opens and closes when right clicked, and is placed like Minecraft's (its rules are in the shared script). Needs `facing` (2 bits), `half` and `open` states, see `blocks/oak_trapdoor.json`. |
| `bworld:fence` | `connects_to` | Joins the blocks in the tag `connects_to` and sturdy sides of other blocks, except leaves. Needs the `north`, `east`, `south` and `west` states. |
Item components work the same way, see [Items](../items/index.md#item-components).