Fluid slots and kegs !
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
// 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 { 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<ItemRegistry>("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 });
|
||||
});
|
||||
Reference in New Issue
Block a user