Implement tags !

This commit is contained in:
2026-09-30 13:41:29 -03:00
parent dc70ada7c4
commit 342b45d593
51 changed files with 978 additions and 219 deletions
+57 -7
View File
@@ -2,14 +2,18 @@
// and the server (on .bmod files) all use it
import {
type BlockJson,
choice_ids,
choice_list,
type EntityJson,
type EntityModelJson,
type ItemJson,
type OreJson,
type RecipeJson,
SHARED_TAG_NAMESPACE,
type TagJson,
type TagRegistry,
} from "./mod_data.ts";
import { BUILTIN_MODELS, type ModelJson } from "./block_models.ts";
import { resolve_tags } from "./tags.ts";
export interface ModReport {
id: string;
@@ -29,6 +33,7 @@ export interface LoadedMod {
ores: { file: string; json: OreJson }[];
entities: { file: string; json: EntityJson }[];
entity_models: { file: string; json: EntityModelJson }[];
tags: { file: string; json: TagJson }[];
textures: string[];
// absolute paths by texture id
texture_files: Map<string, string>;
@@ -68,6 +73,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
const model_owners = new Map<string, string[]>();
const entity_owners = new Map<string, string[]>();
const entity_model_owners = new Map<string, string[]>();
const tag_owners = new Map<TagRegistry, Set<string>>();
const add = (map: Map<string, string[]>, id: string, mod: string) => map.set(id, [...(map.get(id) ?? []), mod]);
const textures = new Set<string>(engine_textures);
@@ -81,6 +87,10 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
for (const { json } of mod.models) add(model_owners, json.id, mod.id);
for (const { json } of mod.entities) add(entity_owners, json.id, mod.id);
for (const { json } of mod.entity_models) add(entity_model_owners, json.id, mod.id);
for (const { json } of mod.tags) {
if (!tag_owners.has(json.registry)) tag_owners.set(json.registry, new Set());
tag_owners.get(json.registry)!.add(json.id);
}
for (const texture of mod.textures) textures.add(texture);
}
@@ -95,12 +105,24 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
if (!mod_ids.has(dep)) errors.push(`manifest.json: depends on "${dep}", which isn't installed`);
}
const uses = (file: string, id: string, what: "block" | "item" | "texture" | "model" | "entity model") => {
const namespace = id.split(":")[0];
// the shared tag namespace belongs to no mod, so using it needs no dependency
const check_namespace = (file: string, id: string) => {
const tag = id.startsWith("#");
const namespace = id.replace("#", "").split(":")[0];
if (tag && namespace === SHARED_TAG_NAMESPACE) return;
if (namespace !== mod.id && namespace !== "engine" && !dependencies.has(namespace)) {
warnings.push(`${file}: uses ${id} but doesn't list "${namespace}" in dependencies`);
}
if (what === "model") {
};
// block and item ids can be #tags, like in recipes and place_on
const uses = (file: string, id: string, what: "block" | "item" | "texture" | "model" | "entity model") => {
check_namespace(file, id);
if (id.startsWith("#") && (what === "block" || what === "item")) {
if (!tag_owners.get(what === "block" ? "blocks" : "items")?.has(id.slice(1))) {
errors.push(`${file}: ${what} tag ${id} doesn't exist`);
}
} else if (what === "model") {
if (!BUILTIN_MODELS[id] && !model_owners.has(id)) errors.push(`${file}: model ${id} doesn't exist`);
} else if (what === "entity model") {
if (!entity_model_owners.has(id)) errors.push(`${file}: entity model ${id} doesn't exist`);
@@ -152,15 +174,15 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
for (const { file, json } of mod.recipes) {
switch (json.type) {
case "shaped":
for (const id of Object.values(json.key).flatMap(choice_ids)) uses(file, id, "item");
for (const id of Object.values(json.key).flatMap(choice_list)) uses(file, id, "item");
uses(file, json.result.id, "item");
break;
case "furnace":
for (const id of choice_ids(json.input)) uses(file, id, "item");
for (const id of choice_list(json.input)) uses(file, id, "item");
uses(file, json.output.id, "item");
break;
case "fuel":
for (const id of choice_ids(json.item)) uses(file, id, "item");
for (const id of choice_list(json.item)) uses(file, id, "item");
break;
case "smithing":
uses(file, json.tool, "item");
@@ -186,5 +208,33 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
errors.push(`${file}: entity model ${json.id} is defined more than once`);
}
}
// whether what a tag holds exists is up to resolve_tags below, since entries can be optional
for (const { file, json } of mod.tags) {
check_namespace(file, `#${json.id}`);
for (const entry of json.values) {
if (typeof entry === "string" || entry.required !== false) {
check_namespace(file, typeof entry === "string" ? entry : entry.id);
}
}
}
}
// tags merge in load order. when there's no order, mods depending on each other in a circle is its own error
let ordered = mods;
try {
ordered = load_order(mods);
} catch {
// checked by the caller
}
const owners: Record<TagRegistry, Map<string, string[]>> = {
blocks: block_owners,
items: item_owners,
entities: entity_owners,
};
const { problems } = resolve_tags(
ordered.flatMap((mod) => mod.tags.map(({ file, json }) => ({ mod: mod.id, file, json }))),
(registry, id) => (registry === "blocks" && id === "bworld:air") || owners[registry].has(id),
);
const by_id = new Map(mods.map((mod) => [mod.id, mod]));
for (const { mod, file, message } of problems) by_id.get(mod)!.report.errors.push(`${file}: ${message}`);
}