Change whole gui system
This commit is contained in:
+141
-74
@@ -11,13 +11,14 @@ import type {
|
||||
CommandRegistry,
|
||||
CommandSource,
|
||||
Container,
|
||||
ContainerScreenOptions,
|
||||
EntityRef,
|
||||
EventSignal,
|
||||
ItemComponent,
|
||||
ItemStack,
|
||||
MenuBuilder,
|
||||
MenuHandle,
|
||||
MenuType,
|
||||
Player,
|
||||
ScreenHandle,
|
||||
ServerAfterEvents,
|
||||
ServerBeforeEvents,
|
||||
ServerContext,
|
||||
@@ -40,12 +41,12 @@ import {
|
||||
} from "$/common/commands.ts";
|
||||
import { AIR_ID } from "$/common/protocol.ts";
|
||||
import type { GameServer } from "./game_server.ts";
|
||||
import type { OpenScreen, ServerPlayer } from "./player.ts";
|
||||
import type { ServerPlayer } from "./player.ts";
|
||||
import { Menu, type MenuSlot } from "./menu.ts";
|
||||
import { MAX_HEALTH } from "./vitals.ts";
|
||||
import type { Entity } from "./entity/entity.ts";
|
||||
import { create_entity } from "./entity/entity_types.ts";
|
||||
import { attribute_type, type AttributeType, MODIFIER_OPERATIONS } from "./entity/attributes.ts";
|
||||
import type { ScreenLayout } from "$/common/protocol.ts";
|
||||
|
||||
// a list of handlers that can't break each other: one throwing is logged under its mod and the rest still run
|
||||
export class Signal<T> {
|
||||
@@ -146,20 +147,33 @@ export class ModRuntime {
|
||||
|
||||
// a player's client sent a mod message. clients are untrusted: too big or too many are dropped
|
||||
receive(player: ServerPlayer, channel: string, data: unknown) {
|
||||
if (!this.#count_message(player)) return;
|
||||
if ((JSON.stringify(data) ?? "").length > MAX_MOD_MESSAGE_LENGTH) return;
|
||||
for (const { mod, handler } of this.#net_handlers.get(channel) ?? []) {
|
||||
run_guarded(mod, `a ${channel} handler`, () => handler(this.player(player), data));
|
||||
}
|
||||
}
|
||||
// a client pressed a button of its open menu. buttons count as mod messages, clients are untrusted
|
||||
menu_button(player: ServerPlayer, menu: Menu, button: number) {
|
||||
if (!this.#count_message(player)) return;
|
||||
for (const fn of [...menu.on_button]) fn(button);
|
||||
}
|
||||
|
||||
// false once a player has sent too many this second
|
||||
#count_message(player: ServerPlayer): boolean {
|
||||
const second = Math.floor(Date.now() / 1000);
|
||||
let counts = this.#net_counts.get(player);
|
||||
if (!counts || counts.second !== second) {
|
||||
counts = { second, count: 0 };
|
||||
this.#net_counts.set(player, counts);
|
||||
}
|
||||
if (++counts.count > MAX_MOD_MESSAGES_PER_SECOND) return;
|
||||
if ((JSON.stringify(data) ?? "").length > MAX_MOD_MESSAGE_LENGTH) return;
|
||||
for (const { mod, handler } of this.#net_handlers.get(channel) ?? []) {
|
||||
run_guarded(mod, `a ${channel} handler`, () => handler(this.player(player), data));
|
||||
}
|
||||
return ++counts.count <= MAX_MOD_MESSAGES_PER_SECOND;
|
||||
}
|
||||
|
||||
// the other way, for apis that take a player
|
||||
#server_players = new WeakMap<Player, ServerPlayer>();
|
||||
// menu types by id, every mod's
|
||||
#menu_types = new Set<string>();
|
||||
game!: GameServer;
|
||||
|
||||
// imports each server script and calls its setup, in load order
|
||||
@@ -555,12 +569,22 @@ export class ModRuntime {
|
||||
},
|
||||
delete: (id) => game().delete_container(id),
|
||||
},
|
||||
menus: {
|
||||
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`);
|
||||
this.#menu_types.add(id);
|
||||
const type: MenuType<Args> = {
|
||||
id,
|
||||
open: (player, args) => this.#open_menu(mod, id, player, (menu) => build(menu, args, player)),
|
||||
};
|
||||
return type;
|
||||
},
|
||||
},
|
||||
ui: {
|
||||
open_container: (player, options) => this.#open_container(mod, player, options),
|
||||
message_form: not_yet(mod, "ui.message_form", "see docs/modding_plan.md"),
|
||||
action_form: not_yet(mod, "ui.action_form", "see docs/modding_plan.md"),
|
||||
modal_form: not_yet(mod, "ui.modal_form", "see docs/modding_plan.md"),
|
||||
open_screen: not_yet(mod, "ui.open_screen", "see docs/modding_plan.md"),
|
||||
},
|
||||
net: {
|
||||
on: (channel, handler) => {
|
||||
@@ -598,89 +622,125 @@ export class ModRuntime {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
#open_container(mod: string, player: Player, options: ContainerScreenOptions): ScreenHandle {
|
||||
// builds a menu with the mod's function and opens it. a build that throws opens nothing
|
||||
#open_menu(mod: string, type: string, player: Player, build: (menu: MenuBuilder) => void): MenuHandle {
|
||||
const game = this.game;
|
||||
const server_player = this.#server_players.get(player);
|
||||
if (!server_player || !game.players().includes(server_player)) {
|
||||
throw new Error(`${player.name} isn't online`);
|
||||
}
|
||||
const container = game.containers.get(options.container.id);
|
||||
if (!container) {
|
||||
throw new Error("open_container needs a container from ctx.containers");
|
||||
}
|
||||
const menu = new Menu(game.next_menu_id(server_player), type);
|
||||
const guarded = <T>(what: string, fn: () => T) => run_guarded(mod, what, fn);
|
||||
|
||||
const filters = new Map<number, (item: EngineItemStack) => boolean>();
|
||||
const on_take = new Map<number, (item: EngineItemStack) => boolean>();
|
||||
for (const { slot, filter, on_take: take } of options.layout) {
|
||||
if (take) {
|
||||
on_take.set(slot, (item) => run_guarded(mod, "an on_take", () => take(item_api(item))) === true);
|
||||
}
|
||||
if (!Number.isInteger(slot) || slot < 0 || slot >= container.size) {
|
||||
throw new Error(`slot ${slot} isn't in the container, it has ${container.size}`);
|
||||
}
|
||||
if (filter === "smeltable") {
|
||||
filters.set(slot, (item) => game.recipes.furnace.has(item.type_id));
|
||||
} else if (filter === "fuel") {
|
||||
filters.set(slot, (item) => game.recipes.fuel.has(item.type_id));
|
||||
} else if (typeof filter === "function") {
|
||||
filters.set(slot, (item) => run_guarded(mod, "a slot filter", () => filter(item_api(item))) === true);
|
||||
}
|
||||
build({
|
||||
get title() {
|
||||
return menu.title;
|
||||
},
|
||||
set title(value) {
|
||||
menu.title = String(value);
|
||||
},
|
||||
get open_data() {
|
||||
return menu.open_data;
|
||||
},
|
||||
set open_data(value) {
|
||||
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);
|
||||
return menu.slots.length - 1;
|
||||
},
|
||||
add_player_inventory(x, y) {
|
||||
const first = menu.slots.length;
|
||||
for (let i = 0; i < server_player.inventory.size; i++) {
|
||||
// the rest of the inventory in rows from x and y, the hotbar under it like minecraft's
|
||||
const row = i < 9 ? 0 : Math.floor(i / 9) - 1;
|
||||
menu.slots.push({
|
||||
container: server_player.inventory,
|
||||
index: i,
|
||||
x: x + (i % 9) * 18,
|
||||
y: i < 9 ? y + 58 : y + row * 18,
|
||||
inventory: i,
|
||||
});
|
||||
}
|
||||
return first;
|
||||
},
|
||||
add_data(name, get) {
|
||||
menu.data.set(String(name), () => guarded(`menu data ${name}`, get) ?? 0);
|
||||
},
|
||||
still_valid(check) {
|
||||
menu.still_valid.push(() => guarded("a still_valid", () => check(player)) !== false);
|
||||
},
|
||||
still_valid_near(block) {
|
||||
const { id, x, y, z } = block;
|
||||
menu.still_valid.push(() => {
|
||||
const [dx, dy, dz] = [
|
||||
server_player.x - x - 0.5,
|
||||
server_player.y - y - 0.5,
|
||||
server_player.z - z - 0.5,
|
||||
];
|
||||
return dx * dx + dy * dy + dz * dz <= MENU_REACH * MENU_REACH &&
|
||||
game.world.get_block_id(x, y, z) === id;
|
||||
});
|
||||
},
|
||||
on_button(fn) {
|
||||
menu.on_button.push((button) => guarded("an on_button", () => fn(player, button)));
|
||||
},
|
||||
quick_move(fn) {
|
||||
menu.quick_move_order = (index) => {
|
||||
const order = guarded("a quick_move", () => fn(player, index));
|
||||
return Array.isArray(order) ? order.filter((i) => Number.isInteger(i)) : [];
|
||||
};
|
||||
},
|
||||
on_tick(fn) {
|
||||
menu.on_tick.push(() => guarded("a menu on_tick", fn));
|
||||
},
|
||||
on_close(fn) {
|
||||
menu.on_close.push(() => guarded("a menu on_close", fn));
|
||||
},
|
||||
});
|
||||
if ((JSON.stringify(menu.open_data) ?? "").length > MAX_MOD_MESSAGE_LENGTH) {
|
||||
throw new Error(`the open_data of ${type} is over 16 KB`);
|
||||
}
|
||||
|
||||
const bars = options.bars ?? [];
|
||||
const bottom = Math.max(0, ...options.layout.map((s) => s.y + 1), ...bars.map((b) => b.y + 1));
|
||||
const layout: ScreenLayout = {
|
||||
rows: options.rows ?? Math.ceil(bottom),
|
||||
slots: options.layout.map(({ slot, x, y, output_only, icon }) => ({
|
||||
index: slot,
|
||||
x,
|
||||
y,
|
||||
...(output_only ? { output: true } : {}),
|
||||
...(icon ? { icon } : {}),
|
||||
})),
|
||||
bars: bars.map(({ x, y, value, max, direction, empty_texture, full_texture }) => ({
|
||||
x,
|
||||
y,
|
||||
value,
|
||||
max,
|
||||
direction,
|
||||
empty_texture,
|
||||
full_texture,
|
||||
})),
|
||||
};
|
||||
|
||||
let open = true;
|
||||
const screen: OpenScreen = {
|
||||
container,
|
||||
layout,
|
||||
properties: {},
|
||||
filters,
|
||||
on_take,
|
||||
on_close: [() => open = false],
|
||||
};
|
||||
game.open_screen(server_player, screen);
|
||||
menu.on_close.unshift(() => open = false);
|
||||
game.open_menu(server_player, menu);
|
||||
|
||||
return {
|
||||
player,
|
||||
get open() {
|
||||
return open;
|
||||
},
|
||||
set_property(id, value) {
|
||||
if (typeof value !== "number" || !Number.isFinite(value)) {
|
||||
throw new Error(`property ${id} must be a number, not ${value}`);
|
||||
}
|
||||
screen.properties[id] = value;
|
||||
},
|
||||
update: not_yet(mod, "update", "custom screens, see docs/modding_plan.md"),
|
||||
close() {
|
||||
if (server_player.screen === screen) game.close_screen(server_player);
|
||||
if (server_player.menu === menu) game.close_menu(server_player);
|
||||
},
|
||||
on_close(fn) {
|
||||
if (!open) {
|
||||
run_guarded(mod, "a screen on_close", fn);
|
||||
guarded("a menu on_close", fn);
|
||||
return;
|
||||
}
|
||||
screen.on_close.push(() => run_guarded(mod, "a screen on_close", fn));
|
||||
menu.on_close.push(() => guarded("a menu on_close", fn));
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -722,6 +782,8 @@ function not_yet(mod: string, name: string, why: string) {
|
||||
}
|
||||
|
||||
const MAX_CONTAINER_SIZE = 256;
|
||||
// how far a player can be from a block whose menu they have open, like minecraft's stillValid
|
||||
const MENU_REACH = 8;
|
||||
// what a client may send on mod channels, see docs/modding/networking/index.md
|
||||
const MAX_MOD_MESSAGE_LENGTH = 16 * 1024;
|
||||
const MAX_MOD_MESSAGES_PER_SECOND = 60;
|
||||
@@ -772,9 +834,12 @@ function new_stack(item: ItemStack): EngineItemStack {
|
||||
return stack;
|
||||
}
|
||||
|
||||
// the engine container behind each one mods get, for menus' slots
|
||||
const engine_containers = new WeakMap<Container, EngineContainer>();
|
||||
|
||||
// an engine container as mods see it
|
||||
function container_api(game: GameServer, id: string, container: EngineContainer): Container {
|
||||
return {
|
||||
const api: Container = {
|
||||
id,
|
||||
size: container.size,
|
||||
get: (slot) => {
|
||||
@@ -794,6 +859,8 @@ function container_api(game: GameServer, id: string, container: EngineContainer)
|
||||
},
|
||||
on_change: (fn) => game.mods.watch_container(running_mod || "unknown", container, fn),
|
||||
};
|
||||
engine_containers.set(api, container);
|
||||
return api;
|
||||
}
|
||||
|
||||
function player_api(game: GameServer, player: ServerPlayer): Player {
|
||||
|
||||
Reference in New Issue
Block a user