134 lines
5.6 KiB
Markdown
134 lines
5.6 KiB
Markdown
# Fluids
|
|
|
|
Fluids are what bottles and tanks hold, like juice or oil. They work like NeoForge's `FluidType`s: a fluid is defined
|
|
once, apart from any block, container or amount.
|
|
|
|
This isn't the same as a [block with `fluid`](../blocks/index.md#fluids), like water, which flows in the world. A fluid
|
|
here has no block, so it can't be placed.
|
|
|
|
## Defining a Fluid
|
|
|
|
`fluids/maple_syrup.json`:
|
|
|
|
```json
|
|
{
|
|
"format_version": 1,
|
|
"fluid": {
|
|
"id": "copper_tools:maple_syrup",
|
|
"name": "Maple Syrup",
|
|
"color": "#a0522d"
|
|
}
|
|
}
|
|
```
|
|
|
|
| Field | Required | What it does |
|
|
| ------- | -------- | ------------------------------------------------------------------------------------------- |
|
|
| `id` | yes | The fluid's id, in the mod's [namespace](../concepts/registries.md#identifiers). |
|
|
| `name` | no | What players see. `copper_tools:maple_syrup` is "Maple Syrup" when not set, like an item's. |
|
|
| `color` | yes | Its color, like `"#a0522d"`, for whatever shows it. |
|
|
|
|
## Fluids With Data
|
|
|
|
A fluid with data is a `FluidVariant`, `{ id, data? }`, like Fabric's. The data is any JSON value, and tells fluids of
|
|
one id apart: `copper_tools:syrup` can be maple syrup or birch syrup,
|
|
`{ "id": "copper_tools:syrup", "data": { "tree": "bworld:birch_log" } }`. A variant has no amount. Whatever holds fluid
|
|
keeps the amount next to it, like a tank's `{ fluid, amount }`.
|
|
|
|
Two variants are the same fluid when their ids are the same and so is their data, whatever order its keys are in. No
|
|
data is the same as `{}`, so a fluid whose data was emptied is the plain fluid again. Maple and birch syrup never share
|
|
a tank.
|
|
|
|
## Rules
|
|
|
|
A fluid's name and color can depend on its data. The mod's [shared script](../concepts/sides.md#shared-scripts) gives
|
|
one of its fluids rules for that, like NeoForge's `FluidType.getDescription` and `FluidTintSource.colorAsStack`:
|
|
|
|
```ts
|
|
import type { SharedContext } from "bworld/shared";
|
|
|
|
interface SyrupData {
|
|
tree?: string;
|
|
}
|
|
|
|
export function setup(ctx: SharedContext) {
|
|
ctx.fluids.register("copper_tools:syrup", {
|
|
name(data) {
|
|
const { tree } = (data ?? {}) as SyrupData;
|
|
return tree ? `${ctx.items.name(tree)} Syrup` : undefined;
|
|
},
|
|
color(data) {
|
|
const { tree } = (data ?? {}) as SyrupData;
|
|
return tree === "bworld:birch_log" ? "#d9a441" : undefined;
|
|
},
|
|
});
|
|
}
|
|
```
|
|
|
|
| Rule | Returns |
|
|
| ------------- | -------------------------------------------------------- |
|
|
| `name(data)` | What players see, or `undefined` for the JSON's name. |
|
|
| `color(data)` | A color like `"#d9a441"`, or `undefined` for the JSON's. |
|
|
|
|
`data` is the variant's, `undefined` when it has none, and mustn't be changed. A rule that throws, or returns something
|
|
that isn't a name or a color, is logged and the JSON's is used. A fluid has one set of rules, from its own mod, and they
|
|
can only be registered during `setup`.
|
|
|
|
The rules are in the shared script because both sides need them: the server for tooltips, and clients to draw the fluid.
|
|
Shared scripts have `ctx.items.exists(id)`, `ctx.items.name(id)` and `ctx.items.components(id)` for rules that read an
|
|
item, like syrup named after its tree.
|
|
|
|
## Using Fluids
|
|
|
|
Server and client scripts have `ctx.fluids`, which goes through the rules:
|
|
|
|
```ts
|
|
const birch = { id: "copper_tools:syrup", data: { tree: "bworld:birch_log" } };
|
|
ctx.fluids.exists("copper_tools:syrup"); // true
|
|
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" } } });
|
|
}
|
|
```
|