220 lines
9.0 KiB
TypeScript
220 lines
9.0 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// what client scripts get, see docs/modding/gui/index.md
|
|
import type { Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi } from "./shared.ts";
|
|
import type { LootTableJson } from "../loot.ts";
|
|
|
|
export type { Id, ItemStack, KeyCode, ModInfo, Position, Season, 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 };
|
|
// the player's settings, read only, like minecraft's Options. a screen closes on key_inventory like the inventory,
|
|
// the way jei's recipes screen checks options.keyInventory
|
|
options: KeyMappings;
|
|
net: ClientNet;
|
|
// is_swimming: lying flat in water in the 0.8 tall box, like minecraft's isSwimming. is_sneaking: holding the sneak
|
|
// key, like isShiftKeyDown. biome and season are where their feet are, undefined until that chunk loads
|
|
player: {
|
|
readonly name: string;
|
|
readonly position: Readonly<Position>;
|
|
readonly biome: Id | undefined;
|
|
readonly season: Season | undefined;
|
|
is_swimming(): boolean;
|
|
is_sneaking(): boolean;
|
|
};
|
|
// read only, what this client sees
|
|
world: {
|
|
get_block(x: number, y: number, z: number): Id | undefined;
|
|
// a field of the block's client data, what server scripts set with BlockRef.set_client_data, a copy.
|
|
// undefined when it isn't set or the chunk isn't loaded
|
|
get_client_data<T>(x: number, y: number, z: number, field: string): T | undefined;
|
|
// the column's biome and its season, undefined when the chunk isn't loaded
|
|
biome_at(x: number, z: number): Id | undefined;
|
|
season_at(x: number, z: number): Season | 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;
|
|
// the components its json lists and their params, a copy, like the server's ctx.items.components. for showing
|
|
// what a component means, like when a fish can be caught
|
|
components(id: Id): Record<Id, unknown>;
|
|
};
|
|
// every block any mod registered
|
|
blocks: {
|
|
exists(id: Id): boolean;
|
|
// the components its json lists and their params, a copy, like the server's ctx.blocks.components. for showing
|
|
// what a component does, like what a crab pot catches
|
|
components(id: Id): Record<Id, unknown>;
|
|
};
|
|
// the mods' loot table files (see docs/modding/resources/loot_tables.md), to show what they give. tables server
|
|
// scripts make with ctx.loot.register_table aren't here
|
|
loot: {
|
|
// the table's json, a copy, or undefined when no mod has a file for it
|
|
table(id: Id): LootTableJson | undefined;
|
|
};
|
|
// 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;
|
|
}
|
|
|
|
// the keys the player has bound, like the key mappings in minecraft's Options
|
|
export interface KeyMappings {
|
|
readonly key_forward: KeyCode;
|
|
readonly key_back: KeyCode;
|
|
readonly key_left: KeyCode;
|
|
readonly key_right: KeyCode;
|
|
readonly key_jump: KeyCode;
|
|
readonly key_sprint: KeyCode;
|
|
readonly key_sneak: KeyCode;
|
|
readonly key_inventory: KeyCode;
|
|
readonly key_drop: KeyCode;
|
|
readonly key_chat: KeyCode;
|
|
}
|
|
|
|
// 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>>;
|
|
// what's in one of its slots, by menu slot index, as the server last synced it. a copy
|
|
item(slot: number): ItemStack | undefined;
|
|
// 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;
|
|
// darkens the world behind it, true when not set. false draws it right over the world, like stardew valley's
|
|
// fishing minigame
|
|
dim_background?: boolean;
|
|
// mods' inventory overlays, like the recipe viewer's item list, show on it. true when not set
|
|
inventory_overlays?: boolean;
|
|
}
|
|
|
|
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;
|
|
// a progress bar: filled (0 to 1) of full drawn over empty, from the left, or from the bottom going "up"
|
|
progress(
|
|
empty: Id,
|
|
full: Id,
|
|
x: number,
|
|
y: number,
|
|
w: number,
|
|
h: number,
|
|
filled: number,
|
|
direction?: "right" | "up",
|
|
): 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;
|
|
}
|