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
+13 -167
View File
@@ -26,15 +26,17 @@ A mod is built from up to three layers. Most mods only need the first one or two
**Built:** the mod loader and `.bmod` files (packing, loading from `server_mods/`, sending players a copy without the
server and worldgen scripts), delivery to clients (hashed downloads, the confirm screen for other sites, credits), the
data layer (blocks, block models with variants, items, recipes including mods' own types, entities and entity models,
ores), server scripts (components, events, commands, timers, storage, recipes, containers, container screens, displayed
items), client scripts (item and recipe lookups, inventory overlays with `Graphics`), mod channels, worldgen features
and ores, the base game's block behavior as a mod, and world time.
ores), server scripts (components, events, commands, timers, storage, recipes, containers, displayed items), menus like
NeoForge's (menu types, slots, data, buttons and `still_valid` on the server, menu screens on the client, and the
player's inventory as menu 0), client scripts (item and recipe lookups, plain screens, inventory overlays, `Graphics` in
GUI pixels), mod channels, worldgen features and ores, the base game's block behavior and screens as a mod, and world
time. See [Menus](modding/inventories/menus.md) and [Screens](modding/gui/index.md).
The code is in `common/mod_loader.ts`, `common/mod_data.ts`, `common/mod_check.ts`, `common/bmod.ts`,
`server/game/mod_runtime.ts`, `server/load_bmods.ts`, `client/mods.ts` and `common/worldgen_loader.ts`.
`server/game/mod_runtime.ts`, `server/game/menu.ts`, `server/load_bmods.ts`, `client/mods.ts`, `client/menu.ts` and
`common/worldgen_loader.ts`.
**In the API types but throwing:** forms, custom screens, HUD elements, keybinds, `g.slider`, `g.slots`,
`register_terrain`.
**In the API types but throwing:** forms, HUD elements, keybinds, `g.slider`, `register_terrain`.
**Not built at all:** common scripts, the registry freeze and hash, overrides, engine access.
@@ -44,6 +46,7 @@ The code is in `common/mod_loader.ts`, `common/mod_data.ts`, `common/mod_check.t
- Mods can't throw a `CommandError` the server recognizes: it's checked with `instanceof`, and the class isn't in the
API, so a mod's bundled copy is a different class. Export it from `bworld/server`, or recognize it by `name`.
- Tools only lose durability when their owner dies. Nothing else uses it.
- The GUI scale is always 3: screens work in GUI pixels like NeoForge's, but there's no option to change it yet.
## Planned API
@@ -197,168 +200,11 @@ function hook<T, K extends keyof T>(target: T, method: K, handlers: {
confirm screen for other sites lists which mods use it.
- Prefer the Game API. Many mods hooking the same thing is a sign it should become part of the Game API.
### Menus
GUIs follow NeoForge's split between a menu and a screen. The **menu** lives on the server: which slots the player can
click, the numbers the screen shows, the buttons it has, and when it closes. The **screen** lives on the client and only
draws the menu and sends clicks. Every click is checked by the server's menu, so a modified client can only do what the
menu allows.
A menu type is registered in the server script with a function that builds each menu when it opens, like NeoForge's
`MenuType` and its server menu constructor:
```ts
// server.ts
const smelter = ctx.menus.register("copper_tools:smelter", (menu, block: BlockRef) => {
const container = ctx.containers.get(block.data.container)!;
// like Slot: x and y in GUI pixels from the screen's top left
menu.add_slot(container, 0, { x: 56, y: 17, filter: "smeltable" });
menu.add_slot(container, 1, { x: 56, y: 53, filter: "fuel" });
menu.add_slot(container, 2, { x: 116, y: 35, output_only: true });
menu.add_player_inventory(8, 84); // like addStandardInventorySlots
// like DataSlot: read every tick, and sent when it changes
menu.add_data("progress", () => block.data.progress);
menu.add_data("progress_max", () => 200);
// like stillValid with a ContainerLevelAccess: closes when the player is over 8 blocks away or the block is gone
menu.still_valid_near(block);
// like clickMenuButton
menu.on_button((player, id) => {
if (id === 0) ctx.containers.get(block.data.container)!.set(2, undefined);
});
menu.title = "Smelter";
});
// in the block's on_interact
smelter.open(player, block);
```
```ts
interface MenuBuilder {
title: string;
// sent to the client once when it opens, at most 16 KB of JSON, like IContainerFactory's extra data
open_data: unknown;
// slot fields are the same as container screens' layout (filter, output_only, on_take, icon). returns its index
add_slot(container: Container, slot: number, options: SlotOptions & { x: number; y: number }): number;
add_player_inventory(x: number, y: number): void;
add_data(name: string, get: () => number): void;
still_valid(check: (player: Player) => boolean): void; // checked every tick
still_valid_near(block: BlockRef): void;
on_button(fn: (player: Player, id: number) => void): void;
// shift clicks: return the slot indexes to try, in order. by default it's the container screens' rule
quick_move(fn: (player: Player, index: number) => number[]): void;
on_tick(fn: () => void): void;
on_close(fn: () => void): void;
}
interface MenuType<Args> {
readonly id: Id;
open(player: Player, args: Args): MenuHandle; // like openMenu with a MenuProvider
}
// what open_container returns today, without set_property and update: data comes from add_data
interface MenuHandle {
readonly player: Player;
readonly open: boolean;
close(): void;
on_close(fn: () => void): void;
}
```
- Slots, data and open data are the whole client side of a menu, like NeoForge's client menu built from what the server
sends. Data values are finite numbers, compared and sent at the end of each tick.
- Button ids are integers the client sends with `menu.click_button(id)`. They only come from the player who has the menu
open, and the menu decides what an id means. Anything richer, like typed text, goes through a mod channel (see below),
where the server checks the player has this menu open before trusting it.
- One menu is open per player. Opening another closes the first, and closing runs `on_close` whatever the reason.
- `ctx.ui.open_container` stays, as the menu the engine draws itself: its layout in slot units under the player's
inventory, like vanilla's chest screen. It needs no client code.
### Menu Screens
The client script gives each menu type a screen, like NeoForge's `RegisterMenuScreensEvent` and
`AbstractContainerScreen`. The engine places the screen in the middle, and draws its slots, the held item and item
tooltips. The screen draws everything else:
```ts
// client.ts
ctx.ui.register_menu_screen("copper_tools:smelter", (menu) => ({
width: 176, // like imageWidth and imageHeight
height: 166,
// under the slots. x and y are from the screen's top left, like leftPos and topPos
on_render_background(g) {
g.texture("copper_tools:smelter_gui", 0, 0, 176, 166);
g.texture("bworld:arrow_full", 79, 34, 24 * menu.data.progress / menu.data.progress_max, 17);
},
// over the slots
on_render_labels(g) {
g.text(menu.title, 8, 6);
if (g.button("Empty", 120, 60, 40, 16)) menu.click_button(0);
},
}));
```
```ts
interface ClientMenu {
readonly type: Id;
readonly title: string;
readonly open_data: unknown;
readonly data: Readonly<Record<string, number>>; // the server's latest values
click_button(id: number): void;
close(): void;
}
interface MenuScreen {
width: number;
height: number;
on_render_background?(g: Graphics): void;
on_render_labels?(g: Graphics): void;
on_tick?(): void; // like containerTick
}
```
A server opening a menu type its client has no screen for closes it again, with an error in the client's console naming
the mod. `check-mods` warns about menu types a mod registers without a screen.
### Plain Screens
For screens with no slots and nothing the server needs to check: maps, settings, a guide book. Like NeoForge's `Screen`,
they're only on the client, opened with `ctx.ui.open(screen)`:
```ts
// client.ts
ctx.ui.open({
on_init(width, height) {}, // opening, and whenever the window or GUI scale changes
on_render(g) {
g.panel(g.width / 2 - 100, 20, 200, 120);
g.text(`Smelted: ${smelted}`, g.width / 2 - 90, 30);
},
});
```
```ts
interface Screen {
on_init?(width: number, height: number): void;
on_tick?(): void; // 20 times a second
on_render(g: Graphics): void;
on_key?(key: KeyCode): boolean; // true when it used the key
should_close_on_escape?(): boolean; // true when not set
on_close?(): void; // the player closed it
on_removed?(): void; // it's gone for any reason, including another screen replacing it
}
```
There's no registry for them and no `open_screen` on the server. A server that wants a player to see one sends a mod
channel message, and the client script's handler opens it, like a NeoForge payload handler calling `setScreen`. A screen
that needs to tell the server something does it the same way.
Screens of every kind work in GUI pixels, like NeoForge's: the window's size divided by the GUI scale players pick in
the options, so `g.width` and `g.height` are in those pixels and a screen looks the same at any window size.
### Forms
NeoForge has nothing like Bedrock's server forms, but they're the quickest way to ask a player something, so they stay,
built on menus: each kind of form is a menu whose screen the engine provides, so they need no client code. The types are
in `common/mod_api/server.ts` (`message_form`, `action_form`, `modal_form`).
built on [menus](modding/inventories/menus.md): each kind of form is a menu whose screen the engine provides, so they
need no client code. The types are in `common/mod_api/server.ts` (`message_form`, `action_form`, `modal_form`).
```ts
const result = await ctx.ui.action_form(player, {
@@ -459,8 +305,8 @@ manifest and its checks, and making the engine hookable where mods will want it.
client and a worldgen method, hooks on one method run in priority order, and a throwing hook is logged without breaking
the game.
**Phase 4: the rest of the Game API.** Keybinds, menus and menu screens, plain screens, forms built on menus, and HUD
elements. More events: items picked up and dropped, containers clicked, players moving between chunks, entities.
**Phase 4: the rest of the Game API.** Keybinds, forms built on menus, and HUD elements. More events: items picked up
and dropped, containers clicked, players moving between chunks, entities.
**Phase 5: gameplay out of the engine.** Everything marked phase 5 in the table above moves into `mods/bworld`. Mining
rules become something `mods/bworld` sets, with the engine asking it on both sides so clients still predict breaking.