Change curios to always show on inventory
This commit is contained in:
+63
-22
@@ -19,9 +19,11 @@ import type {
|
||||
MenuHandle,
|
||||
MenuType,
|
||||
Player,
|
||||
PlayerMenuBuilder,
|
||||
ServerAfterEvents,
|
||||
ServerBeforeEvents,
|
||||
ServerContext,
|
||||
SlotOptions,
|
||||
} from "$/common/mod_api/server.ts";
|
||||
import { ModLoadError } from "$/common/mod_loader.ts";
|
||||
import {
|
||||
@@ -174,6 +176,28 @@ export class ModRuntime {
|
||||
#server_players = new WeakMap<Player, ServerPlayer>();
|
||||
// menu types by id, every mod's
|
||||
#menu_types = new Set<string>();
|
||||
// what each mod adds to the player's own menu, in load order
|
||||
#player_menu_builders: { mod: string; build: (menu: PlayerMenuBuilder, player: Player) => void }[] = [];
|
||||
|
||||
// a player joined: the mods' slots go after menu 0's own. one that throws is logged and the rest still run
|
||||
extend_player_menu(player: ServerPlayer) {
|
||||
const api = this.player(player);
|
||||
for (const { mod, build } of this.#player_menu_builders) {
|
||||
const menu = player.inventory_menu;
|
||||
const added: MenuSlot[] = [];
|
||||
const ok = run_guarded(mod, "an extend_player_menu", () => {
|
||||
build({
|
||||
add_slot: (container, slot, options) => {
|
||||
added.push(menu_slot(this.game, mod, player, container, slot, options));
|
||||
return menu.slots.length + added.length - 1;
|
||||
},
|
||||
}, api);
|
||||
return true;
|
||||
});
|
||||
// all of a mod's slots or none, so a half built set can't shift the next mod's
|
||||
if (ok) menu.slots.push(...added);
|
||||
}
|
||||
}
|
||||
game!: GameServer;
|
||||
|
||||
// imports each server script and calls its setup, in load order
|
||||
@@ -570,6 +594,10 @@ export class ModRuntime {
|
||||
delete: (id) => game().delete_container(id),
|
||||
},
|
||||
menus: {
|
||||
extend_player_menu: (build) => {
|
||||
setup_only("extend_player_menu");
|
||||
this.#player_menu_builders.push({ mod, build });
|
||||
},
|
||||
register: <Args>(id: string, build: (menu: MenuBuilder, args: Args, player: Player) => void) => {
|
||||
own(id, "menu type");
|
||||
if (this.#menu_types.has(id)) throw new Error(`[${mod}] menu type ${id} is already registered`);
|
||||
@@ -646,28 +674,7 @@ export class ModRuntime {
|
||||
menu.open_data = structuredClone(value) ?? null;
|
||||
},
|
||||
add_slot(container, slot, options) {
|
||||
const engine = engine_containers.get(container);
|
||||
if (!engine) throw new Error("add_slot needs a container from ctx.containers or player.inventory");
|
||||
if (!Number.isInteger(slot) || slot < 0 || slot >= engine.size) {
|
||||
throw new Error(`slot ${slot} isn't in the container, it has ${engine.size}`);
|
||||
}
|
||||
if (![options?.x, options?.y].every((n) => typeof n === "number" && Number.isFinite(n))) {
|
||||
throw new Error(`slot ${slot} needs an x and y`);
|
||||
}
|
||||
const { filter, on_take } = options;
|
||||
const added: MenuSlot = { container: engine, index: slot, x: options.x, y: options.y };
|
||||
if (filter === "smeltable") {
|
||||
added.filter = (item) => game.recipes.furnace.has(item.type_id);
|
||||
} else if (filter === "fuel") {
|
||||
added.filter = (item) => game.recipes.fuel.has(item.type_id);
|
||||
} else if (typeof filter === "function") {
|
||||
added.filter = (item) => guarded("a slot filter", () => filter(item_api(item))) === true;
|
||||
}
|
||||
if (options.output_only) added.output = true;
|
||||
if (on_take) added.on_take = (item) => guarded("an on_take", () => on_take(item_api(item))) === true;
|
||||
if (options.icon) added.icon = String(options.icon);
|
||||
if (engine === server_player.inventory) added.inventory = slot;
|
||||
menu.slots.push(added);
|
||||
menu.slots.push(menu_slot(game, mod, server_player, container, slot, options));
|
||||
return menu.slots.length - 1;
|
||||
},
|
||||
add_player_inventory(x, y) {
|
||||
@@ -834,6 +841,40 @@ function new_stack(item: ItemStack): EngineItemStack {
|
||||
return stack;
|
||||
}
|
||||
|
||||
// a menu's slot from what a mod's add_slot got
|
||||
function menu_slot(
|
||||
game: GameServer,
|
||||
mod: string,
|
||||
player: ServerPlayer,
|
||||
container: Container,
|
||||
slot: number,
|
||||
options: SlotOptions,
|
||||
): MenuSlot {
|
||||
const engine = engine_containers.get(container);
|
||||
if (!engine) throw new Error("add_slot needs a container from ctx.containers or player.inventory");
|
||||
if (!Number.isInteger(slot) || slot < 0 || slot >= engine.size) {
|
||||
throw new Error(`slot ${slot} isn't in the container, it has ${engine.size}`);
|
||||
}
|
||||
if (![options?.x, options?.y].every((n) => typeof n === "number" && Number.isFinite(n))) {
|
||||
throw new Error(`slot ${slot} needs an x and y`);
|
||||
}
|
||||
const { filter, on_take } = options;
|
||||
const guarded = <T>(what: string, fn: () => T) => run_guarded(mod, what, fn);
|
||||
const added: MenuSlot = { container: engine, index: slot, x: options.x, y: options.y };
|
||||
if (filter === "smeltable") {
|
||||
added.filter = (item) => game.recipes.furnace.has(item.type_id);
|
||||
} else if (filter === "fuel") {
|
||||
added.filter = (item) => game.recipes.fuel.has(item.type_id);
|
||||
} else if (typeof filter === "function") {
|
||||
added.filter = (item) => guarded("a slot filter", () => filter(item_api(item))) === true;
|
||||
}
|
||||
if (options.output_only) added.output = true;
|
||||
if (on_take) added.on_take = (item) => guarded("an on_take", () => on_take(item_api(item))) === true;
|
||||
if (options.icon) added.icon = String(options.icon);
|
||||
if (engine === player.inventory) added.inventory = slot;
|
||||
return added;
|
||||
}
|
||||
|
||||
// the engine container behind each one mods get, for menus' slots
|
||||
const engine_containers = new WeakMap<Container, EngineContainer>();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user