From 658a6453b7b7a68927d123b160bad52b3f83b1f8 Mon Sep 17 00:00:00 2001 From: Paula Date: Sat, 31 Oct 2026 17:35:27 -0300 Subject: [PATCH] Fluid slots and kegs ! --- assets/ASSETS.md | 33 +++++++++ assets/sprites/gui.png | Bin 2333 -> 2451 bytes client/gui/gui_atlas.ts | 2 + client/gui/gui_screen.ts | 50 ++++++++++--- client/gui/tooltip.ts | 14 ++++ client/menu.ts | 29 +++++++- client/packet_listener.ts | 16 ++++- common/mod_api/server.ts | 12 ++++ common/protocol.ts | 25 ++++++- docs/modding/inventories/menus.md | 43 +++++++++--- docs/modding/resources/fluids.md | 3 + mods/bworld/blocks/keg.json | 1 + mods/bworld/scripts/client.ts | 7 +- mods/bworld/scripts/server.ts | 51 ++++++++++++++ server/game/fluid_handlers.ts | 32 +++++++-- server/game/game_server.ts | 18 +++++ server/game/menu.ts | 23 ++++++ server/game/mod_runtime.ts | 56 ++++++++++++++- tests/keg_test.ts | 113 ++++++++++++++++++++++++++++++ 19 files changed, 494 insertions(+), 34 deletions(-) create mode 100644 tests/keg_test.ts diff --git a/assets/ASSETS.md b/assets/ASSETS.md index 019e39c..bc6243e 100644 --- a/assets/ASSETS.md +++ b/assets/ASSETS.md @@ -31,6 +31,39 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ``` +# Fluid slot + +The droplet in the `fluid_slot` sprite of `sprites/gui.png` is Modern Industrialization's, from its fluid slots, drawn +over the UI's slot by hand. + +https://github.com/AztechMC/Modern-Industrialization + +license: + +``` +MIT License + +Copyright (c) 2020 Azercoco & Technici4n + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +``` + # m6x11.ttf Font by Daniel Linssen (https://managore.itch.io) diff --git a/assets/sprites/gui.png b/assets/sprites/gui.png index 95cd44c7c9d18cd1740f92d579b6901585e6e225..67c9c849b9f24623d906f30a87d5c45eeb59c330 100644 GIT binary patch delta 2163 zcma)7dpMi-9)A;Q=}=NU+SFMDLpv2x;iw_5rL8h+SC_`6t4rcesms!r4$0~$#zEt{ zQEI8%QjItToR;3lar*|)9q~MJm>xIcl%tv-_P&;mF>${%hgji zAUHU9xH>p!d&EWs6T*W4AhERBbUy?nF8^c-015`Kj&^4fOL;TnVOpF6@~a`~O{NVo zjwhO5R<3^+#?UYL_^S4laH`$YP(68}?xx4qxdMBFyS4g98IOGPyH47ztJ*iAnCB7j z<26%;ec10U6H}fdz3=;kC9v_l9>Lc(MZ-To2;H2pnI&OQ7A2(lHMO}fTV1?KSQPsE&w?(x4X`@rR z>H^V9=jG~*N`w0><5Jw4WqGEsM_ktTWR=_Zd80eGj<(%3jwLeL6%AI(kivEixana& zZ8TySYxH;r-*&U(T$q*41<)+HZTa0TRuXn+rEemqD_nSRy9*?CEftCXzLii^5i`%5 z0bB8#x7}CGPh=|0pK=DSSdi$qGVvwgjrcBeF=jC#Le#LhweC-^r%*!1VK{%{-D6h+ z;sYM_tDS#w0HRo|sisoR8N>@tTVQzz%!7g`jQ_GKOvG=~)hV~U!gEs|nOvoD z0xRvOn!0R4Ht`w@L;W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client/gui/gui_atlas.ts @@ -31,6 +31,8 @@ export const GUI_ATLAS = { air_empty: { x: 100, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] }, air_full: { x: 110, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] }, air_half: { x: 120, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] }, + // the slot with modern industrialization's fluid slot droplet, drawn over it by hand + fluid_slot: { x: 160, y: 0, width: 18, height: 18, border: [0, 0, 0, 0] }, } as const; export type GuiSprite = keyof typeof GUI_ATLAS; diff --git a/client/gui/gui_screen.ts b/client/gui/gui_screen.ts index 6813912..10c9024 100644 --- a/client/gui/gui_screen.ts +++ b/client/gui/gui_screen.ts @@ -11,11 +11,11 @@ import { ClientInventories } from "../inventory.ts"; import { canvas, draw_rect, draw_texture_region, Texture } from "../renderer/mod.ts"; import { AssetManager } from "../assets.ts"; import { get_sprite_region } from "../sprites.ts"; -import { draw_item } from "../rendering/render_utils.ts"; -import { draw_tooltip, item_tooltip } from "./tooltip.ts"; +import { draw_fluid, draw_item } from "../rendering/render_utils.ts"; +import { draw_tooltip, item_tooltip, tank_tooltip } from "./tooltip.ts"; import { ModGraphics } from "./mod_graphics.ts"; import { inventory_overlays } from "../mods.ts"; -import type { ClientMenu, ClientSlot } from "../menu.ts"; +import type { ClientFluidSlot, ClientMenu, ClientSlot } from "../menu.ts"; // how far past the slot's edge its highlight goes const HIGHLIGHT_OVERHANG = 3 * GUI_SCALE; @@ -42,8 +42,8 @@ export abstract class GuiScreen { } } -// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots, -// the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed +// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots and +// fluid slots, the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed // locally so they feel instant, then whatever the server syncs back wins. subclasses draw the rest export abstract class GuiMenuScreen extends GuiScreen { inventories: ClientInventories; @@ -54,6 +54,7 @@ export abstract class GuiMenuScreen extends GuiScreen { height: number; hovering: ClientSlot | undefined; + hovering_fluid: ClientFluidSlot | undefined; // what the screen and mods draw with, in gui pixels graphics = new ModGraphics(); @@ -109,6 +110,7 @@ export abstract class GuiMenuScreen extends GuiScreen { if (this.inventory_overlays) this.#render_overlays(); if (g.mouse_blocked()) { this.hovering = undefined; + this.hovering_fluid = undefined; } else { this.handle_interaction(); } @@ -122,15 +124,17 @@ export abstract class GuiMenuScreen extends GuiScreen { if (item) { draw_tooltip(item_tooltip(item), mouse.x, mouse.y); } + } else if (this.hovering_fluid) { + draw_tooltip(tank_tooltip(this.hovering_fluid.tank, this.hovering_fluid.output === true), mouse.x, mouse.y); } g.end(); } - #slot_x(slot: ClientSlot) { + #slot_x(slot: { x: number }) { return this.x + slot.x * GUI_SCALE; } - #slot_y(slot: ClientSlot) { + #slot_y(slot: { y: number }) { return this.y + slot.y * GUI_SCALE; } @@ -138,15 +142,24 @@ export abstract class GuiMenuScreen extends GuiScreen { for (const slot of this.menu.slots) { draw_gui_sprite("slot", this.#slot_x(slot), this.#slot_y(slot)); } + // like modern industrialization's, an empty one shows a droplet + for (const slot of this.menu.fluid_slots) { + draw_gui_sprite(slot.tank.fluid ? "slot" : "fluid_slot", this.#slot_x(slot), this.#slot_y(slot)); + } // like minecraft's, the highlight is a bit bigger than the slot, part of it under the item and part over const highlight = (sprite: "slot_highlight_back" | "slot_highlight_front") => { - if (this.hovering) { - const x = this.#slot_x(this.hovering) - HIGHLIGHT_OVERHANG; - draw_gui_sprite(sprite, x, this.#slot_y(this.hovering) - HIGHLIGHT_OVERHANG); + const hovered = this.hovering ?? this.hovering_fluid; + if (hovered) { + const x = this.#slot_x(hovered) - HIGHLIGHT_OVERHANG; + draw_gui_sprite(sprite, x, this.#slot_y(hovered) - HIGHLIGHT_OVERHANG); } }; highlight("slot_highlight_back"); + // the whole slot in its fluid while there's any, whatever the amount, like modern industrialization's + for (const slot of this.menu.fluid_slots) { + if (slot.tank.fluid) draw_fluid(slot.tank.fluid, this.#slot_x(slot), this.#slot_y(slot)); + } for (const slot of this.menu.slots) { const item = slot.container.get_item(slot.index); if (item) { @@ -218,8 +231,25 @@ export abstract class GuiMenuScreen extends GuiScreen { handle_interaction() { this.hovering = undefined; + this.hovering_fluid = undefined; const mouse = InputManager.get_mouse_position(); + for (const [index, slot] of this.menu.fluid_slots.entries()) { + const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)]; + if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) { + continue; + } + this.hovering_fluid = slot; + for (const button of [0, 2]) { + if (InputManager.is_mouse_pressed(button)) { + InputManager.consume_mouse(button); + // what the cursor's container turns into depends on the tank, the server says + this.send({ type: "click_fluid", menu: this.menu.id, slot: index }); + } + } + return; + } + for (const [index, slot] of this.menu.slots.entries()) { const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)]; if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) { diff --git a/client/gui/tooltip.ts b/client/gui/tooltip.ts index 5101ad4..1d6c9f5 100644 --- a/client/gui/tooltip.ts +++ b/client/gui/tooltip.ts @@ -9,6 +9,7 @@ import { display_name } from "$/common/utils.ts"; import { fluid_rules } from "$/client/fluid_rules.ts"; import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts"; import { AssetManager } from "$/client/assets.ts"; +import type { MenuTank } from "$/common/protocol.ts"; import { get_sprite_region } from "$/client/sprites.ts"; import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts"; @@ -62,6 +63,19 @@ export function item_tooltip(item: ItemStack): TooltipLine[] { return lines; } +// a menu's fluid slot, like modern industrialization's: the fluid, how much of the tank it fills, and what clicking it +// with a fluid container does +export function tank_tooltip(tank: MenuTank, output: boolean): TooltipLine[] { + return [ + tank.fluid ? { text: fluid_rules.name(tank.fluid), color: NAME_COLOR } : { text: "Empty", color: LORE_COLOR }, + { text: `${tank.amount} / ${tank.capacity} mB`, color: FLUID_COLOR }, + { + text: output ? "Fluid Output, Click to Extract" : "Fluid Input, Click to Insert or Extract", + color: LORE_COLOR, + }, + ]; +} + // below and right of the mouse, moved back inside the window when it would go past its edge export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: number) { const line_height = TEXT_HEIGHT * TEXT_SCALE; diff --git a/client/menu.ts b/client/menu.ts index 9b70c7f..fbe45f3 100644 --- a/client/menu.ts +++ b/client/menu.ts @@ -4,7 +4,14 @@ // the client's copy of a menu, like neoforge's client menu: built from what the server sent when it opened, kept up // to date with what it syncs. clicks go to the server, which decides what they do import { Container } from "$/common/inventory.ts"; -import { CRAFTING_RESULT_SLOT, type MenuSlotInfo, PLAYER_MENU, PLAYER_MENU_EXTRA } from "$/common/protocol.ts"; +import { + CRAFTING_RESULT_SLOT, + type MenuFluidSlotInfo, + type MenuSlotInfo, + type MenuTank, + PLAYER_MENU, + PLAYER_MENU_EXTRA, +} from "$/common/protocol.ts"; import type { ClientInventories } from "./inventory.ts"; import type { ItemStack as ApiItemStack } from "$/common/mod_api/client.ts"; @@ -23,6 +30,12 @@ export interface ClientSlot { max_count?: number; } +// a tank the server's menu shows, which clicks with a fluid container on the cursor fill it from or empty it into +export interface ClientFluidSlot extends MenuFluidSlotInfo { + // what the server last said is in it + tank: MenuTank; +} + export class ClientMenu { readonly id: number; readonly type: string; @@ -30,6 +43,7 @@ export class ClientMenu { readonly open_data: unknown; data: Record; slots: ClientSlot[]; + fluid_slots: ClientFluidSlot[] = []; constructor( id: number, @@ -55,9 +69,16 @@ export class ClientMenu { } // one the server opened: its slots' items come in menu_content, into a container of its own - static opened(id: number, type: string, title: string, open_data: unknown, slots: MenuSlotInfo[]) { + static opened( + id: number, + type: string, + title: string, + open_data: unknown, + slots: MenuSlotInfo[], + fluid_slots: MenuFluidSlotInfo[] = [], + ) { const items = new Container(slots.length); - return new ClientMenu( + const menu = new ClientMenu( id, type, title, @@ -72,6 +93,8 @@ export class ClientMenu { max_count: slot.max_count, })), ); + menu.fluid_slots = fluid_slots.map((slot) => ({ ...slot, tank: { amount: 0, capacity: 0 } })); + return menu; } } diff --git a/client/packet_listener.ts b/client/packet_listener.ts index 7ca330e..52f8570 100644 --- a/client/packet_listener.ts +++ b/client/packet_listener.ts @@ -246,6 +246,13 @@ export class ClientPacketListener { menu?.slots[0]?.container.load(message.items); break; } + case "menu_tanks": { + const menu = this.#menu(message.id); + for (const [i, tank] of message.tanks.entries()) { + if (menu?.fluid_slots[i]) menu.fluid_slots[i].tank = tank; + } + break; + } case "menu_data": { const menu = this.#menu(message.id); if (menu) menu.data = message.data; @@ -291,7 +298,14 @@ export class ClientPacketListener { close(); return; } - const menu = ClientMenu.opened(message.id, message.menu_type, message.title, message.open_data, message.slots); + const menu = ClientMenu.opened( + message.id, + message.menu_type, + message.title, + message.open_data, + message.slots, + message.fluid_slots, + ); menu.data = message.data; let screen: GuiModMenuScreen; try { diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index fefcc15..d65e728 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -744,6 +744,14 @@ export interface SlotOptions { max_count?: number; } +export interface FluidSlotOptions { + // gui pixels from the screen's top left to the slot's 18 pixel frame + x: number; + y: number; + // fluid containers only fill from it, like a machine's output + output_only?: boolean; +} + // what a menu type's build function gets, like neoforge's server menu constructor export interface MenuBuilder { title: string; @@ -751,6 +759,10 @@ export interface MenuBuilder { open_data: unknown; // a slot of a container from ctx.containers, or of player.inventory. returns its menu slot index add_slot(container: Container, slot: number, options: SlotOptions): number; + // a tank of handler, like modern industrialization's fluid slots: the screen draws its fluid in an 18 pixel frame, + // with the amount in its tooltip, and clicking it with a fluid container on the cursor fills that from the tank, or + // else empties it in through the handler. returns its index among the menu's fluid slots + add_fluid_slot(handler: FluidHandler, tank: number, options: FluidSlotOptions): number; // the player's 36 slots, like addStandardInventorySlots: the rest of the inventory in 3 rows from x and y, the // hotbar 58 pixels under them. added in inventory order, so inventory slot i is the returned index plus i add_player_inventory(x: number, y: number): number; diff --git a/common/protocol.ts b/common/protocol.ts index c9a3dcc..0b65d1a 100644 --- a/common/protocol.ts +++ b/common/protocol.ts @@ -4,13 +4,14 @@ // messages sent between the client and the server, as json over a websocket. chunks are binary, see level_chunk import type { Faces } from "./constants.ts"; import type { ItemData } from "./inventory.ts"; +import type { FluidVariant } from "./mod_api/shared.ts"; import type { ModListing } from "./mod_loader.ts"; export const AIR_ID = "bworld:air"; // bump when a client and server of different versions can't play together. // the server rejects a different version before the client downloads anything -export const PROTOCOL_VERSION = 24; +export const PROTOCOL_VERSION = 25; export interface PlayerInfo { id: string; @@ -84,6 +85,21 @@ export interface MenuSlotInfo { max_count?: number; } +// a tank shown in a menu, like modern industrialization's fluid slots: an 18 pixel frame at x y, which a fluid +// container on the cursor fills from, or empties into unless it's an output +export interface MenuFluidSlotInfo { + x: number; + y: number; + output?: boolean; +} + +// what's in a menu's tank, no fluid when it's empty +export interface MenuTank { + fluid?: FluidVariant; + amount: number; + capacity: number; +} + // clients send what the player is trying to do, the server decides what happens export type ClientMessage = | { type: "hello"; name: string; protocol: number } @@ -138,6 +154,8 @@ export type ClientMessage = // a slot of the open menu, or of menu 0 when none is. shift moves the stack to the other part of the menu instead, // like minecraft's quick move. clicks for a menu that isn't open any more are ignored | { type: "click"; menu: number; slot: number; button: number; shift?: boolean } + // a fluid slot of the open menu, by its index among the fluid slots, with whatever the cursor holds + | { type: "click_fluid"; menu: number; slot: number } // q on a slot (the held one when no screen is open, in menu 0) throws one item, or the whole stack with ctrl | { type: "drop_item"; menu: number; slot: number; all: boolean } // a button of the open menu, like minecraft's clickMenuButton. the menu decides what the number means @@ -248,7 +266,7 @@ export type ServerMessage = | { type: "container"; container: ContainerKey; items: (ItemData | null)[] } | { type: "cursor"; item: ItemData | null } // a menu opened, replacing any other. id counts 1 to 100 and around, type is the menu type whose screen draws it. - // its slots' items come in menu_content, its data's changes in menu_data + // its slots' items come in menu_content, its tanks in menu_tanks, its data's changes in menu_data | { type: "open_menu"; id: number; @@ -256,10 +274,13 @@ export type ServerMessage = title: string; open_data: unknown; slots: MenuSlotInfo[]; + fluid_slots: MenuFluidSlotInfo[]; data: Record; } // what's in every slot of the open menu, by menu slot index, when any of it changed | { type: "menu_content"; id: number; items: (ItemData | null)[] } + // what's in every fluid slot's tank of the open menu, when any of it changed + | { type: "menu_tanks"; id: number; tanks: MenuTank[] } // all the menu's data, when any of it changed | { type: "menu_data"; id: number; data: Record } // the server closed it (the block broke, the player walked away), it already put everything back diff --git a/docs/modding/inventories/menus.md b/docs/modding/inventories/menus.md index c470525..6338e9a 100644 --- a/docs/modding/inventories/menus.md +++ b/docs/modding/inventories/menus.md @@ -71,6 +71,31 @@ inventory slot `i` is the returned index plus `i`. editing their client. `on_take` is the place to use up a recipe's inputs, so taking the result and spending the inputs happen in the same step, like the smithing table does. +### Fluid Slots + +`add_fluid_slot(handler, tank, options)` shows a tank of a [fluid handler](../resources/fluids.md#fluid-handlers), like +Modern Industrialization's fluid slots. It returns the slot's index among the menu's fluid slots, which are numbered +apart from its item slots. + +```ts +menu.add_fluid_slot(input_tank, 0, { x: 55, y: 34 }); +menu.add_fluid_slot(output_tank, 0, { x: 115, y: 34, output_only: true }); +``` + +| Option | Meaning | +| ------------- | ------------------------------------------------------------------------ | +| `x`, `y` | Where its 18 pixel frame goes, in GUI pixels from the screen's top left. | +| `output_only` | Fluid containers only fill from it, they can't be emptied into it. | + +The engine draws it, so the screen needs no code for it: a droplet while the tank is empty, and the whole slot in its +fluid while there's any. Its tooltip says what the fluid is, how much of the tank it fills, and what clicking it does. + +Clicking it with a fluid container on the cursor fills the container from the tank, or else empties it into the handler, +all on the server. It goes through the handler's `fill` and `drain`, so a handler with more than one tank fills +whichever its own rules say. Give each slot a handler of its own tank when that matters, like the keg's input and +output. One container of a stack on the cursor is used, and what it turns into goes into the inventory while there are +more. + ### Shift Clicking Shift clicking a slot moves its stack to the other part of the menu, like NeoForge's `quickMoveStack`: @@ -216,13 +241,15 @@ for each player like Minecraft's. Menu 0 is the player's own inventory (slots 0 grid (36 to 45), always there and never opened. The slots mods added to it come after, from 46: `join` lists their positions, and the `container` message with `"extra"` syncs what's in them, like the inventory's. -| Message | Way | What | -| -------------------- | ------ | ------------------------------------------------------------------------------- | -| `open_menu` | server | The id, type, title, open data, each slot's position, and the data. | -| `menu_content` | server | Every slot's item, by menu slot index, when any changed. | -| `menu_data` | server | All the data, when any changed. | -| `close_menu` | both | The server closed it, or the player did. 0 is their inventory screen. | -| `click`, `drop_item` | client | A menu slot index of the open menu (or menu 0). Ones for other ids are ignored. | -| `menu_button` | client | A button of the open menu. | +| Message | Way | What | +| -------------------- | ------ | -------------------------------------------------------------------------------- | +| `open_menu` | server | The id, type, title, open data, each slot's and fluid slot's position, the data. | +| `menu_content` | server | Every slot's item, by menu slot index, when any changed. | +| `menu_tanks` | server | Every fluid slot's fluid, amount and capacity, when any changed. | +| `menu_data` | server | All the data, when any changed. | +| `close_menu` | both | The server closed it, or the player did. 0 is their inventory screen. | +| `click`, `drop_item` | client | A menu slot index of the open menu (or menu 0). Ones for other ids are ignored. | +| `click_fluid` | client | A fluid slot of the open menu, by its index among the fluid slots. | +| `menu_button` | client | A button of the open menu. | The code is in `server/game/menu.ts` and `client/menu.ts`. diff --git a/docs/modding/resources/fluids.md b/docs/modding/resources/fluids.md index b0efbec..68fcde7 100644 --- a/docs/modding/resources/fluids.md +++ b/docs/modding/resources/fluids.md @@ -229,3 +229,6 @@ on_interact(block, _params, player) { return tanks !== undefined && ctx.fluids.interact(player, tanks); }, ``` + +A [menu](../inventories/menus.md#fluid-slots) shows a handler's tanks with `add_fluid_slot`, which players fill and +empty fluid containers on from the cursor. diff --git a/mods/bworld/blocks/keg.json b/mods/bworld/blocks/keg.json index b6b1526..ed37b44 100644 --- a/mods/bworld/blocks/keg.json +++ b/mods/bworld/blocks/keg.json @@ -25,6 +25,7 @@ "drops": "bworld:keg", "interactive": true, "components": { + "bworld:keg": {} } } } diff --git a/mods/bworld/scripts/client.ts b/mods/bworld/scripts/client.ts index 61e6f69..f63be27 100644 --- a/mods/bworld/scripts/client.ts +++ b/mods/bworld/scripts/client.ts @@ -29,7 +29,8 @@ export function setup(ctx: ClientContext) { ctx.ui.register_menu_screen("bworld:storage", chest_screen); ctx.ui.register_menu_screen("bworld:furnace", furnace_screen); ctx.ui.register_menu_screen("bworld:crabpot", (menu) => container_screen(menu, HEIGHT)); - ctx.ui.register_menu_screen("bworld:worm_bin", worm_bin_screen); + ctx.ui.register_menu_screen("bworld:worm_bin", input_output_screen); + ctx.ui.register_menu_screen("bworld:keg", input_output_screen); ctx.ui.register_menu_screen("bworld:smithing_table", smithing_screen); ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu)); ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen); @@ -130,8 +131,8 @@ function furnace_screen(menu: ClientMenu): MenuScreen { }); } -// its input on the left, an arrow to what it makes on the right -function worm_bin_screen(menu: ClientMenu): MenuScreen { +// its input on the left, an arrow to what it makes on the right, like the worm bin's slots and the keg's tanks +function input_output_screen(menu: ClientMenu): MenuScreen { return container_screen(menu, HEIGHT, (g) => { g.texture("bworld:arrow_empty", 85, 34, 18, 18); }); diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 802a6ca..a49c0ea 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -27,6 +27,7 @@ export function setup(ctx: ServerContext) { register_trapdoor(ctx); register_buckets(ctx); register_juicer(ctx); + register_keg(ctx); register_curios(ctx); register_effects(ctx); register_growable_block(ctx); @@ -925,6 +926,56 @@ function register_juicer(ctx: ServerContext) { }); } +// the keg holds an input tank and an output tank of KEG_CAPACITY mB each, in its tile data. its screen shows both, and +// its fluid handler, from every side, only fills the input and only drains the output +const KEG_CAPACITY = 4000; +interface KegData { + input?: FluidStack; + output?: FluidStack; +} + +function register_keg(ctx: ServerContext) { + const tank = (block: BlockRef, which: keyof KegData) => + ctx.fluids.tank_handler({ + tanks: 1, + capacity: KEG_CAPACITY, + get: () => { + const fluid = (block.data as KegData | undefined)?.[which]; + return fluid ? [fluid] : []; + }, + set: ([fluid]) => { + block.data = { ...block.data, [which]: fluid } satisfies KegData; + }, + }); + + const menu_type = ctx.menus.register("bworld:keg", (menu, block) => { + menu.title = "Keg"; + menu.add_fluid_slot(tank(block, "input"), 0, { x: 55, y: 34 }); + menu.add_fluid_slot(tank(block, "output"), 0, { x: 115, y: 34, output_only: true }); + menu.add_player_inventory(7, INVENTORY_Y); + menu.still_valid_near(block); + }); + + ctx.components.register_block("bworld:keg", { + on_interact(block, _params, player) { + menu_type.open(player, block); + return true; + }, + fluid_handler(block) { + const input = tank(block, "input"); + const output = tank(block, "output"); + return { + tanks: () => 2, + fluid_in_tank: (index) => (index === 0 ? input : output).fluid_in_tank(0), + tank_capacity: () => KEG_CAPACITY, + is_fluid_valid: (index) => index === 0, + fill: (stack, action) => input.fill(stack, action), + drain: (what, action) => output.drain(what, action), + }; + }, + }); +} + interface GrowableBlockParams { // what grows on top of it block_id: string; diff --git a/server/game/fluid_handlers.ts b/server/game/fluid_handlers.ts index d404896..3273a5d 100644 --- a/server/game/fluid_handlers.ts +++ b/server/game/fluid_handlers.ts @@ -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; } diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 6db136c..1bd5725 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -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 })); } diff --git a/server/game/menu.ts b/server/game/menu.ts index 3b25621..894045d 100644 --- a/server/game/menu.ts +++ b/server/game/menu.ts @@ -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 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); diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index fbf197e..fc1967b 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -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(); diff --git a/tests/keg_test.ts b/tests/keg_test.ts new file mode 100644 index 0000000..f59cdfa --- /dev/null +++ b/tests/keg_test.ts @@ -0,0 +1,113 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// the keg's input and output tanks, and menus' fluid slots, which its screen shows them in +import { assertEquals } from "@std/assert"; +import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; +import { ItemStack } from "$/common/inventory.ts"; +import { opened_menu, test_game } from "./helpers.ts"; + +const JUICE = { id: "bworld:juice" }; +const BUCKET = "bworld:bucket"; +const INPUT = 0; +const OUTPUT = 1; + +// a keg at 0 100 0 next to alice +async function setup() { + const t = await test_game("mods"); + t.join(1, "alice"); + t.game.set_block(0, 99, 0, "bworld:stone"); + t.game.set_block(0, 100, 0, "bworld:keg"); + t.send(1, { type: "move", x: 0.5, y: 100, z: 2.5, yaw: 0, pitch: 0 }); + const player = t.game.players()[0]; + const bucket = EverythingRegistry.get("items", BUCKET)!.fluid_container!.capacity; + const keg = () => (t.game.world.get_tile(0, 100, 0)?.mod_data ?? {}) as { input?: unknown; output?: unknown }; + const open = () => { + t.take(1); + t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] }); + return opened_menu(t.take(1)); + }; + // count buckets on the cursor, full of fluid or empty + const hold = (count: number, fluid?: unknown) => { + const stack = new ItemStack(BUCKET, count); + if (fluid) stack.data = { fluid }; + player.cursor.item = stack; + }; + const cursor = () => player.cursor.item?.to_data(); + const click = (menu: { id: number }, slot: number) => t.send(1, { type: "click_fluid", menu: menu.id, slot }); + const buckets = () => player.inventory.to_data().filter((item) => item?.id === BUCKET); + return { t, player, bucket, keg, open, hold, cursor, click, buckets }; +} + +Deno.test("right clicking a keg opens its input and output tanks, an arrow's width apart", async () => { + const { t } = await setup(); + t.take(1); + t.send(1, { type: "use_block", x: 0, y: 100, z: 0, face: "south", hit: [0.5, 0.5, 1] }); + const messages = t.take(1); + const menu = opened_menu(messages); + assertEquals([menu.menu_type, menu.title], ["bworld:keg", "Keg"]); + assertEquals(menu.slots.length, 36); + assertEquals(menu.fluid_slots, [{ x: 55, y: 34 }, { x: 115, y: 34, output: true }]); + // and what's in them, both empty + const tanks = messages.findLast((m) => m.type === "menu_tanks"); + assertEquals(tanks.id, menu.id); + assertEquals(tanks.tanks.map((tank: { fluid?: unknown; amount: number }) => [tank.fluid, tank.amount]), [ + [undefined, 0], + [undefined, 0], + ]); +}); + +Deno.test("a full container on the cursor empties into the input, and an empty one fills from either tank", async () => { + const { t, bucket, keg, open, hold, cursor, click, buckets } = await setup(); + const menu = open(); + + // one of two empties, and goes into the inventory, the other stays on the cursor + hold(2, JUICE); + click(menu, INPUT); + assertEquals(keg().input, { fluid: JUICE, amount: bucket }); + assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); + assertEquals(buckets(), [{ id: BUCKET, count: 1 }]); + // the server says what's in it + t.game.tick(); + const tanks = t.take(1).findLast((m) => m.type === "menu_tanks").tanks; + assertEquals([tanks[INPUT].fluid, tanks[INPUT].amount], [JUICE, bucket]); + + // nothing goes into the output + click(menu, OUTPUT); + assertEquals(keg().output, undefined); + assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); + + // an empty one takes from the output, or back out of the input + t.game.world.get_tile(0, 100, 0)!.mod_data = { output: { fluid: JUICE, amount: bucket + 1 } }; + hold(1); + click(menu, OUTPUT); + assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); + assertEquals(keg().output, { fluid: JUICE, amount: 1 }); + hold(1); + click(menu, OUTPUT); + assertEquals(cursor(), { id: BUCKET, count: 1 }); + + t.game.world.get_tile(0, 100, 0)!.mod_data = { input: { fluid: JUICE, amount: bucket } }; + click(menu, INPUT); + assertEquals(cursor(), { id: BUCKET, count: 1, data: { fluid: JUICE } }); + assertEquals(keg().input, undefined); + + // clicks for a menu that isn't open, or a fluid slot it doesn't have, do nothing + hold(1, JUICE); + t.send(1, { type: "click_fluid", menu: menu.id + 1, slot: INPUT }); + t.send(1, { type: "click_fluid", menu: menu.id, slot: 5 }); + assertEquals(keg().input, undefined); +}); + +Deno.test("the keg's fluid handler only fills its input and only drains its output", async () => { + const { t, keg } = await setup(); + const handler = () => t.game.mods.block_fluid_handler(0, 100, 0, "top")!; + assertEquals(handler().fill({ fluid: JUICE, amount: 100 }, "execute"), 100); + assertEquals(handler().drain(100, "execute"), undefined); + assertEquals(keg().input, { fluid: JUICE, amount: 100 }); + + t.game.world.get_tile(0, 100, 0)!.mod_data = { output: { fluid: JUICE, amount: 100 } }; + assertEquals(handler().fluid_in_tank(1), { fluid: JUICE, amount: 100 }); + assertEquals(handler().drain(40, "execute"), { fluid: JUICE, amount: 40 }); + assertEquals(keg().output, { fluid: JUICE, amount: 60 }); +});