Crabpot model and gui

This commit is contained in:
2026-10-11 21:12:07 -03:00
parent 7655d5a0ce
commit d4aa663836
11 changed files with 193 additions and 0 deletions
+75
View File
@@ -0,0 +1,75 @@
// the crab pot: a block with a model converted from fishermen's trap's fish trap
import { assert, assertEquals } from "@std/assert";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import { bake_block, type ModelJson } from "$/common/block_models.ts";
import { shape_at } from "$/common/block_shapes.ts";
import { get_state_value } from "$/common/utils.ts";
import { opened_menu, test_game } from "./helpers.ts";
Deno.test("a crab pot is a box 10 pixels tall and deep, drawn with its textures, turned by its facing", async () => {
await test_game("mods");
const pot = EverythingRegistry.get<BlockRegistry>("blocks", "bworld:crabpot")!;
const models = Object.fromEntries(EverythingRegistry.entries<ModelJson>("models"));
const quads = bake_block(pot, 0, models);
assertEquals(quads.length, 6);
assertEquals([...new Set(quads.map((q) => q.texture))].sort(), [
"bworld:crabpot_bottom",
"bworld:crabpot_front",
"bworld:crabpot_side",
"bworld:crabpot_top",
]);
assertEquals(shape_at(pot, 0), [[0, 0, 3 / 16, 1, 10 / 16, 13 / 16]]);
// facing east, turned a quarter
assertEquals(shape_at(pot, 1 << 16), [[3 / 16, 0, 0, 13 / 16, 10 / 16, 1]]);
});
Deno.test("a crab pot placed in water holds it, and leaves it behind when broken", async () => {
const t = await test_game("mods");
t.join(1, "alice");
for (let x = -2; x <= 2; x++) {
for (let z = -2; z <= 2; z++) {
t.game.set_block(x, 99, z, "bworld:stone");
t.game.set_block(x, 100, z, "bworld:air");
}
}
t.game.set_block(0, 100, 0, "bworld:water");
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.send(1, { type: "chat", text: "/give bworld:crabpot 2" });
t.send(1, { type: "select_slot", slot: 0 });
const waterlogged = (x: number) => {
const info = t.game.world.get_block_info(x, 100, 0)!;
return get_state_value(t.game.world.get_block_value(x, 100, 0), info, "waterlogged");
};
t.send(1, { type: "use_block", x: 0, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:crabpot");
assertEquals(waterlogged(0), 1);
// on dry ground it's dry
t.send(1, { type: "use_block", x: 1, y: 99, z: 0, face: "top", hit: [0.5, 1, 0.5] });
assertEquals(waterlogged(1), 0);
t.send(1, { type: "break_block", x: 0, y: 100, z: 0 });
assertEquals(t.game.world.get_block_id(0, 100, 0), "bworld:water");
});
Deno.test("right clicking a crab pot opens its menu: a bait slot on top and 9 output slots under it", async () => {
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:crabpot");
t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 });
t.take(1);
t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] });
const opened = opened_menu(t.take(1));
assertEquals([opened.menu_type, opened.title], ["bworld:crabpot", "Crab Pot"]);
// then the player's 36
assertEquals(opened.slots.length, 1 + 9 + 36);
assertEquals(opened.slots[0], { x: 79, y: 17, icon: "bworld:empty_slot_bait" });
assertEquals(
opened.slots.slice(1, 10).map((s: { x: number; y: number }) => [s.x, s.y]),
[
...Array(9).keys(),
].map((i) => [7 + i * 18, 45]),
);
assert(opened.slots.slice(1, 10).every((s: { output?: boolean }) => s.output));
});