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
+31 -1
View File
@@ -25,6 +25,7 @@ import { physics } from "$/common/physics.ts";
import { clock } from "$/common/time.ts";
import { BlockRules } from "$/common/block_rules.ts";
import { FluidRules } from "$/common/fluids.ts";
import { interact, item_handler, tank_handler, transfer } from "./fluid_handlers.ts";
import type { SharedContext } from "$/common/mod_api/shared.ts";
import { tag_api } from "$/common/tags.ts";
import { loading_mod } from "$/common/hooks.ts";
@@ -38,6 +39,8 @@ import type {
EffectComponent,
EntityRef,
EventSignal,
Face,
FluidHandler,
ItemComponent,
ItemStack,
LivingRef,
@@ -532,6 +535,26 @@ export class ModRuntime {
}
#components_with = new Map<keyof BlockComponent, Map<string, BlockComponentOf[]>>();
// the fluid handler of the block at x y z reached from face, from the first of its components with one
block_fluid_handler(x: number, y: number, z: number, face: Face | undefined): FluidHandler | undefined {
if (face !== undefined && !faces.includes(face)) {
throw new Error(`a face is one of ${faces.join(", ")}, not ${face}`);
}
const id = this.game.world.get_block_id(x, y, z);
const components = this.components_with(id, "fluid_handler");
if (components.length === 0) return undefined;
const block = this.block_ref(x, y, z, id);
for (const { mod, id: component_id, component, params } of components) {
const handler = run_guarded(
mod,
`${component_id} fluid_handler`,
() => component.fluid_handler!(block, params, face),
);
if (handler) return handler;
}
return undefined;
}
// the block at x y z was told the one at from changed, and its rules have had their say: its components'
// on_neighbor_change
#neighbor_changed(x: number, y: number, z: number, from: Position) {
@@ -974,7 +997,14 @@ export class ModRuntime {
return is_solid(block);
},
},
fluids: this.fluid_rules.api(),
fluids: {
...this.fluid_rules.api(),
block_handler: (x, y, z, face) => this.block_fluid_handler(x, y, z, face),
item_handler,
tank_handler,
transfer,
interact,
},
recipes: {
furnace_result: (input) => {
const recipe = game().recipes.furnace.get(input);