Fluid handler

This commit is contained in:
2026-10-31 16:54:24 -03:00
parent 23d02c5229
commit 407ed77385
20 changed files with 602 additions and 127 deletions
+3 -2
View File
@@ -8,6 +8,7 @@ import type {
ClientContext,
ClientMenu,
Color,
FluidStack,
Graphics,
Instance,
Instances,
@@ -17,7 +18,7 @@ import type {
VisualBlock,
} from "bworld/client";
import { MINIGAMES } from "./minigames/mod.ts";
import { JUICER_CAPACITY, JUICER_PRESSES, type JuicerTank } from "./shared.ts";
import { JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
const WIDTH = 176;
@@ -65,7 +66,7 @@ function juicer_visual(block: VisualBlock, instances: Instances): Visual {
target = press_target();
for (const instance of shown) instance.delete();
shown = [];
const tank = block.get_client_data<JuicerTank>("tank");
const tank = block.get_client_data<FluidStack>("tank");
if (tank) {
const juice = instances.fluid(tank.fluid);
const [low, high] = JUICER_INSIDE;
+2 -2
View File
@@ -5,7 +5,7 @@
// bworld:juiceable, its own juice, and mods can add more like any recipe, as files or from their shared scripts.
// whatever makes juice reads them, like the juicer. with create installed, each is also a create:compacting recipe,
// so its press can make juice in a basin
import type { FluidVariant, SharedContext, TagApi } from "bworld/shared";
import type { FluidStack, SharedContext, TagApi } from "bworld/shared";
import type { JuiceableParams } from "./shared.ts";
export const JUICING = "bworld:juicing";
@@ -14,7 +14,7 @@ export interface JuicingRecipe {
// the items it presses
input: string[];
// what one of them makes
juice: { fluid: FluidVariant; amount: number };
juice: FluidStack;
}
// a recipe for every juiceable item, pressing one into the juice named after it, as much as its component says
+25 -26
View File
@@ -2,13 +2,13 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// the base game's block and item behavior, written against the same api as any other mod
import type { BlockRef, Container, Player, Season, ServerContext } from "bworld/server";
import type { BlockRef, Container, FluidHandler, FluidStack, Player, Season, ServerContext } from "bworld/server";
import { register_curios } from "./curios.ts";
import { register_effects } from "./effects.ts";
import { register_fishing } from "./fishing.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
import { register_verdigris } from "./verdigris/server.ts";
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES, type JuicerTank } from "./shared.ts";
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
import { juicing_for, juicing_recipes } from "./juicing.ts";
export function setup(ctx: ServerContext) {
@@ -851,14 +851,14 @@ function register_buckets(ctx: ServerContext) {
// the juicer has no screen. right clicking it with something a juicing recipe takes puts that in, one kind at a time,
// and pressing it (sneaking with an empty hand) JUICER_PRESSES times turns one of it into the recipe's juice, while
// that fits in the tank. right clicking it with an empty fluid container fills it from the tank. breaking it drops what's in it, and the
// juice is lost. clients see what's in it, its juice and how many times it's been pressed toward the next as its
// "stack", "tank" and "presses" client data, which its visual draws (see scripts/client.ts)
// that fits in the tank. its fluid handler only gives the juice out, so right clicking it with an empty fluid container
// fills that from the tank. breaking it drops what's in it, and the juice is lost. clients see what's in it, its juice
// and how many times it's been pressed toward the next as its "stack", "tank" and "presses" client data, which its
// visual draws (see scripts/client.ts)
interface JuicerData {
container?: string;
// how many times it's been pressed toward the next juice
presses?: number;
tank?: JuicerTank;
}
function register_juicer(ctx: ServerContext) {
@@ -871,6 +871,18 @@ function register_juicer(ctx: ServerContext) {
const data = item?.data !== undefined && { data: item.data };
block.set_client_data("stack", item && { id: item.id, count: item.count, ...data });
};
const tank = (block: BlockRef) =>
ctx.fluids.tank_handler({
tanks: 1,
capacity: JUICER_CAPACITY,
get: () => {
const juice = block.get_client_data<FluidStack>("tank");
return juice ? [juice] : [];
},
set: ([juice]) => block.set_client_data("tank", juice),
});
// what others reach, which juice only comes out of
const handler = (block: BlockRef): FluidHandler => ({ ...tank(block), fill: () => 0 });
ctx.components.register_block("bworld:juicer", {
on_interact(block, _params, player) {
@@ -883,29 +895,16 @@ function register_juicer(ctx: ServerContext) {
// true even when none fit, so the fruit isn't eaten instead
return true;
}
const capacity = held && ctx.items.fluid_capacity(held.id);
if (held && capacity !== undefined) {
if ((held.data as { fluid?: unknown } | undefined)?.fluid !== undefined) return false;
const tank = data.tank;
if (!tank || tank.amount < capacity) return false;
const amount = tank.amount - capacity;
const left = amount > 0 ? { ...tank, amount } : undefined;
block.data = { ...data, tank: left } satisfies JuicerData;
block.set_client_data("tank", left);
player.exchange_held({ id: held.id, count: 1, data: { fluid: tank.fluid } });
return true;
if (held && ctx.items.fluid_capacity(held.id) !== undefined) {
return ctx.fluids.interact(player, handler(block));
}
if (held || !player.is_sneaking()) return false;
const item = container.get(0);
const recipe = item && juicing_for(recipes, item.id);
if (!item || !recipe) return false;
const { fluid: juice, amount } = recipe.juice;
const tank = data.tank;
const fits = (tank ? tank.amount : 0) + amount <= JUICER_CAPACITY &&
(!tank || ctx.fluids.same(tank.fluid, juice));
if (!fits) return false;
const juice = tank(block);
if (juice.fill(recipe.juice, "simulate") < recipe.juice.amount) return false;
const presses = (data.presses ?? 0) + 1;
if (presses < JUICER_PRESSES) {
block.data = { ...data, presses } satisfies JuicerData;
@@ -914,15 +913,15 @@ function register_juicer(ctx: ServerContext) {
}
item.count -= 1;
show(block, container);
const filled = { fluid: juice, amount: (tank?.amount ?? 0) + amount };
block.data = { ...data, presses: 0, tank: filled } satisfies JuicerData;
block.set_client_data("tank", filled);
juice.fill(recipe.juice, "execute");
block.data = { ...data, presses: 0 } satisfies JuicerData;
block.set_client_data("presses", undefined);
return true;
},
on_break(block) {
drop_contents(ctx, block);
},
fluid_handler: (block) => handler(block),
});
}
+3 -7
View File
@@ -5,7 +5,7 @@
// server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor
// or a door open the moment they click it. and the juice's rules, which name and color it by the item it's from, and
// the recipes that make it
import type { FluidVariant, SharedBlock, SharedContext } from "bworld/shared";
import type { SharedBlock, SharedContext } from "bworld/shared";
import { register_juicing } from "./juicing.ts";
import { register_verdigris_rules } from "./verdigris/shared.ts";
@@ -147,15 +147,11 @@ export interface JuiceableParams {
color: string;
}
// how much juice the juicer holds, in mB, and the juice in it: in its tile data, and in its "tank" client data for its
// visual to draw (see register_juicer in server.ts and juicer_visual in client.ts)
// how much juice the juicer holds, in mB. the juice in it is its "tank" client data, a FluidStack, which its visual
// draws (see register_juicer in server.ts and juicer_visual in client.ts)
export const JUICER_CAPACITY = 1000;
// how many presses make one of what's in it into juice. its visual lowers its press for each one toward the next
export const JUICER_PRESSES = 4;
export interface JuicerTank {
fluid: FluidVariant;
amount: number;
}
// what juice sells for, as a multiple of what the items pressed to make it sell for
export const JUICE_VALUE = 1.2;