// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // 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 { FluidVariant } from "./mod_api/shared.ts"; import type { ModListing } from "./mod_loader.ts"; import type { WorldShapeJson } from "./world_shape.ts"; export const AIR_ID = "cubi: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 = 27; 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; // 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; // the most it holds of anything, when that's less than a stack max_count?: number; } // a tank shown in a menu, like modern industrialization's fluid slots: an 18 pixel frame at x y, which a fluid // container on the cursor fills from, or empties into unless it's an output export interface MenuFluidSlotInfo { x: number; y: number; output?: boolean; } // what's in a menu's tank, no fluid when it's empty export interface MenuTank { fluid?: FluidVariant; amount: number; capacity: number; } // 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 and sneaking whether they hold the sneak key, both sent right away when they // change | { type: "move"; x: number; y: number; z: number; yaw: number; pitch: number; teleport?: number; swimming?: boolean; sneaking?: boolean; } // seq numbers what this client guessed for it, the server answers with block_ack. see client/level/block_predictions.ts | { type: "break_block"; x: number; y: number; z: number; seq?: 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" } // the use button let go of after a use_item or use_block, which stops using an item over time, like shooting a bow | { type: "release_use" } // right click on a block: interact with it, or place the held block against `face`. hit is where on the block it // clicked, 0 to 1 on each axis, like minecraft's click location. seq is like break_block's | { type: "use_block"; x: number; y: number; z: number; face: Faces; hit?: [number, number, number]; seq?: number } // the mouse wheel turned while looking at a scrollable block, see BlockRegistry.scrollable. amount is how many // notches, up above 0. face and hit are like use_block's | { type: "scroll_block"; x: number; y: number; z: number; face: Faces; hit: [number, number, number]; amount: number; } | { 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 } // a fluid slot of the open menu, by its index among the fluid slots, with whatever the cursor holds | { type: "click_fluid"; menu: number; slot: number } // 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[]; // the dimension's height and sky light, every level_chunk holds all of its height shape: WorldShapeJson; 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 break_block or use_block with this seq is handled, and so is every one before it. the set_blocks it caused // came first, and what they said about blocks this client guessed shows now | { type: "block_ack"; seq: number } // fields of a block's client data, see BlockRef.set_client_data. to the players who have the chunk: every field // right after it, then each field when it changes. null when a field is gone, and every field when the block is | { type: "block_data"; x: number; y: number; z: number; data: Record } // overlay shows it above the hotbar for a moment instead of in chat, like how long a pulse repeater waits | { type: "chat"; from?: string; text: string; overlay?: boolean } // 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] } // something pushed the player, like a fan: add this to how fast they're moving, in blocks per second | { type: "push"; motion: [number, number, number] } // cracks on a block a machine is breaking, from 0 to BREAK_STAGES, to the players who have its chunk. none without // a stage, and when the block changes | { type: "break_progress"; x: number; y: number; z: number; stage?: 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 tanks in menu_tanks, its data's changes in menu_data | { type: "open_menu"; id: number; menu_type: string; title: string; open_data: unknown; slots: MenuSlotInfo[]; fluid_slots: MenuFluidSlotInfo[]; data: Record; } // 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)[] } // what's in every fluid slot's tank of the open menu, when any of it changed | { type: "menu_tanks"; id: number; tanks: MenuTank[] } // all the menu's data, when any of it changed | { type: "menu_data"; id: number; data: Record } // 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 attributes their client needs, when they change: attack speed for the attack indicator under the // crosshair, and how far they reach blocks and entities | { type: "attributes"; attack_speed: number; block_interaction_range: number; entity_interaction_range: number } // whether they're using their held item over time, like drawing a bow, see ctx's player.start_using | { type: "using_item"; using: boolean } // 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;