96 lines
5.7 KiB
Markdown
96 lines
5.7 KiB
Markdown
# 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 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.
|
|
|
|
```
|
|
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' worldgen scripts │ │
|
|
│ └───────────────────────────────────┘ │
|
|
└───────────────────────────────────────┘
|
|
```
|
|
|
|
## The Three 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 |
|
|
| `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
|
|
on every client, before any script runs. See [Registries](registries.md).
|
|
|
|
### Server Scripts
|
|
|
|
```ts
|
|
import type { ServerContext } from "bworld/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](../blocks/tiledata.md), [containers](../inventories/containers.md) and
|
|
[`ctx.storage`](../datastorage/index.md), which are saved with it.
|
|
|
|
### Client Scripts
|
|
|
|
```ts
|
|
import type { ClientContext } from "bworld/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](security.md).
|
|
|
|
### 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](../worldgen/index.md).
|
|
|
|
## 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](../blocks/tiledata.md) | 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 |
|
|
| Server script code | yes | **never** |
|
|
|
|
When a client needs to see something only the server has, the server has to send it: open a
|
|
[container screen](../inventories/menus.md), put it in an item's `data`, or set a block state the block's model shows.
|