Basin
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// 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), and the mechanical press
|
||||
import type { BlockRef, EntityRef, Id, ServerContext } from "bworld/server";
|
||||
// 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 {
|
||||
actual_speed,
|
||||
along,
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
roll_results,
|
||||
} from "./recipes/processing.ts";
|
||||
import { CYCLE, cycle_rate, PRESS_AT, type PressShown, SCAN_TICKS } from "./processing/press.ts";
|
||||
import { type BasinTank, drain, fill, insert_item } from "./processing/basin.ts";
|
||||
|
||||
interface Config {
|
||||
max_speed: number;
|
||||
@@ -194,6 +195,61 @@ export function setup(ctx: ServerContext) {
|
||||
});
|
||||
|
||||
register_press(ctx, pressing);
|
||||
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
|
||||
function register_basin(ctx: ServerContext) {
|
||||
const items_of = (block: BlockRef) => block.get_client_data<ItemStack[]>("items") ?? [];
|
||||
const tanks_of = (block: BlockRef) => block.get_client_data<BasinTank[]>("tanks") ?? [];
|
||||
const set_items = (block: BlockRef, items: readonly ItemStack[]) =>
|
||||
block.set_client_data("items", items.length > 0 ? items : undefined);
|
||||
const set_tanks = (block: BlockRef, tanks: BasinTank[]) =>
|
||||
block.set_client_data("tanks", tanks.length > 0 ? tanks : undefined);
|
||||
|
||||
ctx.components.register_block("create:basin", {
|
||||
on_entity_stand(block, _params, entity) {
|
||||
const stack = entity.get_item();
|
||||
if (!stack) return;
|
||||
const { items, inserted } = insert_item(items_of(block), stack, ctx.items.max_stack(stack.id, stack.data));
|
||||
if (inserted === 0) return;
|
||||
set_items(block, items);
|
||||
entity.set_item({ ...stack, count: stack.count - inserted });
|
||||
},
|
||||
on_interact(block, _params, player) {
|
||||
const held = player.held_item;
|
||||
if (!held) {
|
||||
for (const item of items_of(block)) player.give_item(item.id, item.count, item.data);
|
||||
set_items(block, []);
|
||||
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 } });
|
||||
}
|
||||
}
|
||||
// a fluid container is used up on the basin even when nothing fits, like create's
|
||||
return true;
|
||||
},
|
||||
on_break(block) {
|
||||
for (const item of items_of(block)) ctx.world.drop_item(block.x, block.y, block.z, item);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// presses items lying on the ground under it while it turns, like create's MechanicalPressBlockEntity pressing in the
|
||||
|
||||
Reference in New Issue
Block a user