diff --git a/docs/modding/misc/commands.md b/docs/modding/misc/commands.md index b032321..b32e8af 100644 --- a/docs/modding/misc/commands.md +++ b/docs/modding/misc/commands.md @@ -3,8 +3,8 @@ Players run commands by typing `/name` in chat. Commands run on the server, and players see suggestions above the chat box as they type: Tab puts one in, Up and Down pick one. -bworld's commands work like Fabric's, which are Minecraft's Brigadier: a tree of literal words and typed arguments. Each -argument type reads its part of the text and suggests what could go there. +bworld's commands work like NeoForge's, which are Minecraft's Brigadier: a tree of literal words and typed arguments. +Each argument type reads its part of the text and suggests what could go there. ## Registering a Command diff --git a/docs/modding/resources/tags.md b/docs/modding/resources/tags.md index 7b94f54..2c92ec7 100644 --- a/docs/modding/resources/tags.md +++ b/docs/modding/resources/tags.md @@ -63,8 +63,8 @@ back. Depend on the mod whose tag you change, so your file loads after its. chec ## Choosing a Namespace - A tag for your own mod's behavior, like the blocks your drill can't break, goes in your namespace. -- A tag other mods would want to share, like a new metal's ingots, goes in the `c` namespace, like NeoForge's and - Fabric's shared tags. No mod owns `c`, so using it needs no dependency. +- A tag other mods would want to share, like a new metal's ingots, goes in the `c` namespace, like NeoForge's shared + tags. No mod owns `c`, so using it needs no dependency. - Tag names are plural (`planks`, `ingots`). Group them with folders, with a tag for the whole folder holding each tag in it: `#c:ingots` holds `#c:ingots/iron`, `#c:ingots/copper` and so on. diff --git a/docs/modding_plan.md b/docs/modding_plan.md index 2efc422..149dc9c 100644 --- a/docs/modding_plan.md +++ b/docs/modding_plan.md @@ -9,8 +9,9 @@ bworld's modding takes from three places: crafting, screens, commands and terrain) lives in `mods/bworld`, written against the same API as any other mod. - **Minecraft Bedrock add-ons: easy to start.** Most content is JSON, blocks and items get behavior from named components, scripts use before/after events and server-driven forms, and players get everything from the server. -- **Minecraft Java with Fabric: no ceiling.** A mod that needs to can reach into the engine itself and hook its methods, - like Fabric's mixins. +- **Minecraft Java with NeoForge: no ceiling.** Most mods get by with registries and events on a bus, with priorities + and cancelling. A mod that needs more can reach into the engine itself and hook its methods, like NeoForge's mixins + and access transformers. A mod is built from up to three layers. Most mods only need the first one or two: @@ -18,7 +19,7 @@ A mod is built from up to three layers. Most mods only need the first one or two | ----------------- | ---------------------------------------- | ---------------------------------------------------------------------- | ----------------------------------------- | | **Data** | Bedrock behavior packs | JSON in `blocks/`, `items/`, `models/`, `recipes/`, ... | Versioned by `format_version` | | **Game API** | Minetest's Lua API, Bedrock's Script API | TypeScript against `bworld/server`, `bworld/client`, `bworld/worldgen` | Only breaks with the game's major version | -| **Engine access** | Fabric's mixins | `import { engine, hook } from "bworld/engine"` | None, tied to one game version | +| **Engine access** | NeoForge's mixins | `import { engine, hook } from "bworld/engine"` | None, tied to one game version | ## Status @@ -196,10 +197,168 @@ function hook(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 { + 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>; // 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 -Simple dialogs defined by server code, like Bedrock's `@minecraft/server-ui`, drawn by the client in the game's style. -The types are in `common/mod_api/server.ts` (`message_form`, `action_form`, `modal_form`). +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`). ```ts const result = await ctx.ui.action_form(player, { @@ -210,27 +369,12 @@ const result = await ctx.ui.action_form(player, { if (!result.canceled && result.selection === 0) player.teleport(0, 100, 0); ``` -The promise resolves with `canceled: true` when the player closes the form, disconnects, or another screen replaces it. -The engine checks every response against the form's definition (types, ranges, option counts, text length) before mod -code sees it. - -### Custom Screens - -For what forms and containers can't do: maps, skill trees, minigames. The client script registers a screen, and the -server opens it with props: - -```ts -// client.ts -ctx.ui.register_screen("copper_tools:stats", (props) => new StatsScreen(ctx, props)); - -// server.ts -const screen = ctx.ui.open_screen(player, "copper_tools:stats", { smelted: 42 }); -screen.update({ smelted: 43 }); // calls on_props on the client -``` - -`ModScreen` has `on_open`, `on_tick`, `on_render(g)`, `on_props`, `on_close` and `on_escape`. Screens can show server -containers passed in `open_screen(..., { containers: { input } })` and drawn with `g.slots("input", layout, x, y)`, -still following the container rules. A client script can open its own screens with `ctx.ui.open(id, props)`. +- The form's definition is the menu's open data. A button press is a menu button with the button's index, and a modal + form sends its values with its submit button. +- The promise resolves with `canceled: true` when the menu closes any other way: the player closes it, disconnects, or + another menu replaces it. +- The engine checks every response against the form's definition (types, ranges, option counts, text length) before mod + code sees it. ### HUD, Keybinds and Mod Channels @@ -315,8 +459,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, forms, custom screens 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, 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 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. @@ -334,13 +478,12 @@ and a world saved before any of this plays exactly the same. ## Open Questions - **Client-only mods.** Should players install their own client mods (minimaps, UI tweaks) that work on any server, like - Fabric's? They'd need a separate kind of mod that can't add content or change the registry hash, and servers might - want to forbid them. + NeoForge's mods that only load on the client? They'd need a separate kind of mod that can't add content or change the + registry hash, and servers might want to forbid them. - **Client-side prediction for mods.** Block components only run on the server, so interacting with a mod block always waits a round trip. Components could get optional handlers that clients run to predict. - **Engine access versions.** Tying engine access mods to one minor version is simple but strict. - **Game API versions.** Only breaking with the game's major version needs deprecation warnings and a changelog. -- **Screen scaling.** The GUI works in raw canvas pixels. `Graphics` should probably use a UI scale players can change. - **Entities.** Mods can define simple animals, but there's no API for entity types with their own behavior, animations, or natural spawning. - **Hot reload.** Restarting the server and rejoining is the answer for now.