// 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 = 9; 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; } // 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; // x, y, z, block id, and its state bits when they aren't 0 export type BlockChange = [number, number, number, string, number?]; // the containers a client can see and click. "screen" is whatever the open server screen shows export type ContainerKey = "inventory" | "crafting" | "screen"; export const CRAFTING_RESULT_SLOT = 9; // a screen the server opens, drawn below the player's inventory and hotbar export interface ScreenLayout { // height of the screen's own area, in slots rows: number; // x and y in slots, can be fractional // output slots can only be taken from, like the furnace result slots: { index: number; x: number; y: number; output?: boolean }[]; // progress bars filled from properties[value] / properties[max] bars: { x: number; y: number; value: string; max: string; direction: "up" | "right"; empty_texture: string; full_texture: 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 } | { type: "move"; x: number; y: number; z: number; yaw: number; pitch: number } | { 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 } // 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 } // shift moves the stack to the other part of the screen instead, like minecraft's quick move | { type: "click"; container: ContainerKey; index: number; button: number; shift?: boolean } // q on a slot (the held one when no screen is open) throws one item, or the whole stack with ctrl | { type: "drop_item"; container: ContainerKey; index: number; all: boolean } // closes the open screen, including the player's own inventory screen | { type: "close_screen" } | { 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 }; 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; // 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 } // 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 } | { 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) | { type: "entity_event"; id: string; event: "hurt" | "death" } // 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 } | { type: "teleport"; x: number; y: number; z: number } // the player was knocked back: their speed is this now, in blocks per second | { type: "set_motion"; vx: number; vy: number; vz: number } | { type: "container"; container: ContainerKey; items: (ItemData | null)[] } | { type: "cursor"; item: ItemData | null } | { type: "open_screen"; layout: ScreenLayout; properties: Record } | { type: "screen_properties"; properties: Record } | { type: "close_screen" } // 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, hunger and saturation, when they change | { type: "vitals"; health: number; food: number; saturation: number }; export const MAX_NAME_LENGTH = 16; export const MAX_CHAT_LENGTH = 256;