// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the keg's input and output tanks, and menus' fluid slots, which its screen shows them in import { assertEquals } from "@std/assert"; import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts"; import { ItemStack } from "@/common/inventory.ts"; import type { FermentableRecipe } from "@/mods/bworld/scripts/fermenting.ts"; import { opened_menu, test_game } from "./helpers.ts"; const JUICE = { id: "bworld:juice" }; const BUCKET = "bworld:bucket"; const INPUT = 0; const OUTPUT = 1; // a keg at 0 100 0 next to alice async function setup() { const t = await test_game("mods"); t.join(1, "alice"); t.game.set_block(0, 99, 0, "bworld:stone"); t.game.set_block(0, 100, 0, "bworld:keg"); t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); const player = t.game.players()[0]; const bucket = EverythingRegistry.get("items", BUCKET)!.fluid_container!.capacity; const keg = () => (t.game.world.get_tile(0, 100, 0)?.mod_data ?? {}) as { input?: unknown; output?: unknown }; const open = () => { t.take(1); t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] }); return opened_menu(t.take(1)); }; // count buckets on the cursor, full of fluid or empty const hold = (count: number, fluid?: unknown) => { const stack = new ItemStack(BUCKET, count); if (fluid) stack.data = { fluid }; player.cursor.item = stack; }; const cursor = () => player.cursor.item?.to_data(); const click = (menu: { id: number }, slot: number) => t.send(1, { type: "click_fluid", menu: menu.id, slot }); const buckets = () => player.inventory.to_data().filter((item) => item?.id === BUCKET); return { t, player, bucket, keg, open, hold, cursor, click, buckets }; } Deno.test("right clicking a keg opens its input and output tanks, an arrow's width apart", async () => { const { t } = await setup(); t.take(1); t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] }); const messages = t.take(1); const menu = opened_menu(messages); assertEquals([menu.menu_type, menu.title], ["bworld:keg", "Keg"]); assertEquals(menu.slots.length, 36); assertEquals(menu.fluid_slots, [{ x: 55, y: 34 }, { x: 115, y: 34, output: true }]); // and what's in them, both empty const tanks = messages.findLast((m) => m.type === "menu_tanks"); assertEquals(tanks.id, menu.id); assertEquals(tanks.tanks.map((tank: { fluid?: unknown; amount: number }) => [tank.fluid, tank.amount]), [ [undefined, 0], [undefined, 0], ]); }); Deno.test("a full container on the cursor empties into the input, and an empty one fills from either tank", async () => { const { t, bucket, keg, open, hold, cursor, click, buckets } = await setup(); const menu = open(); // one of two empties, and goes into the inventory, the other stays on the cursor hold(2, JUICE); click(menu, INPUT); assertEquals(keg().input, { fluid: JUICE, amount: bucket }); assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); assertEquals(buckets(), [{ id: BUCKET, count: 1 }]); // the server says what's in it t.game.tick(); const tanks = t.take(1).findLast((m) => m.type === "menu_tanks").tanks; assertEquals([tanks[INPUT].fluid, tanks[INPUT].amount], [JUICE, bucket]); // nothing goes into the output click(menu, OUTPUT); assertEquals(keg().output, undefined); assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); // an empty one takes from the output, or back out of the input t.game.world.get_tile(0, 100, 0)!.mod_data = { output: { fluid: JUICE, amount: bucket + 1 } }; hold(1); click(menu, OUTPUT); assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); assertEquals(keg().output, { fluid: JUICE, amount: 1 }); hold(1); click(menu, OUTPUT); assertEquals(cursor(), { id: BUCKET, count: 1 }); t.game.world.get_tile(0, 100, 0)!.mod_data = { input: { fluid: JUICE, amount: bucket } }; click(menu, INPUT); assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); assertEquals(keg().input, undefined); // clicks for a menu that isn't open, or a fluid slot it doesn't have, do nothing hold(1, JUICE); t.send(1, { type: "click_fluid", menu: menu.id + 1, slot: INPUT }); t.send(1, { type: "click_fluid", menu: menu.id, slot: 5 }); assertEquals(keg().input, undefined); }); Deno.test("the keg's fluid handler only fills its input and only drains its output", async () => { const { t, keg } = await setup(); const handler = () => t.game.mods.block_fluid_handler(0, 100, 0, "top")!; assertEquals(handler().fill({ fluid: JUICE, amount: 100 }, "execute"), 100); assertEquals(handler().drain(100, "execute"), undefined); assertEquals(keg().input, { fluid: JUICE, amount: 100 }); t.game.world.get_tile(0, 100, 0)!.mod_data = { output: { fluid: JUICE, amount: 100 } }; assertEquals(handler().fluid_in_tank(1), { fluid: JUICE, amount: 100 }); assertEquals(handler().drain(40, "execute"), { fluid: JUICE, amount: 40 }); assertEquals(keg().output, { fluid: JUICE, amount: 60 }); }); Deno.test("a keg ferments 1 mB each time its progress reaches the recipe's ticks, a recipe's worth into all its output", async () => { const { t } = await setup(); const [recipe] = t.game.recipes.by_type.get("bworld:fermentable") as unknown as FermentableRecipe[]; const handler = () => t.game.mods.block_fluid_handler(0, 100, 0, "top")!; const tile = () => t.game.world.get_tile(0, 100, 0)!; const tanks = () => [handler().fluid_in_tank(0), handler().fluid_in_tank(1)]; const ticks = (n: number) => { for (let i = 0; i < n; i++) t.game.tick(); }; // what the first mb of a recipe's worth make const made = (mb: number) => Math.floor(mb * recipe.output.amount / recipe.input.amount); const input = (amount: number) => amount > 0 ? { fluid: recipe.input.fluid, amount } : undefined; const output = (amount: number) => amount > 0 ? { fluid: recipe.output.fluid, amount } : undefined; const amount = recipe.input.amount; handler().fill({ fluid: recipe.input.fluid, amount: amount + 10 }, "execute"); ticks(recipe.ticks - 1); assertEquals(tanks(), [input(amount + 10), undefined]); ticks(1); assertEquals(tanks(), [input(amount + 9), output(made(1))]); // a recipe's worth makes all of its output ticks(recipe.ticks * (amount - 1)); assertEquals(tanks(), [input(10), output(recipe.output.amount)]); ticks(recipe.ticks * 10 + 1); assertEquals(tanks(), [undefined, output(recipe.output.amount + made(10))]); // with nothing it can ferment, it stops ticking, and filling it wakes it assertEquals(tile().ticking, false); handler().fill({ fluid: recipe.input.fluid, amount: 1 }, "execute"); assertEquals(tile().ticking, undefined); // something without a recipe stays as it is ticks(recipe.ticks + 1); handler().fill({ fluid: { id: "bworld:wine" }, amount: 100 }, "execute"); ticks(recipe.ticks * 2); assertEquals(tanks()[0], { fluid: { id: "bworld:wine" }, amount: 100 }); }); Deno.test("a keg doesn't ferment into an output with another fluid in it", async () => { const { t } = await setup(); const [recipe] = t.game.recipes.by_type.get("bworld:fermentable") as unknown as FermentableRecipe[]; const other = { fluid: { id: "bworld:juice" }, amount: 100 }; t.game.mods.block_ref(0, 100, 0, "bworld:keg").data = { output: other }; t.game.mods.block_fluid_handler(0, 100, 0, "top")!.fill(recipe.input, "execute"); for (let i = 0; i < recipe.ticks * 2; i++) t.game.tick(); const keg = t.game.world.get_tile(0, 100, 0)!.mod_data as { input?: unknown; output?: unknown }; assertEquals([keg.input, keg.output], [recipe.input, other]); });