Fluid handler
This commit is contained in:
@@ -59,20 +59,21 @@ component.
|
||||
|
||||
## Handlers
|
||||
|
||||
| Handler | Called when | Returns |
|
||||
| --------------------------------------------- | --------------------------------------------------------------------------------------------------- | -------------------------- |
|
||||
| `on_create(block, params)` | The block is placed or set, by a player or a script. | |
|
||||
| `on_break(block, params, player?)` | The block is broken or replaced. `player` when a player broke it. | |
|
||||
| `on_click(block, params, player)` | A player starts hitting it. | |
|
||||
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
|
||||
| `on_scroll(block, params, player, scroll)` | A player turns the mouse wheel looking at it. See [Scrolling](#scrolling). | |
|
||||
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
|
||||
| `on_second(block, params, dt)` | Every second, near a player. | |
|
||||
| `on_new_day(block, params, day)` | Every morning at 06:00. See [Ticking](#ticking) for where. | |
|
||||
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. See [Changes](#changes). | |
|
||||
| `on_state_change(block, params, before)` | Its own states change without it being replaced, by `set_state` or a rule. See [Changes](#changes). | |
|
||||
| `on_scheduled_tick(block, params)` | A tick it asked for comes. See [Scheduled Ticks](#scheduled-ticks). | |
|
||||
| `on_entity_stand(block, params, entity)` | An entity standing on it moves, every tick it does. See [Entities on It](#entities-on-it). | |
|
||||
| Handler | Called when | Returns |
|
||||
| --------------------------------------------- | --------------------------------------------------------------------------------------------------------- | -------------------------------- |
|
||||
| `on_create(block, params)` | The block is placed or set, by a player or a script. | |
|
||||
| `on_break(block, params, player?)` | The block is broken or replaced. `player` when a player broke it. | |
|
||||
| `on_click(block, params, player)` | A player starts hitting it. | |
|
||||
| `on_interact(block, params, player)` | A player right clicks it. | `true` if it did something |
|
||||
| `on_scroll(block, params, player, scroll)` | A player turns the mouse wheel looking at it. See [Scrolling](#scrolling). | |
|
||||
| `on_tick(block, params, dt)` | Every tick, 20 times a second, near a player. | |
|
||||
| `on_second(block, params, dt)` | Every second, near a player. | |
|
||||
| `on_new_day(block, params, day)` | Every morning at 06:00. See [Ticking](#ticking) for where. | |
|
||||
| `on_neighbor_change(block, params, neighbor)` | A block on one of its 6 sides is placed, broken or changes its states. See [Changes](#changes). | |
|
||||
| `on_state_change(block, params, before)` | Its own states change without it being replaced, by `set_state` or a rule. See [Changes](#changes). | |
|
||||
| `on_scheduled_tick(block, params)` | A tick it asked for comes. See [Scheduled Ticks](#scheduled-ticks). | |
|
||||
| `on_entity_stand(block, params, entity)` | An entity standing on it moves, every tick it does. See [Entities on It](#entities-on-it). | |
|
||||
| `fluid_handler(block, params, face?)` | Something reaches for its tanks from `face`. See [Fluid Handlers](../resources/fluids.md#fluid-handlers). | a `FluidHandler`, or `undefined` |
|
||||
|
||||
`block` is `{ id, x, y, z, biome, season, data, get_client_data, set_client_data, set_ticking }`, where `data` is the
|
||||
block's [tile data](tiledata.md) and the client data functions its [part clients see](tiledata.md#client-data), `biome`
|
||||
|
||||
@@ -40,7 +40,7 @@ animated, and any fluid can use it. Without one, the fluid is drawn with the mis
|
||||
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 }`.
|
||||
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
|
||||
@@ -146,13 +146,86 @@ how much it holds, rounded up to a whole number. A jar of maple syrup worth 40 a
|
||||
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:
|
||||
A server script fills and empties one through its [fluid handler](#fluid-handlers), 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`](#fluids-with-data):
|
||||
|
||||
```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" } } });
|
||||
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](../blocks/components.md#handlers) 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`:
|
||||
|
||||
```ts
|
||||
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](../blocks/tiledata.md#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:
|
||||
|
||||
```ts
|
||||
fluid_handler: (block) => ({ ...tanks(block), fill: () => 0 }),
|
||||
```
|
||||
|
||||
### Items
|
||||
|
||||
`ctx.fluids.item_handler(stack)` is the handler of one of a stack of [fluid containers](#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`:
|
||||
|
||||
```ts
|
||||
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);
|
||||
},
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user