Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+1 -1
View File
@@ -42,4 +42,4 @@ Changes are synced to every player viewing the container, whether a script or a
A player's inventory is a container too, `player.inventory`, with the hotbar in slots 0-8.
To let players see and use a container, open a [container screen](menus.md) for it.
To let players see and use a container, give them a [menu](menus.md) with its slots.
+159 -61
View File
@@ -1,99 +1,197 @@
# Container Screens
# Menus
A container screen shows a [container](containers.md)'s slots to a player, above their own inventory and hotbar, so they
can move items in and out. Chests, furnaces, the smithing table and Farmer's Delight's cooking pot are all container
screens. They're defined entirely by the server script, so they need no client code.
Menus are how players use [containers](containers.md), like NeoForge's. A GUI has two halves:
Every click goes to the server, which applies it following the screen's rules, and everyone viewing the container sees
the result.
- 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.
## Opening a Screen
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, Farmer's Delight's cooking pot and Curios are all menus.
Usually from a block's `on_interact`:
## 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
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) {
const screen = ctx.ui.open_container(player, {
container: ctx.containers.get(block.data.container)!,
layout: [
{ slot: 0, x: 3, y: 0, filter: "smeltable" },
{ slot: 1, x: 3, y: 2, filter: "fuel" },
{ slot: 2, x: 5, y: 1, output_only: true },
],
bars: [{
x: 4,
y: 1,
value: "progress",
max: "progress_max",
direction: "right",
empty_texture: "bworld:arrow_empty",
full_texture: "bworld:arrow_full",
}],
});
screen.set_property("progress", block.data.progress);
screen.set_property("progress_max", 200);
return true;
},
});
// server.ts
export function setup(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: "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.
## Layout
`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.
`layout` lists which of the container's slots to show, and where:
## Slots
| Field | Meaning |
`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 |
| ------------- | ----------------------------------------------------------------------------------------------------------------- |
| `slot` | The container's slot. |
| `x`, `y` | Where, in slot units from the top left of the screen's area. Can be fractional. |
| `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. |
`rows` sets the height of the screen's area in slots. By default it fits the slots and bars.
`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 follows the same rules. A stack shift clicked in the player's inventory goes into the screen's slots in
`layout` order, only those whose `filter` takes it and never `output_only` ones: first onto stacks of the same item,
then into empty slots. A stack shift clicked in the screen goes to the player's inventory, and an `output_only` slot
only gives its item that way when it all fits, after `on_take` agrees.
### Shift Clicking
## Progress Bars
Shift clicking a slot moves its stack to the other part of the menu, like NeoForge's `quickMoveStack`:
`bars` draws progress bars, filled with one property divided by another:
- 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.
| Field | Meaning |
| ------------------------------- | ----------------------------------------------------------------- |
| `x`, `y` | Where, in slot units. |
| `value`, `max` | Names of properties set with `screen.set_property(name, number)`. |
| `direction` | `"up"`, like the furnace's fire, or `"right"`, like its arrow. |
| `empty_texture`, `full_texture` | The textures for the empty and full bar. |
`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.
Properties are synced when they change, so a component can update them every tick.
## Data
## The Screen Handle
`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`.
`open_container` returns a handle:
## 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
interface ScreenHandle {
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;
set_property(id: string, value: number): void;
close(): void;
on_close(fn: () => void): void; // when it closes for any reason
}
```
A screen closes when the player closes it, leaves, or opens another screen, when a script calls `close()`, and when its
container is deleted. To keep a furnace's bars moving for everyone looking at it, keep the handles of open screens and
drop them in `on_close`, like the base game's furnace in `mods/bworld/scripts/server.ts`.
## 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<Record<string, number>>; // 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.
## 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 sent.
| 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`.