// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // registers mods' data, the same way on the server and on clients import { BlockRegistry, EverythingRegistry, ItemRegistry } from "./everything_registry.ts"; import { add_to_block_item, block_from_json, BlockJson, choice_ids, EffectJson, EntityJson, EntityModelJson, FluidJson, grid_recipe_from_json, GridRecipe, is_mod_recipe, item_from_json, ItemJson, ModRecipeJson, OreJson, RecipeJson, SpawnJson, TagJson, TagRegistry, } from "./mod_data.ts"; import { resolve_tags, Tags } from "./tags.ts"; import type { LootTableJson } from "./loot.ts"; import { type BiomeJson, register_biome } from "./worldgen/biomes.ts"; import type { BiomeSourceJson, DensityFunctionJson, NoiseJson, NoiseSettingsJson } from "./worldgen/data.ts"; import type { FeatureJson } from "./worldgen/features.ts"; import { register_block_item } from "./utils.ts"; import { find_model, type ModelFileJson, type ModelJson, resolve_models } from "./block_models.ts"; // everything a mod's json files hold, merged into one file by the build (build/mods///data.json) export interface ModData { blocks: BlockJson[]; // block models as mods write them, parents not merged in yet. see common/block_models.ts models: ModelFileJson[]; items: ItemJson[]; recipes: RecipeJson[]; ores: OreJson[]; // natural spawns, see SpawnJson spawns: SpawnJson[]; entities: EntityJson[]; entity_models: EntityModelJson[]; // status effects, see EffectJson effects: EffectJson[]; // kinds of fluid, see FluidJson fluids: FluidJson[]; // tag files, see common/tags.ts tags: TagJson[]; // see common/loot.ts. missing from mods packed before there were loot tables loot_tables?: LootTableJson[]; // see common/worldgen/biomes.ts. missing from mods packed before biomes were data biomes?: BiomeJson[]; // the terrain as data, see common/worldgen/data.ts. server only, players' copies leave them out noises?: NoiseJson[]; density_functions?: DensityFunctionJson[]; noise_settings?: NoiseSettingsJson[]; biome_sources?: BiomeSourceJson[]; // data features, see common/worldgen/features.ts. server only too features?: FeatureJson[]; // the mod's settings, its config.json's "config", with the server's changes once the server loads it. server only // too, see docs/modding/misc/config.md config?: Record; } // a mod as the server lists it to clients, in load order: the .bmod players download, without its server script export interface ModListing { id: string; name: string; version: string; // sha-256 in hex of the .bmod, clients check it before using anything in it sha256: string; // where it's served from, relative to the server's root. named by its hash, so it never changes file: string; size: number; // it imports bworld/engine, see docs/modding/misc/engine.md. the confirm screen for other sites says so engine_access?: boolean; } export class RecipeBook { shaped: GridRecipe[] = []; furnace = new Map(); fuel = new Map(); smithing: Extract[] = []; // recipes of mods' own types, by type by_type = new Map(); // every recipe as its json, in load order, for recipe viewers all: RecipeJson[] = []; // each mod's ores, in load order. each mod's are their own pass, see generate_raw_chunk ores: OreJson[][] = []; spawns: SpawnJson[] = []; } export class ModLoadError extends Error { constructor(mod: string, message: string) { super(`${mod}: ${message}`); this.name = "ModLoadError"; } } // whether a block, item, entity or biome is registered, for tags. air isn't a registered block, but tags can hold it export function is_registered(registry: TagRegistry, id: string): boolean { return (registry === "blocks" && id === "bworld:air") || EverythingRegistry.get(registry, id) !== undefined; } // registers every mod's blocks, items, entities, effects, fluids, tags and recipes, in the order given (dependencies // first). tags load once every mod's content has, since a tag can hold anything, and recipes after them, since they can // use tags export function register_mod_data(mods: { id: string; data: ModData }[]): RecipeBook { const recipes = new RecipeBook(); for (const { id, data } of mods) { try { // parents are this mod's or an earlier one's, which are already registered with theirs merged in const { models, problems } = resolve_models( data.models, (model_id) => find_model(model_id, {}) ?? EverythingRegistry.get("models", model_id), ); if (problems.length > 0) { throw new Error(problems.map(({ id, problem }) => `model ${id}: ${problem}`).join("\n")); } for (const model of models) { EverythingRegistry.register("models", model.id, model); } for (const json of data.blocks) { const { block, has_item } = block_from_json(json); EverythingRegistry.register("blocks", block.id, block); if (has_item) { register_block_item(block); } } for (const json of data.items) { if (json.texture !== undefined) { EverythingRegistry.register("items", json.id, item_from_json(json)); continue; } const block_item = EverythingRegistry.get("blocks", json.id) && EverythingRegistry.get("items", json.id); if (!block_item) throw new Error(`item ${json.id} has no texture and isn't a block's item`); add_to_block_item(json, block_item); } for (const model of data.entity_models) { EverythingRegistry.register("entity_models", model.id, model); } for (const json of data.entities) { EverythingRegistry.register("entities", json.id, json); } for (const json of data.effects) { EverythingRegistry.register("effects", json.id, json); } for (const json of data.fluids) { EverythingRegistry.register("fluids", json.id, json); } for (const json of data.loot_tables ?? []) { EverythingRegistry.register("loot_tables", json.id, json); } for (const json of data.biomes ?? []) { register_biome(json); } // compiled into a generator once every mod's are in, see common/worldgen/generator.ts for (const json of data.noises ?? []) EverythingRegistry.register("noises", json.id, json); for (const json of data.density_functions ?? []) { EverythingRegistry.register("density_functions", json.id, json); } for (const json of data.noise_settings ?? []) EverythingRegistry.register("noise_settings", json.id, json); for (const json of data.biome_sources ?? []) EverythingRegistry.register("biome_sources", json.id, json); // compiled with the types worldgen scripts register, see common/worldgen_loader.ts for (const json of data.features ?? []) EverythingRegistry.register("features", json.id, json); } catch (e) { throw new ModLoadError(id, (e as Error).message); } } const { tags, problems } = resolve_tags( mods.flatMap(({ id, data }) => data.tags.map((json) => ({ mod: id, file: `tags/${json.registry}/${json.id}`, json })) ), is_registered, ); if (problems.length > 0) throw new ModLoadError(problems[0].mod, `${problems[0].file}: ${problems[0].message}`); Tags.load(tags); for (const { id, data } of mods) { const fail = (message: string): never => { throw new ModLoadError(id, message); }; for (const recipe of data.recipes) { recipes.all.push(recipe); if (is_mod_recipe(recipe)) { const list = recipes.by_type.get(recipe.type) ?? []; recipes.by_type.set(recipe.type, list); list.push(recipe); continue; } switch (recipe.type) { case "shaped": recipes.shaped.push(grid_recipe_from_json(recipe)); break; case "furnace": for (const input of choice_ids(recipe.input)) { if (recipes.furnace.has(input)) fail(`two furnace recipes for ${input}`); recipes.furnace.set(input, { output: recipe.output, cook_time: recipe.cook_time }); } break; case "fuel": for (const item of choice_ids(recipe.item)) recipes.fuel.set(item, recipe.burn_time); break; case "smithing": if ( recipes.smithing.some((other) => other.tool === recipe.tool && other.material.id === recipe.material.id && other.addition === recipe.addition ) ) { fail(`two smithing recipes for ${recipe.tool} with ${recipe.material.id}`); } recipes.smithing.push(recipe); break; } } if (data.ores.length > 0) recipes.ores.push(data.ores); recipes.spawns.push(...data.spawns); } return recipes; }