From bea5f10a126fdbda8a88dd53a01240d1aadbbf73 Mon Sep 17 00:00:00 2001 From: Paula Date: Mon, 12 Oct 2026 20:52:41 -0300 Subject: [PATCH] Stonecutter --- CREATE.md | 11 ++- MODS.md | 2 +- client/gui/gui_mod_screens.ts | 1 + client/menu.ts | 8 ++ common/mod_api/client.ts | 2 + docs/modding/blocks/components.md | 1 + docs/modding/inventories/menus.md | 4 + docs/modding/resources/recipes.md | 19 ++++ mods/bworld/blocks/stonecutter.json | 37 ++++++++ mods/bworld/models/stonecutter.json | 28 ++++++ mods/bworld/recipes/crafting_stonecutter.json | 18 ++++ .../recipes/stonecutting_stone_bricks.json | 8 ++ mods/bworld/scripts/client.ts | 67 ++++++++++++++- mods/bworld/scripts/server.ts | 81 ++++++++++++++++++ mods/bworld/scripts/stonecutting.ts | 45 ++++++++++ mods/bworld/textures/stonecutter_side.png | Bin 0 -> 452 bytes mods/bworld/textures/stonecutter_top.png | Bin 0 -> 508 bytes mods/recipe_viewer/scripts/categories.ts | 12 +++ tests/base_game_test.ts | 52 +++++++++++ tests/client_menu_test.ts | 21 +++++ 20 files changed, 411 insertions(+), 6 deletions(-) create mode 100644 mods/bworld/blocks/stonecutter.json create mode 100644 mods/bworld/models/stonecutter.json create mode 100644 mods/bworld/recipes/crafting_stonecutter.json create mode 100644 mods/bworld/recipes/stonecutting_stone_bricks.json create mode 100644 mods/bworld/scripts/stonecutting.ts create mode 100644 mods/bworld/textures/stonecutter_side.png create mode 100644 mods/bworld/textures/stonecutter_top.png create mode 100644 tests/client_menu_test.ts diff --git a/CREATE.md b/CREATE.md index 6ffb8f5..b01df06 100644 --- a/CREATE.md +++ b/CREATE.md @@ -4,8 +4,9 @@ A plan for porting all of Create to bworld as a mod, `mods/create`: its kinetics redstone, contraptions, trains, equipment, decoration and schematics. It aims to do this with as little hand work as possible, and to stay fast on both the server and the client when a world has thousands of moving parts. -Built so far: the importer and Create's imported blocks and items, model `parent` and `x` turns, and S0. Facts about -Create come from reading `ports/create1.21.1` and `ports/create1.19.3`, and facts about bworld from its source. +Built so far: the importer and Create's imported blocks and items, model `parent` and `x` turns, S0, and stonecutting +from S9. Facts about Create come from reading `ports/create1.21.1` and `ports/create1.19.3`, and facts about bworld from +its source. ## Sources @@ -319,6 +320,8 @@ whistles, presses, cranks and trains want it, but nothing breaks without it. **S9. More recipe types in bworld** (P0). `shapeless` crafting (Create's cogwheels and many others), stonecutting (palettes have hundreds of stonecutting recipes, and the mechanical saw uses them), and smoking and blasting for fans. +Stonecutting is built, as `bworld:stonecutting` with the stonecutter, and the saw reads the same recipes with +`ctx.recipes.list("bworld:stonecutting")`. **S10. Block shape families in bworld** (P10). Stairs, slabs and walls don't exist in bworld yet, and Create's palettes are mostly those. They're components with placement rules in `mods/bworld`, like the fence and trapdoor. @@ -427,7 +430,7 @@ it's first needed. ### P0: Importer and Skeleton -- S9 (`shapeless`, stonecutting), R5. +- S9 (`shapeless`), R5. - The importer's other kinds, up to `mod`, and Create's remap file and block table in `mods/create/import/`. - `mods/create` with every block and item imported, static models, names, recipes that only need crafting, tags. @@ -455,7 +458,7 @@ Check: new worlds have zinc and Create's stones, and casings join up. ### P3: Processing -- S1, S4, S8, R9, R10, R11, and stonecutting, smoking and blasting from S9. +- S1, S4, S8, R9, R10, R11, and smoking and blasting from S9. - Mechanical press, millstone, crushing wheels, mechanical saw, encased fan with its processing (washing, smoking, blasting, haunting from `AllFanProcessingTypes`), deployer, mechanical crafters, item application, sand paper, depot, basin, mixer and compacting (items only), blaze burner and its heat. diff --git a/MODS.md b/MODS.md index 8d07d31..f38d05b 100644 --- a/MODS.md +++ b/MODS.md @@ -59,7 +59,7 @@ deno task build && deno task server # packs the mods and starts a se - [Textures](docs/modding/resources/textures.md) - [Block Models](docs/modding/resources/models.md) -- [Recipes](docs/modding/resources/recipes.md): crafting, smelting, smithing and your own recipe types +- [Recipes](docs/modding/resources/recipes.md): crafting, smelting, smithing, stonecutting and your own recipe types - [Tags](docs/modding/resources/tags.md): lists of blocks, items or entities that every mod can add to - [Loot Tables](docs/modding/resources/loot_tables.md): what blocks, mobs, fishing and chests give diff --git a/client/gui/gui_mod_screens.ts b/client/gui/gui_mod_screens.ts index 973ad73..385bec9 100644 --- a/client/gui/gui_mod_screens.ts +++ b/client/gui/gui_mod_screens.ts @@ -44,6 +44,7 @@ export class GuiModMenuScreen extends GuiMenuScreen { get data() { return menu.data; }, + item: (slot) => menu.item(slot), click_button: (id) => { if (!Number.isInteger(id)) throw new Error(`a menu button is a whole number, not ${id}`); if (self.screen && client.screens.includes(self.screen)) { diff --git a/client/menu.ts b/client/menu.ts index 82f7810..9b70c7f 100644 --- a/client/menu.ts +++ b/client/menu.ts @@ -6,6 +6,7 @@ import { Container } from "$/common/inventory.ts"; import { CRAFTING_RESULT_SLOT, type MenuSlotInfo, PLAYER_MENU, PLAYER_MENU_EXTRA } from "$/common/protocol.ts"; import type { ClientInventories } from "./inventory.ts"; +import type { ItemStack as ApiItemStack } from "$/common/mod_api/client.ts"; export interface ClientSlot { // where its item is on this client @@ -46,6 +47,13 @@ export class ClientMenu { this.data = data; } + // what's in a slot, by menu slot index, the way mods see items. a copy + item(slot: number): ApiItemStack | undefined { + const info = this.slots[slot]; + const item = info?.container.get_item(info.index); + return item && { id: item.type_id, count: item.amount, data: structuredClone(item.data) }; + } + // one the server opened: its slots' items come in menu_content, into a container of its own static opened(id: number, type: string, title: string, open_data: unknown, slots: MenuSlotInfo[]) { const items = new Container(slots.length); diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index afcd79d..61818b3 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -106,6 +106,8 @@ export interface ClientMenu { readonly open_data: unknown; // the server's latest values of its add_data numbers readonly data: Readonly>; + // what's in one of its slots, by menu slot index, as the server last synced it. a copy + item(slot: number): ItemStack | undefined; // like clickMenuButton, the server's on_button decides what it does click_button(id: number): void; close(): void; diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 5260dd5..d27fbe8 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -195,6 +195,7 @@ catch up. If it's further behind than that, it skips the rest and logs a warning | `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it with a `base_sell_value` for coins. | | `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. | | `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. | +| `bworld:stonecutter` | none | Cuts one item into another with `bworld:stonecutting` recipes, picked from a list. | | `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. | | `bworld:farmland` | none | Watered with the watering can, dries out every morning. | | `bworld:crop` | see [Crops](crops.md) | Grows a stage every morning its farmland was watered. | diff --git a/docs/modding/inventories/menus.md b/docs/modding/inventories/menus.md index e4ce824..c723946 100644 --- a/docs/modding/inventories/menus.md +++ b/docs/modding/inventories/menus.md @@ -175,11 +175,15 @@ interface ClientMenu { readonly title: string; readonly open_data: unknown; readonly data: Readonly>; // the server's latest add_data values + item(slot: number): ItemStack | undefined; // what's in a slot, by menu slot index, a copy click_button(id: number): void; close(): void; } ``` +`item` is for screens that draw something from what's in the slots, like NeoForge screens reading `menu.slots`: the +stonecutter's lists what its input can be cut into. + When the server opens a menu type the client has no screen for, the client closes it again, with an error in its console. A screen that throws is closed the same way. diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index 684b30e..4fc4e28 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -66,6 +66,25 @@ An optional `addition` is a third item it also takes. Upgrading uses up the tool addition, and the result keeps the tool's `data`, like its [durability](../items/tools.md#durability). Two smithing recipes with the same tool, material and addition are an error. +## Stonecutting: `bworld:stonecutting` + +For the stonecutter, which cuts one item into another. It's the base game's own type, so `mods/bworld` checks these +recipes when the server starts rather than the engine: + +```json +{ + "format_version": 1, + "recipe": { + "type": "bworld:stonecutting", + "input": "bworld:cobblestone", + "output": { "id": "bworld:stone_bricks", "count": 1 } + } +} +``` + +Every recipe that takes the item in the stonecutter's input is listed, in load order, and the player picks one. Each cut +uses up one of the input. + ## Choices Anywhere a recipe takes an item (`key` values, `input`, `item`), an item [tag](tags.md) means anything in it fits, and diff --git a/mods/bworld/blocks/stonecutter.json b/mods/bworld/blocks/stonecutter.json new file mode 100644 index 0000000..f098fdb --- /dev/null +++ b/mods/bworld/blocks/stonecutter.json @@ -0,0 +1,37 @@ +{ + "format_version": 1, + "block": { + "id": "bworld:stonecutter", + "textures": { + "top": "bworld:stonecutter_top", + "side": "bworld:stonecutter_side", + "bottom": "bworld:stonecutter_bottom", + "saw": "bworld:stonecutter_saw" + }, + "model": "bworld:stonecutter", + "item_model": "bworld:stonecutter", + "render_layer": "cutout", + "states": [ + { + "name": "facing", + "bits": 2, + "default": 0 + } + ], + "variants": { + "facing=1": { "y": 90 }, + "facing=2": { "y": 180 }, + "facing=3": { "y": 270 } + }, + "mining": { + "toughness": 5, + "tool": "pickaxe", + "requires_tool": true + }, + "drops": "bworld:stonecutter", + "interactive": true, + "components": { + "bworld:stonecutter": {} + } + } +} diff --git a/mods/bworld/models/stonecutter.json b/mods/bworld/models/stonecutter.json new file mode 100644 index 0000000..8d513a4 --- /dev/null +++ b/mods/bworld/models/stonecutter.json @@ -0,0 +1,28 @@ +{ + "format_version": 1, + "model": { + "id": "bworld:stonecutter", + "elements": [ + { + "from": [0, 0, 0], + "to": [16, 9, 16], + "faces": { + "bottom": { "texture": "#bottom", "cullface": "bottom" }, + "top": { "texture": "#top" }, + "north": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "north" }, + "south": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "south" }, + "west": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "west" }, + "east": { "texture": "#side", "uv": [0, 7, 16, 16], "cullface": "east" } + } + }, + { + "from": [1, 9, 8], + "to": [15, 16, 8], + "faces": { + "north": { "texture": "#saw", "uv": [1, 9, 15, 16] }, + "south": { "texture": "#saw", "uv": [15, 9, 1, 16] } + } + } + ] + } +} diff --git a/mods/bworld/recipes/crafting_stonecutter.json b/mods/bworld/recipes/crafting_stonecutter.json new file mode 100644 index 0000000..6b90cc8 --- /dev/null +++ b/mods/bworld/recipes/crafting_stonecutter.json @@ -0,0 +1,18 @@ +{ + "format_version": 1, + "recipe": { + "type": "shaped", + "pattern": [ + "III", + "SSS" + ], + "key": { + "I": "bworld:iron_ingot", + "S": "bworld:smooth_stone" + }, + "result": { + "id": "bworld:stonecutter", + "count": 1 + } + } +} diff --git a/mods/bworld/recipes/stonecutting_stone_bricks.json b/mods/bworld/recipes/stonecutting_stone_bricks.json new file mode 100644 index 0000000..35dc234 --- /dev/null +++ b/mods/bworld/recipes/stonecutting_stone_bricks.json @@ -0,0 +1,8 @@ +{ + "format_version": 1, + "recipe": { + "type": "bworld:stonecutting", + "input": "bworld:cobblestone", + "output": { "id": "bworld:stone_bricks", "count": 1 } + } +} diff --git a/mods/bworld/scripts/client.ts b/mods/bworld/scripts/client.ts index a83e6c1..ffae0e9 100644 --- a/mods/bworld/scripts/client.ts +++ b/mods/bworld/scripts/client.ts @@ -2,8 +2,9 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // the screens of the base game's menus, see scripts/server.ts for their slots. in gui pixels, like minecraft's -import type { ClientContext, ClientMenu, Graphics, ItemStack, MenuScreen } from "bworld/client"; +import type { ClientContext, ClientMenu, Color, Graphics, ItemStack, MenuScreen } from "bworld/client"; import { MINIGAMES } from "./minigames/mod.ts"; +import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts"; const WIDTH = 176; // like minecraft's 176x166 container screens @@ -15,6 +16,7 @@ export function setup(ctx: ClientContext) { ctx.ui.register_menu_screen("bworld:crabpot", (menu) => container_screen(menu, HEIGHT)); ctx.ui.register_menu_screen("bworld:worm_bin", worm_bin_screen); ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen); + ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu)); ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen); } @@ -60,6 +62,69 @@ function smithing_screen(menu: ClientMenu): MenuScreen { }); } +// what the input can be cut into, as a grid of buttons that scrolls when there are more than fit. a button is the +// recipe's place in that list, which the server's menu reads the same way, see stonecutting.ts +const CUT_X = 52; +const CUT_Y = 14; +const CUT_COLUMNS = 4; +const CUT_ROWS = 3; +const CUT_WIDTH = 16; +const CUT_HEIGHT = 18; +const SCROLLBAR_X = 119; +const SCROLLBAR_WIDTH = 12; +const SCROLLER_HEIGHT = 15; +const TRACK_COLOR: Color = [0.55, 0.55, 0.55, 1]; +const SCROLLER_COLOR: Color = [1, 1, 1, 1]; +const SCROLLER_OFF_COLOR: Color = [0.78, 0.78, 0.78, 1]; +const SELECTED_COLOR: Color = [1, 1, 1, 0.45]; + +function stonecutter_screen(ctx: ClientContext, menu: ClientMenu): MenuScreen { + const recipes = stonecutting_recipes(ctx); + // the first row shown, and whether the scroller is being dragged + let row = 0; + let dragging = false; + return container_screen(menu, HEIGHT, (g) => { + const options = stonecutting_options(recipes, menu.item(0)?.id); + const rows_hidden = Math.max(0, Math.ceil(options.length / CUT_COLUMNS) - CUT_ROWS); + const track_height = CUT_ROWS * CUT_HEIGHT; + const { mouse } = g; + const over = (x: number, y: number, w: number, h: number) => + mouse.x >= x && mouse.y >= y && mouse.x < x + w && mouse.y < y + h; + + if (mouse.pressed && rows_hidden > 0 && over(SCROLLBAR_X, CUT_Y, SCROLLBAR_WIDTH, track_height)) { + dragging = true; + } + if (!mouse.down) dragging = false; + if (dragging) { + const along = (mouse.y - CUT_Y - SCROLLER_HEIGHT / 2) / (track_height - SCROLLER_HEIGHT); + row = Math.round(Math.max(0, Math.min(1, along)) * rows_hidden); + } + row = Math.min(row, rows_hidden); + const scroller_y = CUT_Y + (rows_hidden > 0 ? (row / rows_hidden) * (track_height - SCROLLER_HEIGHT) : 0); + g.rect(SCROLLBAR_X, CUT_Y, SCROLLBAR_WIDTH, track_height, TRACK_COLOR); + g.rect( + SCROLLBAR_X, + scroller_y, + SCROLLBAR_WIDTH, + SCROLLER_HEIGHT, + rows_hidden > 0 ? SCROLLER_COLOR : SCROLLER_OFF_COLOR, + ); + + for (let i = 0; i < CUT_COLUMNS * CUT_ROWS; i++) { + const index = row * CUT_COLUMNS + i; + const recipe = options[index]; + if (!recipe) break; + const x = CUT_X + (i % CUT_COLUMNS) * CUT_WIDTH; + const y = CUT_Y + Math.floor(i / CUT_COLUMNS) * CUT_HEIGHT; + if (g.button("", x, y, CUT_WIDTH, CUT_HEIGHT)) menu.click_button(index); + if (index === menu.data.selected) g.rect(x, y, CUT_WIDTH, CUT_HEIGHT, SELECTED_COLOR); + // an item is drawn in an 18 pixel slot, the button is 16 wide + g.item(recipe.output.id, x - 1, y, recipe.output.count); + if (over(x, y, CUT_WIDTH, CUT_HEIGHT)) g.item_tooltip(recipe.output.id, mouse.x, mouse.y); + } + }); +} + // an 18 pixel square progress bar, value / max of full drawn over empty function bar( g: Graphics, diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 6bc7b34..0ea21ba 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -6,6 +6,7 @@ import type { BlockRef, Container, Player, Season, ServerContext } from "bworld/ import { register_curios } from "./curios.ts"; import { register_effects } from "./effects.ts"; import { register_fishing } from "./fishing.ts"; +import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts"; export function setup(ctx: ServerContext) { register_hoeable(ctx); @@ -16,6 +17,7 @@ export function setup(ctx: ServerContext) { register_crabpot(ctx); register_worm_bin(ctx); register_smithing_table(ctx); + register_stonecutter(ctx); register_farming(ctx); register_fishing(ctx); register_door(ctx); @@ -478,6 +480,85 @@ function register_smithing_table(ctx: ServerContext) { }); } +// stonecutter: one item in, cut into whichever of its stonecutting recipes the player picks with the screen's buttons, +// see stonecutting.ts. like the smithing table, every player gets their own slots, and what's left in the input goes +// back to the player when they close it + +const STONECUTTER_INPUT = 0; +const STONECUTTER_RESULT = 1; + +function register_stonecutter(ctx: ServerContext) { + const recipes = stonecutting_recipes(ctx); + + const menu_type = ctx.menus.register("bworld:stonecutter", (menu, block, player) => { + const container = ctx.containers.create(2); + // the picked recipe, by its place in what the input can be cut into. -1 for none + let selected = -1; + // the item it was picked for, since another item has other recipes + let picked_for: string | undefined; + const options = () => stonecutting_options(recipes, container.get(STONECUTTER_INPUT)?.id); + + // the result slot shows what taking it would give + const show_result = () => { + const recipe = options()[selected]; + const shown = container.get(STONECUTTER_RESULT); + if (!recipe) { + if (shown) container.set(STONECUTTER_RESULT, undefined); + } else if (shown?.id !== recipe.output.id || shown.count !== recipe.output.count) { + container.set(STONECUTTER_RESULT, { ...recipe.output }); + } + }; + const stop = container.on_change((slot) => { + if (slot !== STONECUTTER_INPUT) return; + const id = container.get(STONECUTTER_INPUT)?.id; + if (id !== picked_for) { + picked_for = id; + selected = -1; + } + show_result(); + }); + + menu.title = "Stonecutter"; + menu.add_slot(container, STONECUTTER_INPUT, { x: 19, y: 32 }); + menu.add_slot(container, STONECUTTER_RESULT, { + x: 142, + y: 32, + output_only: true, + // the input is used up in the same step, so it can't be taken out after + on_take(item) { + const recipe = options()[selected]; + const input = container.get(STONECUTTER_INPUT); + if (!recipe || !input || recipe.output.id !== item.id) return false; + input.count -= 1; + return true; + }, + }); + menu.add_player_inventory(7, INVENTORY_Y); + menu.add_data("selected", () => selected); + // a button is a recipe, by its place in options + menu.on_button((_player, id) => { + if (!Number.isInteger(id) || id < 0 || id >= options().length) return; + selected = id; + picked_for = container.get(STONECUTTER_INPUT)?.id; + show_result(); + }); + menu.still_valid_near(block); + menu.on_close(() => { + stop(); + const item = container.get(STONECUTTER_INPUT); + if (item) player.give_item(item.id, item.count, item.data); + ctx.containers.delete(container.id); + }); + }); + + ctx.components.register_block("bworld:stonecutter", { + on_interact(block, _params, player) { + menu_type.open(player, block); + return true; + }, + }); +} + // farming: java's crop rules on stardew valley's clock. every morning at 06:00 a crop grows one stage if its farmland // was watered the day before, then all farmland dries. crops only stand on farmland (their place_on), and pop off when // it's dug up diff --git a/mods/bworld/scripts/stonecutting.ts b/mods/bworld/scripts/stonecutting.ts new file mode 100644 index 0000000..004c28a --- /dev/null +++ b/mods/bworld/scripts/stonecutting.ts @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// stonecutting recipes: the stonecutter cuts one item into another, picked from every recipe that takes it. the +// server's menu and the client's screen both list them from here, in the same order, so a button's number means the +// same recipe to both +import type { TagApi } from "bworld/shared"; + +export const STONECUTTING = "bworld:stonecutting"; + +export interface StonecuttingRecipe { + // the items it cuts + input: string[]; + output: { id: string; count: number }; +} + +// what reading them needs, which server and client scripts both have +interface RecipeReader { + // deno-lint-ignore no-explicit-any + recipes: { list(type: string): any[] }; + tags: TagApi; + items: { exists(id: string): boolean }; +} + +// every stonecutting recipe, in load order. a broken one throws, which stops the server from starting +export function stonecutting_recipes(ctx: RecipeReader): StonecuttingRecipe[] { + return ctx.recipes.list(STONECUTTING).map((json, i) => { + const where = `${STONECUTTING} recipe ${i + 1}`; + const choice = json.input; + const input = typeof choice === "string" || Array.isArray(choice) ? ctx.tags.expand(choice) : []; + if (input.length === 0 || !input.every((id) => ctx.items.exists(id))) { + throw new Error(`${where}: input must be an item, a #tag or a list of them, and they must exist`); + } + const { id, count } = json.output ?? {}; + if (typeof id !== "string" || !ctx.items.exists(id) || !Number.isInteger(count) || count < 1) { + throw new Error(`${where}: output must be { id, count } of an item that exists`); + } + return { input, output: { id, count } }; + }); +} + +// what an item can be cut into, in the order the stonecutter shows them +export function stonecutting_options(recipes: StonecuttingRecipe[], id: string | undefined): StonecuttingRecipe[] { + return id === undefined ? [] : recipes.filter((recipe) => recipe.input.includes(id)); +} diff --git a/mods/bworld/textures/stonecutter_side.png b/mods/bworld/textures/stonecutter_side.png new file mode 100644 index 0000000000000000000000000000000000000000..9c7c44fad742b03a059af79172b80a9a9109a271 GIT binary patch literal 452 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jP7LeL$-D$|*pj^6T^Rm@ z;DWu&Cj&(|3p^r=85p>QL70(Y)*K0-AbW|YuPgf_ZUF%b$X;bMep-kJK0@wbSdQPnEoPo;wCakiRqW&XYC|J&>TGCs}AU-z8hIQ!jrUPlRq*O%se_dmZys`OW#$Lc`! z^2$1^{v_X>S})(cV@?Q{`P>-B8N}73;N;bKNwc57d`+z;!>7PC)1{V*Foe7jsC03<-yKQQZ)BQ9>RX$Px#24YJ`L;(K){{a7>y{D4^000SaNLh0L01FcU01FcV0GgZ_00007bV*G`2jUA5 z5DNrIF31G{00DeSL_t(I%YBp2N+UrK#(#!Lc9=0pr^^9Bg5&ySZZNINrPuKajvlB`P9%x3D zwkq;;4>R28nh2031KKYs!-aOL^eh=*h8sLgijGfx&Fb7H)3=7)ZgtFx&HK#E&KxK> z$O`BDqrz*|J>=6SeX+X9~~~WYw=63`r7YyZMn%DI6mzF0000 { + const t = await setup([["bworld:cobblestone", 3], ["bworld:dirt", 1]]); + t.game.set_block(1, 100, 0, "bworld:stonecutter"); + const messages = t.take(1); + const cobblestone = slot_of(messages, "bworld:cobblestone"); + const dirt = slot_of(messages, "bworld:dirt"); + t.send(1, { type: "use_block", x: 1, y: 100, z: 0, face: "top" }); + const opened = opened_menu(t.take(1)); + const press = (button: number) => t.send(1, { type: "menu_button", menu: opened.id, button }); + const cursor = (messages: Msg[]) => messages.filter((m) => m.type === "cursor").at(-1)?.item ?? null; + + // nothing to take until a recipe is picked, and a button with no recipe picks nothing. data is only sent when it + // changes + assertEquals(opened.data.selected, -1); + move_to_menu(t, opened, cobblestone, 0); + t.game.tick(); + press(99); + t.game.tick(); + let latest = t.take(1); + assertEquals(menu_data(latest), undefined); + assertEquals(menu_items(latest)[1], null); + + press(0); + t.game.tick(); + latest = t.take(1); + assertEquals(menu_data(latest).selected, 0); + const result = menu_items(latest)[1]; + assert(result, "the picked recipe's output shows"); + + // taking it uses up one, and the next is ready a tick later + click(t, opened, 1); + latest = t.take(1); + assertEquals(cursor(latest), result); + assertEquals(menu_items(latest)[0], { id: "bworld:cobblestone", count: 2 }); + t.game.tick(); + assertEquals(menu_items(t.take(1))[1], result); + + // another item forgets the pick, and closing gives back what's in the input + click(t, opened, inventory_slot(opened, 20)); + move_to_menu(t, opened, dirt, 0); + t.game.tick(); + latest = t.take(1); + assertEquals(menu_data(latest).selected, -1); + assertEquals(menu_items(latest)[1], null); + t.send(1, { type: "close_menu", menu: opened.id }); + const inventory = last_container(t.take(1), "inventory").filter((i: Msg) => i); + assertEquals( + inventory.map((i: Msg) => i.id).sort(), + ["bworld:cobblestone", "bworld:dirt", result.id].sort(), + ); +}); + Deno.test("a bucket picks up a water source but not flowing water, and a water bucket puts a source back", async () => { const t = await setup([["bworld:bucket", 1]]); const held = () => t.game.players()[0].held_item?.type_id; diff --git a/tests/client_menu_test.ts b/tests/client_menu_test.ts new file mode 100644 index 0000000..3c8bcd6 --- /dev/null +++ b/tests/client_menu_test.ts @@ -0,0 +1,21 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +import { assertEquals } from "@std/assert"; +import { ClientMenu } from "$/client/menu.ts"; +import { ItemStack } from "$/common/inventory.ts"; + +Deno.test("a mod's screen reads its menu's slots as copies", () => { + const menu = ClientMenu.opened(1, "testmod:box", "Box", null, [{ x: 0, y: 0 }, { x: 18, y: 0 }]); + const stack = new ItemStack("testmod:gem", 3); + stack.data = { shiny: true }; + menu.slots[0].container.set_item(0, stack); + + const item = menu.item(0)!; + assertEquals(item, { id: "testmod:gem", count: 3, data: { shiny: true } }); + item.count = 1; + (item.data as { shiny: boolean }).shiny = false; + assertEquals(menu.item(0), { id: "testmod:gem", count: 3, data: { shiny: true } }); + assertEquals(menu.item(1), undefined); + assertEquals(menu.item(5), undefined); +});