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
+1
View File
@@ -7,6 +7,7 @@ import type { LootTableJson } from "../loot.ts";
export type {
FluidApi,
FluidStack,
FluidVariant,
Id,
ItemStack,
+81 -3
View File
@@ -2,7 +2,19 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
// what server scripts get, see docs/modding/
import type { EventSignal, Face, FluidApi, Id, ItemStack, ModInfo, Position, Season, TagApi } from "./shared.ts";
import type {
EventSignal,
Face,
FluidApi,
FluidStack,
FluidVariant,
Id,
ItemStack,
ModInfo,
Position,
Season,
TagApi,
} from "./shared.ts";
import type { ArgumentType, CommandNode } from "../commands.ts";
import type { LootTableJson } from "../loot.ts";
@@ -12,6 +24,7 @@ export type {
EventSignal,
Face,
FluidApi,
FluidStack,
FluidVariant,
Id,
ItemStack,
@@ -37,8 +50,8 @@ export interface ServerContext {
containers: ContainerApi;
items: ItemApi;
blocks: BlockApi;
// every fluid, named and colored by its rules, see docs/modding/resources/fluids.md
fluids: FluidApi;
// every fluid, named and colored by its rules, and fluid handlers, see docs/modding/resources/fluids.md
fluids: ServerFluidApi;
recipes: RecipeApi;
tags: TagApi;
loot: LootApi;
@@ -100,6 +113,10 @@ export interface BlockComponent<P = any> {
// an entity is standing on it, every tick the entity moves, like minecraft's updateEntityAfterFallOn: an item
// dropped into a basin going in. items lying still move every tick too
on_entity_stand?(block: BlockRef, params: P, entity: EntityRef): void;
// what pipes, buckets and other blocks put fluid in and take it out by, reached from face, or from no side in
// particular when it's undefined, like neoforge's Capabilities.FluidHandler.BLOCK. undefined when it has none
// there. see ServerFluidApi.block_handler
fluid_handler?(block: BlockRef, params: P, face: Face | undefined): FluidHandler | undefined;
// placing and neighbor rules are in shared scripts, see BlockRules in bworld/shared
}
@@ -563,6 +580,67 @@ export interface BlockApi {
solid(id: Id): boolean;
}
// fluid handlers, like neoforge's IFluidHandler: tanks others can fill and drain without knowing how a block or an
// item keeps what's in them. see docs/modding/resources/fluids.md
// whether fill and drain do it, or only say what they would, like neoforge's FluidAction
export type FluidAction = "execute" | "simulate";
export interface FluidHandler {
// how many tanks it has, like getTanks
tanks(): number;
// what's in a tank, undefined when it's empty, like getFluidInTank. not to be changed
fluid_in_tank(tank: number): FluidStack | undefined;
// how much a tank holds in mB, like getTankCapacity
tank_capacity(tank: number): number;
// whether a tank can hold the fluid at all, full or not, like isFluidValid
is_fluid_valid(tank: number, fluid: FluidVariant): boolean;
// puts in what of stack fits, returning how much that is, like fill
fill(stack: FluidStack, action: FluidAction): number;
// takes out up to stack's amount of its fluid, or up to a number of mB of whatever it gives first, returning what
// came out, undefined for nothing, like neoforge's two drains
drain(what: FluidStack | number, action: FluidAction): FluidStack | undefined;
}
// a fluid container's, see ServerFluidApi.item_handler. like IFluidHandlerItem
export interface FluidHandlerItem extends FluidHandler {
// one of the container, as filling and draining left it, like getContainer
readonly container: ItemStack;
}
// tanks kept by a script, see ServerFluidApi.tank_handler
export interface TankHandlerOptions {
// how many tanks, and how much each holds in mB
tanks: number;
capacity: number;
// what's in them, without empty ones, read every time the handler is used, so it's never out of date
get(): readonly FluidStack[];
// what's in them after an executed fill or drain changed it, without empty ones, to be kept for get
set(tanks: FluidStack[]): void;
}
// ctx.fluids in server scripts: FluidApi, and fluid handlers
export interface ServerFluidApi extends FluidApi {
// the handler of the block at x y z reached from face, from the first of its components with one, like
// level.getCapability(Capabilities.FluidHandler.BLOCK, pos, side). undefined for a block without one there
block_handler(x: number, y: number, z: number, face?: Face): FluidHandler | undefined;
// the handler of one of a stack of fluid containers, undefined for an item that isn't one, like
// Capabilities.FluidHandler.ITEM. it holds a single fluid, full or empty, so it only fills with its whole capacity
// and drains all of it, like neoforge's FluidBucketWrapper. it changes container, not stack
item_handler(stack: ItemStack): FluidHandlerItem | undefined;
// a handler for tanks a script keeps, like neoforge's FluidTank: each holds one fluid, and there's never two of the
// same fluid. a fluid goes into the tank that has it, or else an empty one, and a number of mB drains from the
// first one
tank_handler(options: TankHandlerOptions): FluidHandler;
// pours up to max mB from one handler into the other, what the second takes of what the first gives, returning what
// moved, undefined for nothing, like FluidUtil.tryFluidTransfer
transfer(from: FluidHandler, to: FluidHandler, max: number): FluidStack | undefined;
// the fluid container the player holds filled from handler, or else emptied into it, the held one becoming what
// it's turned into, like FluidUtil.interactWithFluidHandler. returns whether any fluid moved, false when they don't
// hold a fluid container
interact(player: Player, handler: FluidHandler): boolean;
}
export interface RecipeApi {
furnace_result(input: Id): { output: ItemStack; cook_time: number } | undefined;
fuel_value(item: Id): number; // burn time in ticks, 0 if it isn't fuel
+6
View File
@@ -24,6 +24,12 @@ export interface FluidVariant {
readonly data?: unknown;
}
// an amount of a fluid in mB, like neoforge's FluidStack: what's in a tank, or what's poured from one into another
export interface FluidStack {
fluid: FluidVariant;
amount: number;
}
// ctx.fluids in server and client scripts: every fluid any mod defines, the same on the server and every client
export interface FluidApi {
// every fluid's id, in load order, like ctx.items.ids. each without data, which a fluid like juice has many of