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
+5 -3
View File
@@ -4,6 +4,7 @@
// farmer's delight's cutting board: holds a stack of anything, shows it on top, and a knife cuts it one at a time
import { assert, assertEquals } from "@std/assert";
import { get_state_value } from "$/common/utils.ts";
import type { ItemData } from "$/common/inventory.ts";
import { stone_floor, test_game } from "./helpers.ts";
const BOARD = [0, 100, 0] as const;
@@ -26,7 +27,7 @@ async function setup(yaw = 0) {
const state = (name: string) =>
get_state_value(t.game.world.get_block_value(...BOARD), t.game.world.get_block_info(...BOARD)!, name);
const on_board = () => {
const item = t.game.world.get_tile(...BOARD)?.item;
const item = t.game.world.get_tile(...BOARD)?.client_data?.item as ItemData | undefined;
return item ? `${item.id} ${item.count}` : "";
};
const dropped = () => t.game.item_entities().map((e) => `${e.item.type_id} ${e.item.amount}`).sort();
@@ -103,7 +104,8 @@ Deno.test("what a block shows goes to players with its chunk and when it changes
t.take(1);
t.use(0);
const shown = (conn: number) =>
t.take(conn).filter((m) => m.type === "set_block_item").map((m) => m.item && `${m.item.id} ${m.item.count}`);
t.take(conn).filter((m) => m.type === "block_data" && "item" in m.data)
.map((m) => m.data.item && `${m.data.item.id} ${m.data.item.count}`);
assertEquals(shown(1), ["bworld:salmon 2"]);
// someone joining gets it with the chunk
@@ -112,7 +114,7 @@ Deno.test("what a block shows goes to players with its chunk and when it changes
assert(shown(2).includes("bworld:salmon 2"));
const loaded = await test_game("mods", t.game.save());
assertEquals(loaded.game.world.get_tile(...BOARD)?.item, { id: "bworld:salmon", count: 2 });
assertEquals(loaded.game.world.get_tile(...BOARD)?.client_data?.item, { id: "bworld:salmon", count: 2 });
// breaking it takes it away for everyone
t.game.destroy_block(...BOARD);