Recipe viewer

This commit is contained in:
2026-09-27 01:25:47 -03:00
parent e4c9a2f7f4
commit 7f4a8719de
12 changed files with 849 additions and 13 deletions
+35 -3
View File
@@ -905,11 +905,37 @@ interface ClientContext {
net: ClientNet; // see Mod channels
player: { readonly name: string; readonly position: { x: number; y: number; z: number } };
world: { get_block(x: number, y: number, z: number): string | undefined }; // read only, what the client sees
// every item any mod registered, in load order: ids(), exists(id), name(id) ("Cooked Rice"), mod(id) (its mod's name)
items: ClientItems;
// every recipe as its json, including mods' own types, in load order
recipes: { list(type?: string): RecipeJson[] };
log(...args: unknown[]): void;
}
```
Only `ctx.player`, `ctx.world` and `ctx.log` work so far, the rest is phase 4.
`ctx.player`, `ctx.world`, `ctx.items`, `ctx.recipes`, `ctx.log` and `ctx.ui.add_inventory_overlay` work so far, the rest
is phase 4.
### Inventory overlays
`ctx.ui.add_inventory_overlay` draws on every inventory screen (the player's inventory, chests, furnaces and other
container screens) with [`Graphics`](#3-custom-screens-client-code), over the screen, like Just Enough Items' item list.
`mods/recipe_viewer` is one.
```ts
ctx.ui.add_inventory_overlay({
on_render(g, screen) {
// screen is the inventory's box, and what's in the slot under the mouse
const x = screen.x + screen.width + 16;
g.text(screen.hovered_item ? ctx.items.name(screen.hovered_item.id) : "", x, 16);
},
});
```
Overlays get the mouse before the slots. A click they use (`g.button`, or `g.consume_click()`) doesn't reach the slots,
and `g.block_mouse(x, y, w, h)` keeps the slots from taking the mouse under something drawn over them. While a
`g.text_input` has the keyboard, keybinds like the inventory key don't fire. An overlay that throws is removed, with the
error in the console.
Client scripts are for presentation. They can't change the world or inventories; they ask the server over a
[mod channel](#mod-channels). Anything they show should be treated as a view of the server's state. Everything in a
@@ -1102,7 +1128,7 @@ interface ModScreen {
interface Graphics {
readonly width: number;
readonly height: number;
readonly mouse: { x: number; y: number; down: boolean; pressed: boolean };
readonly mouse: { x: number; y: number; down: boolean; pressed: boolean; right_pressed: boolean };
rect(x: number, y: number, w: number, h: number, color?: [number, number, number, number]): void;
panel(x: number, y: number, w: number, h: number): void; // nine slice background
text(text: string, x: number, y: number, options?: { scale?: number; color?: number[] }): void;
@@ -1115,10 +1141,16 @@ interface Graphics {
slider(id: string, x: number, y: number, w: number, min: number, max: number): number;
// server synced slots, same behavior as container screens
slots(container: string, layout: { slot: number; x: number; y: number }[], x: number, y: number): void;
key_pressed(key: KeyCode): boolean;
key_pressed(key: KeyCode): boolean; // false while a text input has the keyboard
tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number): void; // over everything
item_tooltip(id: string, x: number, y: number, extra?: (string | { text: string; color?: Color })[]): void;
consume_click(): void; // nothing under it gets the click
block_mouse(x: number, y: number, w: number, h: number): void; // the screen underneath ignores the mouse there
}
```
Everything but `slider` and `slots` works, for [inventory overlays](#inventory-overlays).
Custom screens can also show server containers. The server passes them in when opening:
`ctx.ui.open_screen(player, id, props, { containers: { input: container } })`, and the screen draws them with
`g.slots("input", layout, x, y)`. Clicks still go to the server and follow the container rules, so a custom screen can't