Files
bworld/docs/modding/inventories/menus.md
T
2026-09-30 00:30:33 -03:00

99 lines
4.5 KiB
Markdown

# Container Screens
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.
Every click goes to the server, which applies it following the screen's rules, and everyone viewing the container sees
the result.
## Opening a Screen
Usually from a block's `on_interact`:
```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;
},
});
```
Give the block `"interactive": true` so clients don't guess that the click places a block.
## Layout
`layout` lists which of the container's slots to show, and where:
| Field | Meaning |
| ------------- | ----------------------------------------------------------------------------------------------------------------- |
| `slot` | The container's slot. |
| `x`, `y` | Where, in slot units from the top left of the screen's area. Can be fractional. |
| `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. |
`rows` sets the height of the screen's area in slots. By default it fits the slots and bars.
`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.
## Progress Bars
`bars` draws progress bars, filled with one property divided by another:
| 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. |
Properties are synced when they change, so a component can update them every tick.
## The Screen Handle
`open_container` returns a handle:
```ts
interface ScreenHandle {
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`.