// what server scripts get, see docs/modding/
import type { EventSignal, Face, Id, ItemStack, ModInfo, Position, Season, TagApi } from "./shared.ts";
import type { ArgumentType, CommandNode } from "../commands.ts";
import type { LootTableJson } from "../loot.ts";
export type { ArgumentType, CommandContext, CommandNode, Suggestion } from "../commands.ts";
export type { EventSignal, Face, Id, ItemStack, ModInfo, Position, Season, 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;
blocks: BlockApi;
recipes: RecipeApi;
tags: TagApi;
loot: LootApi;
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;
// the biome of its column and that biome's season, see ServerWorld.biome_at
readonly biome: Id;
readonly season: Season;
// 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
{
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;
// placing and neighbor rules are in shared scripts, see BlockRules in bworld/shared
}
// deno-lint-ignore no-explicit-any
export interface ItemComponent
{
on_create?(item: ItemStack, params: P): void;
get_lore?(item: ItemStack, params: P): string;
on_use?(item: ItemStack, params: P, player: Player): void;
}
// P is the component's params from the effect json. see docs/modding/entities/effects.md
// deno-lint-ignore no-explicit-any
export interface EffectComponent
{
// whether on_tick runs this tick, like neoforge's shouldApplyEffectTickThisTick. ticks is what's left of the
// effect, or how long it's been on for ones that last forever. every tick when not set
should_tick?(ticks: number, amplifier: number, params: P): boolean;
// like neoforge's applyEffectTick. returning false takes the effect off. instant effects run it once, when added
on_tick?(entity: LivingRef, amplifier: number, params: P): boolean | void;
}
// a player or a mob, what effects work on
export interface LivingRef {
readonly id: string;
readonly health: number;
readonly max_health: number;
// cause is for events and the death message, like "magic"
damage(amount: number, cause?: string): void;
heal(amount: number): void;
}
export interface ComponentRegistry {
register_block
(id: Id, component: BlockComponent
): void;
register_item
(id: Id, component: ItemComponent
): void;
register_effect
(id: Id, component: EffectComponent
): 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): void;
register(name: string, command: Command): void;
literal(name: string): CommandNode;
argument(name: string, type: ArgumentType): CommandNode;
// argument types. they parse their text and suggest what could go there as players type
types: {
integer(min?: number, max?: number): ArgumentType;
number(min?: number, max?: number): ArgumentType;
boolean(): ArgumentType;
word(): ArgumentType;
greedy_string(): ArgumentType;
choice(options: readonly T[]): ArgumentType;
// an item or block id. without a namespace, whichever has that name
item(): ArgumentType;
block(): ArgumentType;
// an online player's name
player(): ArgumentType;
};
}
// 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;
// where on the clicked block the player clicked, 0 to 1 on each axis
readonly hit: readonly [number, number, number];
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;
block_place: EventSignal;
block_interact: EventSignal;
chat_send: EventSignal;
player_damage: EventSignal;
}
export interface ServerAfterEvents {
block_break: EventSignal;
block_place: EventSignal;
block_interact: EventSignal;
chat_send: EventSignal>;
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>;
// they drop what they carried and respawn right after
player_death: EventSignal;
server_start: EventSignal>;
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;
// whether the block's side is a full square things can attach to, like minecraft's isFaceSturdy: its collision
// shape covers the whole face. false for air, plants, water and the sides of a slab
is_face_sturdy(x: number, y: number, z: number, face: Face): boolean;
get_block_data(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;
// the first block on the line from one point to another, like minecraft's Level.clip. it hits what entities bump
// into, so it goes through plants and other blocks without collision. undefined when nothing is in the way
raycast(from: Position, to: Position, options?: RaycastOptions): RaycastHit | undefined;
is_loaded(x: number, z: number): boolean;
// the column's biome, like "bworld:plains", and that biome's season. in chunks that aren't loaded it's what
// generation makes there, which takes a while the first time
biome_at(x: number, z: number): Id;
season_at(x: number, z: number): Season;
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;
// the time on a 24 hour clock, in hours: 14.5 is 14:30. hours go by faster at night, see common/time.ts
readonly hour: number;
set_time(time: number): void;
}
export interface RaycastOptions {
// like minecraft's ClipContext.Fluid: "none" (the default) goes through liquids, "source_only" hits their sources
// and goes through where they flow, "any" hits all of them. a liquid is hit at its surface
liquids?: "none" | "source_only" | "any";
}
export interface RaycastHit {
// the block it hit, and where that is
readonly id: Id;
readonly x: number;
readonly y: number;
readonly z: number;
// the side it went in by
readonly face: Face;
// the exact point it hit
readonly position: Readonly;
}
// 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;
// the biome where its feet are and that biome's season
readonly biome: Id;
readonly season: Season;
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;
// where they see from, lower while swimming, and which way they look as a vector 1 long. for aiming, like
// ctx.world.raycast(player.eye_position, ...)
readonly eye_position: Readonly;
readonly look_direction: Readonly;
// the biome where their feet are and that biome's season
readonly biome: Id;
readonly season: Season;
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, any mod's, like the base game's "bworld:regeneration". 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;
// lying flat in water in the 0.8 tall box, like minecraft's isSwimming. their client says when
is_swimming(): 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(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;
// every item, in load order
ids(): Id[];
// 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;
// what one of it sells for, from the item's "base_sell_value", or undefined if it can't be sold
sell_value(id: Id): number | 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;
}
export interface BlockApi {
exists(id: Id): boolean;
// the components its json lists and their params, a copy, like ItemApi.components
components(id: Id): Record;
}
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[];
}
// loot tables, see docs/modding/resources/loot_tables.md
// what's happening when a table is rolled. its built in conditions read block, tool and killed_by_player, mods'
// conditions and functions get all of it
export interface LootContext {
// where it happens, like the broken block or the fishing hook
position?: Position;
// the block being broken. its states are read from the world when not given
block?: { id: Id; x: number; y: number; z: number; states?: Record };
// what it's done with, like the tool breaking the block or the fishing rod
tool?: ItemStack;
// who did it: broke the block, fished, killed the mob
player?: Player;
// the mob that died
entity?: EntityRef;
// a player killed it, for the "killed_by_player" condition
killed_by_player?: boolean;
}
// a condition's or function's json, with its "condition" or "function" id and whatever params it has
// deno-lint-ignore no-explicit-any
export type LootParams = Record;
export interface LootApi {
exists(id: Id): boolean;
// what rolling the table gives, stacks split to fit a slot. throws for a table that doesn't exist
roll(id: Id, context?: LootContext): ItemStack[];
// rolls the table into the container's empty slots, at random like minecraft's chests. returns what didn't fit
fill(container: Container, id: Id, context?: LootContext): ItemStack[];
// a condition tables can use by its id, in the mod's namespace. only during setup
register_condition(id: Id, condition: (params: LootParams, context: LootContext) => boolean): void;
// a function tables can use by its id: the item changed, or undefined to take it away. only during setup
register_function(
id: Id,
fn: (item: ItemStack, params: LootParams, context: LootContext) => ItemStack | undefined,
): void;
// changes what a table gives every time it's rolled, or every table's with "*", like neoforge's global loot
// modifiers. return the new items, or change the list. only during setup
modify(table: Id | "*", fn: (items: ItemStack[], context: LootContext, table: Id) => ItemStack[] | void): void;
// a table made by the script instead of a file, like the base game's fishing table built from its fish. the json
// is what a file's "loot_table" is, the id is in the mod's namespace. only during setup
register_table(id: Id, table: Omit): void;
}
// 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 = ({ canceled: true } & Partial) | ({ 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 {
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(
id: Id,
build: (menu: MenuBuilder, args: Args, player: Player) => void,
): MenuType;
}
export interface ServerUi {
message_form(player: Player, form: MessageForm): Promise>;
action_form(player: Player, form: ActionForm): Promise>;
modal_form(player: Player, form: ModalForm): Promise>;
}
// mod channels
export interface ServerNet {
on(channel: Id, handler: (player: Player, data: T) => void): void;
send(player: Player, channel: Id, data: unknown): void;
broadcast(channel: Id, data: unknown): void;
}