ctx.recipes.list api on shared side

This commit is contained in:
2026-10-30 23:17:54 -03:00
parent 4717e45a9a
commit 43aa6b2551
5 changed files with 41 additions and 3 deletions
+1 -1
View File
@@ -262,7 +262,7 @@ export function client_of(game: GameServer, area: Area) {
items: shared_items_api("bworld"),
// only its blocks are looked at here
fluids: { register() {} },
recipes: { register() {} },
recipes: { register() {}, list: () => [] },
world,
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
});
+23 -1
View File
@@ -35,7 +35,8 @@ const SEEN = (side: string) => `
`;
// the base game, and testrecipes: gravel that sifts into flint, sand that doesn't sift, and the scripts above
function recipe_mod(shared_extra = "") {
// and recipes/ files by name
function recipe_mod(shared_extra = "", recipes: Record<string, unknown> = {}) {
const dir = Deno.makeTempDirSync({ prefix: "bworld_script_recipes_" });
copy_dir("mods/bworld", `${dir}/bworld`);
const write = (path: string, json: unknown) => {
@@ -60,6 +61,7 @@ function recipe_mod(shared_extra = "") {
write("scripts/shared.ts", SHARED.replace("SHARED_EXTRA", shared_extra));
write("scripts/server.ts", SEEN("server"));
write("scripts/client.ts", SEEN("client"));
for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}`, { format_version: 1, recipe });
return dir;
}
@@ -89,6 +91,26 @@ Deno.test("recipes a shared script registers are on the server and clients, for
assertEquals(seen.testrecipes_client, SIFTING);
});
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(
`for (const sifting of ctx.recipes.list("testrecipes:sifting")) {
ctx.recipes.register("testrecipes:washing", { input: sifting.result.id, result: { id: sifting.input, count: 1 } });
}`,
{
"sand.json": {
type: "testrecipes:sifting",
input: "testrecipes:sand",
result: { id: "testrecipes:flint", count: 1 },
},
},
);
const t = await test_game(dir);
Deno.removeSync(dir, { recursive: true });
const washing = t.game.recipes.by_type.get("testrecipes:washing")!.map((recipe) => recipe.result);
assertEquals(washing as unknown, [{ id: "testrecipes:sand", count: 1 }, { id: "testrecipes:gravel", count: 1 }]);
});
Deno.test("a broken recipe from a shared script stops the mod loading, like a broken file", async () => {
const dir = recipe_mod(
`ctx.recipes.register("shaped", { pattern: ["#"], key: {}, result: { id: "testrecipes:sand", count: 1 } });`,