12 KiB
Fluids
Fluids are what bottles and tanks hold, like juice or oil. They work like NeoForge's FluidTypes: a fluid is defined
once, apart from any block, container or amount.
This isn't the same as a block with fluid, 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:
{
"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. |
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 and a slot of it 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, 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, in a FluidStack { fluid, amount } like NeoForge's, which is what a tank holds.
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
gives one of its fluids rules for that, like NeoForge's FluidType.getDescription and FluidTintSource.colorAsStack:
import type { SharedContext } from "cubi/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, data?) 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:
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.
Shared scripts have ctx.fluids.exists and ctx.fluids.same too, which don't depend on rules, like a
recipe type that's found by a fluid.
Containers
An item with fluid_container holds one fluid, full or empty, like a bucket or a bottle:
{
"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 tinted "fluid", which is drawn in the fluid's
color 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. Client and shared scripts have it too,
and the base game shows it in tooltips.
A server script fills and empties one through its fluid handler, and ctx.items.fluid_capacity(id)
says how much one holds.
Fluid Handlers
A fluid handler is how fluid goes in and out of something without knowing how it keeps it, like NeoForge's
IFluidHandler: a machine's tanks, a fluid container, another mod's pipe. It's a list of tanks, each with a fluid and
an amount in mB, a FluidStack:
interface FluidHandler {
tanks(): number;
fluid_in_tank(tank: number): FluidStack | undefined; // undefined when it's empty
tank_capacity(tank: number): number;
is_fluid_valid(tank: number, fluid: FluidVariant): boolean; // whether it can ever hold it
fill(stack: FluidStack, action: FluidAction): number; // how much of stack went in
drain(what: FluidStack | number, action: FluidAction): FluidStack | undefined; // what came out
}
fill and drain take an action, "execute" or "simulate", like NeoForge's FluidAction. Simulating says what
would happen without changing anything, so a script can check before it moves fluid. drain takes a FluidStack for up
to that much of that fluid, or a number for up to that many mB of whatever the handler gives first.
Blocks
A block has a fluid handler when one of its components gives one, like registering
NeoForge's Capabilities.FluidHandler.BLOCK. fluid_handler(block, params, face) returns it, made new each time from
what the block keeps. face is the side it's reached from, undefined for no side in particular, so a block can give
different tanks to different sides, or none. Others get it with ctx.fluids.block_handler:
ctx.components.register_block("copper_tools:barrel", {
fluid_handler(block, _params, face) {
if (face === "bottom") return undefined;
return ctx.fluids.tank_handler({
tanks: 2,
capacity: 4000,
get: () => block.get_client_data<FluidStack[]>("tanks") ?? [],
set: (tanks) => block.set_client_data("tanks", tanks),
});
},
});
// from somewhere else, like a pump below the barrel's side
const barrel = ctx.fluids.block_handler(x, y, z, "west");
ctx.fluids.tank_handler(options) is the handler for tanks a script keeps, like NeoForge's FluidTank. A fluid goes
into the tank that has it, or else an empty one, so no two tanks hold the same fluid, and a number of mB drains from the
first. get gives what's in them, without empty ones, and set keeps what an executed fill or drain left. Here
it's the block's client data, so its visual can draw it, but it can be its tile
data or anywhere else.
A handler is a plain object, so a block that only gives fluid out, like the base game's juicer, can change one method:
fluid_handler: (block) => ({ ...tanks(block), fill: () => 0 }),
Items
ctx.fluids.item_handler(stack) is the handler of one of a stack of fluid containers, undefined for an
item that isn't one, like NeoForge's Capabilities.FluidHandler.ITEM. It has one tank, and like NeoForge's
FluidBucketWrapper it's full or empty: it only fills with its whole capacity at once, and only drains all of it.
Filling and draining change its container, one item as it is now, not the stack it came from.
Moving Fluid
ctx.fluids.transfer(from, to, max) pours up to max mB from one handler into the other, what to takes of what
from gives, and returns what moved, undefined for nothing, like NeoForge's FluidUtil.tryFluidTransfer.
ctx.fluids.interact(player, handler) is a player using the fluid container they hold on a handler, like
FluidUtil.interactWithFluidHandler: it fills the container from the handler, or else empties it into it, and the held
container becomes the full or empty one. It returns whether any fluid moved, and false when they don't hold a fluid
container. A block with tanks lets players fill and empty bottles and buckets on it from its on_interact:
on_interact(block, _params, player) {
const tanks = ctx.fluids.block_handler(block.x, block.y, block.z);
return tanks !== undefined && ctx.fluids.interact(player, tanks);
},
A menu shows a handler's tanks with add_fluid_slot, which players fill and
empty fluid containers on from the cursor.