Implement tags !
This commit is contained in:
+27
-5
@@ -14,7 +14,10 @@ import {
|
||||
ModRecipeJson,
|
||||
OreJson,
|
||||
RecipeJson,
|
||||
TagJson,
|
||||
TagRegistry,
|
||||
} from "./mod_data.ts";
|
||||
import { resolve_tags, Tags } from "./tags.ts";
|
||||
import { register_block_item } from "./utils.ts";
|
||||
import type { ModelJson } from "./block_models.ts";
|
||||
|
||||
@@ -28,6 +31,8 @@ export interface ModData {
|
||||
ores: OreJson[];
|
||||
entities: EntityJson[];
|
||||
entity_models: EntityModelJson[];
|
||||
// tag files, see common/tags.ts
|
||||
tags: TagJson[];
|
||||
}
|
||||
|
||||
// a mod as the server lists it to clients, in load order: the .bmod players download, without its server script
|
||||
@@ -61,14 +66,17 @@ export class ModLoadError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
// registers every mod's blocks, items, entities and recipes, in the order given (dependencies first)
|
||||
// whether a block, item or entity 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, 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) {
|
||||
const fail = (message: string): never => {
|
||||
throw new ModLoadError(id, message);
|
||||
};
|
||||
try {
|
||||
for (const model of data.models) {
|
||||
EverythingRegistry.register<ModelJson>("models", model.id, model);
|
||||
@@ -90,9 +98,23 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
|
||||
EverythingRegistry.register<EntityJson>("entities", json.id, json);
|
||||
}
|
||||
} catch (e) {
|
||||
fail((e as Error).message);
|
||||
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)) {
|
||||
|
||||
Reference in New Issue
Block a user