Files
bworld/common/mod_api/client.ts
T
2026-10-02 11:07:09 -03:00

151 lines
6.1 KiB
TypeScript

// what client scripts get, see docs/modding/gui/index.md
import type { Id, ItemStack, KeyCode, ModInfo, Position, TagApi } from "./shared.ts";
export type { Id, ItemStack, KeyCode, ModInfo, Position, TagApi, TagRegistry } from "./shared.ts";
export interface ClientContext {
mod: ModInfo;
ui: ClientUi;
hud: HudRegistry;
input: { bind(id: Id, default_key: KeyCode, on_press: () => void): void };
net: ClientNet;
// is_swimming: lying flat in water in the 0.8 tall box, like minecraft's isSwimming
player: { readonly name: string; readonly position: Readonly<Position>; is_swimming(): boolean };
// read only, what this client sees
world: {
get_block(x: number, y: number, z: number): Id | undefined;
// ticks since 06:00 of day 1, see ServerWorld.time
readonly time: number;
};
// every item any mod registered, in load order
items: {
ids(): Id[];
exists(id: Id): boolean;
// what players see, like "Cooked Rice"
name(id: Id): string;
// the mod that registered it
mod(id: Id): string;
};
// every recipe as its json (see docs/modding/resources/recipes.md), including mods' own types, in load order. copies
recipes: {
// deno-lint-ignore no-explicit-any
list(type?: Id): any[];
};
// every mod's block, item and entity tags
tags: TagApi;
log(...args: unknown[]): void;
}
// a screen with no menu, only on this client, like neoforge's Screen. see docs/modding/gui/index.md
export interface Screen {
// when it opens, and whenever the window changes size. in gui pixels
on_init?(width: number, height: number): void;
// 20 times a second
on_tick?(): void;
on_render(g: Graphics): void;
// true when not set
should_close_on_escape?(): boolean;
// the player closed it, with escape
on_close?(): void;
// it's gone for any reason, including another screen replacing it
on_removed?(): void;
}
// the client side of a menu the server opened, see docs/modding/inventories/menus.md
export interface ClientMenu {
readonly type: Id;
readonly title: string;
// what the server's build function set, sent once
readonly open_data: unknown;
// the server's latest values of its add_data numbers
readonly data: Readonly<Record<string, number>>;
// like clickMenuButton, the server's on_button decides what it does
click_button(id: number): void;
close(): void;
}
// draws a menu, like neoforge's AbstractContainerScreen. the engine puts it in the middle of the window, and draws
// the menu's slots, the item on the cursor and item tooltips. g's 0, 0 is the screen's top left
export interface MenuScreen {
// in gui pixels, like imageWidth and imageHeight
width: number;
height: number;
// under the slots
on_render_background?(g: Graphics): void;
// over the slots
on_render_labels?(g: Graphics): void;
// 20 times a second, like containerTick
on_tick?(): void;
}
export type Color = [number, number, number, number];
// immediate mode drawing, like the debug ui, styled with the gui sprites (assets/sprites/gui.png). everything is in
// gui pixels, like neoforge's: 18 of them are a slot
export interface Graphics {
readonly width: number;
readonly height: number;
readonly mouse: { x: number; y: number; down: boolean; pressed: boolean; right_pressed: boolean };
rect(x: number, y: number, w: number, h: number, color?: Color): void;
panel(x: number, y: number, w: number, h: number): void;
// how tall a line of text is at scale 1, the game's text size
readonly text_height: number;
text(text: string, x: number, y: number, options?: { scale?: number; color?: Color }): void;
measure_text(text: string, scale?: number): number;
// part is the part of the texture to draw, [u, v, width, height] from 0 to 1, like a progress bar's filled part
texture(id: Id, x: number, y: number, w: number, h: number, part?: [number, number, number, number]): void;
item(id: Id, x: number, y: number, count?: number): void;
clip(x: number, y: number, w: number, h: number, draw: () => void): void;
button(label: string, x: number, y: number, w: number, h: number): boolean;
text_input(id: string, x: number, y: number, w: number): string;
slider(id: string, x: number, y: number, w: number, min: number, max: number): number;
// false while a text input has the keyboard
key_pressed(key: KeyCode): boolean;
// shown next to the mouse over everything else, the last one this frame wins
tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number): void;
// the game's tooltip for an item (its name, food, lore), with extra lines under it
item_tooltip(id: Id, x: number, y: number, extra?: (string | { text: string; color?: Color })[]): void;
// the click was used, so nothing under it (like slots) gets it
consume_click(): void;
// the screen underneath ignores the mouse over this area this frame, for something drawn over it
block_mouse(x: number, y: number, w: number, h: number): void;
}
// drawn on every menu's screen (the player's inventory, chests, furnaces...), over it, like just enough items' item
// list. it gets the screen's box to lay itself out around
export interface InventoryOverlay {
on_render(g: Graphics, screen: InventoryScreenView): void;
// the screen closed for any reason, like the player closing their inventory
on_close?(): void;
}
export interface InventoryScreenView {
// the open menu's type, like "bworld:furnace". undefined on the player's own inventory
readonly menu_type: Id | undefined;
readonly x: number;
readonly y: number;
readonly width: number;
readonly height: number;
// what's in the slot under the mouse
readonly hovered_item: ItemStack | undefined;
}
export interface ClientUi {
// the screen for a menu type of this mod, like RegisterMenuScreensEvent. called each time one opens
register_menu_screen(type: Id, create: (menu: ClientMenu) => MenuScreen): void;
// a screen that doesn't involve the server, like a settings page, over whatever's open
open(screen: Screen): void;
// closes the screen on top
close(): void;
add_inventory_overlay(overlay: InventoryOverlay): void;
}
export interface HudRegistry {
register(id: Id, element: { on_render(g: Graphics): void }): void;
}
export interface ClientNet {
on<T = unknown>(channel: Id, handler: (data: T) => void): void;
send(channel: Id, data: unknown): void;
}