Change whole gui system

This commit is contained in:
2026-10-01 16:22:02 -03:00
parent 013a4fbb97
commit deba36260e
48 changed files with 1925 additions and 1302 deletions
+95 -152
View File
@@ -11,21 +11,10 @@ import {
TICKS_PER_SECOND,
} from "$/common/constants.ts";
import { BlockRegistry, EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import {
can_take_output,
click_slot,
Container,
ItemData,
ItemStack,
move_into,
room_for,
take_output,
} from "$/common/inventory.ts";
import { Container, ItemData, ItemStack } from "$/common/inventory.ts";
import {
AIR_ID,
ClientMessage,
ContainerKey,
CRAFTING_RESULT_SLOT,
MAX_CHAT_LENGTH,
MAX_NAME_LENGTH,
PROTOCOL_VERSION,
@@ -58,8 +47,9 @@ import {
word,
} from "$/common/commands.ts";
import type { GameLoop } from "./game_loop.ts";
import { consume_recipe_items, update_crafting_result } from "./crafting.ts";
import { INVENTORY_SIZE, OpenScreen, SavedPlayer, ServerPlayer } from "./player.ts";
import { update_crafting_result } from "./crafting.ts";
import type { Menu } from "./menu.ts";
import { SavedPlayer, ServerPlayer } from "./player.ts";
import { encode_skin, MAX_SKIN_BYTES, skin_problem } from "./skins.ts";
import { ServerWorld, Tile } from "./world.ts";
import type { WorldSave } from "./saves.ts";
@@ -274,7 +264,7 @@ export class GameServer {
if (!player) {
return;
}
this.#close_screen(player);
this.#close_menu(player);
this.#saved_players[player.name] = player.save_player();
this.world.dirty = true;
this.#players.delete(conn);
@@ -345,6 +335,7 @@ export class GameServer {
this.#tick_entities();
this.mods.tick();
this.#tick_menus();
this.mods.check_watched_containers();
for (const [conn, pending] of this.#pending) {
@@ -609,7 +600,7 @@ export class GameServer {
return { id, container };
}
// anyone looking at it gets their screen closed
// anyone with a menu showing it gets it closed
delete_container(id: string) {
const container = this.containers.get(id);
if (!container) {
@@ -618,29 +609,46 @@ export class GameServer {
this.containers.delete(id);
this.world.dirty = true;
for (const player of this.#players.values()) {
if (player.screen?.container === container) {
this.close_screen(player);
if (player.menu?.uses(container)) {
this.close_menu(player);
}
}
}
// screens
// menus
open_screen(player: ServerPlayer, screen: OpenScreen) {
this.#close_screen(player);
player.screen = screen;
this.#send(player, { type: "open_screen", layout: screen.layout, properties: { ...screen.properties } });
player.sent.set("properties", JSON.stringify(screen.properties));
player.sent.delete("screen");
// the id a new menu for this player gets, 1 to 100 and around, like minecraft's containerCounter
next_menu_id(player: ServerPlayer): number {
player.menu_id = player.menu_id % 100 + 1;
return player.menu_id;
}
// replaces whatever they had open
open_menu(player: ServerPlayer, menu: Menu) {
this.#close_menu(player);
player.menu = menu;
const data = menu.data_values();
this.#send(player, {
type: "open_menu",
id: menu.id,
menu_type: menu.type,
title: menu.title,
open_data: menu.open_data,
slots: menu.slot_info(),
data,
});
player.sent.set("menu_data", JSON.stringify(data));
player.sent.delete("menu_content");
}
// closes it for the player too, not just on the server
close_screen(player: ServerPlayer) {
if (!player.screen) {
close_menu(player: ServerPlayer) {
const menu = player.menu;
if (!menu) {
return;
}
this.#close_screen(player);
this.#send(player, { type: "close_screen" });
this.#close_menu(player);
this.#send(player, { type: "close_menu", id: menu.id });
}
// version 2 saves had chests and furnaces in the engine, with their fields in data and their items in
@@ -692,7 +700,13 @@ export class GameServer {
}
#join(conn: number, raw_name: unknown, view_distance: number) {
const player = new ServerPlayer(this.entities, conn, this.#unique_name(raw_name), view_distance);
const player = new ServerPlayer(
this.entities,
conn,
this.#unique_name(raw_name),
view_distance,
() => this.recipes.shaped,
);
const saved = this.#saved_players[player.name];
if (saved) {
player.load_player(saved);
@@ -784,13 +798,21 @@ export class GameServer {
}
break;
case "click":
this.#click(player, message.container, message.index, message.button, message.shift === true);
this.#click(player, message.menu, message.slot, message.button, message.shift === true);
break;
case "drop_item":
this.#drop_item(player, message.container, message.index, message.all === true);
this.#drop_item(player, message.menu, message.slot, message.all === true);
break;
case "close_screen":
this.#close_screen(player);
case "menu_button":
if (player.menu && message.menu === player.menu.id && is_int(message.button)) {
this.mods.menu_button(player, player.menu, message.button);
}
break;
case "close_menu":
// one that's already gone, the server closed it, or menu 0: the inventory screen
if (message.menu === (player.menu?.id ?? 0)) {
this.#close_menu(player);
}
break;
case "chat":
this.#chat(player, message.text);
@@ -989,113 +1011,31 @@ export class GameServer {
);
}
#drop_item(player: ServerPlayer, key: unknown, index: unknown, all: boolean) {
if (!is_int(index)) {
#drop_item(player: ServerPlayer, id: unknown, slot: unknown, all: boolean) {
const menu = player.container_menu;
if (id !== menu.id || !is_int(slot)) {
return;
}
const container = this.#get_container(player, key as ContainerKey);
// the crafting result isn't an item yet, minecraft crafts it first but that's not supported
if (
!container || index < 0 || index >= container.size || (key === "crafting" && index === CRAFTING_RESULT_SLOT)
) {
return;
}
const item = container.get_item(index);
if (!item) {
return;
}
const thrown = item.clone();
thrown.amount = all ? item.amount : 1;
item.amount -= thrown.amount;
container.set_item(index, item.amount > 0 ? item : undefined);
if (key === "crafting") {
update_crafting_result(container, this.recipes.shaped);
}
this.throw_item(player, thrown);
}
#click(player: ServerPlayer, key: unknown, index: unknown, button: unknown, shift: boolean) {
if (!is_int(index) || !is_int(button)) {
return;
}
const container = this.#get_container(player, key as ContainerKey);
if (!container || index < 0 || index >= container.size) {
return;
}
if (shift) {
this.#quick_move(player, key as ContainerKey, container, index);
} else if (key === "crafting" && index === CRAFTING_RESULT_SLOT) {
const result = container.get_item(CRAFTING_RESULT_SLOT);
if (result && take_output(result, player.cursor)) {
consume_recipe_items(container);
}
} else if (key === "screen" && player.screen?.layout.slots.find((s) => s.index === index)?.output) {
const item = container.get_item(index);
const allowed = (item: ItemStack) => player.screen?.on_take.get(index)?.(item) ?? true;
if (item && can_take_output(item, player.cursor) && allowed(item) && take_output(item, player.cursor)) {
container.set_item(index, undefined);
}
} else if (
key === "screen" && player.cursor.item && !(player.screen?.filters.get(index)?.(player.cursor.item) ?? true)
) {
// the slot doesn't take what the cursor holds
return;
} else {
click_slot(container, index, player.cursor, button);
}
if (key === "crafting") {
update_crafting_result(container, this.recipes.shaped);
}
if (key === "screen" || (shift && player.screen)) {
const thrown = menu.drop(slot, all);
if (thrown) {
this.throw_item(player, thrown);
this.world.dirty = true;
}
}
// shift click, like minecraft's quick move: the stack goes to the other part of the screen, as much as fits. with a
// container open that's between it and the inventory, otherwise between the hotbar and the rest of the inventory
#quick_move(player: ServerPlayer, key: ContainerKey, container: Container, index: number) {
const inventory = player.inventory;
const hotbar = Array.from({ length: 9 }, (_, i) => i);
const main = Array.from({ length: INVENTORY_SIZE - 9 }, (_, i) => i + 9);
// like minecraft, what comes out of a container fills the hotbar from the right first
const from_container = [...main, ...hotbar].reverse();
const item = container.get_item(index);
if (!item) {
// clicks for a menu that's already closed are ignored, like minecraft's
#click(player: ServerPlayer, id: unknown, slot: unknown, button: unknown, shift: boolean) {
const menu = player.container_menu;
if (id !== menu.id || !is_int(slot) || !is_int(button) || slot < 0 || slot >= menu.slots.length) {
return;
}
if (key === "crafting" && index === CRAFTING_RESULT_SLOT) {
// crafts as many as fit, while the grid keeps making the same thing
const id = item.type_id;
let result: ItemStack | undefined = item;
while (result?.type_id === id && room_for(result, inventory, from_container) >= result.amount) {
move_into(result.clone(), inventory, from_container);
consume_recipe_items(container);
update_crafting_result(container, this.recipes.shaped);
result = container.get_item(CRAFTING_RESULT_SLOT);
}
} else if (key === "crafting") {
move_into(item, inventory, [...main, ...hotbar]);
} else if (key === "screen") {
const screen = player.screen!;
const output = screen.layout.slots.find((s) => s.index === index)?.output;
if (!output) {
move_into(item, inventory, from_container);
} else if (
room_for(item, inventory, from_container) >= item.amount && (screen.on_take.get(index)?.(item) ?? true)
) {
move_into(item.clone(), inventory, from_container);
container.set_item(index, undefined);
}
} else if (player.screen) {
const screen = player.screen;
const inputs = screen.layout.slots.filter((s) => !s.output).map((s) => s.index);
move_into(item, screen.container, inputs, (i) => screen.filters.get(i)?.(item) ?? true);
if (shift) {
menu.quick_move(slot);
} else {
move_into(item, inventory, index < 9 ? main : hotbar);
menu.click(slot, button, player.cursor);
}
if (menu !== player.inventory_menu) {
this.world.dirty = true;
}
}
@@ -1346,12 +1286,12 @@ export class GameServer {
.finally(() => player.downloading_skin = false);
}
#close_screen(player: ServerPlayer) {
const screen = player.screen;
player.screen = undefined;
for (const fn of screen?.on_close ?? []) fn();
player.sent.delete("screen");
player.sent.delete("properties");
#close_menu(player: ServerPlayer) {
const menu = player.menu;
player.menu = undefined;
for (const fn of menu?.on_close ?? []) fn();
player.sent.delete("menu_content");
player.sent.delete("menu_data");
// the crafting grid and whatever the cursor holds go back into the inventory
for (let i = 0; i < 9; i++) {
@@ -1400,7 +1340,7 @@ export class GameServer {
#kill(player: ServerPlayer, source: DamageSource) {
const cause = source.cause;
const { x, y, z } = player;
this.close_screen(player);
this.close_menu(player);
for (let i = 0; i < player.inventory.size; i++) {
const item = player.inventory.get_item(i);
if (item) {
@@ -1555,14 +1495,16 @@ export class GameServer {
// helpers
#get_container(player: ServerPlayer, key: ContainerKey): Container | undefined {
switch (key) {
case "inventory":
return player.inventory;
case "crafting":
return player.crafting;
case "screen":
return player.screen?.container;
// the open menus' still_valid and on_tick
#tick_menus() {
for (const player of this.#players.values()) {
const menu = player.menu;
if (!menu) continue;
if (!menu.still_valid.every((valid) => valid())) {
this.close_menu(player);
continue;
}
for (const fn of menu.on_tick) fn();
}
}
@@ -1590,11 +1532,12 @@ export class GameServer {
const attack_speed = player.attribute(ATTRIBUTES.attack_speed);
sync("attack_speed", attack_speed, () => ({ type: "attack_speed", value: attack_speed }));
if (player.screen) {
const items = player.screen.container.to_synced_data();
sync("screen", items, () => ({ type: "container", container: "screen", items }));
const properties = { ...player.screen.properties };
sync("properties", properties, () => ({ type: "screen_properties", properties }));
const menu = player.menu;
if (menu) {
const items = menu.content();
sync("menu_content", items, () => ({ type: "menu_content", id: menu.id, items }));
const data = menu.data_values();
sync("menu_data", data, () => ({ type: "menu_data", id: menu.id, data }));
}
}