// 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 MenuSlotInfo, PLAYER_MENU, PLAYER_MENU_EXTRA } from "$/common/protocol.ts"; import type { ClientInventories } from "./inventory.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; } export class ClientMenu { readonly id: number; readonly type: string; readonly title: string; readonly open_data: unknown; data: Record; slots: ClientSlot[]; 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; } // 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); return 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, })), ); } } // 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)); } for (let i = 0; i < CRAFTING_RESULT_SLOT; i++) { slots.push(slot(inventories.crafting, i, 25 + (i % 3) * 18, 16 + Math.floor(i / 3) * 18)); } slots.push(slot(inventories.crafting, CRAFTING_RESULT_SLOT, 115, 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 }); } return new ClientMenu(PLAYER_MENU, "", "Crafting", null, slots); }