159 lines
8.2 KiB
Markdown
159 lines
8.2 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",
|
|
"texture": "bworld:water_still",
|
|
"base_sell_value": 40
|
|
}
|
|
}
|
|
```
|
|
|
|
| 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. |
|
|
| `texture` | no | What a [box of it](../blocks/visuals.md#fluids) and [a slot of it](../gui/index.md#graphics) are drawn with, tinted by its color, like NeoForge's still texture. See below. |
|
|
| `base_sell_value` | no | What 1000 mB is worth sold in a [container](#containers), a whole number of 0 or more. None when not set. |
|
|
|
|
Like a fluid container's layer, the texture is multiplied by the fluid's color, so a light gray one comes out in the
|
|
fluid's own color, and its alpha is how much shows through. The base game's `bworld:water_still` is such a texture,
|
|
animated, and any fluid can use it. Without one, the fluid is drawn with the missing texture.
|
|
|
|
## 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, color and value 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;
|
|
},
|
|
// worth twice the logs it takes to make, 4 to a bucket
|
|
sell_value(data) {
|
|
const { tree } = (data ?? {}) as SyrupData;
|
|
const value = tree && ctx.items.sell_value(tree);
|
|
return value ? value * 4 * 2 : 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. |
|
|
| `sell_value(data)` | What 1000 mB is worth, 0 or more and not always whole, or `undefined` for the JSON's `base_sell_value`. |
|
|
|
|
`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, a color or a value, 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)`, `ctx.items.components(id)` and
|
|
`ctx.items.sell_value(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.ids(); // every fluid's id in load order, like ["bworld:juice", "copper_tools:syrup"]
|
|
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
|
|
```
|
|
|
|
`ids` gives each fluid once, without data, so a fluid that's many things by its data, like syrup from each tree, is only
|
|
there as plain `copper_tools:syrup`. Client scripts also have `ctx.fluids.mod(id)`, the name of the mod that added it,
|
|
like `ctx.items.mod`.
|
|
|
|
## 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 full one sells for its own `base_sell_value` and what its fluid is worth on top: the fluid's value for 1000 mB times
|
|
how much it holds, rounded up to a whole number. A jar of maple syrup worth 40 a bucket sells for 10, and the jar's own
|
|
value when it has one. It can't be sold if neither has a value. Server scripts get this from
|
|
`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by, and tooltips show it.
|
|
|
|
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" } } });
|
|
}
|
|
```
|