Fluid slots and kegs !
This commit is contained in:
@@ -31,6 +31,39 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
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
|
# m6x11.ttf
|
||||||
|
|
||||||
Font by Daniel Linssen (https://managore.itch.io)
|
Font by Daniel Linssen (https://managore.itch.io)
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 2.3 KiB After Width: | Height: | Size: 2.4 KiB |
@@ -31,6 +31,8 @@ export const GUI_ATLAS = {
|
|||||||
air_empty: { x: 100, y: 213, width: 9, height: 9, border: [0, 0, 0, 0] },
|
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_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] },
|
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;
|
} as const;
|
||||||
|
|
||||||
export type GuiSprite = keyof typeof GUI_ATLAS;
|
export type GuiSprite = keyof typeof GUI_ATLAS;
|
||||||
|
|||||||
+40
-10
@@ -11,11 +11,11 @@ import { ClientInventories } from "../inventory.ts";
|
|||||||
import { canvas, draw_rect, draw_texture_region, Texture } from "../renderer/mod.ts";
|
import { canvas, draw_rect, draw_texture_region, Texture } from "../renderer/mod.ts";
|
||||||
import { AssetManager } from "../assets.ts";
|
import { AssetManager } from "../assets.ts";
|
||||||
import { get_sprite_region } from "../sprites.ts";
|
import { get_sprite_region } from "../sprites.ts";
|
||||||
import { draw_item } from "../rendering/render_utils.ts";
|
import { draw_fluid, draw_item } from "../rendering/render_utils.ts";
|
||||||
import { draw_tooltip, item_tooltip } from "./tooltip.ts";
|
import { draw_tooltip, item_tooltip, tank_tooltip } from "./tooltip.ts";
|
||||||
import { ModGraphics } from "./mod_graphics.ts";
|
import { ModGraphics } from "./mod_graphics.ts";
|
||||||
import { inventory_overlays } from "../mods.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
|
// how far past the slot's edge its highlight goes
|
||||||
const HIGHLIGHT_OVERHANG = 3 * GUI_SCALE;
|
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,
|
// a menu's screen, like minecraft's AbstractContainerScreen: in the middle of the window, it draws the menu's slots and
|
||||||
// the item on the cursor and tooltips, and sends clicks. the server owns what's in the slots: clicks are guessed
|
// 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
|
// locally so they feel instant, then whatever the server syncs back wins. subclasses draw the rest
|
||||||
export abstract class GuiMenuScreen extends GuiScreen {
|
export abstract class GuiMenuScreen extends GuiScreen {
|
||||||
inventories: ClientInventories;
|
inventories: ClientInventories;
|
||||||
@@ -54,6 +54,7 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
|||||||
height: number;
|
height: number;
|
||||||
|
|
||||||
hovering: ClientSlot | undefined;
|
hovering: ClientSlot | undefined;
|
||||||
|
hovering_fluid: ClientFluidSlot | undefined;
|
||||||
// what the screen and mods draw with, in gui pixels
|
// what the screen and mods draw with, in gui pixels
|
||||||
graphics = new ModGraphics();
|
graphics = new ModGraphics();
|
||||||
|
|
||||||
@@ -109,6 +110,7 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
|||||||
if (this.inventory_overlays) this.#render_overlays();
|
if (this.inventory_overlays) this.#render_overlays();
|
||||||
if (g.mouse_blocked()) {
|
if (g.mouse_blocked()) {
|
||||||
this.hovering = undefined;
|
this.hovering = undefined;
|
||||||
|
this.hovering_fluid = undefined;
|
||||||
} else {
|
} else {
|
||||||
this.handle_interaction();
|
this.handle_interaction();
|
||||||
}
|
}
|
||||||
@@ -122,15 +124,17 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
|||||||
if (item) {
|
if (item) {
|
||||||
draw_tooltip(item_tooltip(item), mouse.x, mouse.y);
|
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();
|
g.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
#slot_x(slot: ClientSlot) {
|
#slot_x(slot: { x: number }) {
|
||||||
return this.x + slot.x * GUI_SCALE;
|
return this.x + slot.x * GUI_SCALE;
|
||||||
}
|
}
|
||||||
|
|
||||||
#slot_y(slot: ClientSlot) {
|
#slot_y(slot: { y: number }) {
|
||||||
return this.y + slot.y * GUI_SCALE;
|
return this.y + slot.y * GUI_SCALE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,15 +142,24 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
|||||||
for (const slot of this.menu.slots) {
|
for (const slot of this.menu.slots) {
|
||||||
draw_gui_sprite("slot", this.#slot_x(slot), this.#slot_y(slot));
|
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
|
// 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") => {
|
const highlight = (sprite: "slot_highlight_back" | "slot_highlight_front") => {
|
||||||
if (this.hovering) {
|
const hovered = this.hovering ?? this.hovering_fluid;
|
||||||
const x = this.#slot_x(this.hovering) - HIGHLIGHT_OVERHANG;
|
if (hovered) {
|
||||||
draw_gui_sprite(sprite, x, this.#slot_y(this.hovering) - HIGHLIGHT_OVERHANG);
|
const x = this.#slot_x(hovered) - HIGHLIGHT_OVERHANG;
|
||||||
|
draw_gui_sprite(sprite, x, this.#slot_y(hovered) - HIGHLIGHT_OVERHANG);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
highlight("slot_highlight_back");
|
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) {
|
for (const slot of this.menu.slots) {
|
||||||
const item = slot.container.get_item(slot.index);
|
const item = slot.container.get_item(slot.index);
|
||||||
if (item) {
|
if (item) {
|
||||||
@@ -218,8 +231,25 @@ export abstract class GuiMenuScreen extends GuiScreen {
|
|||||||
|
|
||||||
handle_interaction() {
|
handle_interaction() {
|
||||||
this.hovering = undefined;
|
this.hovering = undefined;
|
||||||
|
this.hovering_fluid = undefined;
|
||||||
const mouse = InputManager.get_mouse_position();
|
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()) {
|
for (const [index, slot] of this.menu.slots.entries()) {
|
||||||
const [x, y] = [this.#slot_x(slot), this.#slot_y(slot)];
|
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)) {
|
if (!point_inside_rec(mouse.x, mouse.y, x, y, SLOT_SIZE, SLOT_SIZE)) {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import { display_name } from "$/common/utils.ts";
|
|||||||
import { fluid_rules } from "$/client/fluid_rules.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 { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts";
|
||||||
import { AssetManager } from "$/client/assets.ts";
|
import { AssetManager } from "$/client/assets.ts";
|
||||||
|
import type { MenuTank } from "$/common/protocol.ts";
|
||||||
import { get_sprite_region } from "$/client/sprites.ts";
|
import { get_sprite_region } from "$/client/sprites.ts";
|
||||||
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
|
import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts";
|
||||||
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
|
import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts";
|
||||||
@@ -62,6 +63,19 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
|
|||||||
return lines;
|
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
|
// 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) {
|
export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: number) {
|
||||||
const line_height = TEXT_HEIGHT * TEXT_SCALE;
|
const line_height = TEXT_HEIGHT * TEXT_SCALE;
|
||||||
|
|||||||
+26
-3
@@ -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
|
// 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
|
// to date with what it syncs. clicks go to the server, which decides what they do
|
||||||
import { Container } from "$/common/inventory.ts";
|
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 { ClientInventories } from "./inventory.ts";
|
||||||
import type { ItemStack as ApiItemStack } from "$/common/mod_api/client.ts";
|
import type { ItemStack as ApiItemStack } from "$/common/mod_api/client.ts";
|
||||||
|
|
||||||
@@ -23,6 +30,12 @@ export interface ClientSlot {
|
|||||||
max_count?: number;
|
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 {
|
export class ClientMenu {
|
||||||
readonly id: number;
|
readonly id: number;
|
||||||
readonly type: string;
|
readonly type: string;
|
||||||
@@ -30,6 +43,7 @@ export class ClientMenu {
|
|||||||
readonly open_data: unknown;
|
readonly open_data: unknown;
|
||||||
data: Record<string, number>;
|
data: Record<string, number>;
|
||||||
slots: ClientSlot[];
|
slots: ClientSlot[];
|
||||||
|
fluid_slots: ClientFluidSlot[] = [];
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
id: number,
|
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
|
// 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);
|
const items = new Container(slots.length);
|
||||||
return new ClientMenu(
|
const menu = new ClientMenu(
|
||||||
id,
|
id,
|
||||||
type,
|
type,
|
||||||
title,
|
title,
|
||||||
@@ -72,6 +93,8 @@ export class ClientMenu {
|
|||||||
max_count: slot.max_count,
|
max_count: slot.max_count,
|
||||||
})),
|
})),
|
||||||
);
|
);
|
||||||
|
menu.fluid_slots = fluid_slots.map((slot) => ({ ...slot, tank: { amount: 0, capacity: 0 } }));
|
||||||
|
return menu;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -246,6 +246,13 @@ export class ClientPacketListener {
|
|||||||
menu?.slots[0]?.container.load(message.items);
|
menu?.slots[0]?.container.load(message.items);
|
||||||
break;
|
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": {
|
case "menu_data": {
|
||||||
const menu = this.#menu(message.id);
|
const menu = this.#menu(message.id);
|
||||||
if (menu) menu.data = message.data;
|
if (menu) menu.data = message.data;
|
||||||
@@ -291,7 +298,14 @@ export class ClientPacketListener {
|
|||||||
close();
|
close();
|
||||||
return;
|
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;
|
menu.data = message.data;
|
||||||
let screen: GuiModMenuScreen;
|
let screen: GuiModMenuScreen;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -744,6 +744,14 @@ export interface SlotOptions {
|
|||||||
max_count?: number;
|
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
|
// what a menu type's build function gets, like neoforge's server menu constructor
|
||||||
export interface MenuBuilder {
|
export interface MenuBuilder {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -751,6 +759,10 @@ export interface MenuBuilder {
|
|||||||
open_data: unknown;
|
open_data: unknown;
|
||||||
// a slot of a container from ctx.containers, or of player.inventory. returns its menu slot index
|
// 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;
|
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
|
// 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
|
// 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;
|
add_player_inventory(x: number, y: number): number;
|
||||||
|
|||||||
+23
-2
@@ -4,13 +4,14 @@
|
|||||||
// messages sent between the client and the server, as json over a websocket. chunks are binary, see level_chunk
|
// 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 { Faces } from "./constants.ts";
|
||||||
import type { ItemData } from "./inventory.ts";
|
import type { ItemData } from "./inventory.ts";
|
||||||
|
import type { FluidVariant } from "./mod_api/shared.ts";
|
||||||
import type { ModListing } from "./mod_loader.ts";
|
import type { ModListing } from "./mod_loader.ts";
|
||||||
|
|
||||||
export const AIR_ID = "bworld:air";
|
export const AIR_ID = "bworld:air";
|
||||||
|
|
||||||
// bump when a client and server of different versions can't play together.
|
// bump when a client and server of different versions can't play together.
|
||||||
// the server rejects a different version before the client downloads anything
|
// the server rejects a different version before the client downloads anything
|
||||||
export const PROTOCOL_VERSION = 24;
|
export const PROTOCOL_VERSION = 25;
|
||||||
|
|
||||||
export interface PlayerInfo {
|
export interface PlayerInfo {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -84,6 +85,21 @@ export interface MenuSlotInfo {
|
|||||||
max_count?: number;
|
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
|
// clients send what the player is trying to do, the server decides what happens
|
||||||
export type ClientMessage =
|
export type ClientMessage =
|
||||||
| { type: "hello"; name: string; protocol: number }
|
| { 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,
|
// 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
|
// 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 }
|
| { 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
|
// 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 }
|
| { 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
|
// 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: "container"; container: ContainerKey; items: (ItemData | null)[] }
|
||||||
| { type: "cursor"; item: 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.
|
// 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";
|
type: "open_menu";
|
||||||
id: number;
|
id: number;
|
||||||
@@ -256,10 +274,13 @@ export type ServerMessage =
|
|||||||
title: string;
|
title: string;
|
||||||
open_data: unknown;
|
open_data: unknown;
|
||||||
slots: MenuSlotInfo[];
|
slots: MenuSlotInfo[];
|
||||||
|
fluid_slots: MenuFluidSlotInfo[];
|
||||||
data: Record<string, number>;
|
data: Record<string, number>;
|
||||||
}
|
}
|
||||||
// what's in every slot of the open menu, by menu slot index, when any of it changed
|
// 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)[] }
|
| { 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
|
// all the menu's data, when any of it changed
|
||||||
| { type: "menu_data"; id: number; data: Record<string, number> }
|
| { type: "menu_data"; id: number; data: Record<string, number> }
|
||||||
// the server closed it (the block broke, the player walked away), it already put everything back
|
// the server closed it (the block broke, the player walked away), it already put everything back
|
||||||
|
|||||||
@@ -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
|
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.
|
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
|
||||||
|
|
||||||
Shift clicking a slot moves its stack to the other part of the menu, like NeoForge's `quickMoveStack`:
|
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
|
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.
|
positions, and the `container` message with `"extra"` syncs what's in them, like the inventory's.
|
||||||
|
|
||||||
| Message | Way | What |
|
| Message | Way | What |
|
||||||
| -------------------- | ------ | ------------------------------------------------------------------------------- |
|
| -------------------- | ------ | -------------------------------------------------------------------------------- |
|
||||||
| `open_menu` | server | The id, type, title, open data, each slot's position, and the data. |
|
| `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_content` | server | Every slot's item, by menu slot index, when any changed. |
|
||||||
| `menu_data` | server | All the data, when any changed. |
|
| `menu_tanks` | server | Every fluid slot's fluid, amount and capacity, when any changed. |
|
||||||
| `close_menu` | both | The server closed it, or the player did. 0 is their inventory screen. |
|
| `menu_data` | server | All the data, when any changed. |
|
||||||
| `click`, `drop_item` | client | A menu slot index of the open menu (or menu 0). Ones for other ids are ignored. |
|
| `close_menu` | both | The server closed it, or the player did. 0 is their inventory screen. |
|
||||||
| `menu_button` | client | A button of the open menu. |
|
| `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`.
|
The code is in `server/game/menu.ts` and `client/menu.ts`.
|
||||||
|
|||||||
@@ -229,3 +229,6 @@ on_interact(block, _params, player) {
|
|||||||
return tanks !== undefined && ctx.fluids.interact(player, tanks);
|
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.
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
"drops": "bworld:keg",
|
"drops": "bworld:keg",
|
||||||
"interactive": true,
|
"interactive": true,
|
||||||
"components": {
|
"components": {
|
||||||
|
"bworld:keg": {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ export function setup(ctx: ClientContext) {
|
|||||||
ctx.ui.register_menu_screen("bworld:storage", chest_screen);
|
ctx.ui.register_menu_screen("bworld:storage", chest_screen);
|
||||||
ctx.ui.register_menu_screen("bworld:furnace", furnace_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: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:smithing_table", smithing_screen);
|
||||||
ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu));
|
ctx.ui.register_menu_screen("bworld:stonecutter", (menu) => stonecutter_screen(ctx, menu));
|
||||||
ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen);
|
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
|
// 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 worm_bin_screen(menu: ClientMenu): MenuScreen {
|
function input_output_screen(menu: ClientMenu): MenuScreen {
|
||||||
return container_screen(menu, HEIGHT, (g) => {
|
return container_screen(menu, HEIGHT, (g) => {
|
||||||
g.texture("bworld:arrow_empty", 85, 34, 18, 18);
|
g.texture("bworld:arrow_empty", 85, 34, 18, 18);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ export function setup(ctx: ServerContext) {
|
|||||||
register_trapdoor(ctx);
|
register_trapdoor(ctx);
|
||||||
register_buckets(ctx);
|
register_buckets(ctx);
|
||||||
register_juicer(ctx);
|
register_juicer(ctx);
|
||||||
|
register_keg(ctx);
|
||||||
register_curios(ctx);
|
register_curios(ctx);
|
||||||
register_effects(ctx);
|
register_effects(ctx);
|
||||||
register_growable_block(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<BlockRef>("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 {
|
interface GrowableBlockParams {
|
||||||
// what grows on top of it
|
// what grows on top of it
|
||||||
block_id: string;
|
block_id: string;
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
|
||||||
import { contained_fluid } from "$/common/inventory.ts";
|
import { contained_fluid } from "$/common/inventory.ts";
|
||||||
import type {
|
import type {
|
||||||
|
FluidAction,
|
||||||
FluidHandler,
|
FluidHandler,
|
||||||
FluidHandlerItem,
|
FluidHandlerItem,
|
||||||
FluidStack,
|
FluidStack,
|
||||||
@@ -94,9 +95,30 @@ export function transfer(from: FluidHandler, to: FluidHandler, max: number): Flu
|
|||||||
|
|
||||||
export function interact(player: Player, handler: FluidHandler): boolean {
|
export function interact(player: Player, handler: FluidHandler): boolean {
|
||||||
const held = player.held_item;
|
const held = player.held_item;
|
||||||
const item = held && item_handler(held);
|
const result = held && use_container(held, handler);
|
||||||
if (!item) return false;
|
if (result) player.exchange_held(result);
|
||||||
const moved = transfer(handler, item, Infinity) ?? transfer(item, handler, Infinity);
|
return result !== undefined;
|
||||||
if (moved) player.exchange_held(item.container);
|
}
|
||||||
return moved !== 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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -928,10 +928,12 @@ export class GameServer {
|
|||||||
title: menu.title,
|
title: menu.title,
|
||||||
open_data: menu.open_data,
|
open_data: menu.open_data,
|
||||||
slots: menu.slot_info(),
|
slots: menu.slot_info(),
|
||||||
|
fluid_slots: menu.fluid_slot_info(),
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
player.sent.set("menu_data", JSON.stringify(data));
|
player.sent.set("menu_data", JSON.stringify(data));
|
||||||
player.sent.delete("menu_content");
|
player.sent.delete("menu_content");
|
||||||
|
player.sent.delete("menu_tanks");
|
||||||
}
|
}
|
||||||
|
|
||||||
// closes it for the player too, not just on the server
|
// closes it for the player too, not just on the server
|
||||||
@@ -1140,6 +1142,17 @@ export class GameServer {
|
|||||||
case "click":
|
case "click":
|
||||||
this.#click(player, message.menu, message.slot, message.button, message.shift === true);
|
this.#click(player, message.menu, message.slot, message.button, message.shift === true);
|
||||||
break;
|
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":
|
case "drop_item":
|
||||||
this.#drop_item(player, message.menu, message.slot, message.all === true);
|
this.#drop_item(player, message.menu, message.slot, message.all === true);
|
||||||
break;
|
break;
|
||||||
@@ -1920,6 +1933,7 @@ export class GameServer {
|
|||||||
player.menu = undefined;
|
player.menu = undefined;
|
||||||
for (const fn of menu?.on_close ?? []) fn();
|
for (const fn of menu?.on_close ?? []) fn();
|
||||||
player.sent.delete("menu_content");
|
player.sent.delete("menu_content");
|
||||||
|
player.sent.delete("menu_tanks");
|
||||||
player.sent.delete("menu_data");
|
player.sent.delete("menu_data");
|
||||||
|
|
||||||
// the crafting grid and whatever the cursor holds go back into the inventory
|
// the crafting grid and whatever the cursor holds go back into the inventory
|
||||||
@@ -2218,6 +2232,10 @@ export class GameServer {
|
|||||||
if (menu) {
|
if (menu) {
|
||||||
const items = menu.content();
|
const items = menu.content();
|
||||||
sync("menu_content", items, () => ({ type: "menu_content", id: menu.id, items }));
|
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();
|
const data = menu.data_values();
|
||||||
sync("menu_data", data, () => ({ type: "menu_data", id: menu.id, data }));
|
sync("menu_data", data, () => ({ type: "menu_data", id: menu.id, data }));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,9 @@ import {
|
|||||||
} from "$/common/inventory.ts";
|
} from "$/common/inventory.ts";
|
||||||
import {
|
import {
|
||||||
CRAFTING_RESULT_SLOT,
|
CRAFTING_RESULT_SLOT,
|
||||||
|
type MenuFluidSlotInfo,
|
||||||
type MenuSlotInfo,
|
type MenuSlotInfo,
|
||||||
|
type MenuTank,
|
||||||
PLAYER_MENU,
|
PLAYER_MENU,
|
||||||
PLAYER_MENU_CRAFTING,
|
PLAYER_MENU_CRAFTING,
|
||||||
PLAYER_MENU_EXTRA,
|
PLAYER_MENU_EXTRA,
|
||||||
@@ -44,6 +46,18 @@ export interface MenuSlot {
|
|||||||
inventory?: number;
|
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 {
|
export class Menu {
|
||||||
readonly id: number;
|
readonly id: number;
|
||||||
readonly type: string;
|
readonly type: string;
|
||||||
@@ -51,6 +65,7 @@ export class Menu {
|
|||||||
// sent once when it opens
|
// sent once when it opens
|
||||||
open_data: unknown = null;
|
open_data: unknown = null;
|
||||||
slots: MenuSlot[] = [];
|
slots: MenuSlot[] = [];
|
||||||
|
fluid_slots: MenuFluidSlot[] = [];
|
||||||
// numbers the screen shows, read every tick and sent when they change, like DataSlot
|
// numbers the screen shows, read every tick and sent when they change, like DataSlot
|
||||||
data = new Map<string, () => number>();
|
data = new Map<string, () => number>();
|
||||||
// checked every tick, any false closes it, like stillValid
|
// 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
|
// what players are sent, with lore
|
||||||
content(): (ItemData | null)[] {
|
content(): (ItemData | null)[] {
|
||||||
return this.slots.map((slot) => slot.container.get_item(slot.index)?.to_synced_data() ?? null);
|
return this.slots.map((slot) => slot.container.get_item(slot.index)?.to_synced_data() ?? null);
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ import { physics } from "$/common/physics.ts";
|
|||||||
import { clock } from "$/common/time.ts";
|
import { clock } from "$/common/time.ts";
|
||||||
import { BlockRules } from "$/common/block_rules.ts";
|
import { BlockRules } from "$/common/block_rules.ts";
|
||||||
import { FluidRules } from "$/common/fluids.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 type { SharedContext } from "$/common/mod_api/shared.ts";
|
||||||
import { tag_api } from "$/common/tags.ts";
|
import { tag_api } from "$/common/tags.ts";
|
||||||
import { loading_mod } from "$/common/hooks.ts";
|
import { loading_mod } from "$/common/hooks.ts";
|
||||||
@@ -41,6 +41,7 @@ import type {
|
|||||||
EventSignal,
|
EventSignal,
|
||||||
Face,
|
Face,
|
||||||
FluidHandler,
|
FluidHandler,
|
||||||
|
FluidSlotOptions,
|
||||||
ItemComponent,
|
ItemComponent,
|
||||||
ItemStack,
|
ItemStack,
|
||||||
LivingRef,
|
LivingRef,
|
||||||
@@ -78,7 +79,7 @@ import { AIR_ID } from "$/common/protocol.ts";
|
|||||||
import type { GameServer } from "./game_server.ts";
|
import type { GameServer } from "./game_server.ts";
|
||||||
import { MAX_PRIORITY, MIN_PRIORITY } from "./scheduled_ticks.ts";
|
import { MAX_PRIORITY, MIN_PRIORITY } from "./scheduled_ticks.ts";
|
||||||
import type { ServerPlayer } from "./player.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 { damage_source, type Entity } from "./entity/entity.ts";
|
||||||
import type { LivingEntity } from "./entity/living_entity.ts";
|
import type { LivingEntity } from "./entity/living_entity.ts";
|
||||||
import type { EffectJson } from "$/common/mod_data.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));
|
menu.slots.push(menu_slot(game, mod, server_player, container, slot, options));
|
||||||
return menu.slots.length - 1;
|
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) {
|
add_player_inventory(x, y) {
|
||||||
const first = menu.slots.length;
|
const first = menu.slots.length;
|
||||||
for (let i = 0; i < server_player.inventory.size; i++) {
|
for (let i = 0; i < server_player.inventory.size; i++) {
|
||||||
@@ -1365,6 +1370,53 @@ function menu_slot(
|
|||||||
return added;
|
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
|
// the engine container behind each one mods get, for menus' slots
|
||||||
const engine_containers = new WeakMap<Container, EngineContainer>();
|
const engine_containers = new WeakMap<Container, EngineContainer>();
|
||||||
|
|
||||||
|
|||||||
@@ -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<ItemRegistry>("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 });
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user