Fluid containers

This commit is contained in:
2026-10-25 15:02:31 -03:00
parent fc27771a92
commit 26b59a1899
24 changed files with 486 additions and 82 deletions
+5
View File
@@ -29,6 +29,7 @@ interface Player {
readonly held_item: ItemStack | undefined;
give_item(id: string, count?: number, data?: unknown): void; // what doesn't fit drops at their feet
exchange_held(result: ItemStack): void; // one of the held item becomes result, see below
send_message(text: string, options?: { overlay?: boolean }): void; // in chat, or above the hotbar with overlay
set_worn(key: string, items: ItemStack[]): void; // what's drawn on them, see Worn Items
teleport(x: number, y: number, z: number): void;
@@ -63,6 +64,10 @@ interface Player {
`inventory` is a live [container](../inventories/containers.md), and `held_item` a live stack: changing them changes
what the player has.
`exchange_held(result)` is for an item that turns into another when used, like an empty bucket filling: one of the held
stack goes, and `result` goes straight into their hand when that was the last one, or into their inventory (at their
feet when it's full) when there are more.
`eye_position` and `look_direction` are for aiming. With [`ctx.world.raycast`](../blocks/index.md#raycasts) they find
what a player is looking at, like Minecraft's `getPlayerPOVHitResult`:
+47 -16
View File
@@ -19,6 +19,9 @@ interface ItemStack {
`data` is how one stack differs from another of the same item, like the water left in a watering can or the damage on a
tool. It's saved, and synced to clients as part of the container the stack is in, so client code can read it.
Two stacks of one item only merge into one when their `data` is the same, whatever order its keys are in, in an
inventory or on the ground. No data is the same as `{}`.
Stacks handed to server scripts are live: setting `count` or `data` on one changes the real stack, and a stack counted
down to 0 is gone.
@@ -39,22 +42,24 @@ down to 0 is gone.
## Properties
| Field | Default | Meaning |
| ----------------- | ------------------- | ------------------------------------------------------------------------------------- |
| `id` | required | The item's id. |
| `texture` | required | The texture it's drawn with. Leave it out to [fill in a block's item](#block-items). |
| `name` | from the id | What players see. `copper_tools:copper_ingot` is "Copper Ingot" when not set. |
| `lore` | none | Tooltip text under the name. `\n` starts a new line. |
| `max_stack` | `64`, `1` for tools | How many fit in one slot. |
| `base_sell_value` | none | What one is worth when sold, a whole number of 0 or more. |
| `places` | none | A block it places when used on a block, like seeds. |
| `tool` | none | Makes it a [tool](tools.md). |
| `food` | none | Makes it [edible](food.md). |
| `data_textures` | none | Other textures, picked by the stack's `data`, see below. |
| `model` | none | A [model](../resources/models.md#item-models) it's drawn with instead of its texture. |
| `worn` | none | How it looks worn on a player, see [below](#worn-models). |
| `targets_fluids` | `false` | Players holding it aim at fluids, like buckets and fishing rods. Others aim through. |
| `components` | none | [Item components](#item-components) and their parameters. |
| Field | Default | Meaning |
| ----------------- | ------------------- | -------------------------------------------------------------------------------------- |
| `id` | required | The item's id. |
| `texture` | required | The texture it's drawn with. Leave it out to [fill in a block's item](#block-items). |
| `name` | from the id | What players see. `copper_tools:copper_ingot` is "Copper Ingot" when not set. |
| `lore` | none | Tooltip text under the name. `\n` starts a new line. |
| `max_stack` | `64`, `1` for tools | How many fit in one slot. |
| `base_sell_value` | none | What one is worth when sold, a whole number of 0 or more. |
| `places` | none | A block it places when used on a block, like seeds. |
| `tool` | none | Makes it a [tool](tools.md). |
| `food` | none | Makes it [edible](food.md). |
| `data_textures` | none | Other textures, picked by the stack's `data`, see below. |
| `model` | none | A [model](../resources/models.md#item-models) it's drawn with instead of its texture. |
| `worn` | none | How it looks worn on a player, see [below](#worn-models). |
| `targets_fluids` | `false` | Players holding it aim at fluids, like buckets and fishing rods. Others aim through. |
| `fluid_container` | none | Makes it hold a fluid, like a bucket, see [Fluids](../resources/fluids.md#containers). |
| `layers` | none | Textures drawn over its own, each in a color, see [below](#layers). |
| `components` | none | [Item components](#item-components) and their parameters. |
### Textures From Data
@@ -67,6 +72,29 @@ is out:
A stack whose `data.cast` is truthy uses `bworld:fishing_rod_cast`. When several keys are, the first listed wins.
### Layers
`layers` are textures drawn over the item's own, in order, like the fluid in a bucket or the trim on armor. Each can be
drawn in a color, with `tint`:
```json
"layers": [
{ "texture": "copper_tools:jar_fluid", "tint": "fluid" },
{ "texture": "copper_tools:jar_label", "tint": "#c42d39" }
]
```
| `tint` | The layer's drawn in |
| --------------------------------- | ---------------------------------------------------------------------------------------------------- |
| none | Its own colors. |
| a color, like `"#c42d39"` | That color. |
| `"grass"`, `"foliage"`, `"water"` | That color outside any biome, like a block's [tint](../blocks/index.md#tinting) in its item. |
| `"fluid"` | The color of the fluid a [fluid container](../resources/fluids.md#containers) holds. Not when empty. |
A tint multiplies the texture's colors, so a tinted layer is painted in light grays. A layer whose tint has no color for
a stack, like `"fluid"` on an empty container, isn't drawn. Layers are drawn on the front and back of a flat item, so
they should stay inside the item's own shape, and an item drawn with a `model` can't have them.
### Worn Models
`worn` says how an item looks on a player who wears it, like Bedrock's attachables. A server script says what a player
@@ -218,8 +246,11 @@ const { item, given } = ctx.world.use_item_on({ id: "bworld:cobblestone", count:
ctx.items.exists("bworld:carrot"); // true
ctx.items.ids(); // every item, in load order
ctx.items.max_stack("bworld:wood_pickaxe"); // 1
ctx.items.max_stack(id, data); // the same for a stack with this data, fewer for a full fluid container
ctx.items.fluid_capacity("bworld:bucket"); // how much a fluid container holds full, or undefined
ctx.items.tool("bworld:wood_pickaxe"); // "pickaxe", or undefined for items that aren't tools
ctx.items.sell_value("bworld:copper_coin"); // 1, or undefined for items without a base_sell_value
player.give_item("bworld:carrot", 3); // what doesn't fit drops at their feet
player.exchange_held({ id: "bworld:bucket", count: 1, data }); // one of the held item becomes this
```
+43
View File
@@ -88,3 +88,46 @@ ctx.fluids.name(birch); // "Birch Log Syrup"
ctx.fluids.color(birch); // "#d9a441"
ctx.fluids.same(birch, { id: "copper_tools:syrup" }); // false, the data is different
```
## Containers
An item with `fluid_container` holds one fluid, full or empty, like a bucket or a bottle:
```json
{
"format_version": 1,
"item": {
"id": "copper_tools:jar",
"texture": "copper_tools:jar",
"max_stack": 16,
"fluid_container": { "capacity": 250, "full_max_stack": 4 },
"layers": [{ "texture": "copper_tools:jar_fluid", "tint": "fluid" }]
}
}
```
| Field | Required | What it does |
| ---------------- | -------- | ---------------------------------------------------------------------- |
| `capacity` | yes | How much a full one holds, in mB. A bucket is 1000. |
| `full_max_stack` | no | How many full ones fit in a slot. The item's `max_stack` when not set. |
A full one's stack data is `{ "fluid": <variant> }`, like
`{ "fluid": { "id": "copper_tools:syrup", "data": { ... } } }`, and one without a `fluid` is empty. It's only ever full
or empty, so there's no amount: a full one holds `capacity`. Stacks only merge when their data is the same, so jars of
maple and birch syrup don't stack, and an empty jar doesn't stack with a full one.
What's in it is drawn with a [layer](../items/index.md#layers) tinted `"fluid"`, which is drawn in the fluid's
[color](#rules) while it's full and not at all while it's empty. Its texture is the size of the item's, opaque only
where the fluid shows, and light gray, since the color multiplies it: white comes out as the fluid's own color. Its
tooltip says what's in it and how much, like "Birch Log Syrup, 250 mB".
A server script fills one with [`player.exchange_held`](../entities/players.md#the-player-api), which turns one of the
held stack into the full one, and `ctx.items.fluid_capacity(id)` says how much it takes:
```ts
const held = player.held_item;
const capacity = held && ctx.items.fluid_capacity(held.id);
if (capacity !== undefined && !held!.data) {
player.exchange_held({ id: held!.id, count: 1, data: { fluid: { id: "copper_tools:syrup" } } });
}
```