Change curios to always show on inventory
This commit is contained in:
@@ -714,6 +714,7 @@ export class GameServer {
|
||||
Object.assign(player, this.world.spawn_point());
|
||||
}
|
||||
|
||||
this.mods.extend_player_menu(player);
|
||||
this.#send(player, {
|
||||
type: "join",
|
||||
id: player.id,
|
||||
@@ -723,6 +724,7 @@ export class GameServer {
|
||||
blocks: this.world.block_table(),
|
||||
spawn: { x: player.x, y: player.y, z: player.z, yaw: player.yaw, pitch: player.pitch },
|
||||
selected_slot: player.selected_slot,
|
||||
player_menu: player.inventory_menu.extra.map(({ x, y, icon }) => ({ x, y, ...(icon ? { icon } : {}) })),
|
||||
time: this.world.time,
|
||||
});
|
||||
this.#players.set(conn, player);
|
||||
@@ -1525,6 +1527,11 @@ export class GameServer {
|
||||
sync("crafting", crafting, () => ({ type: "container", container: "crafting", items: crafting }));
|
||||
const cursor = player.cursor.item?.to_synced_data() ?? null;
|
||||
sync("cursor", cursor, () => ({ type: "cursor", item: cursor }));
|
||||
const extra = player.inventory_menu.extra;
|
||||
if (extra.length > 0) {
|
||||
const items = extra.map((slot) => slot.container.get_item(slot.index)?.to_synced_data() ?? null);
|
||||
sync("extra", items, () => ({ type: "container", container: "extra", items }));
|
||||
}
|
||||
|
||||
const { food, saturation } = player.vitals;
|
||||
const health = player.health;
|
||||
|
||||
+18
-6
@@ -11,7 +11,13 @@ import {
|
||||
room_for,
|
||||
take_output,
|
||||
} from "$/common/inventory.ts";
|
||||
import { CRAFTING_RESULT_SLOT, type MenuSlotInfo, PLAYER_MENU, PLAYER_MENU_CRAFTING } from "$/common/protocol.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";
|
||||
|
||||
@@ -155,8 +161,8 @@ export class Menu {
|
||||
}
|
||||
}
|
||||
|
||||
// menu 0, the player's own inventory and crafting grid, like minecraft's InventoryMenu. clients know its layout, so
|
||||
// it's never sent
|
||||
// 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[];
|
||||
@@ -198,8 +204,13 @@ export class PlayerMenu extends Menu {
|
||||
}
|
||||
}
|
||||
|
||||
// the inventory moves between the hotbar and the rest, never into the grid. the grid and its result go to the
|
||||
// inventory, the rest first
|
||||
// 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);
|
||||
@@ -208,6 +219,7 @@ export class PlayerMenu extends Menu {
|
||||
? [...main, ...hotbar].reverse()
|
||||
: [...main, ...hotbar];
|
||||
}
|
||||
return index < 9 ? main : hotbar;
|
||||
const extra = this.extra.map((_, i) => PLAYER_MENU_EXTRA + i);
|
||||
return [...extra, ...(index < 9 ? main : hotbar)];
|
||||
}
|
||||
}
|
||||
|
||||
+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