Files
bworld/common/protocol.ts
T
2026-10-02 18:45:08 -03:00

254 lines
12 KiB
TypeScript

// messages sent between the client and the server, as json over a websocket. chunks are binary, see level_chunk
import type { Faces } from "./constants.ts";
import type { ItemData } from "./inventory.ts";
import type { ModListing } from "./mod_loader.ts";
export const AIR_ID = "bworld:air";
// bump when a client and server of different versions can't play together.
// the server rejects a different version before the client downloads anything
export const PROTOCOL_VERSION = 18;
export interface PlayerInfo {
id: string;
name: string;
x: number;
y: number;
z: number;
yaw: number;
pitch: number;
// a 64x64 png as base64, see /skin. the default skin when there isn't one
skin?: string;
// the skin is for the model with 3 pixel wide arms
slim?: boolean;
// what they're wearing that shows, see player_worn
worn?: ItemData[];
// lying flat in the short box, see PLAYER_SWIM_HEIGHT
swimming?: boolean;
}
// an entity that isn't a player, as the server first sends it. players have their own messages. items on the
// ground have their own kind, everything else is "entity" with its type id
export type EntityInfo =
| { kind: "item"; id: string; x: number; y: number; z: number; item: ItemData }
| {
kind: "entity";
type: string;
id: string;
x: number;
y: number;
z: number;
// radians, see the server's Entity
yaw: number;
pitch: number;
// for things whose head turns apart from their body
head_yaw?: number;
data: EntityData;
};
// what an entity looks like that can change, like minecraft's SynchedEntityData: custom_name, custom_name_visible,
// glowing, on_fire, silent, left_handed, equipment (slot to item). only what's set is there
export type EntityData = Record<string, unknown>;
// what happens to an entity that clients show, see entity_event
export type EntityEvent = "hurt" | "death" | "swing";
// x, y, z, block id, and its state bits when they aren't 0
export type BlockChange = [number, number, number, string, number?];
// the player's own containers, synced to them whether or not a menu is open. "extra" is what's in the slots mods added
// to menu 0, like curios', in their order
export type ContainerKey = "inventory" | "crafting" | "extra";
export const CRAFTING_RESULT_SLOT = 9;
// menus, like neoforge's: the server's menu decides what every click does, clients draw it and send clicks by menu
// slot index. menu 0 is the player's own inventory and crafting grid, always there and never opened, like minecraft's
// InventoryMenu: slots 0-35 are the inventory (the hotbar is 0-8), 36-45 the crafting grid, then the slots mods added,
// which join's player_menu lists
export const PLAYER_MENU = 0;
export const PLAYER_MENU_CRAFTING = 36;
export const PLAYER_MENU_EXTRA = 46;
// one slot of an opened menu as the client draws it. x and y in gui pixels from the screen's top left, output slots
// can only be taken from, icon is a texture drawn while it's empty
export interface MenuSlotInfo {
x: number;
y: number;
output?: boolean;
icon?: string;
}
// clients send what the player is trying to do, the server decides what happens
export type ClientMessage =
| { type: "hello"; name: string; protocol: number }
// sent once the client has downloaded, checked and loaded everything in welcome. view_distance is in chunks, the
// server may use less
| { type: "ready"; view_distance?: number }
// the player changed their view distance, like minecraft's client information
| { type: "view_distance"; distance: number }
// answers chunk_batch_finished once the batch is loaded, with how many chunks a tick the client can take
| { type: "chunk_batch_received"; chunks_per_tick: number }
// teleport is the id of the last teleport the client heard of. moves from before the latest one are ignored.
// swimming is whether they're swimming, sent right away when it changes
| {
type: "move";
x: number;
y: number;
z: number;
yaw: number;
pitch: number;
teleport?: number;
swimming?: boolean;
}
| { type: "break_block"; x: number; y: number; z: number }
// the player started hitting a block, for on_click
| { type: "hit_block"; x: number; y: number; z: number }
// left click on an entity, by id
| { type: "attack_entity"; id: string }
// left click at nothing, a swing at the air. it starts the attack cooldown over, like an attack
| { type: "swing" }
// right click without looking at a block, for items' on_use
| { type: "use_item" }
// right click on a block: interact with it, or place the held block against `face`
| { type: "use_block"; x: number; y: number; z: number; face: Faces }
| { type: "select_slot"; slot: number }
// a slot of the open menu, or of menu 0 when none is. shift moves the stack to the other part of the menu instead,
// like minecraft's quick move. clicks for a menu that isn't open any more are ignored
| { type: "click"; menu: number; slot: number; button: number; shift?: boolean }
// q on a slot (the held one when no screen is open, in menu 0) throws one item, or the whole stack with ctrl
| { type: "drop_item"; menu: number; slot: number; all: boolean }
// a button of the open menu, like minecraft's clickMenuButton. the menu decides what the number means
| { type: "menu_button"; menu: number; button: number }
// closes the open menu, or the player's own inventory screen with 0
| { type: "close_menu"; menu: number }
| { type: "chat"; text: string }
// what could come next in a command being typed, answered with suggestions. id matches them up
| { type: "suggest"; id: number; text: string }
// a mod's client script talking to its server script, see ctx.net
| { type: "mod"; channel: string; data: unknown };
export type ServerMessage =
// what to download before joining, see docs/modding/networking/index.md
| {
type: "welcome";
protocol: number;
// in load order, the client downloads and loads them before joining the world
mods: ModListing[];
}
// the connection is closed right after
| { type: "rejected"; reason: string }
// answers ready, the player is in the world from here
| {
type: "join";
id: string;
// the server may change the name asked for, like alice to alice2
name: string;
players: PlayerInfo[];
// the entities within their view distance
entities: EntityInfo[];
// the server's block id for each of its numeric ids, which level_chunk uses. clients number blocks their own way
blocks: string[];
spawn: { x: number; y: number; z: number; yaw: number; pitch: number };
selected_slot: number;
// the slots mods added to menu 0, from PLAYER_MENU_EXTRA on, see ctx.menus.extend_player_menu
player_menu: MenuSlotInfo[];
// world time in ticks, see common/time.ts
time: number;
}
// a whole chunk, sent as a binary message and not json. data is what common/chunk_streaming.ts decodes
| { type: "level_chunk"; data: Uint8Array }
// the client can drop the chunk, it's out of view. the server won't send changes to it anymore
| { type: "forget_level_chunk"; x: number; z: number }
// the chunk the player is in, sent when it changes. chunks come in around it
| { type: "set_center_chunk"; x: number; z: number }
// level_chunk messages come between these, size is how many. the client answers with chunk_batch_received
| { type: "chunk_batch_start" }
| { type: "chunk_batch_finished"; size: number }
| { type: "player_join"; player: PlayerInfo }
| { type: "player_leave"; id: string }
| {
type: "player_move";
id: string;
x: number;
y: number;
z: number;
yaw: number;
pitch: number;
swimming?: boolean;
}
// a player changed their skin, including the one receiving it. without skin it's back to the default
| { type: "player_skin"; id: string; skin?: string; slim?: boolean }
// what a player wears that shows changed, to everyone including them: the items whose json has worn, see
// ctx's player.set_worn. also sent to a player right after join, for what they wear themselves
| { type: "player_worn"; id: string; worn: ItemData[] }
| { type: "add_entity"; entity: EntityInfo }
| {
type: "move_entity";
id: string;
x: number;
y: number;
z: number;
yaw: number;
pitch: number;
head_yaw?: number;
}
// all of an entity's synced data, when any of it changed
| { type: "entity_data"; id: string; data: EntityData }
// hurt flashes it red, death starts it falling over (it's removed a second later), swing is it attacking
| { type: "entity_event"; id: string; event: EntityEvent }
// what's riding it now, by id
| { type: "set_passengers"; id: string; passengers: string[] }
// a dropped item's stack changed, like when two stacks merge
| { type: "set_entity_item"; id: string; item: ItemData }
| { type: "remove_entity"; id: string }
// a player picked up an item, it flies to them and goes away
| { type: "take_entity"; id: string; player: string }
// to the players who have the chunk, including the one who caused it, which corrects anything their client
// predicted wrong
| { type: "set_block"; x: number; y: number; z: number; id: string; state?: number }
// the item a block shows, see BlockRegistry.item_display. to the players who have the chunk, after it and whenever
// it changes. null when it shows nothing any more
| { type: "set_block_item"; x: number; y: number; z: number; item: ItemData | null }
| { type: "chat"; from?: string; text: string }
// the player is here now. with motion, they're moving that fast (blocks per second) too: the server is moving them,
// like when knocked back, and the client keeps going that way until the next one. id counts up, see move
| { type: "teleport"; id: number; x: number; y: number; z: number; motion?: [number, number, number] }
| { type: "container"; container: ContainerKey; items: (ItemData | null)[] }
| { type: "cursor"; item: ItemData | null }
// a menu opened, replacing any other. id counts 1 to 100 and around, type is the menu type whose screen draws it.
// its slots' items come in menu_content, its data's changes in menu_data
| {
type: "open_menu";
id: number;
menu_type: string;
title: string;
open_data: unknown;
slots: MenuSlotInfo[];
data: Record<string, number>;
}
// what's in every slot of the open menu, by menu slot index, when any of it changed
| { type: "menu_content"; id: number; items: (ItemData | null)[] }
// all the menu's data, when any of it changed
| { type: "menu_data"; id: number; data: Record<string, number> }
// the server closed it (the block broke, the player walked away), it already put everything back
| { type: "close_menu"; id: number }
// the world time, every second and when it's changed. clients count ticks in between
| { type: "time"; time: number }
// answers suggest: start is where in the text the suggestions replace from, up to the caret
| { type: "suggestions"; id: number; start: number; suggestions: { text: string; tooltip?: string }[] }
// the player's own health, max health, hunger, saturation and air (out of 20, 2 a bubble), when they change
| { type: "vitals"; health: number; max_health: number; food: number; saturation: number; air: number }
// the player's own attack speed attribute, when it changes, for the attack indicator under the crosshair
| { type: "attack_speed"; value: number }
// whether the player flies, see /fly, when it changes
| { type: "flying"; flying: boolean }
// the player's own effects that show an icon, when they change. duration is in ticks, -1 for forever, and the
// client counts it down itself, so it isn't sent every tick
| { type: "effects"; effects: { id: string; amplifier: number; duration: number }[] }
// a mod's server script talking to its client script, see ctx.net
| { type: "mod"; channel: string; data: unknown };
export const MAX_NAME_LENGTH = 16;
export const MAX_CHAT_LENGTH = 256;