// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // the client's copy of a menu, like neoforge's client menu: built from what the server sent when it opened, kept up // to date with what it syncs. clicks go to the server, which decides what they do import { Container } from "$/common/inventory.ts"; import { CRAFTING_RESULT_SLOT, type MenuFluidSlotInfo, type MenuSlotInfo, type MenuTank, 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 container: Container; index: number; // gui pixels from the screen's top left to its frame x: number; y: number; // can only be taken from, like the crafting or furnace result output: boolean; // a texture drawn while it's empty, like minecraft's armor slots icon: string | undefined; // the most it holds of anything, when that's less than a stack max_count?: number; } // a tank the server's menu shows, which clicks with a fluid container on the cursor fill it from or empty it into export interface ClientFluidSlot extends MenuFluidSlotInfo { // what the server last said is in it tank: MenuTank; } export class ClientMenu { readonly id: number; readonly type: string; readonly title: string; readonly open_data: unknown; data: Record; slots: ClientSlot[]; fluid_slots: ClientFluidSlot[] = []; constructor( id: number, type: string, title: string, open_data: unknown, slots: ClientSlot[], data: Record = {}, ) { this.id = id; this.type = type; this.title = title; this.open_data = open_data; this.slots = slots; 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[], fluid_slots: MenuFluidSlotInfo[] = [], ) { const items = new Container(slots.length); const menu = new ClientMenu( id, type, title, open_data, slots.map((slot, index) => ({ container: items, index, x: slot.x, y: slot.y, output: slot.output === true, icon: slot.icon, max_count: slot.max_count, })), ); menu.fluid_slots = fluid_slots.map((slot) => ({ ...slot, tank: { amount: 0, capacity: 0 } })); return menu; } } // menu 0, the player's inventory and crafting grid, laid out for the inventory screen, then the slots mods added. its // slots are the player's own containers, which the server syncs whether or not a menu is open export function player_menu(inventories: ClientInventories): ClientMenu { const slot = (container: Container, index: number, x: number, y: number, output = false): ClientSlot => ({ container, index, x, y, output, icon: undefined, }); const slots: ClientSlot[] = []; for (let i = 0; i < 36; i++) { const row = Math.floor(i / 9); slots.push(slot(inventories.inventory, i, 7 + (i % 9) * 18, i < 9 ? 141 : 83 + (row - 1) * 18)); } // the crafting grid on the right, the player drawn left of it, see GuiPlayerInventory for (let i = 0; i < CRAFTING_RESULT_SLOT; i++) { slots.push(slot(inventories.crafting, i, 79 + (i % 3) * 18, 16 + Math.floor(i / 3) * 18)); } slots.push(slot(inventories.crafting, CRAFTING_RESULT_SLOT, 151, 34, true)); if (slots.length !== PLAYER_MENU_EXTRA) throw new Error("menu 0 has the wrong slots"); for (const [i, info] of inventories.extra_slots.entries()) { slots.push({ ...slot(inventories.extra, i, info.x, info.y, info.output === true), icon: info.icon, max_count: info.max_count, }); } return new ClientMenu(PLAYER_MENU, "", "Crafting", null, slots); }