Better recipe api and port smithing to it

This commit is contained in:
2026-11-01 19:21:40 -03:00
parent ad782839ec
commit c8b5f2a3bd
50 changed files with 371 additions and 120 deletions
+80 -9
View File
@@ -30,6 +30,10 @@ const SEEN = (side: string) => `
export function setup(ctx: any) {
ctx.components?.register_item?.("testrecipes:siftable", {});
(globalThis as any).testrecipes_${side} = ctx.recipes.list("testrecipes:sifting");
(globalThis as any).testrecipes_${side}_parsed = {
all: ctx.recipes.all("testrecipes:sifting"),
sand: ctx.recipes.find("testrecipes:sifting", "testrecipes:sand"),
};
(globalThis as any).testrecipes_late?.();
}
`;
@@ -71,16 +75,10 @@ const SIFTING = [{
result: { id: "testrecipes:flint", count: 1 },
}];
Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => {
const dir = recipe_mod();
const t = await test_game(dir);
const seen = globalThis as unknown as Record<string, unknown>;
assertEquals(seen.testrecipes_server, SIFTING);
assertEquals(t.game.recipes.by_type.get("testrecipes:sifting") as unknown, SIFTING);
assert(
t.game.recipes.shaped.some((recipe) => recipe.result.id === "testrecipes:flint" && recipe.result.count === 2),
);
const seen = globalThis as unknown as Record<string, unknown>;
// packs testrecipes from dir, which it then removes, and loads it on a client like one joining would
async function load_client(dir: string) {
const mod = load_and_check(dir).find((m) => m.id === "testrecipes")!;
assertEquals(mod.report.errors, []);
const bmod = read_bmod(await pack_mod(mod), "testrecipes.bmod");
@@ -88,9 +86,82 @@ Deno.test("recipes a shared script registers are on the server and clients, for
const listing: ModListing = { id: "testrecipes", name: "Test", version: "1.0.0", sha256: "", file: "", size: 0 };
EverythingRegistry.clear();
await load_client_mods([listing], [bmod]);
}
Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => {
const dir = recipe_mod();
const t = await test_game(dir);
assertEquals(seen.testrecipes_server, SIFTING);
assertEquals(t.game.recipes.by_type.get("testrecipes:sifting") as unknown, SIFTING);
assert(
t.game.recipes.shaped.some((recipe) => recipe.result.id === "testrecipes:flint" && recipe.result.count === 2),
);
await load_client(dir);
assertEquals(seen.testrecipes_client, SIFTING);
});
// sifting as a type of testrecipes' own, matched by the item put in, and a recipe registered after it
const SIFTING_TYPE = `
ctx.recipes.register_type("testrecipes:sifting", {
parse: (json, read) => ({ input: read.item(json, "input"), result: read.stack(json, "result") }),
matches: (recipe, input) => recipe.input === input,
});
ctx.recipes.register("testrecipes:sifting", { input: "testrecipes:sand", result: { id: "testrecipes:gravel", count: 1 } });
`;
const SAND_FILE = {
"sand.json": {
type: "testrecipes:sifting",
input: "testrecipes:sand",
result: { id: "testrecipes:flint", count: 1 },
},
};
Deno.test("a recipe type parses its recipes, and find gives the first one an input makes, on every side", async () => {
const dir = recipe_mod(SIFTING_TYPE, SAND_FILE);
await test_game(dir);
await load_client(dir);
for (const side of ["server", "client"]) {
const parsed = seen[`testrecipes_${side}_parsed`] as { all: { input: string }[]; sand: unknown };
// the file's, then the ones registered before the type and after it
assertEquals(parsed.all.map((recipe) => recipe.input), [
"testrecipes:sand",
"testrecipes:gravel",
"testrecipes:sand",
]);
assertEquals(parsed.sand, { input: "testrecipes:sand", result: { id: "testrecipes:flint", count: 1 } });
}
});
Deno.test("a recipe its type can't parse stops the mod loading, naming it", async () => {
const dir = recipe_mod(SIFTING_TYPE, {
"sand.json": {
type: "testrecipes:sifting",
input: "testrecipes:nothing",
result: { id: "testrecipes:flint", count: 1 },
},
});
await assertRejects(
() => test_game(dir),
Error,
"testrecipes:sifting recipe 1: input: there's no item testrecipes:nothing",
);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a mod can only register recipe types in its own namespace", async () => {
const dir = recipe_mod(
`ctx.recipes.register_type("bworld:sifting", { parse: (json) => json, matches: () => true });`,
);
await assertRejects(
() => test_game(dir),
Error,
'recipe type bworld:sifting must be in the namespace "testrecipes"',
);
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a shared script lists the recipes there are so far, from files and the scripts before it", async () => {
// a washing recipe for every sifting one, whatever made it
const dir = recipe_mod(