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
+1 -1
View File
@@ -240,7 +240,7 @@ Deno.test("the atlas fits textures of any size, tallest first, without overlappi
Deno.test("a mod with only data packs and reads", () => {
const bmod = {
manifest: { format_version: 1, id: "tiny", name: "Tiny", version: "1.0.0" },
data: { blocks: [], models: [], items: [], recipes: [], ores: [], entities: [], entity_models: [] },
data: { blocks: [], models: [], items: [], recipes: [], ores: [], entities: [], entity_models: [], tags: [] },
scripts: {},
textures: new Map(),
};
+1
View File
@@ -34,6 +34,7 @@ export function mod_sources(mods_dir: string): ServerModSource[] {
ores: mod.ores.map((o) => o.json),
entities: mod.entities.map((e) => e.json),
entity_models: mod.entity_models.map((m) => m.json),
tags: mod.tags.map((t) => t.json),
},
server_url: url(scripts.server),
worldgen_url: url(scripts.worldgen),
+4 -1
View File
@@ -1,9 +1,12 @@
// the recipe viewer mod reads every mod's recipes, its own types included
import { assert, assertEquals } from "@std/assert";
import { category_name, inputs, outputs } from "../mods/recipe_viewer/scripts/client.ts";
import { category_name, inputs as recipe_inputs, outputs } from "../mods/recipe_viewer/scripts/client.ts";
import { tag_api } from "$/common/tags.ts";
import { test_game } from "./helpers.ts";
const ids = (ingredients: { ids: string[] }[]) => ingredients.map((i) => i.ids.join("|"));
// deno-lint-ignore no-explicit-any
const inputs = (recipe: any) => recipe_inputs(recipe, tag_api());
Deno.test("the recipe viewer finds what makes and uses an item, in any recipe type", async () => {
const { game } = await test_game("mods");
+212
View File
@@ -0,0 +1,212 @@
// tags: lists of blocks, items or entities that every mod can add to, see docs/modding/resources/tags.md
import { assert, assertEquals, assertThrows } from "@std/assert";
import { Container, ItemStack } from "$/common/inventory.ts";
import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts";
import { type BlockRegistry, EverythingRegistry } from "$/common/everything_registry.ts";
import type { TagJson } from "$/common/mod_data.ts";
import { resolve_tags, tag_api, type TagSource } from "$/common/tags.ts";
import { can_stand_on } from "$/common/utils.ts";
import { read_bmod, write_bmod } from "$/common/bmod.ts";
import { update_crafting_result } from "$/server/game/crafting.ts";
import { check_mods } from "$/tools/check_mods.ts";
import { copy_dir, test_game } from "./helpers.ts";
// tag files in load order, all item tags unless they say otherwise
function resolve(files: [mod: string, json: Partial<TagJson> & { id: string }][], existing: string[]) {
const sources: TagSource[] = files.map(([mod, json], i) => ({
mod,
file: `file${i}`,
json: { registry: "items", values: [], ...json },
}));
const known = new Set(existing);
const { tags, problems } = resolve_tags(sources, (_, id) => known.has(id));
return { items: tags.get("items") ?? new Map(), problems: problems.map((p) => `${p.mod} ${p.file}: ${p.message}`) };
}
Deno.test("tags from every mod merge in load order, and hold other tags", () => {
const { items, problems } = resolve([
["a", { id: "c:ingots/iron", values: ["a:iron"] }],
["a", { id: "c:ingots", values: ["#c:ingots/iron", "#c:ingots/tin"] }],
["b", { id: "c:ingots/tin", values: ["b:tin"] }],
["b", { id: "c:ingots/iron", values: ["b:iron", "a:iron"] }],
], ["a:iron", "b:iron", "b:tin"]);
assertEquals(problems, []);
assertEquals(items.get("c:ingots/iron"), ["a:iron", "b:iron"]);
// a tag can hold one defined after it
assertEquals(items.get("c:ingots"), ["a:iron", "b:iron", "b:tin"]);
});
Deno.test("replace drops what came before, and removes apply in order", () => {
const { items, problems } = resolve([
["a", { id: "a:things", values: ["a:one", "a:two", "a:three"] }],
["b", { id: "a:things", values: [], remove: ["a:two", "#a:others"] }],
["c", { id: "a:others", values: ["a:three"] }],
// added back after b removed it
["c", { id: "a:things", values: ["a:two"] }],
["d", { id: "a:replaced", values: ["a:one"] }],
["e", { id: "a:replaced", values: ["a:two"], replace: true }],
], ["a:one", "a:two", "a:three"]);
assertEquals(problems, []);
assertEquals(items.get("a:things"), ["a:one", "a:two"]);
assertEquals(items.get("a:replaced"), ["a:two"]);
});
Deno.test("optional entries can be missing, required ones and circles stop a tag loading", () => {
const { items, problems } = resolve([
["a", {
id: "a:soft",
values: ["a:real", { id: "other:maybe", required: false }, { id: "#x:y", required: false }],
}],
// removing what doesn't exist is fine
["a", { id: "a:soft", values: [], remove: ["other:gone", "#nobody:defines"] }],
["a", { id: "a:hard", values: ["a:real", "other:missing"] }],
["a", { id: "a:undefined", values: ["#nobody:defines"] }],
["a", { id: "a:loop1", values: ["#a:loop2"] }],
["a", { id: "a:loop2", values: ["#a:loop1"] }],
["a", { id: "a:self", values: ["#a:self"] }],
// holding a tag that failed fails too, without repeating why
["a", { id: "a:outer", values: ["#a:hard"] }],
], ["a:real"]);
assertEquals(items.get("a:soft"), ["a:real"]);
for (const failed of ["a:hard", "a:undefined", "a:loop1", "a:loop2", "a:self", "a:outer"]) {
assert(!items.has(failed), failed);
}
assertEquals(problems, [
"a file2: #a:hard needs other:missing, which doesn't exist",
"a file3: #a:undefined needs #nobody:defines, which no mod defines",
"a file5: tags contain each other in a circle: #a:loop1 -> #a:loop2 -> #a:loop1",
"a file6: tags contain each other in a circle: #a:self -> #a:self",
]);
});
Deno.test("the base game's tags, from scripts", async () => {
await test_game("mods");
const tags = tag_api();
assertEquals(tags.get("items", "bworld:planks").length, 7);
assert(tags.has("items", "#bworld:logs", "bworld:cherry_log"));
assert(tags.has("blocks", "bworld:leaves", "bworld:birch_leaves"));
assert(!tags.has("items", "bworld:planks", "bworld:stone"));
assertEquals(tags.get("items", "c:ingots"), [
"bworld:copper_ingot",
"bworld:tin_ingot",
"bworld:iron_ingot",
"bworld:gold_ingot",
]);
// in the order the tags were defined
assertEquals(tags.of("items", "bworld:iron_ingot"), ["c:ingots", "c:ingots/iron"]);
// nobody's tag is empty, not an error
assert(!tags.exists("items", "nobody:tag"));
assertEquals(tags.get("items", "nobody:tag"), []);
assertEquals(tags.expand(["#c:ingots/tin", "bworld:coal", "bworld:tin_ingot"]), [
"bworld:tin_ingot",
"bworld:coal",
]);
// copies
tags.get("items", "bworld:planks").pop();
assertEquals(tags.get("items", "bworld:planks").length, 7);
// deno-lint-ignore no-explicit-any
assertThrows(() => tags.has("recipes" as any, "bworld:planks", "bworld:planks"), Error, 'not "recipes"');
});
// mods/bworld and a mod that adds maple wood to the base game's tags
function mods_with_maple() {
const dir = Deno.makeTempDirSync({ prefix: "bworld_tags_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const write = (path: string, json: unknown) => write_json(`${dir}/maple/${path}`, json);
write("manifest.json", {
format_version: 1,
id: "maple",
name: "Maple",
version: "1.0.0",
dependencies: [{ id: "bworld", version: "*" }],
});
write("blocks/maple_planks.json", {
format_version: 1,
block: { id: "maple:maple_planks", textures: "bworld:oak_planks" },
});
write("blocks/maple_log.json", { format_version: 1, block: { id: "maple:maple_log", textures: "bworld:oak_log" } });
write("blocks/shelf_fungus.json", {
format_version: 1,
block: { id: "maple:shelf_fungus", textures: "bworld:dirt", place_on: ["#bworld:logs"] },
});
write("tags/items/bworld/planks.json", { format_version: 1, tag: { values: ["maple:maple_planks"] } });
write("tags/blocks/bworld/logs.json", { format_version: 1, tag: { values: ["maple:maple_log"] } });
// the shared namespace, with a folder
write("tags/items/c/wood/maple.json", {
format_version: 1,
tag: { values: ["maple:maple_planks", { id: "other_mod:maple_syrup", required: false }] },
});
return dir;
}
function write_json(path: string, json: unknown) {
Deno.mkdirSync(path.replace(/\/[^/]+$/, ""), { recursive: true });
Deno.writeTextFileSync(path, JSON.stringify(json));
}
Deno.test("a mod's planks work in every recipe that takes #bworld:planks", async () => {
const dir = mods_with_maple();
const { game } = await test_game(dir);
const grid = new Container(10);
grid.set_item(0, new ItemStack("maple:maple_planks", 1));
grid.set_item(3, new ItemStack("bworld:birch_planks", 1));
update_crafting_result(grid, game.recipes.shaped);
assertEquals(grid.get_item(CRAFTING_RESULT_SLOT)?.type_id, "bworld:stick");
// place_on takes block tags
const fungus = EverythingRegistry.get<BlockRegistry>("blocks", "maple:shelf_fungus");
assert(can_stand_on(fungus, "maple:maple_log"));
assert(can_stand_on(fungus, "bworld:spruce_log"));
assert(!can_stand_on(fungus, "bworld:stone"));
assertEquals(tag_api().get("items", "c:wood/maple"), ["maple:maple_planks"]);
const report = (await check_mods(dir, { typecheck: false })).find((r) => r.id === "maple")!;
assertEquals(report, { id: "maple", errors: [], warnings: [] });
Deno.removeSync(dir, { recursive: true });
});
Deno.test("check-mods finds broken tags and tags nobody defines", async () => {
const dir = mods_with_maple();
const write = (path: string, json: unknown) => write_json(`${dir}/maple/${path}`, json);
write("tags/items/maple/broken.json", { format_version: 1, tag: { values: ["maple:no_such_item"] } });
write("tags/items/maple/bad.json", { format_version: 1, tag: { values: ["Not An Id"], replace: "yes" } });
write("tags/items/misplaced.json", { format_version: 1, tag: { values: [] } });
write("tags/fluids/maple/sap.json", { format_version: 1, tag: { values: [] } });
write("recipes/uses_missing_tag.json", {
format_version: 1,
recipe: { type: "fuel", item: "#maple:nothing_here", burn_time: 100 },
});
// another mod's namespace, without depending on it
write("tags/items/farmersdelight/knives.json", { format_version: 1, tag: { values: ["maple:maple_planks"] } });
const report = (await check_mods(dir, { typecheck: false })).find((r) => r.id === "maple")!;
const has = (list: string[], text: string) =>
assert(list.some((e) => e.includes(text)), `expected "${text}" in\n${list.join("\n")}`);
has(report.errors, "tags/items/maple/broken.json: #maple:broken needs maple:no_such_item, which doesn't exist");
has(report.errors, `tags/items/maple/bad.json: values must be an id like "my_mod:thing"`);
has(report.errors, "tags/items/maple/bad.json: replace must be true or false");
has(report.errors, "tags/items/misplaced.json: tag files go in tags/<registry>/<namespace>/");
has(report.errors, "tags/fluids/maple/sap.json: registry must be one of blocks, items, entities");
has(report.errors, "recipes/uses_missing_tag.json: item tag #maple:nothing_here doesn't exist");
has(report.warnings, `uses #farmersdelight:knives but doesn't list "farmersdelight" in dependencies`);
// the shared namespace and optional entries need no dependency
assert(!report.warnings.some((w) => w.includes("c:wood") || w.includes("other_mod")), report.warnings.join("\n"));
Deno.removeSync(dir, { recursive: true });
});
Deno.test("tags survive packing into a .bmod", () => {
const tag: TagJson = { registry: "items", id: "c:ingots/tin", values: ["x:tin", { id: "y:tin", required: false }] };
const data = {
blocks: [],
models: [],
items: [],
recipes: [],
ores: [],
entities: [],
entity_models: [],
tags: [tag],
};
const manifest = { format_version: 1, id: "x", name: "X", version: "1.0.0" };
const bmod = read_bmod(write_bmod({ manifest, data, scripts: {}, textures: new Map() }), "x.bmod");
assertEquals(bmod.data.tags, [tag]);
});