# Menus Menus are how players use [containers](containers.md), like NeoForge's. A GUI has two halves: - The **menu** lives in the server script. It decides which slots the player can click, what goes in them, the numbers the screen shows, what its buttons do, and when it closes. - The **screen** lives in the client script. It only draws the menu and sends the player's clicks. Every click goes to the server, which applies it following the menu's rules, so a modified client can only do what the menu allows. Chests, furnaces, the smithing table and Farmer's Delight's cooking pot are all menus, and Curios adds its slots to [the player's inventory](#the-players-inventory). ## Registering a Menu Type A menu type is registered in the server script with a function that builds each menu when it opens, like NeoForge's `MenuType` and its server menu constructor. It gets the menu to build, whatever the code opening it passes, and the player: ```ts // server.ts export function setup(ctx: ServerContext) { const smelter = ctx.menus.register("copper_tools:smelter", (menu, block, player) => { const container = ctx.containers.get(block.data.container)!; menu.title = "Smelter"; menu.add_slot(container, 0, { x: 55, y: 16, filter: "smeltable" }); menu.add_slot(container, 1, { x: 55, y: 52, filter: "fuel" }); menu.add_slot(container, 2, { x: 115, y: 34, output_only: true }); menu.add_player_inventory(7, 84); menu.add_data("progress", () => block.data.progress); menu.add_data("progress_max", () => 200); menu.still_valid_near(block); }); ctx.components.register_block("copper_tools:smelter", { on_create(block) { block.data = { container: ctx.containers.create(3).id, progress: 0 }; }, on_interact(block, _params, player) { smelter.open(player, block); return true; }, }); } ``` Give the block `"interactive": true` so clients don't guess that the click places a block. `open(player, args)` builds a menu for the player and opens it, closing whatever they had open. A build function that throws opens nothing. Menu type ids are in the mod's namespace, and each needs a [screen](#screens) in the client script. ## Slots `add_slot(container, slot, options)` adds one slot of a container from `ctx.containers`, or of `player.inventory`. It returns the slot's **menu slot index**: slots are numbered in the order they're added, whatever container they're from. | Option | Meaning | | ------------- | ----------------------------------------------------------------------------------------------------------------- | | `x`, `y` | Where its 18 pixel frame goes, in GUI pixels from the screen's top left. | | `filter` | What can be put in: `"smeltable"` (has a furnace recipe), `"fuel"`, or a function `(item) => boolean`. | | `output_only` | Can only be taken from, all at once, like the furnace result. | | `on_take` | For `output_only` slots: runs when a player takes from it, before they get the item. Return `false` to stop them. | | `icon` | A texture drawn in the slot while it's empty, like Minecraft's armor slots. | `add_player_inventory(x, y)` adds the player's 36 slots, like NeoForge's `addStandardInventorySlots`: the rest of the inventory in 3 rows from `x` and `y`, and the hotbar 58 pixels under them. They're added in inventory order, so inventory slot `i` is the returned index plus `i`. `filter`, `output_only` and `on_take` run **on the server**, so players can't put the wrong items in by editing their client. `on_take` is the place to use up a recipe's inputs, so taking the result and spending the inputs happen in the same step, like the smithing table does. ### Shift Clicking Shift clicking a slot moves its stack to the other part of the menu, like NeoForge's `quickMoveStack`: - From the player's inventory, it goes into the menu's other slots in the order they were added, only those whose `filter` takes it and never `output_only` ones: first onto stacks of the same item, then into empty slots. A menu with no such slots moves it between the hotbar and the rest of the inventory. - From the menu's other slots, it goes to the player's inventory, filling the hotbar from the right first. An `output_only` slot only gives its item when it all fits, after `on_take` agrees. `menu.quick_move((player, index) => number[])` replaces the rule: return the menu slot indexes the stack in `index` tries, in order. Filters and `output_only` still apply. ## Data `add_data(name, get)` is a number the screen shows, like NeoForge's `DataSlot`. `get` runs every tick, and the value is sent to the player when it changes. Values are finite numbers. The screen reads them from `menu.data`. ## Buttons A screen can send a button press with `menu.click_button(id)`, like NeoForge's `clickMenuButton`. The menu decides what an id means: ```ts menu.on_button((player, id) => { if (id === 0) container.set(2, undefined); }); ``` Buttons only come from the player who has the menu open, at most 60 a second together with their mod messages. Any number can come, so check it. Anything richer than a number, like typed text, goes through a [mod channel](../networking/index.md), where the server should check the player has the menu open before trusting it. ## Open Data `menu.open_data` is any JSON up to 16 KB, sent to the client once when the menu opens, like NeoForge's `IContainerFactory` extra data. A chest sends how many rows it has, so its screen knows how tall to be. ## Closing One menu is open per player. It closes when the player closes it, leaves, dies or opens another one, when a script calls `close()`, when a container it shows is deleted, and when a `still_valid` check fails: - `menu.still_valid((player) => boolean)` is checked every tick. - `menu.still_valid_near(block)` closes it once the player is over 8 blocks from the block, or the block is broken or replaced, like NeoForge's `stillValid` with a `ContainerLevelAccess`. `menu.on_close(fn)` runs when it closes for any reason. `menu.on_tick(fn)` runs every tick while it's open. `open` returns a handle: ```ts interface MenuHandle { readonly player: Player; readonly open: boolean; close(): void; on_close(fn: () => void): void; // when it closes for any reason } ``` ## Screens The client script gives each of the mod's menu types a screen, like NeoForge's `RegisterMenuScreensEvent` and `AbstractContainerScreen`. The function runs each time one opens, with the client's copy of the menu: ```ts // client.ts export function setup(ctx: ClientContext) { ctx.ui.register_menu_screen("copper_tools:smelter", (menu) => ({ width: 176, height: 166, on_render_background(g) { g.panel(0, 0, 176, 166); const done = menu.data.progress / menu.data.progress_max || 0; g.texture("bworld:arrow_full", 85, 34, 18 * done, 18, [0, 0, done, 1]); }, on_render_labels(g) { g.text(menu.title, 8, 6); g.text("Inventory", 8, 73); }, })); } ``` The engine puts the screen in the middle of the window, and draws the menu's slots, the item on the cursor and item tooltips. The screen draws everything else with [`Graphics`](../gui/index.md#graphics), from its own top left: | Field | Meaning | | ---------------------- | ------------------------------------------------------------------ | | `width`, `height` | Its size in GUI pixels, like `imageWidth` and `imageHeight`. | | `on_render_background` | Every frame, under the slots: a panel, textures, progress bars. | | `on_render_labels` | Every frame, over the slots: the title, labels, buttons. | | `on_tick` | 20 times a second while it's open, like `containerTick`. Optional. | The menu it gets: ```ts interface ClientMenu { readonly type: string; readonly title: string; readonly open_data: unknown; readonly data: Readonly>; // the server's latest add_data values click_button(id: number): void; close(): void; } ``` 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. ## The Player's Inventory The player's own inventory screen is a menu too, menu 0, like Minecraft's `InventoryMenu`: their inventory and crafting grid, always there while they play. Mods can add slots to it with `ctx.menus.extend_player_menu` during setup. The function runs when each player joins: ```ts ctx.menus.extend_player_menu((menu, player) => { const pouch = pouch_of(player); // a container from ctx.containers, kept for each player for (let slot = 0; slot < 4; slot++) { menu.add_slot(pouch, slot, { x: -25, y: 7 + slot * 18 }); } }); ``` `add_slot` takes the same options as a menu's. `x` and `y` are from the inventory screen's top left, and can be outside it: a negative `x` is left of it. The engine draws a panel behind the slots outside its own, so they need no client code. Curios puts its slots in 2 columns this way. Shift clicking in the inventory puts a stack into the first of these slots whose `filter` takes it, before moving it between the hotbar and the rest of the inventory. Shift clicking one of them moves its stack back into the inventory. ## Messages How menus go over the network, for anyone working on the engine. Menus have ids from 1 to 100, counting up and around for each player like Minecraft's. Menu 0 is the player's own inventory (slots 0 to 35, the hotbar first) and crafting grid (36 to 45), always there and never opened. The slots mods added to it come after, from 46: `join` lists their positions, and the `container` message with `"extra"` syncs what's in them, like the inventory's. | Message | Way | What | | -------------------- | ------ | ------------------------------------------------------------------------------- | | `open_menu` | server | The id, type, title, open data, each slot's position, and the data. | | `menu_content` | server | Every slot's item, by menu slot index, when any changed. | | `menu_data` | server | All the data, when any changed. | | `close_menu` | both | The server closed it, or the player did. 0 is their inventory screen. | | `click`, `drop_item` | client | A menu slot index of the open menu (or menu 0). Ones for other ids are ignored. | | `menu_button` | client | A button of the open menu. | The code is in `server/game/menu.ts` and `client/menu.ts`.