Files
2026-11-22 07:44:13 -03:00

266 lines
13 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// types every side of a mod shares. these are the api mods see, not the engine's own classes:
// items are { id, count, data } here, like on the network
export type Id = string;
// a biome's season, from its json. see docs/modding/worldgen/biomes.md
export type Season = "spring" | "summer" | "autumn" | "winter";
export interface ItemStack {
readonly id: Id;
count: number;
// any json value, set by server phases
data?: unknown;
}
// a fluid with its data, like the juice in a bottle, see docs/modding/resources/fluids.md. it has no amount: whatever
// holds fluid keeps that beside it, like a tank's { fluid, amount }
export interface FluidVariant {
readonly id: Id;
// any json value, which tells fluids of one id apart, like which fruit a juice is from
readonly data?: unknown;
}
// an amount of a fluid in mB, like neoforge's FluidStack: what's in a tank, or what's poured from one into another
export interface FluidStack {
fluid: FluidVariant;
amount: number;
}
// ctx.fluids in server and client phases: every fluid any mod defines, the same on the server and every client
export interface FluidApi {
// every fluid's id, in load order, like ctx.items.ids. each without data, which a fluid like juice has many of
ids(): Id[];
exists(id: Id): boolean;
// what players see, from the fluid's rules for its data, or else its json
name(fluid: FluidVariant): string;
// a color like "#c42d39", from the fluid's rules for its data, or else its json
color(fluid: FluidVariant): string;
// whether they're one fluid: the same id, and the same data whatever order its keys are in. no data is the same as
// {}, so a fluid whose data was emptied is the plain fluid again
same(a: FluidVariant, b: FluidVariant): boolean;
}
// what a fluid's data makes it, registered by a mod's shared phase, like neoforge's FluidType.getDescription and
// FluidTintSource.colorAsStack without the amount. data is the variant's, undefined for none, and isn't to be changed.
// returning undefined keeps what the fluid's json says
export interface FluidRules {
name?(data: unknown): string | undefined;
// a color like "#c42d39"
color?(data: unknown): string | undefined;
// what 1000 mB of it is worth when sold, 0 or more and not always whole
sell_value?(data: unknown): number | undefined;
}
// a recipe type of a mod's own, like neoforge's RecipeType with its serializer and Recipe.matches: R is a recipe of it
// once parsed, and I what find is asked with, like the slots of the machine that makes it. see
// docs/modding/resources/recipes.md
export interface RecipeType<R, I> {
// a recipe's json turned into R, checked, like a serializer's codec. throwing stops the mod loading, like a broken
// file would. it runs once for each recipe of the type, in load order, on the server and every client
// deno-lint-ignore no-explicit-any
parse(json: any, read: RecipeReader): R;
// whether the recipe is what input makes, for find
matches(recipe: R, input: I): boolean;
}
// reads a value from a recipe's json for RecipeType.parse, checked like the engine's own recipes' fields. name is where
// it is, like "result" or "ingredients[0]", for the error thrown when it isn't one, or what it names isn't registered
export interface RecipeReader {
item(value: unknown, name: string): Id;
stack(value: unknown, name: string): { id: Id; count: number };
// an item, a #tag or a list of them, any of which fits, as every item that does, like the engine's shaped keys
ingredient(value: unknown, name: string): Id[];
// { fluid, amount } in mB
fluid_stack(value: unknown, name: string): FluidStack;
// a whole number, at least 1, like a time in ticks
positive_int(value: unknown, name: string): number;
}
// the recipes of registered types, as their parse made them, in load order. not to be changed
export interface RecipeLookup {
// the first one input makes, like neoforge's RecipeManager.getRecipeFor. undefined for a type nobody registered
find<R>(type: Id, input: unknown): R | undefined;
// every one of type, empty for a type nobody registered
all<R>(type: Id): readonly R[];
}
// the registries tags are for, see docs/modding/resources/tags.md
export type TagRegistry = "blocks" | "items" | "entities" | "biomes";
// every mod's tags, the same on the server and every client. tags can be written with or without their #
export interface TagApi {
// whether any mod defines the tag and it loaded
exists(registry: TagRegistry, tag: Id): boolean;
// whether id is in the tag, like has("items", "bworld:planks", "bworld:birch_planks"). false for a tag nobody defines
has(registry: TagRegistry, tag: Id, id: Id): boolean;
// every id in the tag, including its tags' ids, in the order they were added. empty for a tag nobody defines
get(registry: TagRegistry, tag: Id): Id[];
// every tag id is in
of(registry: TagRegistry, id: Id): Id[];
// the items a recipe's item choice stands for, once each: an item id or "#tag", or a list of those
expand(choice: Id | Id[]): Id[];
}
export interface ModInfo {
readonly id: string;
readonly version: string;
}
export interface Position {
x: number;
y: number;
z: number;
}
// how a sound plays, on top of what its definition in sounds.json says. see docs/modding/resources/sounds.md
export interface PlayOptions {
// multiplies the sound's own. over 1 it's heard further away, not louder
volume?: number;
// multiplies the sound's own: 2 is an octave up and twice as fast
pitch?: number;
}
export type Face = "west" | "east" | "bottom" | "top" | "north" | "south";
// KeyboardEvent.code values, like "KeyE" or "Digit1"
export type KeyCode = string;
export type Unsubscribe = () => void;
export interface EventSignal<T> {
subscribe(handler: (event: T) => void): Unsubscribe;
}
// shared phases: block rules that run on the server and on every client, so a client places blocks the way the
// server will, like minecraft's Block code that both sides run. see docs/modding/gettingstarted/modfiles.md
export interface SharedContext {
readonly mod: ModInfo;
// which one this is running on. the server's say is final, clients only guess ahead of it
readonly side: "server" | "client";
readonly tags: TagApi;
// the world rules look at and change. on a client it's what that client sees, and what rules change there is a
// guess until the server says what really happened. only for use in rules, not during setup
readonly world: SharedWorld;
// every block any mod registered, like the server's and clients' ctx.blocks. for rules that look at what's around,
// like a shaft lining up with the axle of the block it's placed against
readonly blocks: {
exists(id: Id): boolean;
// the components its json lists and their params, a copy
components(id: Id): Record<Id, unknown>;
// whether it's solid like stone, like the server's ctx.blocks.solid
solid(id: Id): boolean;
};
// every item any mod registered, like the client's ctx.items, for rules that read one, like a juice named after the
// fruit it's from
readonly items: {
// every item, in load order, like the server's ctx.items.ids
ids(): Id[];
exists(id: Id): boolean;
// what players see, like "Cooked Rice"
name(id: Id): string;
// the components its json lists and their params, a copy
components(id: Id): Record<Id, unknown>;
// what one of it sells for with this data, or undefined if it can't be sold: the item's "base_sell_value", and a
// full fluid container's fluid on top, like the server's ctx.items.sell_value
sell_value(id: Id, data?: unknown): number | undefined;
};
// every fluid any mod defines, like the server's and clients' ctx.fluids, without what rules decide
readonly fluids: Pick<FluidApi, "exists" | "same"> & {
// the rules of one of the mod's fluids, which the server and every client name and color it by
register(id: Id, rules: FluidRules): void;
};
readonly recipes: {
// adds a recipe of type, like "shaped" or "bworld:cooking", as a file in the mod's recipes/ with that
// json would, only during setup. for recipes made from other data, like one for every item with a component.
// shared phases run on the server and every client alike, so they all have it without it being sent. it's
// there for server and client phases, which set up after, and it throws when it's broken like a file would
// stop the mod loading
register(type: string, json: Record<string, unknown>): void;
// every recipe, or every one of type, as its json, copies, in load order: the mods' recipes/ files, and what the
// shared phases that set up before this one registered, which are the dependencies'. like the client's
// deno-lint-ignore no-explicit-any
list(type?: string): any[];
// a recipe type of the mod's own, in its namespace, only during setup: its recipes, from files and register,
// are parsed by it then, and find and all read them from there on every side. a recipe of a type no mod
// registers stops its mod loading once every shared phase has set up. see RecipeType
register_type<R, I>(type: Id, rules: RecipeType<R, I>): void;
} & RecipeLookup;
readonly sounds: {
// once at x y z, from a rule. on the server, for every player in range but the one whose break, place or click
// ran the rule, since their client ran it too and played it already. on a client, only there. throws for a
// sound no mod defines. see docs/modding/resources/sounds.md
play(sound: Id, x: number, y: number, z: number, options?: PlayOptions): void;
};
readonly components: {
// the rules of a block component, the same id the server phase registers the rest of it under
// deno-lint-ignore no-explicit-any
register_block<P = any>(id: Id, rules: BlockRules<P>): void;
};
}
export interface SharedWorld {
// undefined when the chunk isn't loaded, "cubi:air" for air
get_block(x: number, y: number, z: number): Id | undefined;
get_state(x: number, y: number, z: number, name: string): number | undefined;
// changing what a block is tells the blocks beside it, see BlockRules.on_neighbor_change
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;
// the biome at a block, like "bworld:plains", and that biome's season, like neoforge's level.getBiome(pos). one per
// block across and every 4 blocks up, see docs/modding/worldgen/biomes.md. undefined when the chunk isn't loaded
biome_at(x: number, y: number, z: number): Id | undefined;
season_at(x: number, y: number, z: number): Season | undefined;
}
// a block in the world, for rules
export interface SharedBlock extends Position {
readonly id: Id;
}
// a player placing a block at x y z by clicking face of the block next to it, at hit (0 to 1 on each axis of that
// block), like minecraft's BlockPlaceContext. facing is the way the player looks along the ground, and looking the face
// of the six they look most toward, up and down too, like getNearestLookingDirection. sneaking is whether they hold the
// sneak key, for blocks placed another way round when they do
export interface BlockPlacement extends Position {
// the item placing it, like minecraft's getItemInHand: usually the block's own, or another that places it, like a
// vertical gearbox placing a gearbox on its side
readonly item: Id;
readonly face: Face;
readonly hit: readonly [number, number, number];
readonly facing: "north" | "east" | "south" | "west";
readonly looking: Face;
readonly sneaking: boolean;
}
// a player right clicking face of a block, at hit (0 to 1 on each axis of the block), like minecraft's BlockHitResult.
// facing is the way the player looks, along the ground, and sneaking whether they hold the sneak key. sneaking with
// an item in hand doesn't interact at all, the item is used or placed instead
export interface BlockInteraction {
readonly face: Face;
readonly hit: readonly [number, number, number];
readonly facing: "north" | "east" | "south" | "west";
readonly sneaking: boolean;
}
// P is the component's params from the block json
// deno-lint-ignore no-explicit-any
export interface BlockRules<P = any> {
// the states it's placed with when a player places it, like minecraft's getStateForPlacement. states it doesn't
// return keep their defaults, and a 2 bit "facing" faces the player. false when it can't go there, so it isn't
// placed, like getStateForPlacement returning null
state_for_placement?(placement: BlockPlacement, params: P): Record<string, number> | false | void;
// a block next to it (on any of the 6 sides) was placed, broken or changed its states, like minecraft's
// updateShape. neighbor is where, and what's there now
on_neighbor_change?(block: SharedBlock, params: P, neighbor: SharedBlock): void;
// a player right clicks it, like minecraft's useWithoutItem: changes only its states and its neighbors', which
// the clicking client shows right away instead of waiting for the server. return true if it did something, then
// nothing is placed. the server runs the server phase's on_interact too
on_interact?(block: SharedBlock, params: P, interaction: BlockInteraction): boolean | void;
}