Fluid slots and kegs !

This commit is contained in:
2026-10-31 17:35:27 -03:00
parent 65934d34a1
commit 658a6453b7
19 changed files with 494 additions and 34 deletions
+54 -2
View File
@@ -25,7 +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 { interact, item_handler, tank_handler, transfer, use_container } 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";
@@ -41,6 +41,7 @@ import type {
EventSignal,
Face,
FluidHandler,
FluidSlotOptions,
ItemComponent,
ItemStack,
LivingRef,
@@ -78,7 +79,7 @@ import { AIR_ID } from "$/common/protocol.ts";
import type { GameServer } from "./game_server.ts";
import { MAX_PRIORITY, MIN_PRIORITY } from "./scheduled_ticks.ts";
import type { ServerPlayer } from "./player.ts";
import { Menu, type MenuSlot } from "./menu.ts";
import { Menu, type MenuFluidSlot, type MenuSlot } from "./menu.ts";
import { damage_source, type Entity } from "./entity/entity.ts";
import type { LivingEntity } from "./entity/living_entity.ts";
import type { EffectJson } from "$/common/mod_data.ts";
@@ -1156,6 +1157,10 @@ export class ModRuntime {
menu.slots.push(menu_slot(game, mod, server_player, container, slot, options));
return menu.slots.length - 1;
},
add_fluid_slot(handler, tank, options) {
menu.fluid_slots.push(menu_fluid_slot(game, mod, server_player, handler, tank, options));
return menu.fluid_slots.length - 1;
},
add_player_inventory(x, y) {
const first = menu.slots.length;
for (let i = 0; i < server_player.inventory.size; i++) {
@@ -1365,6 +1370,53 @@ function menu_slot(
return added;
}
// a menu's fluid slot from what a mod's add_fluid_slot got
function menu_fluid_slot(
game: GameServer,
mod: string,
player: ServerPlayer,
handler: FluidHandler,
tank: number,
options: FluidSlotOptions,
): MenuFluidSlot {
if (typeof handler?.fluid_in_tank !== "function") throw new Error("add_fluid_slot needs a fluid handler");
const tanks = handler.tanks();
if (!Number.isInteger(tank) || tank < 0 || tank >= tanks) {
throw new Error(`tank ${tank} isn't in the handler, it has ${tanks}`);
}
if (![options?.x, options?.y].every((n) => typeof n === "number" && Number.isFinite(n))) {
throw new Error(`fluid slot ${tank} needs an x and y`);
}
const output = options.output_only === true;
return {
x: options.x,
y: options.y,
output,
tank: () =>
run_guarded(mod, "a fluid slot's handler", () => {
const fluid = handler.fluid_in_tank(tank);
const capacity = handler.tank_capacity(tank);
return fluid
? { fluid: structuredClone(fluid.fluid), amount: fluid.amount, capacity }
: { amount: 0, capacity };
}) ?? { amount: 0, capacity: 0 },
click() {
const cursor = player.cursor.item;
if (!cursor) return;
const result = run_guarded(
mod,
"a fluid slot's handler",
() => use_container(item_copy(cursor), handler, { tank, drain_only: output }),
);
if (!result) return;
// the one used becomes what it was turned into, which goes in the inventory while there are more of it
cursor.amount -= 1;
if (cursor.amount <= 0) player.cursor.item = new_stack(result);
else game.give_stack(player, new_stack(result));
},
};
}
// the engine container behind each one mods get, for menus' slots
const engine_containers = new WeakMap<Container, EngineContainer>();