// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the base game's block and item behavior: hoeing, chests, furnaces, crops and the watering can import { assert, assertEquals } from "@std/assert"; import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts"; import { sell_value } from "$/common/inventory.ts"; import { CYCLE_TICKS } from "$/common/time.ts"; import { display_name, get_state_value } from "$/common/utils.ts"; import { container_items, menu_data, menu_items, opened_menu, stone_floor, test_game } from "./helpers.ts"; // deno-lint-ignore no-explicit-any type Msg = any; type Game = Awaited>; // a player standing on a stone floor at y 99, high above the terrain, with the items given in their inventory async function setup(items: [string, number][] = []) { const t = await test_game("mods"); t.join(1, "alice"); stone_floor(t.game); t.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); for (const [id, count] of items) t.send(1, { type: "chat", text: `/give ${id} ${count}` }); return t; } // the inventory slot holding an item, from what the server sent last function slot_of(messages: Msg[], id: string) { const slot = (container_items(messages, "inventory") ?? []).findIndex((i: Msg) => i?.id === id); assert(slot >= 0, `no ${id} in the inventory`); return slot; } // clicks a slot of the open menu function click(t: Game, menu: Msg, slot: number) { t.send(1, { type: "click", menu: menu.id, slot, button: 0 }); } // the menu slot of an inventory slot: the base game's menus add the player's inventory last const inventory_slot = (menu: Msg, index: number) => menu.slots.length - 36 + index; // picks a whole inventory stack up and puts it into a slot of the open menu function move_to_menu(t: Game, menu: Msg, from: number, to: number) { click(t, menu, inventory_slot(menu, from)); click(t, menu, to); } Deno.test("a hoe turns grass and dirt into hoed dirt, anything else places nothing", async () => { const t = await setup([["bworld:wood_hoe", 1]]); t.game.set_block(1, 100, 0, "bworld:grass_block"); t.game.set_block(2, 100, 0, "bworld:dirt"); t.send(1, { type: "select_slot", slot: slot_of(t.take(1), "bworld:wood_hoe") }); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top" }); assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:hoed_dirt"); assertEquals(t.game.world.get_block_id(2, 100, 0), "bworld:hoed_dirt"); // hoed dirt stays hoed dirt, and the hand doesn't do it t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:hoed_dirt"); t.game.set_block(1, 100, 1, "bworld:grass_block"); t.send(1, { type: "select_slot", slot: 8 }); t.send(1, { type: "use_block", x: 1, y: 100, z: 1, face: "top" }); assertEquals(t.game.world.get_block_id(1, 100, 1), "bworld:grass_block"); }); Deno.test("a chest keeps items, shows them to everyone, and drops them when broken", async () => { const t = await setup([["bworld:chest", 1], ["bworld:coal", 10]]); t.game.set_block(1, 100, 0, "bworld:chest"); const coal = slot_of(t.take(1), "bworld:coal"); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); assertEquals(opened.menu_type, "bworld:storage"); assertEquals(opened.open_data, { rows: 3 }); assertEquals(opened.slots.length, 27 + 36); // nothing got placed on top assertEquals(t.game.world.get_block_id(1, 101, 0), "cubi:air"); move_to_menu(t, opened, coal, 4); assertEquals(menu_items(t.take(1))[4], { id: "bworld:coal", count: 10 }); t.send(1, { type: "close_menu", menu: opened.id }); // still there after opening it again, and after saving and loading const loaded = await test_game("mods", t.game.save()); loaded.join(1, "alice"); loaded.send(1, { type: "move", x: 0.5, y: 100, z: 0.5, yaw: 0, pitch: 0 }); loaded.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(menu_items(loaded.take(1))[4], { id: "bworld:coal", count: 10 }); // breaking it closes the menu and drops the chest and what was inside loaded.send(1, { type: "break_block", x: 1, y: 100, z: 0 }); const messages = loaded.take(1); assert(messages.some((m) => m.type === "close_menu")); const dropped = loaded.game.item_entities().map((e) => e.item.to_data()); assertEquals( dropped.sort((a, b) => a.id.localeCompare(b.id)), [{ id: "bworld:chest", count: 1 }, { id: "bworld:coal", count: 10 }], ); }); Deno.test("a shipping bin sells what's in it every morning for as few coins as it can", async () => { // oak planks sell for 2, so 70 of them and 5 copper are 145: 2 brass, 2 nickel and 1 copper const t = await setup([["bworld:oak_planks", 64], ["bworld:oak_planks", 6], ["bworld:copper_coin", 5]]); assertEquals(EverythingRegistry.get("items", "bworld:oak_planks")?.base_sell_value, 2); t.game.set_block(1, 100, 0, "bworld:shipping_bin"); const messages = t.take(1); const inventory = container_items(messages, "inventory"); const planks = inventory.flatMap((item: Msg, slot: number) => item?.id === "bworld:oak_planks" ? [slot] : []); const copper = slot_of(messages, "bworld:copper_coin"); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); assertEquals(opened.menu_type, "bworld:storage"); assertEquals(opened.title, "Shipping Bin"); assertEquals(opened.slots.length, 27 + 36); move_to_menu(t, opened, planks[0], 0); move_to_menu(t, opened, planks[1], 1); move_to_menu(t, opened, copper, 2); t.send(1, { type: "close_menu", menu: opened.id }); t.take(1); t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1); t.game.tick(); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const sold = menu_items(t.take(1)).slice(0, 27).filter((item: Msg) => item); assertEquals(sold, [ { id: "bworld:brass_coin", count: 2 }, { id: "bworld:nickel_coin", count: 2 }, { id: "bworld:copper_coin", count: 1 }, ]); }); Deno.test("a shipping bin sells a stack for what its data makes it worth, like a bottle by its juice", async () => { const t = await setup(); const [fruit] = EverythingRegistry.entries("items") .find(([, item]) => item.components?.["bworld:juiceable"] && item.base_sell_value !== undefined)!; const full = { fluid: { id: "bworld:juice", data: { item: fruit } } }; t.send(1, { type: "chat", text: `/give bworld:glass_bottle 3 ${JSON.stringify(full)}` }); t.game.set_block(1, 100, 0, "bworld:shipping_bin"); const bottles = slot_of(t.take(1), "bworld:glass_bottle"); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); move_to_menu(t, opened, bottles, 0); t.send(1, { type: "close_menu", menu: opened.id }); t.take(1); t.game.set_time((t.game.world.day + 1) * CYCLE_TICKS - 1); t.game.tick(); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const sold = menu_items(t.take(1)).slice(0, 27).filter((item: Msg) => item); const coin_value = (id: string) => EverythingRegistry.get("items", id)!.base_sell_value!; const paid = sold.reduce((total: number, coin: Msg) => total + coin.count * coin_value(coin.id), 0); const value = sell_value("bworld:glass_bottle", full, t.game.mods.fluid_rules)!; assert(value > 0); assertEquals(paid, 3 * value); }); Deno.test("a furnace burns fuel to smelt, shows its progress, and only lets the result be taken", async () => { const t = await setup([["bworld:iron_ore", 2], ["bworld:coal", 1]]); t.game.set_block(1, 100, 0, "bworld:furnace"); const messages = t.take(1); const ore = slot_of(messages, "bworld:iron_ore"); const coal = slot_of(messages, "bworld:coal"); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); assertEquals(opened.slots.slice(0, 3).map((s: Msg) => s.output ?? false), [false, false, true]); assertEquals(Object.keys(opened.data), ["progress", "progress_max", "fuel", "fuel_max"]); move_to_menu(t, opened, ore, 0); move_to_menu(t, opened, coal, 1); t.take(1); // iron takes 200 ticks for (let i = 0; i < 100; i++) t.game.tick(); const halfway = menu_data(t.take(1)); assertEquals(halfway.progress_max, 200); assert(halfway.progress >= 98 && halfway.progress <= 100, `progress ${halfway.progress}`); assertEquals(halfway.fuel_max, 1000); for (let i = 0; i < 110; i++) t.game.tick(); const items = menu_items(t.take(1)); assertEquals(items[0], { id: "bworld:iron_ore", count: 1 }); assertEquals(items[1], null, "the coal got used"); assertEquals(items[2], { id: "bworld:iron_ingot", count: 1 }); // nothing can be put into the result slot, but the result can be taken click(t, opened, 0); click(t, opened, 2); assertEquals(menu_items(t.take(1))[2], { id: "bworld:iron_ingot", count: 1 }); click(t, opened, 0); click(t, opened, 2); const after = t.take(1); assertEquals(menu_items(after)[2], null); assertEquals(after.filter((m) => m.type === "cursor").at(-1).item, { id: "bworld:iron_ingot", count: 1 }); }); Deno.test("bone meal grows wheat until it's fully grown", async () => { const t = await setup([["bworld:bone_meal", 8]]); t.game.set_block(1, 100, 0, "bworld:wheat"); const wheat = EverythingRegistry.get("blocks", "bworld:wheat")!; const age = () => get_state_value(t.game.world.get_block_value(1, 100, 0), wheat, "age")!; assertEquals(age(), 0); const slot = slot_of(t.take(1), "bworld:bone_meal"); t.send(1, { type: "select_slot", slot }); // one stage at a time, and once it's fully grown it stops using bone meal t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(age(), 1); for (let i = 0; i < 7; i++) t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(age(), 7); assertEquals(container_items(t.take(1), "inventory")[slot]?.count, 1); assertEquals(t.game.world.get_block_id(1, 101, 0), "cubi:air"); }); Deno.test("the last bone meal is used up", async () => { const t = await setup([["bworld:bone_meal", 1]]); t.game.set_block(1, 100, 0, "bworld:wheat"); const slot = slot_of(t.take(1), "bworld:bone_meal"); t.send(1, { type: "select_slot", slot }); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(container_items(t.take(1), "inventory")[slot], null); }); Deno.test("a new watering can starts empty, and players are told so in its lore", async () => { const t = await setup([["bworld:copper_watering_can", 1]]); const can = container_items(t.take(1), "inventory").find((i: Msg) => i?.id === "bworld:copper_watering_can"); assertEquals(can.data, { water: 0, max_water: 32 }); assertEquals(can.lore, "Water: 0/32"); // lore is worked out for players, it isn't saved const saved = JSON.parse(t.game.save().level); const saved_can = saved.players.alice?.inventory?.find((i: Msg) => i?.id === "bworld:copper_watering_can"); assert(!saved_can || saved_can.lore === undefined); t.game.on_disconnect(1); const after = JSON.parse(t.game.save().level).players.alice.inventory.find((i: Msg) => i?.id === "bworld:copper_watering_can" ); assertEquals(after.lore, undefined); }); Deno.test("items are called by their name, or by one made from their id", async () => { await test_game("mods"); assertEquals(display_name("bworld:iron_ingot"), "Iron Ingot"); assertEquals(display_name("bworld:stone_pickaxe"), "Stone Pickaxe"); assertEquals(display_name("bworld:hoed_dirt"), "Hoed Dirt"); EverythingRegistry.register("items", "test:thing", { texture_id: "cubi:missing", name: "A Thing" }); assertEquals(display_name("test:thing"), "A Thing"); }); Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone and a brass coin, using up exactly that", async () => { const t = await setup([["bworld:wood_pickaxe", 1], ["bworld:stone", 7], ["bworld:brass_coin", 1]]); t.game.set_block(1, 100, 0, "bworld:smithing_table"); let messages = t.take(1); const pickaxe = slot_of(messages, "bworld:wood_pickaxe"); const stone = slot_of(messages, "bworld:stone"); const coin = slot_of(messages, "bworld:brass_coin"); // mods' own, like the player's curios const containers = t.game.containers.size; t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); assertEquals(opened.slots.slice(0, 4).map((s: Msg) => s.output ?? false), [false, false, false, true]); // with only the tool there's nothing to take move_to_menu(t, opened, pickaxe, 0); t.game.tick(); click(t, opened, 3); assertEquals(t.take(1).filter((m) => m.type === "cursor").at(-1)?.item ?? null, null); // nor with the stone but not the coin move_to_menu(t, opened, stone, 1); t.game.tick(); assertEquals(menu_items(t.take(1))[3], null); // the result shows up once the coin is in, and taking it uses up the pickaxe, 5 stone and the coin move_to_menu(t, opened, coin, 2); t.game.tick(); assertEquals(menu_items(t.take(1))[3], { id: "bworld:stone_pickaxe", count: 1 }); click(t, opened, 3); messages = t.take(1); assertEquals(messages.filter((m) => m.type === "cursor").at(-1).item, { id: "bworld:stone_pickaxe", count: 1 }); assertEquals(menu_items(messages).slice(0, 4), [null, { id: "bworld:stone", count: 2 }, null, null]); // closing gives back the leftover stone, and the cursor's pickaxe goes into the inventory t.send(1, { type: "close_menu", menu: opened.id }); const inventory = container_items(t.take(1), "inventory").filter((i: Msg) => i); assertEquals( inventory.sort((a: Msg, b: Msg) => a.id.localeCompare(b.id)), [{ id: "bworld:stone", count: 2 }, { id: "bworld:stone_pickaxe", count: 1 }], ); assertEquals(t.game.containers.size, containers, "its slots are gone"); }); Deno.test("a smithing addition needs its whole count, and all of it is used up", async () => { const t = await setup([["bworld:stone_pickaxe", 1], ["bworld:copper_ingot", 5], ["bworld:brass_coin", 6]]); t.game.set_block(1, 100, 0, "bworld:smithing_table"); const messages = t.take(1); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); move_to_menu(t, opened, slot_of(messages, "bworld:stone_pickaxe"), 0); move_to_menu(t, opened, slot_of(messages, "bworld:copper_ingot"), 1); // right-clicking the coins onto the slot puts down one at a time const coins = inventory_slot(opened, slot_of(messages, "bworld:brass_coin")); click(t, opened, coins); for (let i = 0; i < 4; i++) t.send(1, { type: "click", menu: opened.id, slot: 2, button: 2 }); t.game.tick(); assertEquals(menu_items(t.take(1))[3], null); t.send(1, { type: "click", menu: opened.id, slot: 2, button: 2 }); t.game.tick(); assertEquals(menu_items(t.take(1))[3], { id: "bworld:copper_pickaxe", count: 1 }); click(t, opened, coins); click(t, opened, 3); assertEquals(menu_items(t.take(1)).slice(0, 4), [null, null, null, null]); }); Deno.test("a smithing result can't be taken once its inputs are gone, even before it updates", async () => { const t = await setup([["bworld:copper_pickaxe", 1], ["bworld:iron_ingot", 5]]); t.game.set_block(1, 100, 0, "bworld:smithing_table"); const messages = t.take(1); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); move_to_menu(t, opened, slot_of(messages, "bworld:copper_pickaxe"), 0); move_to_menu(t, opened, slot_of(messages, "bworld:iron_ingot"), 1); t.game.tick(); // take the pickaxe back out and try the result in the same tick click(t, opened, 0); click(t, opened, inventory_slot(opened, 20)); click(t, opened, 3); const cursor = t.take(1).filter((m) => m.type === "cursor").at(-1).item; assertEquals(cursor, null); // and an extra item in the third slot doesn't match a recipe without one click(t, opened, inventory_slot(opened, 20)); click(t, opened, 0); t.send(1, { type: "chat", text: "/give bworld:coal 1" }); const coal = slot_of(t.take(1), "bworld:coal"); move_to_menu(t, opened, coal, 2); t.game.tick(); assertEquals(menu_items(t.take(1))[3], null); }); Deno.test("a stonecutter cuts its input into the recipe picked with a button, one at a time", async () => { const t = await setup([["bworld:cobblestone", 3], ["bworld:dirt", 1]]); t.game.set_block(1, 100, 0, "bworld:stonecutter"); const messages = t.take(1); const cobblestone = slot_of(messages, "bworld:cobblestone"); const dirt = slot_of(messages, "bworld:dirt"); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); const opened = opened_menu(t.take(1)); const press = (button: number) => t.send(1, { type: "menu_button", menu: opened.id, button }); const cursor = (messages: Msg[]) => messages.filter((m) => m.type === "cursor").at(-1)?.item ?? null; // nothing to take until a recipe is picked, and a button with no recipe picks nothing. data is only sent when it // changes assertEquals(opened.data.selected, -1); move_to_menu(t, opened, cobblestone, 0); t.game.tick(); press(99); t.game.tick(); let latest = t.take(1); assertEquals(menu_data(latest), undefined); assertEquals(menu_items(latest)[1], null); press(0); t.game.tick(); latest = t.take(1); assertEquals(menu_data(latest).selected, 0); const result = menu_items(latest)[1]; assert(result, "the picked recipe's output shows"); // taking it uses up one, and the next is ready a tick later click(t, opened, 1); latest = t.take(1); assertEquals(cursor(latest), result); assertEquals(menu_items(latest)[0], { id: "bworld:cobblestone", count: 2 }); t.game.tick(); assertEquals(menu_items(t.take(1))[1], result); // another item forgets the pick, and closing gives back what's in the input click(t, opened, inventory_slot(opened, 20)); move_to_menu(t, opened, dirt, 0); t.game.tick(); latest = t.take(1); assertEquals(menu_data(latest).selected, -1); assertEquals(menu_items(latest)[1], null); t.send(1, { type: "close_menu", menu: opened.id }); const inventory = container_items(t.take(1), "inventory").filter((i: Msg) => i); assertEquals( inventory.map((i: Msg) => i.id).sort(), ["bworld:cobblestone", "bworld:dirt", result.id].sort(), ); }); Deno.test("a bucket picks up a water source but not flowing water, and a water bucket puts a source back", async () => { const t = await setup([["bworld:bucket", 1]]); const held = () => t.game.players()[0].held_item?.type_id; t.game.set_block(1, 100, 0, "bworld:water"); // flowing water a block from it, the flow keeps it there t.game.set_block(2, 100, 0, "bworld:water", undefined, 1); t.send(1, { type: "use_block", x: 2, y: 100, z: 0, face: "top" }); assertEquals(held(), "bworld:bucket"); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(held(), "bworld:water_bucket"); assertEquals(t.game.world.get_block_id(1, 100, 0), "cubi:air"); t.send(1, { type: "use_block", x: -2, y: 99, z: 0, face: "top" }); assertEquals(held(), "bworld:bucket"); assertEquals(t.game.world.get_block_id(-2, 100, 0), "bworld:water"); assertEquals(t.game.world.get_block_value(-2, 100, 0) >>> 16, 0); }); Deno.test("only an empty bucket picks up water, and from a stack the water bucket goes in the inventory", async () => { const t = await setup(); const player = t.game.players()[0]; const slots = () => player.inventory.to_data().flatMap((item) => item ? [[item.id, item.count]] : []); t.game.set_block(1, 100, 0, "bworld:water"); t.game.give(player, "bworld:bucket", 1, { fluid: { id: "bworld:juice" } }); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(t.game.world.get_block_id(1, 100, 0), "bworld:water"); assertEquals(slots(), [["bworld:bucket", 1]]); player.inventory.set_item(0, undefined); t.game.give(player, "bworld:bucket", 3); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(t.game.world.get_block_id(1, 100, 0), "cubi:air"); assertEquals(slots(), [["bworld:bucket", 2], ["bworld:water_bucket", 1]]); }); Deno.test("a bucket picks up a lava source too, and a lava bucket puts one back", async () => { const t = await setup([["bworld:bucket", 1]]); const held = () => t.game.players()[0].held_item?.type_id; t.game.set_block(1, 100, 0, "bworld:lava"); t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); assertEquals(held(), "bworld:lava_bucket"); assertEquals(t.game.world.get_block_id(1, 100, 0), "cubi:air"); t.send(1, { type: "use_block", x: -2, y: 99, z: 0, face: "top" }); assertEquals(held(), "bworld:bucket"); assertEquals(t.game.world.get_block_id(-2, 100, 0), "bworld:lava"); assertEquals(t.game.world.get_block_value(-2, 100, 0) >>> 16, 0); });