Fluid handler
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
|
||||
// create's blocks that do something: so far what turns (create:kinetic), the generators that turn it (the hand crank,
|
||||
// the creative motor and the water wheel), the mechanical press and the basin
|
||||
import type { BlockRef, EntityRef, FluidVariant, Id, ItemStack, ServerContext } from "bworld/server";
|
||||
import type { BlockRef, EntityRef, FluidStack, Id, ItemStack, ServerContext } from "bworld/server";
|
||||
import {
|
||||
actual_speed,
|
||||
along,
|
||||
@@ -30,10 +30,9 @@ import {
|
||||
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
|
||||
import {
|
||||
apply_basin_recipe,
|
||||
BASIN_TANK_CAPACITY,
|
||||
BASIN_TANKS,
|
||||
type BasinContents,
|
||||
type BasinTank,
|
||||
drain,
|
||||
fill,
|
||||
insert_item,
|
||||
} from "./processing/basin.ts";
|
||||
|
||||
@@ -214,15 +213,20 @@ export function setup(ctx: ServerContext) {
|
||||
register_basin(ctx);
|
||||
}
|
||||
|
||||
// holds items dropped into it and fluid poured in, like create's BasinBlock: an item that lands in it goes in, a full
|
||||
// fluid container used on it empties into it and an empty one fills from it, and an empty hand takes every item out.
|
||||
// what's in it is its "items" and "tanks" client data, kept nowhere else, which its visual draws. breaking it drops
|
||||
// the items, and the fluid is lost
|
||||
// holds items dropped into it and fluid poured in, like create's BasinBlock: an item that lands in it goes in, and an
|
||||
// empty hand takes every item out. its tanks are its fluid handler, which a full fluid container used on it empties
|
||||
// into and an empty one fills from. what's in it is its "items" and "tanks" client data, kept nowhere else, which its
|
||||
// visual draws. breaking it drops the items, and the fluid is lost
|
||||
function register_basin(ctx: ServerContext) {
|
||||
const items_of = (block: BlockRef) => basin_contents(block).items;
|
||||
const tanks_of = (block: BlockRef) => basin_contents(block).tanks;
|
||||
const set_items = (block: BlockRef, items: readonly ItemStack[]) => set_basin(block, { items });
|
||||
const set_tanks = (block: BlockRef, tanks: readonly BasinTank[]) => set_basin(block, { tanks });
|
||||
const tanks = (block: BlockRef) =>
|
||||
ctx.fluids.tank_handler({
|
||||
tanks: BASIN_TANKS,
|
||||
capacity: BASIN_TANK_CAPACITY,
|
||||
get: () => basin_contents(block).tanks,
|
||||
set: (tanks) => set_basin(block, { tanks }),
|
||||
});
|
||||
|
||||
ctx.components.register_block("create:basin", {
|
||||
on_entity_stand(block, _params, entity) {
|
||||
@@ -241,23 +245,9 @@ function register_basin(ctx: ServerContext) {
|
||||
set_basin(block, { items: [], outputs: [] });
|
||||
return true;
|
||||
}
|
||||
const capacity = ctx.items.fluid_capacity(held.id);
|
||||
// anything else is used or placed as if the basin weren't there
|
||||
if (capacity === undefined) return false;
|
||||
const fluid = (held.data as { fluid?: FluidVariant } | undefined)?.fluid;
|
||||
if (fluid) {
|
||||
const tanks = fill(tanks_of(block), fluid, capacity, (a, b) => ctx.fluids.same(a, b));
|
||||
if (tanks) {
|
||||
set_tanks(block, tanks);
|
||||
player.exchange_held({ id: held.id, count: 1 });
|
||||
}
|
||||
} else {
|
||||
const drained = drain(tanks_of(block), capacity);
|
||||
if (drained) {
|
||||
set_tanks(block, drained.tanks);
|
||||
player.exchange_held({ id: held.id, count: 1, data: { fluid: drained.fluid } });
|
||||
}
|
||||
}
|
||||
if (ctx.items.fluid_capacity(held.id) === undefined) return false;
|
||||
ctx.fluids.interact(player, tanks(block));
|
||||
// a fluid container is used up on the basin even when nothing fits, like create's
|
||||
return true;
|
||||
},
|
||||
@@ -265,6 +255,7 @@ function register_basin(ctx: ServerContext) {
|
||||
const { items, outputs } = basin_contents(block);
|
||||
for (const item of [...items, ...outputs]) ctx.world.drop_item(block.x, block.y, block.z, item);
|
||||
},
|
||||
fluid_handler: (block) => tanks(block),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -273,7 +264,7 @@ function basin_contents(block: BlockRef): BasinContents {
|
||||
return {
|
||||
items: block.get_client_data<ItemStack[]>("items") ?? [],
|
||||
outputs: block.get_client_data<ItemStack[]>("outputs") ?? [],
|
||||
tanks: block.get_client_data<BasinTank[]>("tanks") ?? [],
|
||||
tanks: block.get_client_data<FluidStack[]>("tanks") ?? [],
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user