80 lines
4.5 KiB
Markdown
80 lines
4.5 KiB
Markdown
# Networking
|
||
|
||
Players never install mods. When they join a server, their client downloads that server's mods and loads them, then the
|
||
server streams them the world. This page is what happens along the way, and what a mod can rely on.
|
||
|
||
## Joining
|
||
|
||
```
|
||
client server
|
||
│ hello { name, protocol } │
|
||
├────────────────────────────────────────►│ a different protocol is turned away
|
||
│ welcome { protocol, mods[] } │ mods[i] = { id, name, version, sha256, file, size }
|
||
│◄────────────────────────────────────────┤
|
||
│ │
|
||
│ [another site: confirm screen] │
|
||
│ download every .bmod, check each one's │
|
||
│ sha256, build the texture atlas, load │
|
||
│ data in the listed order, run client │
|
||
│ scripts' setup, start chunk workers │
|
||
│ │
|
||
│ ready { view_distance } │
|
||
├────────────────────────────────────────►│
|
||
│ join { id, name, players, entities, │ the player is created here and player_join
|
||
│ blocks, spawn, selected_slot } │ fires. their inventory follows
|
||
│◄────────────────────────────────────────┤
|
||
│ chunks, nearest first │
|
||
│◄────────────────────────────────────────┤
|
||
```
|
||
|
||
- The client loads mods **in the order the server lists them**, so both sides register the same content the same way.
|
||
- Every `.bmod` is checked against its SHA-256 before it's opened, and scripts run from the checked bytes, so what runs
|
||
is exactly what the server listed.
|
||
- Players get each mod's `.bmod` **without its server and worldgen scripts**. The server makes that copy when it starts,
|
||
named by its hash so it can be cached forever.
|
||
- If any download, hash check or client `setup` fails, the client disconnects and says which mod failed. It never joins
|
||
with some mods missing.
|
||
- Until `ready`, the connection isn't a player: nobody sees it, and anything else it sends is ignored. A client that
|
||
doesn't send `ready` within 60 seconds is turned away.
|
||
- Leaving a server reloads the page, so one server's mod code never stays loaded while playing on another.
|
||
|
||
When the game's protocol changes (`PROTOCOL_VERSION` in `common/protocol.ts`), clients on another version are turned
|
||
away before anything is downloaded.
|
||
|
||
## The World
|
||
|
||
The server sends the world the way Minecraft Java does since 1.20.2. Clients never generate terrain, so worldgen scripts
|
||
stay on the server.
|
||
|
||
- Chunks are 16×256×16 and sent whole, compressed, in batches, nearest first. The server paces batches by how fast each
|
||
client says it loads them.
|
||
- Players see chunks up to their view distance, at most 16 on the server's side. Light isn't sent: clients light chunks
|
||
themselves.
|
||
- Block changes go only to players who have the chunk. Everyone else gets the change with the chunk when they come
|
||
close.
|
||
- Entities near a player are sent to them, and their movement follows.
|
||
|
||
For a mod, this means: what a client knows about the world is what's near its player. Tile data, `ctx.storage` and
|
||
server code are never sent at all. See [Sides](../concepts/sides.md#what-each-side-knows).
|
||
|
||
## Mod Channels
|
||
|
||
Mod channels let a mod's client and server scripts send each other JSON messages:
|
||
|
||
```ts
|
||
// server.ts
|
||
ctx.net.on("copper_tools:reset_stats", (player, data) => {/* check everything, the client is untrusted */});
|
||
ctx.net.send(player, "copper_tools:heat", { value: 12 });
|
||
ctx.net.broadcast("copper_tools:announcement", { text: "the smelter exploded" });
|
||
|
||
// client.ts
|
||
ctx.net.on("copper_tools:heat", (data) => {/* ... */});
|
||
ctx.net.send("copper_tools:reset_stats", {});
|
||
```
|
||
|
||
- Channel ids are in the mod's namespace, and a mod only listens and sends on its own.
|
||
- A message from a client is at most 16 KB of JSON, and each player can send at most 60 a second. The server drops
|
||
anything over that without telling anyone.
|
||
- Everything a client sends is untrusted: check it like a click or a chat message.
|
||
- A handler that throws is logged under its mod, and the other handlers still run.
|