232 lines
8.5 KiB
TypeScript
232 lines
8.5 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// menus, like neoforge's AbstractContainerMenu: what a player can do in a gui. the server's menu decides what every
|
|
// click does, clients only draw it. see docs/modding/inventories/menus.md
|
|
import {
|
|
can_take_output,
|
|
click_slot,
|
|
type Container,
|
|
type Cursor,
|
|
type ItemData,
|
|
type ItemStack,
|
|
move_into,
|
|
room_for,
|
|
take_output,
|
|
} from "$/common/inventory.ts";
|
|
import {
|
|
CRAFTING_RESULT_SLOT,
|
|
type MenuSlotInfo,
|
|
PLAYER_MENU,
|
|
PLAYER_MENU_CRAFTING,
|
|
PLAYER_MENU_EXTRA,
|
|
} from "$/common/protocol.ts";
|
|
import type { GridRecipe } from "$/common/mod_data.ts";
|
|
import { consume_recipe_items, update_crafting_result } from "./crafting.ts";
|
|
|
|
export interface MenuSlot {
|
|
container: Container;
|
|
index: number;
|
|
// gui pixels from the screen's top left, for the client
|
|
x: number;
|
|
y: number;
|
|
// what can be put in
|
|
filter?: (item: ItemStack) => boolean;
|
|
// can only be taken from, all at once, like the furnace result
|
|
output?: boolean;
|
|
// asked before a player takes from an output slot, false stops them
|
|
on_take?: (item: ItemStack) => boolean;
|
|
// drawn in the slot while it's empty
|
|
icon?: string;
|
|
// the most it holds of anything, when that's less than a stack, like minecraft's Slot.getMaxStackSize
|
|
max_count?: number;
|
|
// one of the player's own inventory slots, this one
|
|
inventory?: number;
|
|
}
|
|
|
|
export class Menu {
|
|
readonly id: number;
|
|
readonly type: string;
|
|
title = "";
|
|
// sent once when it opens
|
|
open_data: unknown = null;
|
|
slots: MenuSlot[] = [];
|
|
// numbers the screen shows, read every tick and sent when they change, like DataSlot
|
|
data = new Map<string, () => number>();
|
|
// checked every tick, any false closes it, like stillValid
|
|
still_valid: (() => boolean)[] = [];
|
|
on_button: ((button: number) => void)[] = [];
|
|
// the menu slots a shift click on a slot tries, in order. the default rule when not set
|
|
quick_move_order: ((index: number) => number[]) | undefined;
|
|
on_tick: (() => void)[] = [];
|
|
on_close: (() => void)[] = [];
|
|
// after anything a click changed, like the crafting grid working out its result
|
|
on_changed: (() => void) | undefined;
|
|
|
|
constructor(id: number, type: string) {
|
|
this.id = id;
|
|
this.type = type;
|
|
}
|
|
|
|
accepts(slot: MenuSlot, item: ItemStack): boolean {
|
|
return !slot.output && (slot.filter?.(item) ?? true);
|
|
}
|
|
|
|
click(index: number, button: number, cursor: Cursor) {
|
|
const slot = this.slots[index];
|
|
if (!slot) return;
|
|
if (slot.output) {
|
|
const item = slot.container.get_item(slot.index);
|
|
if (item && can_take_output(item, cursor) && (slot.on_take?.(item) ?? true) && take_output(item, cursor)) {
|
|
slot.container.set_item(slot.index, undefined);
|
|
}
|
|
} else if (cursor.item && !this.accepts(slot, cursor.item)) {
|
|
// the slot doesn't take what the cursor holds
|
|
return;
|
|
} else {
|
|
click_slot(slot.container, slot.index, cursor, button, slot.max_count);
|
|
}
|
|
this.on_changed?.();
|
|
}
|
|
|
|
// shift click, like minecraft's quickMoveStack: the stack goes to the other part of the menu, as much as fits. an
|
|
// output slot only gives its item when it all fits
|
|
quick_move(index: number) {
|
|
const slot = this.slots[index];
|
|
const item = slot?.container.get_item(slot.index);
|
|
if (!item) return;
|
|
const targets = this.quick_move_targets(index).filter((i) => i !== index).map((i) => this.slots[i])
|
|
.filter((target) => target !== undefined);
|
|
const accepts = (target: { container: Container; index: number }) => this.accepts(target as MenuSlot, item);
|
|
if (!slot.output) {
|
|
move_into(item, targets, accepts);
|
|
} else if (room_for(item, targets, accepts) >= item.amount && (slot.on_take?.(item) ?? true)) {
|
|
move_into(item.clone(), targets, accepts);
|
|
slot.container.set_item(slot.index, undefined);
|
|
}
|
|
this.on_changed?.();
|
|
}
|
|
|
|
// what's in the player's inventory goes into the menu's own slots that take it, what's in those goes to the
|
|
// inventory. with nothing to go into, between the hotbar and the rest of the inventory
|
|
quick_move_targets(index: number): number[] {
|
|
if (this.quick_move_order) return this.quick_move_order(index);
|
|
const slot = this.slots[index];
|
|
const inventory = this.slots.map((s, i) => ({ s, i })).filter(({ s }) => s.inventory !== undefined)
|
|
.sort((a, b) => a.s.inventory! - b.s.inventory!);
|
|
const hotbar = inventory.filter(({ s }) => s.inventory! < 9).map(({ i }) => i);
|
|
const main = inventory.filter(({ s }) => s.inventory! >= 9).map(({ i }) => i);
|
|
if (slot.inventory === undefined) {
|
|
// like minecraft, what comes out of a container fills the hotbar from the right first
|
|
return [...main, ...hotbar].reverse();
|
|
}
|
|
const inputs = this.slots.flatMap((s, i) => s.inventory === undefined && !s.output ? [i] : []);
|
|
if (inputs.length > 0) return inputs;
|
|
return slot.inventory < 9 ? main : hotbar;
|
|
}
|
|
|
|
// q on a slot: takes one item out, or the whole stack, for the server to throw. not from output slots
|
|
drop(index: number, all: boolean): ItemStack | undefined {
|
|
const slot = this.slots[index];
|
|
const item = slot && !slot.output ? slot.container.get_item(slot.index) : undefined;
|
|
if (!item) return undefined;
|
|
const thrown = item.clone();
|
|
thrown.amount = all ? item.amount : 1;
|
|
item.amount -= thrown.amount;
|
|
slot.container.set_item(slot.index, item.amount > 0 ? item : undefined);
|
|
this.on_changed?.();
|
|
return thrown;
|
|
}
|
|
|
|
uses(container: Container): boolean {
|
|
return this.slots.some((slot) => slot.container === container);
|
|
}
|
|
|
|
slot_info(): MenuSlotInfo[] {
|
|
return this.slots.map(({ x, y, output, icon, max_count }) => ({
|
|
x,
|
|
y,
|
|
...(output ? { output: true } : {}),
|
|
...(icon ? { icon } : {}),
|
|
...(max_count !== undefined ? { max_count } : {}),
|
|
}));
|
|
}
|
|
|
|
// what players are sent, with lore
|
|
content(): (ItemData | null)[] {
|
|
return this.slots.map((slot) => slot.container.get_item(slot.index)?.to_synced_data() ?? null);
|
|
}
|
|
|
|
data_values(): Record<string, number> {
|
|
const values: Record<string, number> = {};
|
|
for (const [name, get] of this.data) {
|
|
const value = get();
|
|
values[name] = typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
}
|
|
return values;
|
|
}
|
|
}
|
|
|
|
// menu 0, the player's own inventory and crafting grid, like minecraft's InventoryMenu, then the slots mods add with
|
|
// ctx.menus.extend_player_menu. clients know its layout, only the mods' slots are sent, with join
|
|
export class PlayerMenu extends Menu {
|
|
#crafting: Container;
|
|
#recipes: () => GridRecipe[];
|
|
|
|
constructor(inventory: Container, crafting: Container, recipes: () => GridRecipe[]) {
|
|
super(PLAYER_MENU, "");
|
|
this.#crafting = crafting;
|
|
this.#recipes = recipes;
|
|
for (let i = 0; i < inventory.size; i++) {
|
|
this.slots.push({ container: inventory, index: i, x: 0, y: 0, inventory: i });
|
|
}
|
|
for (let i = 0; i < crafting.size; i++) {
|
|
this.slots.push({ container: crafting, index: i, x: 0, y: 0 });
|
|
}
|
|
const result = this.slots[PLAYER_MENU_CRAFTING + CRAFTING_RESULT_SLOT];
|
|
result.output = true;
|
|
// taking the result uses up the grid
|
|
result.on_take = () => {
|
|
consume_recipe_items(crafting);
|
|
return true;
|
|
};
|
|
this.on_changed = () => update_crafting_result(crafting, this.#recipes());
|
|
}
|
|
|
|
override quick_move(index: number) {
|
|
if (index !== PLAYER_MENU_CRAFTING + CRAFTING_RESULT_SLOT) {
|
|
super.quick_move(index);
|
|
return;
|
|
}
|
|
// crafts as many as fit, while the grid keeps making the same thing
|
|
const targets = this.quick_move_targets(index).map((i) => this.slots[i]);
|
|
const id = this.#crafting.get_item(CRAFTING_RESULT_SLOT)?.type_id;
|
|
let result = this.#crafting.get_item(CRAFTING_RESULT_SLOT);
|
|
while (result && result.type_id === id && room_for(result, targets) >= result.amount) {
|
|
move_into(result.clone(), targets);
|
|
consume_recipe_items(this.#crafting);
|
|
update_crafting_result(this.#crafting, this.#recipes());
|
|
result = this.#crafting.get_item(CRAFTING_RESULT_SLOT);
|
|
}
|
|
}
|
|
|
|
// the slots mods added
|
|
get extra(): MenuSlot[] {
|
|
return this.slots.slice(PLAYER_MENU_EXTRA);
|
|
}
|
|
|
|
// the inventory goes into the slots mods added that take it, then between the hotbar and the rest, never into the
|
|
// grid. the grid, its result and the mods' slots go to the inventory, the rest first
|
|
override quick_move_targets(index: number): number[] {
|
|
const main = Array.from({ length: PLAYER_MENU_CRAFTING - 9 }, (_, i) => i + 9);
|
|
const hotbar = Array.from({ length: 9 }, (_, i) => i);
|
|
if (index >= PLAYER_MENU_CRAFTING) {
|
|
return index === PLAYER_MENU_CRAFTING + CRAFTING_RESULT_SLOT
|
|
? [...main, ...hotbar].reverse()
|
|
: [...main, ...hotbar];
|
|
}
|
|
const extra = this.extra.map((_, i) => PLAYER_MENU_EXTRA + i);
|
|
return [...extra, ...(index < 9 ? main : hotbar)];
|
|
}
|
|
}
|