// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later import type { TintColor } from "./worldgen/biome_colors.ts"; import { ID_MASK } from "./constants.ts"; import type { ItemStack } from "./inventory.ts"; import type { BlockPart, BlockVariant } from "./block_models.ts"; export class EverythingRegistry { static #key_to_id = new Map>(); static #id_to_value = new Map(); static register(registry: string, key: string, value: T): T { if (this.#key_to_id.get(registry)?.has(key)) { throw new Error(`${key} is already registered in ${registry}`); } if (!this.#key_to_id.has(registry)) { this.#key_to_id.set(registry, new Map()); this.#id_to_value.set(registry, []); } const key_map = this.#key_to_id.get(registry)!; const values = this.#id_to_value.get(registry)!; const id = values.length + 1; key_map.set(key, id); values[id] = value; return value; } static get(registry: string, key: string): T | undefined { const id = this.#key_to_id.get(registry)?.get(key); if (id === undefined) { return undefined; } return this.#id_to_value.get(registry)?.[id] as T; } static get_id(registry: string, key: string): number | undefined { return this.#key_to_id.get(registry)?.get(key); } static get_full(registry: string, key: string): [number, T] | undefined { const id = this.#key_to_id.get(registry)?.get(key); if (id === undefined) { return undefined; } return [id, this.#id_to_value.get(registry)?.[id] as T]; } static get_by_id(registry: string, id: number): T | undefined { return this.#id_to_value.get(registry)?.[id] as T; } // empty for a registry nothing has been added to, like blocks in a game whose mods have none static get_registry(registry: string): T[] { return (this.#id_to_value.get(registry) ?? []) as T[]; } // forget everything, for tests that load mods more than once static clear() { this.#key_to_id.clear(); this.#id_to_value.clear(); } // [key, value] pairs in registration order static entries(registry: string): [string, T][] { const values = this.#id_to_value.get(registry) ?? []; return [...(this.#key_to_id.get(registry) ?? [])].map(([key, id]) => [key, values[id] as T]); } } // one texture for everything, or the model's texture variables: { top, bottom, side } or { front, side } for // cubes, { crop } for cubi:crop and so on export type BlockTextures = string | Record; export interface BlockStateDefinition { name: string; bits: number; default: number; } interface CompiledStateDefinition { name: string; mask: number; shift: number; } // solid: fully opaque. cutout: texels are either opaque or see-through (leaves). // translucent: blended and sorted back to front (water, glass) export const RENDER_LAYERS = ["solid", "cutout", "translucent"] as const; export type RenderLayer = typeof RENDER_LAYERS[number]; // like minecraft: solid blocks stop light, everything else lets it through unless it says otherwise export function block_light_opacity(block: BlockRegistry | undefined): number { if (!block) return 0; return block.light_opacity ?? ((block.render_layer ?? "solid") === "solid" ? 15 : 0); } export function block_light_emission(block: BlockRegistry | undefined): number { return block?.light_emission ?? 0; } // every block's light opacity and emission by numeric id, for lighting a lot of blocks at once. blocks is the blocks // registry, with its gaps export function light_tables(blocks: readonly (BlockRegistry | undefined)[]) { const opacity = new Uint8Array(ID_MASK + 1); const emission = new Uint8Array(ID_MASK + 1); blocks.forEach((block, nid) => { opacity[nid] = block_light_opacity(block); emission[nid] = block_light_emission(block); }); return { opacity, emission }; } export interface BlockRegistry { id: string; // what players see, like "Iron Ore". made from the id when not set, see display_name in common/utils.ts name?: string; textures: BlockTextures; // the block model, cubi:cube when not set. see common/block_models.ts model?: string; // solid when not set. anything else doesn't hide its neighbors' faces render_layer?: RenderLayer; // hide faces between two of this block, like glass and water. defaults to true for translucent blocks cull_same?: boolean; // drawn in a color: the biome's grass or foliage color, like minecraft's grass block and leaves, or a fixed one // like "#80a755". only the texture variables listed (like "top"), or all of them tint?: { color: TintColor; textures?: string[] }; // light level 0-15 it gives off light_emission?: number; // how much light going through it loses, 0-15. see block_light_opacity for the default light_opacity?: number; alpha?: number; has_collision: boolean; // boxes in pixels, see BlockVariant.shape and collision_shape shape?: number[][]; collision_shape?: number[][]; // made of pieces instead of one model, see BlockPart parts?: BlockPart[]; // the model its item is drawn with, with the block's textures, like minecraft's fence_inventory item_model?: string; toughness?: number; requires_tool?: boolean; tool_to_break?: string; // the lowest tool tier it drops for when requires_tool, like minecraft's incorrect_for_*_tool tags. 0 when not set tool_tier?: number; drop_table?: string; // what it drops when broken, see common/loot.ts. drop_table is one of an item instead loot_table?: string; states?: BlockStateDefinition[]; // a different model or textures for some states, by conditions like "age=7". the first match wins variants?: Record; // right clicking it does something instead of placing a block, clients don't predict placing against it. // behavior runs on the server, see server/game/blocks.ts interactive?: boolean; // the mouse wheel turned while looking at it goes to its components' on_scroll instead of the hotbar scrollable?: boolean; // what its sounds' ids start with, see BLOCK_SOUNDS in common/mod_data.ts sounds?: string; // placing a block into it replaces it, like water replaceable?: boolean; // the blocks (ids or #tags) it can stand on, like crops on farmland. it can't be placed elsewhere, and breaks when // the block under it becomes something else. anything when not set place_on?: string[]; // the blocks (ids or #tags) it can be placed into, replacing them, like rice into water. air and replaceable blocks // when not set place_in?: string[]; // custom components from mods and their params, the server runs them components?: Record; // draws the item a mod's server phase gives it (ServerWorld.set_block_item) on top of it, like the // cutting board. surface is the height in pixels it lies on. turned with the variant's y item_display?: { surface: number }; // a fluid like water, drawn like luanti's: its top slopes down towards where it flows. see FluidProperties fluid?: FluidProperties; // the fluid it can hold, like minecraft's waterlogged fences. it gets a "waterlogged" state, see WATERLOGGED waterloggable?: string; // it can only be placed into a source of that fluid, so it's always placed holding it, like farmer's delight's rice waterlogged_only?: boolean; compiled_states?: CompiledStateDefinition[]; } // a fluid's "level" state is 0 for a source, the full block, and 1 to range as it flows away from one, each lower, // like minecraft's. fluids get that state without listing it. their textures are one texture, or { still, flowing }: // still on the top and bottom, flowing on the sides, like luanti's special tiles export interface FluidProperties { // how many blocks it flows from a source, 1 to 7, like luanti's liquid_range range: number; // ticks between each step it flows, like minecraft's 5 for water and 30 for lava ticks: number; // a spot with two sources beside it and something solid or a source under it becomes a source, like minecraft's // infinite water and luanti's liquid_renewable renewable: boolean; } export const FLUID_LEVEL: BlockStateDefinition = { name: "level", bits: 3, default: 0 }; // a waterloggable block's state, 1 while it holds a source of its fluid. blocks get it without listing it, after // their own states export const WATERLOGGED: BlockStateDefinition = { name: "waterlogged", bits: 1, default: 0 }; export const MAX_FLUID_RANGE = 7; // what a fluid that doesn't say has export const FLUID_DEFAULTS: FluidProperties = { range: MAX_FLUID_RANGE, ticks: 5, renewable: true }; // what entities swim in, and where falling doesn't hurt export function is_fluid_block(block: BlockRegistry | undefined): boolean { return block?.fluid !== undefined; } // a fluid's textures as the top, bottom and sides of a cube, for its item and its particles export function fluid_textures(textures: BlockTextures): { still: string; flowing: string } { if (typeof textures === "string") return { still: textures, flowing: textures }; const still = textures.still ?? "cubi:missing"; return { still, flowing: textures.flowing ?? still }; } // how high a fluid's surface is in a block of it at this level, 0 to 1, like luanti's flowing levels export function fluid_height(level: number, range: number) { if (level === 0) return 1; return (range - Math.min(level, range) + 0.5) / range; } // what a tool is made of, like minecraft's ToolMaterial export interface ToolProperties { // like "pickaxe", matched against blocks' tool_to_break type: string; // blocks that need a tool only drop for tools of their tool_tier or higher. wood and gold are 0, stone 1, iron 2 tier: number; // how many times faster than a hand it breaks blocks of its type speed: number; // how many deaths it lasts: each one takes 1 off every tool the player carried. what's used up is the stack's // data.damage, see ItemStack.durability durability: number; } // how an item looks worn on a player, like bedrock's attachables: an entity model (see EntityModelJson) whose bones // named like the player's parts (PLAYER_PARTS) move with them, drawn with a texture from the atlas. the slim ones are // for players with 3 pixel wide arms, the others when not set. see docs/modding/items/index.md export interface WornProperties { model: string; texture: string; slim_model?: string; slim_texture?: string; } // the parts of minecraft's player model, the bones of a worn model that move with them export const PLAYER_PARTS = ["head", "body", "right_arm", "left_arm", "right_leg", "left_leg"] as const; // what eating an item does, like minecraft's food properties export interface FoodProperties { // hunger points it restores, a bar is 20 nutrition: number; // saturation it gives is nutrition * saturation_modifier * 2, like minecraft. bread's is 0.6 saturation_modifier: number; // can be eaten when not hungry always_edible?: boolean; // what's left in the hand after, like a bowl after soup returns?: string; } // an item that holds one fluid, full or empty, like a bucket. a full one's stack data is { fluid: FluidVariant }, see // docs/modding/resources/fluids.md. what it looks like full is a layer tinted "fluid" export interface FluidContainerProperties { // how much a full one holds, in mB capacity: number; // how many full ones fit in a slot, the item's max_stack when not set full_max_stack?: number; } // a texture drawn over a flat item's own, like the fluid in a bucket, drawn in its tint's color: a block tint's color // outside any biome, or "fluid" for the fluid a fluid container holds. a layer whose tint has no color for a stack, like // "fluid" on an empty container, isn't drawn export interface ItemLayer { texture: string; tint?: TintColor | "fluid"; } export interface ItemRegistry { texture_id: string | ((item: ItemStack) => string); // shown in place of texture_id while the stack's data has that key set, like a cast fishing rod. the first wins data_textures?: Record; // a model it's drawn with instead of its texture, with the model's own textures, like a 3d wrench. or an entity // model (see EntityModelJson), drawn in its texture model?: string; // the entity it lies in the world as instead of a cubi:item, holding the stack, like neoforge's hasCustomEntity. // an "object" (see EntityJson.behavior) entity?: string; // what players see, like "Iron Ingot". made from the id when not set, see display_name in common/utils.ts name?: string; block_id?: string; tool?: ToolProperties; // 1 for tools and 64 for everything else when not set max_stack?: number; // what one is worth when sold base_sell_value?: number; lore?: string; food?: FoodProperties; worn?: WornProperties; // whether players holding it aim at fluids, like buckets and fishing rods. others look through them, so they can // break and place blocks under water targets_fluids?: boolean; fluid_container?: FluidContainerProperties; // drawn over its texture in order, see ItemLayer layers?: ItemLayer[]; components?: Record; on_create?(item: ItemStack): void; get_lore?(item: ItemStack): string; }