Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+10 -9
View File
@@ -1,7 +1,7 @@
// artifacts: items worn in curios slots that change the wearer's attributes, see mods/artifacts
import { assertEquals } from "@std/assert";
import { ATTRIBUTES } from "$/server/game/entity/attributes.ts";
import { test_game } from "./helpers.ts";
import { menu_items, opened_menu, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
@@ -18,16 +18,17 @@ async function setup() {
Deno.test("the power glove and feral claws go in the hands slot, and only work while worn", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
const screen = t.take(1).find((m: Msg) => m.type === "open_screen");
assertEquals(screen.layout.slots.map((s: Msg) => s.icon), ["curios:empty_hands_slot"]);
const menu = opened_menu(t.take(1));
// the hands slot, then the inventory's
assertEquals(menu.slots.map((s: Msg) => s.icon).filter(Boolean), ["curios:empty_hands_slot"]);
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
t.send(1, { type: "click", menu: menu.id, slot: 1, button: 0, shift: true });
t.game.tick();
assertEquals(t.alice.attribute(ATTRIBUTES.attack_damage), 1 + 4);
// swapped for the claws by hand: the glove's damage goes, the claws' speed comes
t.send(1, { type: "click", container: "inventory", index: 1, button: 0 });
t.send(1, { type: "click", container: "screen", index: 6, button: 0 });
t.send(1, { type: "click", menu: menu.id, slot: 2, button: 0 });
t.send(1, { type: "click", menu: menu.id, slot: 0, button: 0 });
t.game.tick();
assertEquals(t.alice.attribute(ATTRIBUTES.attack_damage), 1);
assertEquals(t.alice.attribute(ATTRIBUTES.attack_speed), 4 * 1.3);
@@ -36,9 +37,9 @@ Deno.test("the power glove and feral claws go in the hands slot, and only work w
Deno.test("worn artifacts work again after rejoining, and say what they do", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
const curios = t.take(1).filter((m: Msg) => m.type === "container" && m.container === "screen").at(-1).items;
assertEquals(curios[6].lore, "When worn:\n+4 Attack Damage");
const { id } = opened_menu(t.take(1));
t.send(1, { type: "click", menu: id, slot: 1, button: 0, shift: true });
assertEquals(menu_items(t.take(1))[0].lore, "When worn:\n+4 Attack Damage");
t.game.on_disconnect(1);
t.join(1, "alice");
+58 -54
View File
@@ -2,7 +2,7 @@
import { assert, assertEquals } from "@std/assert";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import { display_name, get_state_value } from "$/common/utils.ts";
import { test_game } from "./helpers.ts";
import { menu_data, menu_items, opened_menu, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
@@ -30,10 +30,18 @@ function slot_of(messages: Msg[], id: string) {
return slot;
}
// picks a whole inventory stack up and puts it into a slot of the open screen
function move_to_screen(t: Game, from: number, to: number) {
t.send(1, { type: "click", container: "inventory", index: from, button: 0 });
t.send(1, { type: "click", container: "screen", index: to, button: 0 });
// 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 () => {
@@ -62,27 +70,28 @@ Deno.test("a chest keeps items, shows them to everyone, and drops them when brok
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 = t.take(1).find((m) => m.type === "open_screen");
assertEquals(opened.layout.rows, 3);
assertEquals(opened.layout.slots.length, 27);
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), "bworld:air");
move_to_screen(t, coal, 4);
assertEquals(last_container(t.take(1), "screen")[4], { id: "bworld:coal", count: 10 });
t.send(1, { type: "close_screen" });
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(last_container(loaded.take(1), "screen")[4], { id: "bworld:coal", count: 10 });
assertEquals(menu_items(loaded.take(1))[4], { id: "bworld:coal", count: 10 });
// breaking it closes the screen and drops the chest and what was inside
// 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_screen"));
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)),
@@ -98,36 +107,35 @@ Deno.test("a furnace burns fuel to smelt, shows its progress, and only lets the
const coal = slot_of(messages, "bworld:coal");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = t.take(1).find((m) => m.type === "open_screen");
assertEquals(opened.layout.slots.map((s: Msg) => s.index), [0, 1, 2]);
assertEquals(opened.layout.slots[2].output, true);
assertEquals(opened.layout.bars.map((b: Msg) => b.value), ["fuel", "progress"]);
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_screen(t, ore, 0);
move_to_screen(t, coal, 1);
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 = t.take(1).filter((m) => m.type === "screen_properties").at(-1).properties;
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 screen = last_container(t.take(1), "screen");
assertEquals(screen[0], { id: "bworld:iron_ore", count: 1 });
assertEquals(screen[1], null, "the coal got used");
assertEquals(screen[2], { id: "bworld:iron_ingot", count: 1 });
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
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
t.send(1, { type: "click", container: "screen", index: 2, button: 0 });
assertEquals(last_container(t.take(1), "screen")[2], { id: "bworld:iron_ingot", count: 1 });
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
t.send(1, { type: "click", container: "screen", index: 2, button: 0 });
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(last_container(after, "screen")[2], null);
assertEquals(menu_items(after)[2], null);
assertEquals(after.filter((m) => m.type === "cursor").at(-1).item, { id: "bworld:iron_ingot", count: 1 });
});
@@ -192,31 +200,26 @@ Deno.test("a smithing table upgrades a wooden pickaxe with 5 stone, using up exa
const stone = slot_of(messages, "bworld:stone");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const opened = t.take(1).find((m) => m.type === "open_screen");
assertEquals(opened.layout.slots.map((s: Msg) => [s.index, s.output ?? false]), [
[0, false],
[1, false],
[2, false],
[3, true],
]);
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_screen(t, pickaxe, 0);
move_to_menu(t, opened, pickaxe, 0);
t.game.tick();
t.send(1, { type: "click", container: "screen", index: 3, button: 0 });
click(t, opened, 3);
assertEquals(t.take(1).filter((m) => m.type === "cursor").at(-1)?.item ?? null, null);
// the result shows up once there's enough stone, and taking it uses up the pickaxe and 5 stone
move_to_screen(t, stone, 1);
move_to_menu(t, opened, stone, 1);
t.game.tick();
assertEquals(last_container(t.take(1), "screen")[3], { id: "bworld:stone_pickaxe", count: 1 });
t.send(1, { type: "click", container: "screen", index: 3, button: 0 });
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(last_container(messages, "screen"), [null, { id: "bworld:stone", count: 2 }, null, null]);
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_screen" });
t.send(1, { type: "close_menu", menu: opened.id });
const inventory = last_container(t.take(1), "inventory").filter((i: Msg) => i);
assertEquals(
inventory.sort((a: Msg, b: Msg) => a.id.localeCompare(b.id)),
@@ -230,23 +233,24 @@ Deno.test("a smithing result can't be taken once its inputs are gone, even befor
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" });
move_to_screen(t, slot_of(messages, "bworld:wood_pickaxe"), 0);
move_to_screen(t, slot_of(messages, "bworld:stone"), 1);
const opened = opened_menu(t.take(1));
move_to_menu(t, opened, slot_of(messages, "bworld:wood_pickaxe"), 0);
move_to_menu(t, opened, slot_of(messages, "bworld:stone"), 1);
t.game.tick();
// take the pickaxe back out and try the result in the same tick
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
t.send(1, { type: "click", container: "inventory", index: 20, button: 0 });
t.send(1, { type: "click", container: "screen", index: 3, button: 0 });
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
t.send(1, { type: "click", container: "inventory", index: 20, button: 0 });
t.send(1, { type: "click", container: "screen", index: 0, button: 0 });
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_screen(t, coal, 2);
move_to_menu(t, opened, coal, 2);
t.game.tick();
assertEquals(last_container(t.take(1), "screen")[3], null);
assertEquals(menu_items(t.take(1))[3], null);
});
+12 -2
View File
@@ -43,7 +43,7 @@ Deno.test("packing a mod keeps its data, scripts, textures and credits, and give
const bworld = read_bmod(Deno.readFileSync(`${folder}/bworld.bmod`), "bworld.bmod");
assertEquals(bworld.data.blocks.length, 42);
assertEquals(Object.keys(bworld.scripts), ["server"]);
assertEquals(Object.keys(bworld.scripts), ["server", "client"]);
Deno.removeSync(sources, { recursive: true });
Deno.removeSync(folder, { recursive: true });
@@ -240,7 +240,17 @@ Deno.test("the atlas fits textures of any size, tallest first, without overlappi
Deno.test("a mod with only data packs and reads", () => {
const bmod = {
manifest: { format_version: 1, id: "tiny", name: "Tiny", version: "1.0.0" },
data: { blocks: [], models: [], items: [], recipes: [], ores: [], spawns: [], entities: [], entity_models: [], tags: [] },
data: {
blocks: [],
models: [],
items: [],
recipes: [],
ores: [],
spawns: [],
entities: [],
entity_models: [],
tags: [],
},
scripts: {},
textures: new Map(),
};
+17 -15
View File
@@ -1,6 +1,6 @@
// curios: accessory slots for items in the curios tags, opened over mod channels, see mods/curios
import { assert, assertEquals } from "@std/assert";
import { copy_dir, test_game } from "./helpers.ts";
import { copy_dir, menu_items, opened_menu, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
@@ -35,40 +35,42 @@ async function setup() {
Deno.test("the curios screen shows the slot types some item goes in, with their icons", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
const screen = t.take(1).find((m) => m.type === "open_screen");
assertEquals(screen.layout.slots, [
{ index: 0, x: 3.5, y: 0, icon: "curios:empty_curio_slot" },
{ index: 7, x: 4.5, y: 0, icon: "curios:empty_ring_slot" },
const menu = opened_menu(t.take(1));
assertEquals(menu.menu_type, "curios:curios");
// in the middle of the row, then the player's inventory
assertEquals(menu.slots.slice(0, 2), [
{ x: 70, y: 17, icon: "curios:empty_curio_slot" },
{ x: 88, y: 17, icon: "curios:empty_ring_slot" },
]);
assertEquals(menu.slots.length, 2 + 36);
});
Deno.test("items only go in the slots they fit, and fall out when the player dies", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: {} });
t.take(1);
const { id } = opened_menu(t.take(1));
// coal fits nowhere, the ring only in the ring slot, and #curios:curio items in any slot, the curio slot first
t.send(1, { type: "click", container: "inventory", index: 2, button: 0, shift: true });
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
t.send(1, { type: "click", container: "inventory", index: 1, button: 0, shift: true });
// coal fits nowhere, the ring only in the ring slot, and #curios:curio items in any slot, the curio slot first.
// the inventory's slots come after the 2 curios ones
for (const index of [2, 0, 1]) t.send(1, { type: "click", menu: id, slot: 2 + index, button: 0, shift: true });
const messages = t.take(1);
assertEquals(last_container(messages, "inventory").slice(0, 3), [null, null, { id: "bworld:coal", count: 1 }]);
const curios = last_container(messages, "screen");
const curios = menu_items(messages);
assertEquals(curios[0], { id: "jewelry:amulet", count: 1 });
assertEquals(curios[7], { id: "jewelry:ring", count: 1 });
assertEquals(curios[1], { id: "jewelry:ring", count: 1 });
const player = t.game.players()[0];
t.game.damage_player(player, 100, "fall");
const dropped = t.game.item_entities().map((e) => e.item.type_id).sort();
assertEquals(dropped, ["bworld:coal", "jewelry:amulet", "jewelry:ring"]);
t.send(1, { type: "mod", channel: "curios:open", data: {} });
assertEquals(last_container(t.take(1), "screen").filter(Boolean), []);
assertEquals(menu_items(t.take(1)).filter(Boolean), []);
});
Deno.test("mod messages that are too big or too many are dropped", async () => {
const t = await setup();
t.send(1, { type: "mod", channel: "curios:open", data: "x".repeat(20_000) });
assertEquals(t.take(1).filter((m) => m.type === "open_screen"), []);
assertEquals(t.take(1).filter((m) => m.type === "open_menu"), []);
for (let i = 0; i < 100; i++) t.send(1, { type: "mod", channel: "curios:open", data: {} });
assert(t.take(1).filter((m) => m.type === "open_screen").length < 100);
assert(t.take(1).filter((m) => m.type === "open_menu").length < 100);
});
+13
View File
@@ -107,3 +107,16 @@ export function copy_dir(from: string, to: string) {
Deno.copyFileSync(from, to);
}
}
// menus, see server/game/menu.ts. menu 0 is the player's inventory (slots 0-35) and crafting grid (36-45)
// deno-lint-ignore no-explicit-any
type Msg = any;
// the last menu opened among messages
export const opened_menu = (messages: Msg[]) => messages.findLast((m) => m.type === "open_menu");
// what the open menu's slots last held among messages, by menu slot index
export const menu_items = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_content")?.items;
// what the open menu's data last was among messages
export const menu_data = (messages: Msg[]) => messages.findLast((m) => m.type === "menu_data")?.data;
+5 -4
View File
@@ -1,4 +1,5 @@
import { assert, assertEquals } from "@std/assert";
import { CRAFTING_RESULT_SLOT, PLAYER_MENU_CRAFTING } from "$/common/protocol.ts";
import { ItemStack } from "$/common/inventory.ts";
import { test_game } from "./helpers.ts";
@@ -95,7 +96,7 @@ Deno.test("q throws one of an item the way the player looks, ctrl throws the who
send(1, { type: "chat", text: "/give bworld:stone 5" });
take(1);
send(1, { type: "drop_item", container: "inventory", index: 0, all: false });
send(1, { type: "drop_item", menu: 0, slot: 0, all: false });
const [thrown] = game.item_entities();
assertEquals(thrown.item.amount, 1);
assert(thrown.vz < -5 && Math.abs(thrown.vx) < 1, "thrown forwards");
@@ -105,13 +106,13 @@ Deno.test("q throws one of an item the way the player looks, ctrl throws the who
assert(thrown.z < -1, "it flew away from the player");
assertEquals(game.item_entities().length, 1, "and isn't picked straight back up");
send(1, { type: "drop_item", container: "inventory", index: 0, all: true });
send(1, { type: "drop_item", menu: 0, slot: 0, all: true });
assertEquals(game.item_entities().map((entity) => entity.item.amount).sort(), [1, 4]);
assertEquals(inventory_count(take(1), "bworld:stone"), 0);
// empty slots and the crafting result don't throw anything
send(1, { type: "drop_item", container: "inventory", index: 0, all: true });
send(1, { type: "drop_item", container: "crafting", index: 9, all: true });
send(1, { type: "drop_item", menu: 0, slot: 0, all: true });
send(1, { type: "drop_item", menu: 0, slot: PLAYER_MENU_CRAFTING + CRAFTING_RESULT_SLOT, all: true });
assertEquals(game.item_entities().length, 2);
});
+4 -3
View File
@@ -6,7 +6,7 @@ import { generate_raw_chunk } from "$/common/generation.ts";
import { block_variant } from "$/common/block_models.ts";
import { load_worldgen } from "$/common/worldgen_loader.ts";
import { create_mod } from "$/tools/new_mod.ts";
import { PROTOCOL_VERSION } from "$/common/protocol.ts";
import { PLAYER_MENU_CRAFTING, PROTOCOL_VERSION } from "$/common/protocol.ts";
import { copy_dir, test_game } from "./helpers.ts";
// mods/bworld plus a mod made from the template, in a temp folder
@@ -101,8 +101,9 @@ Deno.test("a mod made from the template loads and runs", async () => {
// the shaped recipe: 4 example items in a square
send(1, { type: "chat", text: "/give copper_tools:example_item 4" });
const items_slot = inventory_of(take(1)).findIndex((i: Msg) => i?.id === "copper_tools:example_item");
send(1, { type: "click", container: "inventory", index: items_slot, button: 0 });
for (const slot of [0, 1, 3, 4]) send(1, { type: "click", container: "crafting", index: slot, button: 2 });
// the player's own menu: the inventory, then the crafting grid
send(1, { type: "click", menu: 0, slot: items_slot, button: 0 });
for (const slot of [0, 1, 3, 4]) send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING + slot, button: 2 });
const crafting = take(1).filter((m) => m.container === "crafting").at(-1).items;
assertEquals(crafting[9]?.id, "copper_tools:example_block");
+55 -29
View File
@@ -1,10 +1,10 @@
import { assert, assertEquals } from "@std/assert";
import { copy_dir, sent_block, test_game } from "./helpers.ts";
import { copy_dir, menu_data, menu_items, opened_menu, sent_block, test_game } from "./helpers.ts";
import type { WorldSave } from "$/server/game/saves.ts";
// a mod that uses every part of the server api worth testing, and writes down what happened in its storage
const SERVER_SCRIPT = `
import type { ServerContext } from "bworld/server";
import type { BlockRef, ServerContext } from "bworld/server";
export function setup(ctx: ServerContext) {
const log = (key: string, value: unknown = 1) => {
const all = ctx.storage.get<Record<string, unknown[]>>("log") ?? {};
@@ -26,16 +26,22 @@ export function setup(ctx: ServerContext) {
on_click(block, _params, player) { log("click", [block.id, player.name]); },
});
// a box that only takes coal, counts how often it's opened, and gives up its items when broken
const coal_box = ctx.menus.register<BlockRef>("testmod:coal_box", (menu, block) => {
const container = ctx.containers.get(block.data.container)!;
menu.title = "Coal Box";
menu.add_slot(container, 0, { x: 7, y: 17, filter: (item) => item.id === "bworld:coal" });
menu.add_slot(container, 1, { x: 25, y: 17 });
menu.add_player_inventory(7, 49);
menu.add_data("opened", () => block.data.opened);
menu.on_button((player, id) => log("button", [player.name, id]));
menu.still_valid_near(block);
});
ctx.components.register_block("testmod:coal_box", {
on_interact(block, _params, player) {
block.data ??= { container: ctx.containers.create(2).id, opened: 0 };
block.data.opened += 1;
const screen = ctx.ui.open_container(player, {
container: ctx.containers.get(block.data.container)!,
layout: [{ slot: 0, x: 0, y: 0, filter: (item) => item.id === "bworld:coal" }, { slot: 1, x: 1, y: 0 }],
});
screen.set_property("opened", block.data.opened);
screen.on_close(() => log("closed", screen.open));
const menu = coal_box.open(player, block);
menu.on_close(() => log("closed", menu.open));
return true;
},
on_break(block) {
@@ -75,7 +81,7 @@ export function setup(ctx: ServerContext) {
},
});
// fabric's way
// brigadier's way, like neoforge's
const { literal, argument, types } = ctx.commands;
ctx.commands.register(
literal("shout")
@@ -278,7 +284,7 @@ Deno.test("/tps reports the loop, and mods can't take engine commands", async ()
Deno.removeSync(clash, { recursive: true });
});
Deno.test("mods build commands fabric's way: typed arguments, suggestions, requirements and aliases", async () => {
Deno.test("mods build commands brigadier's way, like neoforge's: typed arguments, suggestions, requirements and aliases", async () => {
const { join, send, take, dir } = await setup();
join(2, "bob");
take(1);
@@ -319,7 +325,7 @@ Deno.test("mods build commands fabric's way: typed arguments, suggestions, requi
Deno.removeSync(dir, { recursive: true });
});
Deno.test("container screens: filters, properties, closing, saving, and dropping what's inside", async () => {
Deno.test("menus: filters, data, buttons, closing, saving, and dropping what's inside", async () => {
const { game, send, take, log, dir } = await setup();
game.set_block(1, 100, 0, "testmod:coal_box");
send(1, { type: "chat", text: "/give bworld:coal 3" });
@@ -328,38 +334,58 @@ Deno.test("container screens: filters, properties, closing, saving, and dropping
send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
let messages = take(1);
const opened = messages.find((m) => m.type === "open_screen");
assertEquals(opened.layout, { rows: 1, slots: [{ index: 0, x: 0, y: 0 }, { index: 1, x: 1, y: 0 }], bars: [] });
assertEquals(messages.find((m) => m.type === "screen_properties").properties, { opened: 1 });
const opened = opened_menu(messages);
assertEquals(opened.menu_type, "testmod:coal_box");
assertEquals(opened.title, "Coal Box");
assertEquals(opened.slots.slice(0, 2), [{ x: 7, y: 17 }, { x: 25, y: 17 }]);
// the hotbar's first slot, under the rest of the inventory
assertEquals(opened.slots[2], { x: 7, y: 49 + 58 });
assertEquals(opened.data, { opened: 1 });
// stone doesn't go into the coal slot, but does into the other one. coal goes in
const screen = (messages: { type: string; container?: string; items?: unknown[] }[]) =>
messages.filter((m) => m.type === "container" && m.container === "screen").at(-1)?.items;
send(1, { type: "click", container: "inventory", index: 1, button: 0 });
send(1, { type: "click", container: "screen", index: 0, button: 0 });
send(1, { type: "click", container: "screen", index: 1, button: 0 });
send(1, { type: "click", container: "inventory", index: 0, button: 0 });
send(1, { type: "click", container: "screen", index: 0, button: 0 });
// stone doesn't go into the coal slot, but does into the other one. coal goes in. the inventory's slots come after
// the box's 2
const click = (slot: number, menu = opened.id) => send(1, { type: "click", menu, slot, button: 0 });
click(2 + 1);
click(0);
click(1);
click(2 + 0);
click(0);
messages = take(1);
assertEquals(screen(messages), [{ id: "bworld:coal", count: 3 }, { id: "bworld:stone", count: 1 }]);
assertEquals(menu_items(messages).slice(0, 2), [{ id: "bworld:coal", count: 3 }, { id: "bworld:stone", count: 1 }]);
send(1, { type: "close_screen" });
// clicks and buttons for a menu that isn't open are ignored
click(0, opened.id + 1);
assertEquals(take(1).filter((m) => m.type === "cursor"), []);
send(1, { type: "menu_button", menu: opened.id + 1, button: 7 });
send(1, { type: "menu_button", menu: opened.id, button: 3 });
assertEquals(log().button, [["alice", 3]]);
send(1, { type: "close_menu", menu: opened.id });
assertEquals(log().closed, [false]);
// walking away closes it
send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
game.teleport(game.players()[0], 20.5, 100, 0.5);
game.tick();
assert(take(1).some((m) => m.type === "close_menu"));
assertEquals(log().closed, [false, false]);
// the items and the block's data are saved
const loaded = await test_game(dir, 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" });
messages = loaded.take(1);
assertEquals(screen(messages), [{ id: "bworld:coal", count: 3 }, { id: "bworld:stone", count: 1 }]);
assertEquals(messages.find((m) => m.type === "screen_properties").properties, { opened: 2 });
assertEquals(menu_items(messages).slice(0, 2), [{ id: "bworld:coal", count: 3 }, { id: "bworld:stone", count: 1 }]);
assertEquals(opened_menu(messages).data, { opened: 3 });
loaded.game.tick();
assertEquals(menu_data(loaded.take(1)), undefined, "data is only sent again when it changes");
// breaking it closes the screen, runs on_close, and drops what was inside
// breaking it closes the menu, runs on_close, and drops what was inside
loaded.send(1, { type: "break_block", x: 1, y: 100, z: 0 });
assert(loaded.take(1).some((m) => m.type === "close_screen"));
assert(loaded.take(1).some((m) => m.type === "close_menu"));
const logged = loaded.game.mods.storage.testmod.log as Record<string, unknown[]>;
assertEquals(logged.closed, [false, false]);
assertEquals(logged.closed, [false, false, false]);
const dropped = loaded.game.item_entities().map((e) => e.item.to_data().id).sort();
assertEquals(dropped, ["bworld:coal", "bworld:stone"]);
assertEquals(loaded.game.containers.size, 0);
+26 -22
View File
@@ -1,7 +1,7 @@
// shift clicking a slot moves its stack to the other part of the screen, like minecraft's quick move
import { assertEquals } from "@std/assert";
import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts";
import { test_game } from "./helpers.ts";
import { CRAFTING_RESULT_SLOT, PLAYER_MENU_CRAFTING } from "$/common/protocol.ts";
import { menu_items, opened_menu, test_game } from "./helpers.ts";
// deno-lint-ignore no-explicit-any
type Msg = any;
@@ -24,13 +24,13 @@ async function setup(items: [string, number][] = []) {
Deno.test("without a container open, shift clicking moves between the hotbar and the rest of the inventory", async () => {
const t = await setup([["bworld:coal", 10], ["bworld:stick", 5]]);
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
let inventory = last_container(t.take(1), "inventory");
assertEquals(inventory[0], null);
assertEquals(inventory[9], { id: "bworld:coal", count: 10 });
// back down it goes to the first free hotbar slot, the sticks are in the second
t.send(1, { type: "click", container: "inventory", index: 9, button: 0, shift: true });
t.send(1, { type: "click", menu: 0, slot: 9, button: 0, shift: true });
inventory = last_container(t.take(1), "inventory");
assertEquals(inventory[0], { id: "bworld:coal", count: 10 });
assertEquals(inventory[1], { id: "bworld:stick", count: 5 });
@@ -40,9 +40,9 @@ Deno.test("without a container open, shift clicking moves between the hotbar and
Deno.test("shift clicking tops up stacks of the same item before using empty slots", async () => {
const t = await setup([["bworld:coal", 20]]);
// half of it to the main inventory by hand, then the other half shift clicked onto it
t.send(1, { type: "click", container: "inventory", index: 0, button: 2 });
t.send(1, { type: "click", container: "inventory", index: 9, button: 0 });
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
t.send(1, { type: "click", menu: 0, slot: 0, button: 2 });
t.send(1, { type: "click", menu: 0, slot: 9, button: 0 });
t.send(1, { type: "click", menu: 0, slot: 0, button: 0, shift: true });
const inventory = last_container(t.take(1), "inventory");
assertEquals(inventory[0], null);
assertEquals(inventory[9], { id: "bworld:coal", count: 20 });
@@ -53,16 +53,18 @@ Deno.test("with a chest open, shift clicking moves between it and the inventory"
const t = await setup([["bworld:coal", 10]]);
t.game.set_block(1, 100, 0, "bworld:chest");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const { id } = opened_menu(t.take(1));
t.send(1, { type: "click", container: "inventory", index: 0, button: 0, shift: true });
// the chest's 27 slots, then the inventory's
t.send(1, { type: "click", menu: id, slot: 27, button: 0, shift: true });
let messages = t.take(1);
assertEquals(last_container(messages, "inventory")[0], null);
assertEquals(last_container(messages, "screen")[0], { id: "bworld:coal", count: 10 });
assertEquals(menu_items(messages)[0], { id: "bworld:coal", count: 10 });
// like minecraft, out of a container it fills the hotbar from the right
t.send(1, { type: "click", container: "screen", index: 0, button: 0, shift: true });
t.send(1, { type: "click", menu: id, slot: 0, button: 0, shift: true });
messages = t.take(1);
assertEquals(last_container(messages, "screen")[0], null);
assertEquals(menu_items(messages)[0], null);
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:coal", count: 10 });
});
@@ -70,11 +72,13 @@ Deno.test("shift clicking into a furnace goes by its slot filters, and takes the
const t = await setup([["bworld:iron_ore", 1], ["bworld:coal", 1], ["bworld:stick", 1]]);
t.game.set_block(1, 100, 0, "bworld:furnace");
t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" });
const { id } = opened_menu(t.take(1));
// the ore goes in the input, the coal in the fuel slot, and sticks fit neither so they stay
for (const index of [0, 1, 2]) t.send(1, { type: "click", container: "inventory", index, button: 0, shift: true });
// the ore goes in the input, the coal in the fuel slot, and sticks fit neither so they stay. the furnace's 3 slots
// come before the inventory's
for (const index of [0, 1, 2]) t.send(1, { type: "click", menu: id, slot: 3 + index, button: 0, shift: true });
let messages = t.take(1);
assertEquals(last_container(messages, "screen"), [
assertEquals(menu_items(messages).slice(0, 3), [
{ id: "bworld:iron_ore", count: 1 },
{ id: "bworld:coal", count: 1 },
null,
@@ -82,18 +86,18 @@ Deno.test("shift clicking into a furnace goes by its slot filters, and takes the
assertEquals(last_container(messages, "inventory")[2], { id: "bworld:stick", count: 1 });
for (let i = 0; i < 210; i++) t.game.tick();
t.send(1, { type: "click", container: "screen", index: 2, button: 0, shift: true });
t.send(1, { type: "click", menu: id, slot: 2, button: 0, shift: true });
messages = t.take(1);
assertEquals(last_container(messages, "screen")[2], null);
assertEquals(menu_items(messages)[2], null);
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:iron_ingot", count: 1 });
});
Deno.test("shift clicking the crafting result crafts as many as fit", async () => {
const t = await setup([["bworld:log", 3]]);
// the logs into the grid, then the result shift clicked: 3 crafts of 2 planks
t.send(1, { type: "click", container: "inventory", index: 0, button: 0 });
t.send(1, { type: "click", container: "crafting", index: 0, button: 0 });
t.send(1, { type: "click", container: "crafting", index: CRAFTING_RESULT_SLOT, button: 0, shift: true });
t.send(1, { type: "click", menu: 0, slot: 0, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING + CRAFTING_RESULT_SLOT, button: 0, shift: true });
const messages = t.take(1);
assertEquals(last_container(messages, "crafting").every((item: Msg) => item === null), true);
assertEquals(last_container(messages, "inventory")[8], { id: "bworld:planks", count: 6 });
@@ -101,9 +105,9 @@ Deno.test("shift clicking the crafting result crafts as many as fit", async () =
Deno.test("shift clicking the crafting grid puts its items back in the inventory", async () => {
const t = await setup([["bworld:log", 3]]);
t.send(1, { type: "click", container: "inventory", index: 0, button: 0 });
t.send(1, { type: "click", container: "crafting", index: 4, button: 0 });
t.send(1, { type: "click", container: "crafting", index: 4, button: 0, shift: true });
t.send(1, { type: "click", menu: 0, slot: 0, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING + 4, button: 0 });
t.send(1, { type: "click", menu: 0, slot: PLAYER_MENU_CRAFTING + 4, button: 0, shift: true });
const messages = t.take(1);
assertEquals(last_container(messages, "crafting").every((item: Msg) => item === null), true);
assertEquals(last_container(messages, "inventory")[9], { id: "bworld:log", count: 3 });