256 lines
14 KiB
Markdown
256 lines
14 KiB
Markdown
# Menus
|
|
|
|
Menus are how players use [containers](containers.md), like NeoForge's. A GUI has two halves:
|
|
|
|
- The **menu** lives in the server phase. 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 phase. 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 cooking pot are all menus, and the base game's curios slots are
|
|
part of [the player's inventory](#the-players-inventory).
|
|
|
|
## Registering a Menu Type
|
|
|
|
A menu type is registered in the server phase 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 server(ctx: ServerContext) {
|
|
const smelter = ctx.menus.register<BlockRef>("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: (item) => !!ctx.recipes.find("bworld:smelting", item.id) });
|
|
menu.add_slot(container, 1, { x: 55, y: 52, filter: (item) => !!ctx.recipes.find("bworld:fuel", item.id) });
|
|
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: a function `(item) => boolean`, like one that looks for a [recipe](../resources/recipes.md). |
|
|
| `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. |
|
|
| `max_count` | The most the slot holds, when that's less than a stack, like Minecraft's `Slot.getMaxStackSize`. |
|
|
|
|
`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`, `on_take` and `max_count` 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.
|
|
|
|
### Fluid Slots
|
|
|
|
`add_fluid_slot(handler, tank, options)` shows a tank of a [fluid handler](../resources/fluids.md#fluid-handlers), like
|
|
Modern Industrialization's fluid slots. It returns the slot's index among the menu's fluid slots, which are numbered
|
|
apart from its item slots.
|
|
|
|
```ts
|
|
menu.add_fluid_slot(input_tank, 0, { x: 55, y: 34 });
|
|
menu.add_fluid_slot(output_tank, 0, { x: 115, y: 34, output_only: true });
|
|
```
|
|
|
|
| Option | Meaning |
|
|
| ------------- | ------------------------------------------------------------------------ |
|
|
| `x`, `y` | Where its 18 pixel frame goes, in GUI pixels from the screen's top left. |
|
|
| `output_only` | Fluid containers only fill from it, they can't be emptied into it. |
|
|
|
|
The engine draws it, so the screen needs no code for it: a droplet while the tank is empty, and the whole slot in its
|
|
fluid while there's any. Its tooltip says what the fluid is, how much of the tank it fills, and what clicking it does.
|
|
|
|
Clicking it with a fluid container on the cursor fills the container from the tank, or else empties it into the handler,
|
|
all on the server. It goes through the handler's `fill` and `drain`, so a handler with more than one tank fills
|
|
whichever its own rules say. Give each slot a handler of its own tank when that matters, like the keg's input and
|
|
output. One container of a stack on the cursor is used, and what it turns into goes into the inventory while there are
|
|
more.
|
|
|
|
### 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 phase 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 client(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 { progress, progress_max } = menu.data;
|
|
g.progress("bworld:arrow_empty", "bworld:arrow_full", 85, 34, 18, 18, progress / progress_max);
|
|
},
|
|
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. |
|
|
| `dim_background` | `false` draws it right over the world, without darkening it. Optional. |
|
|
| `inventory_overlays` | `false` keeps [inventory overlays](../gui/index.md#inventory-overlays), like the recipe viewer's item list, off it. Optional. |
|
|
|
|
The menu it gets:
|
|
|
|
```ts
|
|
interface ClientMenu {
|
|
readonly type: string;
|
|
readonly title: string;
|
|
readonly open_data: unknown;
|
|
readonly data: Readonly<Record<string, number>>; // 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.
|
|
|
|
## 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` while the server phase
|
|
runs. 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. The base game puts its curios slots in 2 columns this way, in `mods/bworld/scripts/curios.ts`.
|
|
|
|
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 and fluid slot's position, the data. |
|
|
| `menu_content` | server | Every slot's item, by menu slot index, when any changed. |
|
|
| `menu_tanks` | server | Every fluid slot's fluid, amount and capacity, 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. |
|
|
| `click_fluid` | client | A fluid slot of the open menu, by its index among the fluid slots. |
|
|
| `menu_button` | client | A button of the open menu. |
|
|
|
|
The code is in `server/game/menu.ts` and `client/menu.ts`.
|