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
+27 -5
View File
@@ -6,6 +6,7 @@
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import { contained_fluid } from "$/common/inventory.ts";
import type {
FluidAction,
FluidHandler,
FluidHandlerItem,
FluidStack,
@@ -94,9 +95,30 @@ export function transfer(from: FluidHandler, to: FluidHandler, max: number): Flu
export function interact(player: Player, handler: FluidHandler): boolean {
const held = player.held_item;
const item = held && item_handler(held);
if (!item) return false;
const moved = transfer(handler, item, Infinity) ?? transfer(item, handler, Infinity);
if (moved) player.exchange_held(item.container);
return moved !== undefined;
const result = held && use_container(held, handler);
if (result) player.exchange_held(result);
return result !== undefined;
}
// one of a stack of fluid containers filled from handler, or else emptied into it unless it's only drained from, like
// a menu's output tank. with a tank, only that tank's fluid fills it. returns what the one became, undefined when it
// isn't a fluid container or no fluid moved
export function use_container(
stack: ItemStack,
handler: FluidHandler,
options: { tank?: number; drain_only?: boolean } = {},
): ItemStack | undefined {
const item = item_handler(stack);
if (!item) return undefined;
const { tank, drain_only } = options;
const from = tank === undefined ? handler : {
...handler,
drain(what: FluidStack | number, action: FluidAction) {
if (typeof what !== "number") return handler.drain(what, action);
const fluid = handler.fluid_in_tank(tank)?.fluid;
return fluid && handler.drain({ fluid, amount: what }, action);
},
};
const moved = transfer(from, item, Infinity) ?? (drain_only ? undefined : transfer(item, handler, Infinity));
return moved && item.container;
}
+18
View File
@@ -928,10 +928,12 @@ export class GameServer {
title: menu.title,
open_data: menu.open_data,
slots: menu.slot_info(),
fluid_slots: menu.fluid_slot_info(),
data,
});
player.sent.set("menu_data", JSON.stringify(data));
player.sent.delete("menu_content");
player.sent.delete("menu_tanks");
}
// closes it for the player too, not just on the server
@@ -1140,6 +1142,17 @@ export class GameServer {
case "click":
this.#click(player, message.menu, message.slot, message.button, message.shift === true);
break;
case "click_fluid": {
const menu = player.menu;
const slot = menu && message.menu === menu.id && is_int(message.slot)
? menu.fluid_slots[message.slot]
: undefined;
if (slot) {
slot.click();
this.world.dirty = true;
}
break;
}
case "drop_item":
this.#drop_item(player, message.menu, message.slot, message.all === true);
break;
@@ -1920,6 +1933,7 @@ export class GameServer {
player.menu = undefined;
for (const fn of menu?.on_close ?? []) fn();
player.sent.delete("menu_content");
player.sent.delete("menu_tanks");
player.sent.delete("menu_data");
// the crafting grid and whatever the cursor holds go back into the inventory
@@ -2218,6 +2232,10 @@ export class GameServer {
if (menu) {
const items = menu.content();
sync("menu_content", items, () => ({ type: "menu_content", id: menu.id, items }));
if (menu.fluid_slots.length > 0) {
const tanks = menu.tanks();
sync("menu_tanks", tanks, () => ({ type: "menu_tanks", id: menu.id, tanks }));
}
const data = menu.data_values();
sync("menu_data", data, () => ({ type: "menu_data", id: menu.id, data }));
}
+23
View File
@@ -16,7 +16,9 @@ import {
} from "$/common/inventory.ts";
import {
CRAFTING_RESULT_SLOT,
type MenuFluidSlotInfo,
type MenuSlotInfo,
type MenuTank,
PLAYER_MENU,
PLAYER_MENU_CRAFTING,
PLAYER_MENU_EXTRA,
@@ -44,6 +46,18 @@ export interface MenuSlot {
inventory?: number;
}
// a tank the menu shows, see MenuBuilder.add_fluid_slot
export interface MenuFluidSlot {
x: number;
y: number;
// a fluid container on the cursor only fills from it, it can't be emptied into it
output?: boolean;
// what's in it now
tank(): MenuTank;
// the player clicked it, with whatever their cursor holds
click(): void;
}
export class Menu {
readonly id: number;
readonly type: string;
@@ -51,6 +65,7 @@ export class Menu {
// sent once when it opens
open_data: unknown = null;
slots: MenuSlot[] = [];
fluid_slots: MenuFluidSlot[] = [];
// numbers the screen shows, read every tick and sent when they change, like DataSlot
data = new Map<string, () => number>();
// checked every tick, any false closes it, like stillValid
@@ -152,6 +167,14 @@ export class Menu {
}));
}
fluid_slot_info(): MenuFluidSlotInfo[] {
return this.fluid_slots.map(({ x, y, output }) => ({ x, y, ...(output ? { output: true } : {}) }));
}
tanks(): MenuTank[] {
return this.fluid_slots.map((slot) => slot.tank());
}
// what players are sent, with lore
content(): (ItemData | null)[] {
return this.slots.map((slot) => slot.container.get_item(slot.index)?.to_synced_data() ?? null);
+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>();