# 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 ```