Files
bworld/docs/modding/networking/index.md
T
2026-11-14 12:18:41 -03:00

80 lines
4.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 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 .cubi, check each one's │
│ sha256, build the texture atlas, load │
│ data in the listed order, run shared │
│ and client phases, 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 `.cubi` 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 `.cubi` **without its terrain data and config**. Its script is all there, and players run its
shared and client phases. The server makes that copy when it starts, named by its hash so it can be cached forever.
- If any download, hash check, shared or client phase 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 they never run
the worldgen phase.
- Chunks are 16×16 and as tall as the dimension, 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 and `ctx.storage` 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 phases 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.