Every recipe type registered from shared scripts

This commit is contained in:
2026-11-03 15:34:41 -03:00
committed by paula
parent 65113862f7
commit b329a61d96
25 changed files with 334 additions and 371 deletions
+45 -11
View File
@@ -8,11 +8,13 @@ import { EverythingRegistry } from "$/common/everything_registry.ts";
import type { ModListing } from "$/common/mod_loader.ts";
import { read_bmod } from "$/common/bmod.ts";
import { load_client_mods } from "$/client/mods.ts";
import { load_order } from "$/common/mod_check.ts";
import { load_and_check } from "$/tools/check_mods.ts";
import { pack_mod } from "$/tools/pack_mod.ts";
import { copy_dir, test_game } from "./helpers.ts";
// sifts into what its component says, and a shaped recipe made in code
// sifts into what its component says, and a shaped recipe made in code. then sifting and washing as testrecipes' own
// types, found by the item put in
const SHARED = `
import type { SharedContext } from "bworld/shared";
export function setup(ctx: SharedContext) {
@@ -21,6 +23,12 @@ const SHARED = `
if (sifts) ctx.recipes.register("testrecipes:sifting", { input: id, result: { id: sifts.into, count: 1 } });
}
ctx.recipes.register("shaped", { pattern: ["#"], key: { "#": "testrecipes:gravel" }, result: { id: "testrecipes:flint", count: 2 } });
for (const type of ["testrecipes:sifting", "testrecipes:washing"]) {
ctx.recipes.register_type(type, {
parse: (json, read) => ({ input: read.item(json.input, "input"), result: read.stack(json.result, "result") }),
matches: (recipe, input) => recipe.input === input,
});
}
SHARED_EXTRA
}
`;
@@ -101,14 +109,9 @@ Deno.test("recipes a shared script registers are on the server and clients, for
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 } });
`;
// a sifting recipe registered after the type
const SAND_AFTER =
`ctx.recipes.register("testrecipes:sifting", { input: "testrecipes:sand", result: { id: "testrecipes:gravel", count: 1 } });`;
const SAND_FILE = {
"sand.json": {
@@ -119,7 +122,7 @@ const SAND_FILE = {
};
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);
const dir = recipe_mod(SAND_AFTER, SAND_FILE);
await test_game(dir);
await load_client(dir);
for (const side of ["server", "client"]) {
@@ -135,7 +138,7 @@ Deno.test("a recipe type parses its recipes, and find gives the first one an inp
});
Deno.test("a recipe its type can't parse stops the mod loading, naming it", async () => {
const dir = recipe_mod(SIFTING_TYPE, {
const dir = recipe_mod("", {
"sand.json": {
type: "testrecipes:sifting",
input: "testrecipes:nothing",
@@ -150,6 +153,22 @@ Deno.test("a recipe its type can't parse stops the mod loading, naming it", asyn
Deno.removeSync(dir, { recursive: true });
});
Deno.test("a recipe of a type no mod registers stops its mod loading, like a misspelled one", async () => {
const dir = recipe_mod("", {
"sand.json": {
type: "testrecipes:sieving",
input: "testrecipes:sand",
result: { id: "testrecipes:flint", count: 1 },
},
});
await assertRejects(
() => test_game(dir),
Error,
"testrecipes: it has testrecipes:sieving recipes, but no mod registers that type",
);
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 });`,
@@ -201,3 +220,18 @@ Deno.test("recipes can only be registered while shared scripts set up", async ()
Deno.removeSync(dir, { recursive: true });
}
});
Deno.test("every mod's recipe types are registered on a client too, so it loads them", async () => {
const mods = load_order(load_and_check("mods"));
const bmods = await Promise.all(mods.map(async (mod) => read_bmod(await pack_mod(mod), `${mod.id}.bmod`)));
const listings = mods.map((mod): ModListing => ({
id: mod.id,
name: mod.id,
version: "1.0.0",
sha256: "",
file: "",
size: 0,
}));
EverythingRegistry.clear();
await load_client_mods(listings, bmods);
});