Update modding docs

This commit is contained in:
2026-09-29 23:29:14 -03:00
parent f0cf4e4eeb
commit 1ac7a5c943
50 changed files with 2672 additions and 1924 deletions
+45
View File
@@ -0,0 +1,45 @@
# Containers
A container is a list of item slots that the server owns and saves: a chest's contents, a furnace's input, fuel and
result, a player's inventory. Server scripts make and use them through `ctx.containers`.
## Making Containers
```ts
const container = ctx.containers.create(27); // 1 to 256 slots, saved with the world
const id = container.id; // keep this, like in a block's tile data
ctx.containers.get(id); // the container, or undefined once it's deleted
ctx.containers.delete(id); // closes any screen showing it
```
Containers are saved with the world until they're deleted. A block that owns one keeps its id in
[tile data](../blocks/tiledata.md#blocks-with-containers), and deletes it when it's broken.
## Using Containers
```ts
interface Container {
readonly id: string;
readonly size: number;
get(slot: number): ItemStack | undefined; // live, see below
set(slot: number, item: ItemStack | undefined): void;
add(item: ItemStack): ItemStack | undefined; // fills matching stacks, then empty slots. returns what didn't fit
on_change(fn: (slot: number) => void): () => void; // returns a function that stops watching
}
```
```ts
container.set(0, { id: "bworld:coal", count: 8 });
const left = container.add({ id: "bworld:coal", count: 100 });
if (left) ctx.world.drop_item(x, y, z, left);
```
Stacks from `get` are live: setting `count` or `data` changes the stack in the container, and a stack counted down to 0
is gone. `set` and `add` copy what they're given.
Changes are synced to every player viewing the container, whether a script or a player made them.
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.
+93
View File
@@ -0,0 +1,93 @@
# 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.
## 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`.