Send block data to clients

This commit is contained in:
2026-10-13 23:26:10 -03:00
parent a10b15a3fd
commit 55ad08266e
18 changed files with 283 additions and 90 deletions
+3
View File
@@ -29,6 +29,9 @@ export interface ClientContext {
// read only, what this client sees
world: {
get_block(x: number, y: number, z: number): Id | undefined;
// a field of the block's client data, what server scripts set with BlockRef.set_client_data, a copy.
// undefined when it isn't set or the chunk isn't loaded
get_client_data<T>(x: number, y: number, z: number, field: string): T | undefined;
// the column's biome and its season, undefined when the chunk isn't loaded
biome_at(x: number, z: number): Id | undefined;
season_at(x: number, z: number): Season | undefined;
+8 -2
View File
@@ -44,6 +44,12 @@ export interface BlockRef {
// tile data, any json value. saved with the world, never sent to clients
// deno-lint-ignore no-explicit-any
data: any;
// the part of its data clients see too, like how fast a shaft turns, by field. each is any json value, saved with
// the world, sent to players with the block's chunk and again when it's set to something else, so a field that
// changes often doesn't resend the rest. undefined removes a field, and they all go with the block. "item" is the
// item a block with item_display shows, see set_block_item. get returns a copy
get_client_data<T>(field: string): T | undefined;
set_client_data(field: string, value: unknown): void;
// turns on_tick and on_second off for this block, or back on, like a machine with nothing to do. it stays off until
// it's turned on again, saved with the world, and goes with the block
set_ticking(ticking: boolean): void;
@@ -234,8 +240,8 @@ export interface ServerWorld {
get_block_data<T>(x: number, y: number, z: number): T | undefined;
// BlockRef.set_ticking for a block, for waking one from somewhere else, like a machine getting something to work on
set_block_ticking(x: number, y: number, z: number, ticking: boolean): void;
// the item a block with item_display shows on top of it, like a block entity renderer: saved with the block's data
// and synced to players. undefined shows nothing. it goes away with the block, without dropping
// the item a block with item_display shows on top of it, like a block entity renderer: its "item" client data, see
// BlockRef.set_client_data. undefined shows nothing. it goes away with the block, without dropping
set_block_item(x: number, y: number, z: number, item: ItemStack | undefined): void;
// a copy
get_block_item(x: number, y: number, z: number): ItemStack | undefined;
+4 -4
View File
@@ -10,7 +10,7 @@ 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 = 21;
export const PROTOCOL_VERSION = 22;
export interface PlayerInfo {
id: string;
@@ -221,9 +221,9 @@ export type ServerMessage =
// 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 }
// 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 }
// 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<string, unknown> }
| { 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