442 lines
17 KiB
TypeScript
442 lines
17 KiB
TypeScript
// what server scripts get, see docs/modding/
|
|
import type { EventSignal, Face, Id, ItemStack, ModInfo, Position, TagApi } from "./shared.ts";
|
|
import type { ArgumentType, CommandNode } from "../commands.ts";
|
|
|
|
export type { ArgumentType, CommandContext, CommandNode, Suggestion } from "../commands.ts";
|
|
|
|
export type { EventSignal, Face, Id, ItemStack, ModInfo, Position, TagApi, TagRegistry } from "./shared.ts";
|
|
|
|
export interface ServerContext {
|
|
mod: ModInfo;
|
|
components: ComponentRegistry; // only during setup
|
|
commands: CommandRegistry; // only during setup
|
|
events: { before: ServerBeforeEvents; after: ServerAfterEvents };
|
|
system: System;
|
|
world: ServerWorld;
|
|
players: PlayerList;
|
|
containers: ContainerApi;
|
|
items: ItemApi;
|
|
recipes: RecipeApi;
|
|
tags: TagApi;
|
|
menus: MenuRegistry;
|
|
ui: ServerUi;
|
|
net: ServerNet;
|
|
storage: ModStorage;
|
|
log(...args: unknown[]): void;
|
|
}
|
|
|
|
// components
|
|
|
|
export interface BlockRef {
|
|
readonly id: Id;
|
|
readonly x: number;
|
|
readonly y: number;
|
|
readonly z: number;
|
|
// tile data, any json value. saved with the world, never sent to clients
|
|
// deno-lint-ignore no-explicit-any
|
|
data: any;
|
|
}
|
|
|
|
// P is the component's params from the block json
|
|
// deno-lint-ignore no-explicit-any
|
|
export interface BlockComponent<P = any> {
|
|
on_create?(block: BlockRef, params: P): void;
|
|
on_break?(block: BlockRef, params: P, player: Player | undefined): void;
|
|
on_click?(block: BlockRef, params: P, player: Player): void;
|
|
// return true if it did something, so no block gets placed
|
|
on_interact?(block: BlockRef, params: P, player: Player): boolean;
|
|
on_tick?(block: BlockRef, params: P, dt: number): void;
|
|
on_second?(block: BlockRef, params: P, dt: number): void;
|
|
// every morning at 06:00, for every block with data in the world, near a player or not. day is ServerWorld.day
|
|
on_new_day?(block: BlockRef, params: P, day: number): void;
|
|
}
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
export interface ItemComponent<P = any> {
|
|
on_create?(item: ItemStack, params: P): void;
|
|
get_lore?(item: ItemStack, params: P): string;
|
|
on_use?(item: ItemStack, params: P, player: Player): void;
|
|
}
|
|
|
|
export interface ComponentRegistry {
|
|
register_block<P>(id: Id, component: BlockComponent<P>): void;
|
|
register_item<P>(id: Id, component: ItemComponent<P>): void;
|
|
}
|
|
|
|
// commands, brigadier's way like neoforge's: see common/commands.ts and docs/modding/misc/commands.md
|
|
|
|
// who ran a command
|
|
export interface CommandSource {
|
|
readonly player: Player;
|
|
// answers them in chat
|
|
send_message(text: string): void;
|
|
}
|
|
|
|
// the simple way: the words after the name, split on spaces
|
|
export interface Command {
|
|
description: string;
|
|
usage: string;
|
|
run(args: string[], player: Player): void;
|
|
}
|
|
|
|
export interface CommandRegistry {
|
|
// a tree of literal() and argument() nodes, like neoforge's dispatcher.register(literal("heal").then(...))
|
|
register(node: CommandNode<CommandSource>): void;
|
|
register(name: string, command: Command): void;
|
|
literal(name: string): CommandNode<CommandSource>;
|
|
argument<T>(name: string, type: ArgumentType<T, CommandSource>): CommandNode<CommandSource>;
|
|
// argument types. they parse their text and suggest what could go there as players type
|
|
types: {
|
|
integer(min?: number, max?: number): ArgumentType<number, CommandSource>;
|
|
number(min?: number, max?: number): ArgumentType<number, CommandSource>;
|
|
boolean(): ArgumentType<boolean, CommandSource>;
|
|
word(): ArgumentType<string, CommandSource>;
|
|
greedy_string(): ArgumentType<string, CommandSource>;
|
|
choice<T extends string>(options: readonly T[]): ArgumentType<T, CommandSource>;
|
|
// an item or block id. without a namespace, whichever has that name
|
|
item(): ArgumentType<Id, CommandSource>;
|
|
block(): ArgumentType<Id, CommandSource>;
|
|
// an online player's name
|
|
player(): ArgumentType<Player, CommandSource>;
|
|
};
|
|
}
|
|
|
|
// events
|
|
|
|
export interface Cancelable {
|
|
cancel: boolean;
|
|
}
|
|
|
|
export interface BlockBreakEvent {
|
|
readonly player: Player;
|
|
readonly block: BlockRef;
|
|
readonly item: ItemStack | undefined;
|
|
}
|
|
|
|
export interface BlockPlaceEvent {
|
|
readonly player: Player;
|
|
readonly block: { readonly id: Id } & Position;
|
|
readonly face: Face;
|
|
readonly item: ItemStack | undefined;
|
|
}
|
|
|
|
export interface BlockInteractEvent {
|
|
readonly player: Player;
|
|
readonly block: BlockRef;
|
|
readonly item: ItemStack | undefined;
|
|
}
|
|
|
|
export interface PlayerDamageEvent {
|
|
readonly player: Player;
|
|
// before events can change it
|
|
amount: number;
|
|
// what hurt them: "fall", "void", "starve", or what a mod passed to damage
|
|
readonly cause: string;
|
|
}
|
|
|
|
export interface PlayerDeathEvent {
|
|
readonly player: Player;
|
|
readonly cause: string;
|
|
}
|
|
|
|
export interface ChatSendEvent {
|
|
readonly player: Player;
|
|
message: string;
|
|
}
|
|
|
|
export interface ServerBeforeEvents {
|
|
block_break: EventSignal<BlockBreakEvent & Cancelable>;
|
|
block_place: EventSignal<BlockPlaceEvent & Cancelable>;
|
|
block_interact: EventSignal<BlockInteractEvent & Cancelable>;
|
|
chat_send: EventSignal<ChatSendEvent & Cancelable>;
|
|
player_damage: EventSignal<PlayerDamageEvent & Cancelable>;
|
|
}
|
|
|
|
export interface ServerAfterEvents {
|
|
block_break: EventSignal<BlockBreakEvent>;
|
|
block_place: EventSignal<BlockPlaceEvent>;
|
|
block_interact: EventSignal<BlockInteractEvent>;
|
|
chat_send: EventSignal<Readonly<ChatSendEvent>>;
|
|
player_join: EventSignal<{ readonly player: Player }>;
|
|
player_leave: EventSignal<{ readonly player: Player }>;
|
|
// they were hurt, after before handlers had their say. amount is what they took
|
|
player_damage: EventSignal<Readonly<PlayerDamageEvent>>;
|
|
// they drop what they carried and respawn right after
|
|
player_death: EventSignal<PlayerDeathEvent>;
|
|
server_start: EventSignal<Record<never, never>>;
|
|
tick: EventSignal<{ readonly dt: number }>;
|
|
// 06:00, after every block's on_new_day
|
|
new_day: EventSignal<{ readonly day: number }>;
|
|
}
|
|
|
|
// world and players
|
|
|
|
export interface ServerWorld {
|
|
get_block(x: number, y: number, z: number): Id | undefined; // undefined when the chunk isn't loaded
|
|
set_block(x: number, y: number, z: number, id: Id): boolean; // runs on_break / on_create, synced to everyone
|
|
// like breaking it by hand without a tool: on_break, then its drops. false when there was nothing to break
|
|
break_block(x: number, y: number, z: number): boolean;
|
|
get_state(x: number, y: number, z: number, name: string): number | undefined;
|
|
set_state(x: number, y: number, z: number, name: string, value: number): boolean;
|
|
get_block_data<T>(x: number, y: number, z: number): T | undefined;
|
|
// the item a block with item_display shows on top of it, like a block entity renderer: saved with the block's data
|
|
// and synced to players. undefined shows nothing. it goes away with the block, without dropping
|
|
set_block_item(x: number, y: number, z: number, item: ItemStack | undefined): void;
|
|
// a copy
|
|
get_block_item(x: number, y: number, z: number): ItemStack | undefined;
|
|
// pops an item out of the block at x y z, like a broken block's drops
|
|
drop_item(x: number, y: number, z: number, item: ItemStack): void;
|
|
// an entity from entities/ with its feet at x y z. throws for kinds that don't exist
|
|
spawn_entity(id: Id, x: number, y: number, z: number): EntityRef;
|
|
is_loaded(x: number, z: number): boolean;
|
|
readonly seed: string;
|
|
// ticks since 06:00 of day 1. a day is 36000 ticks (06:00 to 18:00), a night 12000
|
|
readonly time: number;
|
|
// days since the world started, going up at 06:00 each morning: 0 on the first day
|
|
readonly day: number;
|
|
set_time(time: number): void;
|
|
}
|
|
|
|
// an entity in the world. once it's removed, by a script or anything else, it stays removed and moving it does nothing
|
|
export interface EntityRef {
|
|
readonly id: string;
|
|
readonly type: Id;
|
|
readonly position: Readonly<Position>;
|
|
readonly removed: boolean;
|
|
teleport(x: number, y: number, z: number): void;
|
|
remove(): void;
|
|
}
|
|
|
|
export interface Player {
|
|
readonly id: string;
|
|
readonly name: string;
|
|
readonly position: Readonly<Position>;
|
|
readonly inventory: Container; // 36 slots, hotbar is 0-8
|
|
readonly selected_slot: number;
|
|
readonly held_item: ItemStack | undefined;
|
|
// what doesn't fit in the inventory drops at their feet
|
|
give_item(id: Id, count?: number, data?: unknown): void;
|
|
send_message(text: string): void;
|
|
// items drawn on them, under a key so each mod keeps its own list, like "bworld:curios". replaces what that key
|
|
// had, [] clears it. only items whose json has worn show. not saved: set it again when they join
|
|
set_worn(key: string, items: ItemStack[]): void;
|
|
teleport(x: number, y: number, z: number): void;
|
|
// health and hunger, like minecraft: 20 each, saturation is never above hunger. max_health is the
|
|
// bworld:max_health attribute
|
|
readonly health: number;
|
|
readonly max_health: number;
|
|
readonly food: number;
|
|
readonly saturation: number;
|
|
// cause is for events and the death message, like "fall"
|
|
damage(amount: number, cause?: string): void;
|
|
heal(amount: number): void;
|
|
// like eating: nutrition in hunger points, saturation_modifier like minecraft's (bread's is 0.6)
|
|
feed(nutrition: number, saturation_modifier: number): void;
|
|
// tires them out: 4 costs a point of saturation, or of hunger once it's gone
|
|
add_exhaustion(amount: number): void;
|
|
// their stats, like minecraft's attributes: "bworld:attack_damage", "bworld:attack_speed", "bworld:max_health"...
|
|
// see docs/modding/entities/players.md. modifiers aren't saved, they're gone when the player leaves
|
|
attribute(id: Id): number;
|
|
// replaces one with the same id on that attribute
|
|
add_modifier(attribute: Id, modifier: AttributeModifier): void;
|
|
// whether it had it
|
|
remove_modifier(attribute: Id, id: string): boolean;
|
|
// status effects, like minecraft's: "bworld:regeneration", "bworld:speed"... duration in ticks, -1 for forever.
|
|
// adding one they have merges them like minecraft: the stronger wins, and a weaker longer one waits under it
|
|
add_effect(id: Id, duration: number, options?: EffectOptions): void;
|
|
// the one they have of that type, undefined when they don't
|
|
effect(id: Id): { readonly amplifier: number; readonly duration: number } | undefined;
|
|
// whether they had it
|
|
remove_effect(id: Id): boolean;
|
|
}
|
|
|
|
export interface EffectOptions {
|
|
amplifier?: number; // level minus 1: 1 is regeneration II
|
|
ambient?: boolean; // fainter particles, like from a beacon
|
|
show_particles?: boolean;
|
|
show_icon?: boolean;
|
|
}
|
|
|
|
// like minecraft's: the base, plus every add_value, plus that times each add_multiplied_base, then times 1 + each
|
|
// add_multiplied_total
|
|
export interface AttributeModifier {
|
|
id: string;
|
|
amount: number;
|
|
operation: "add_value" | "add_multiplied_base" | "add_multiplied_total";
|
|
}
|
|
|
|
export interface PlayerList {
|
|
all(): Player[];
|
|
get(id: string): Player | undefined;
|
|
by_name(name: string): Player | undefined;
|
|
}
|
|
|
|
export interface System {
|
|
run_timeout(fn: () => void, ticks: number): number;
|
|
run_interval(fn: () => void, ticks: number): number;
|
|
clear_run(handle: number): void;
|
|
readonly current_tick: number;
|
|
}
|
|
|
|
// small key value store per mod, saved with the world
|
|
export interface ModStorage {
|
|
get<T>(key: string): T | undefined;
|
|
set(key: string, value: unknown): void; // json values only
|
|
delete(key: string): void;
|
|
}
|
|
|
|
// containers and recipes
|
|
|
|
export interface Container {
|
|
readonly id: string;
|
|
readonly size: number;
|
|
get(slot: number): ItemStack | undefined;
|
|
set(slot: number, item: ItemStack | undefined): void;
|
|
add(item: ItemStack): ItemStack | undefined; // returns what didn't fit
|
|
on_change(fn: (slot: number) => void): () => void;
|
|
}
|
|
|
|
export interface ContainerApi {
|
|
create(size: number): Container; // saved with the world
|
|
get(id: string): Container | undefined;
|
|
delete(id: string): void;
|
|
}
|
|
|
|
export interface ItemApi {
|
|
exists(id: Id): boolean;
|
|
// how many fit in one slot
|
|
max_stack(id: Id): number;
|
|
// the tool type it is, like "pickaxe" or "shovel", from the item's "tool"
|
|
tool(id: Id): string | undefined;
|
|
// the components its json lists and their params, a copy. a mod can read params of its components this way
|
|
// without a stack, like the curios slots do for bworld:curio_modifiers
|
|
components(id: Id): Record<Id, unknown>;
|
|
}
|
|
|
|
export interface RecipeApi {
|
|
furnace_result(input: Id): { output: ItemStack; cook_time: number } | undefined;
|
|
fuel_value(item: Id): number; // burn time in ticks, 0 if it isn't fuel
|
|
is_fuel(item: Id): boolean;
|
|
is_smeltable(item: Id): boolean;
|
|
// the smithing recipe for these items, whatever the material's count. addition is the optional third item
|
|
smithing(tool: Id, material: Id, addition?: Id): { result: Id; material_count: number } | undefined;
|
|
// every recipe of a mod's own type, like "farmersdelight:cooking", as its json. copies, in load order
|
|
// deno-lint-ignore no-explicit-any
|
|
list(type: Id): any[];
|
|
}
|
|
|
|
// forms, not built yet: menus whose screens the engine provides, see docs/modding_plan.md
|
|
|
|
export interface ActionForm {
|
|
title: string;
|
|
body?: string;
|
|
buttons: { text: string; icon?: Id }[];
|
|
}
|
|
|
|
export interface MessageForm {
|
|
title: string;
|
|
body: string;
|
|
buttons: [string, string];
|
|
}
|
|
|
|
export type FormField =
|
|
| { type: "toggle"; label: string; default?: boolean }
|
|
| { type: "slider"; label: string; min: number; max: number; step?: number; default?: number }
|
|
| { type: "dropdown"; label: string; options: string[]; default?: number }
|
|
| { type: "text"; label: string; placeholder?: string; default?: string; max_length?: number };
|
|
|
|
export interface ModalForm {
|
|
title: string;
|
|
fields: FormField[];
|
|
}
|
|
|
|
export type FormResult<T> = ({ canceled: true } & Partial<T>) | ({ canceled: false } & T);
|
|
|
|
// menus, like neoforge's: the server side of a gui, see docs/modding/inventories/menus.md
|
|
|
|
export type SlotFilter = "smeltable" | "fuel" | ((item: ItemStack) => boolean);
|
|
|
|
export interface SlotOptions {
|
|
// gui pixels from the screen's top left to the slot's 18 pixel frame
|
|
x: number;
|
|
y: number;
|
|
// what can be put in, checked on the server
|
|
filter?: SlotFilter;
|
|
// can only be taken from, all at once, like the furnace result
|
|
output_only?: boolean;
|
|
// runs when a player takes from an output_only slot, before they get it. false stops them taking it
|
|
on_take?: (item: ItemStack) => boolean;
|
|
// a texture drawn in the slot while it's empty, like minecraft's armor slots
|
|
icon?: Id;
|
|
}
|
|
|
|
// what a menu type's build function gets, like neoforge's server menu constructor
|
|
export interface MenuBuilder {
|
|
title: string;
|
|
// sent to the client once when it opens, any json up to 16 KB, like IContainerFactory's extra data
|
|
open_data: unknown;
|
|
// a slot of a container from ctx.containers, or of player.inventory. returns its menu slot index
|
|
add_slot(container: Container, slot: number, options: SlotOptions): number;
|
|
// the player's 36 slots, like addStandardInventorySlots: the rest of the inventory in 3 rows from x and y, the
|
|
// hotbar 58 pixels under them. added in inventory order, so inventory slot i is the returned index plus i
|
|
add_player_inventory(x: number, y: number): number;
|
|
// a number the screen shows, like DataSlot: read every tick and sent when it changes
|
|
add_data(name: string, get: () => number): void;
|
|
// checked every tick, false closes it
|
|
still_valid(check: (player: Player) => boolean): void;
|
|
// closes it when the player is over 8 blocks from the block, or it's been broken or replaced
|
|
still_valid_near(block: BlockRef): void;
|
|
// a button the client pressed, like clickMenuButton. any number can come, check it
|
|
on_button(fn: (player: Player, id: number) => void): void;
|
|
// shift clicks: the menu slot indexes the stack in this one tries, in order. the default rule when not set
|
|
quick_move(fn: (player: Player, index: number) => number[]): void;
|
|
on_tick(fn: () => void): void;
|
|
// when it closes for any reason: the player closed it, left, opened another, or it was closed
|
|
on_close(fn: () => void): void;
|
|
}
|
|
|
|
export interface MenuHandle {
|
|
readonly player: Player;
|
|
readonly open: boolean;
|
|
close(): void;
|
|
on_close(fn: () => void): void;
|
|
}
|
|
|
|
export interface MenuType<Args> {
|
|
readonly id: Id;
|
|
// like openMenu: builds a menu for the player with these args and opens it, closing whatever they had open
|
|
open(player: Player, args: Args): MenuHandle;
|
|
}
|
|
|
|
// the player's own inventory menu, see MenuRegistry.extend_player_menu
|
|
export interface PlayerMenuBuilder {
|
|
// x and y are from the inventory screen's top left, and can be outside it, like left of it with a negative x. the
|
|
// engine draws a panel behind slots outside its own. returns the menu slot index, from 46 on
|
|
add_slot(container: Container, slot: number, options: SlotOptions): number;
|
|
}
|
|
|
|
export interface MenuRegistry {
|
|
// adds slots to every player's own inventory menu, always there while they play, like the curios slots next to the
|
|
// inventory. build runs when each player joins. only during setup
|
|
extend_player_menu(build: (menu: PlayerMenuBuilder, player: Player) => void): void;
|
|
// like registering a MenuType. the client script gives it a screen with ctx.ui.register_menu_screen
|
|
register<Args = undefined>(
|
|
id: Id,
|
|
build: (menu: MenuBuilder, args: Args, player: Player) => void,
|
|
): MenuType<Args>;
|
|
}
|
|
|
|
export interface ServerUi {
|
|
message_form(player: Player, form: MessageForm): Promise<FormResult<{ selection: 0 | 1 }>>;
|
|
action_form(player: Player, form: ActionForm): Promise<FormResult<{ selection: number }>>;
|
|
modal_form(player: Player, form: ModalForm): Promise<FormResult<{ values: (boolean | number | string)[] }>>;
|
|
}
|
|
|
|
// mod channels
|
|
|
|
export interface ServerNet {
|
|
on<T = unknown>(channel: Id, handler: (player: Player, data: T) => void): void;
|
|
send(player: Player, channel: Id, data: unknown): void;
|
|
broadcast(channel: Id, data: unknown): void;
|
|
}
|