Change whole gui system
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user