Shared state between client and server
This commit is contained in:
@@ -9,10 +9,11 @@ Clients send what the player is _trying_ to do: "break the block at x, y, z", "u
|
||||
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 and placing blocks and shows it right away. When the
|
||||
server disagrees, like when a before event cancels a placement, it sends that player the real block and the guess is
|
||||
undone. Blocks with `"interactive": true` tell the client not to guess, since right clicking them does something other
|
||||
than placing.
|
||||
For responsiveness, the client guesses the result of breaking and placing 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.
|
||||
Blocks with `"interactive": true` tell the client not to guess, since right clicking them does something other than
|
||||
placing.
|
||||
|
||||
```
|
||||
server process browser
|
||||
@@ -21,18 +22,20 @@ than placing.
|
||||
│ ┌───────────────────────────────────┐ │ ws │ + mods' client scripts │
|
||||
│ │ game server worker │◄├──────────┤► │
|
||||
│ │ world, inventories, tile data │ │ protocol │ chunk workers (meshing, light) │
|
||||
│ │ + mods' server scripts │ │ chunks └──────────────────────────────────┘
|
||||
│ │ + mods' server scripts │ │ chunks │ + mods' shared scripts │
|
||||
│ │ + mods' shared scripts │ │ └──────────────────────────────────┘
|
||||
│ │ + mods' worldgen scripts │ │
|
||||
│ └───────────────────────────────────┘ │
|
||||
└───────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## The Three Scripts
|
||||
## The Four 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 |
|
||||
|
||||
Data (blocks, items, models, recipes, entities, textures) isn't a script: it's registered the same way on the server and
|
||||
@@ -73,6 +76,34 @@ 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
|
||||
|
||||
```ts
|
||||
import type { SharedContext } from "bworld/shared";
|
||||
|
||||
export function setup(ctx: SharedContext) {
|
||||
ctx.components.register_block("copper_tools:pipe", {
|
||||
state_for_placement(placement) {/* ... */},
|
||||
on_neighbor_change(block, params, neighbor) {/* ... */},
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
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`) and how it changes with the blocks beside it (`on_neighbor_change`). See
|
||||
[Block Components](../blocks/components.md#placing). The server script registers the rest of the same component, like
|
||||
its `on_interact`, under the same id.
|
||||
|
||||
On the server they run on the real world. On a client they run on what that client shows, when its player places or
|
||||
breaks 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.
|
||||
|
||||
### Worldgen Scripts
|
||||
|
||||
Worldgen scripts register features that run on every chunk the server generates. They must be deterministic: the same
|
||||
@@ -90,6 +121,7 @@ seed and chunk always give the same blocks. See [World Generation](../worldgen/i
|
||||
| Entities | yes | near the player |
|
||||
| `ctx.storage` | yes | no |
|
||||
| Server script code | yes | **never** |
|
||||
| Shared script code | yes | yes, and runs it |
|
||||
|
||||
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