Fluid data
This commit is contained in:
@@ -96,7 +96,7 @@ Shared scripts hold a block component's rules, the code Minecraft runs on both s
|
||||
(`state_for_placement`), how it changes with the blocks beside it (`on_neighbor_change`) and what right clicking it
|
||||
changes (`on_interact`, like a trapdoor opening). See [Block Components](../blocks/components.md#rules). The server
|
||||
script registers the rest of the same component, like its `on_break` or an `on_interact` that opens a screen, under the
|
||||
same id.
|
||||
same id. They also hold [fluids' rules](../resources/fluids.md#rules), which name and color a fluid by its data.
|
||||
|
||||
On the server they run on the real world. On a client they run on what that client shows, when its player places, breaks
|
||||
or right clicks a block, so a fence joins its neighbors the moment it's placed. What they change there is a guess, and
|
||||
|
||||
@@ -165,6 +165,7 @@ ctx.items.mod(id); // the name of the mod that added it
|
||||
ctx.items.components(id); // its components and their params, like the server's, to show what they mean
|
||||
ctx.blocks.exists(id);
|
||||
ctx.blocks.components(id); // the same for a block, like what a crab pot catches with each bait
|
||||
ctx.fluids.name(fluid); // and color, exists and same, see ../resources/fluids.md#using-fluids
|
||||
ctx.loot.table(id); // a loot table file's JSON, or undefined. tables server scripts make aren't here
|
||||
ctx.recipes.list(type?); // every recipe as its JSON, including mods' own types
|
||||
ctx.options.key_inventory; // the key the player has bound, like Minecraft's Options key mappings
|
||||
|
||||
@@ -26,3 +26,65 @@ here has no block, so it can't be placed.
|
||||
| `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
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user