From 43aa6b2551f3a6884bd2de37dabcf42f6878c4a7 Mon Sep 17 00:00:00 2001 From: Paula Date: Fri, 30 Oct 2026 23:17:54 -0300 Subject: [PATCH] ctx.recipes.list api on shared side --- common/mod_api/shared.ts | 4 ++++ common/mod_loader.ts | 3 ++- docs/modding/resources/recipes.md | 11 +++++++++++ tests/helpers.ts | 2 +- tests/script_recipes_test.ts | 24 +++++++++++++++++++++++- 5 files changed, 41 insertions(+), 3 deletions(-) diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index cffc47d..f8ba275 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -132,6 +132,10 @@ export interface SharedContext { // there for server and client scripts, which set up after, and it throws when it's broken like a file would // stop the mod loading register(type: string, json: Record): void; + // every recipe, or every one of type, as its json, copies, in load order: the mods' recipes/ files, and what the + // shared scripts that set up before this one registered, which are the dependencies'. like the client's + // deno-lint-ignore no-explicit-any + list(type?: string): any[]; }; readonly components: { // the rules of a block component, the same id the server script registers the rest of it under diff --git a/common/mod_loader.ts b/common/mod_loader.ts index de3f916..6490efe 100644 --- a/common/mod_loader.ts +++ b/common/mod_loader.ts @@ -237,7 +237,7 @@ export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) { // ctx.recipes for shared scripts, the same on the server and every client: a recipe registered there is in the book // on every side without being sent, like one from the mod's recipes/. checked like a data file's. fail throws, as the -// side's error for the mod +// side's error for the mod. list reads the book as it is, so a script sees the recipes of the ones before it export function shared_recipes_api( recipes: () => RecipeBook, setting_up: () => boolean, @@ -255,5 +255,6 @@ export function shared_recipes_api( fail((e as Error).message); } }, + list: (type) => structuredClone(type ? recipes().all.filter((recipe) => recipe.type === type) : recipes().all), }; } diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index a628089..2dc0cfe 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -187,6 +187,17 @@ like any other. That's also why it's only in shared scripts. Recipes can only be server or client script sets up, and one is checked like a file would be: a broken one, or a second furnace recipe for the same input, throws, which stops the mod loading. +`ctx.recipes.list(type?)` reads the recipes there are so far, copies in load order, like the client's: every mod's +`recipes/` files, and what the shared scripts that set up before this one registered. Dependencies set up first, so a +mod can make recipes of its own from another mod's, like a machine of its own for every one of the base game's juicing +recipes: + +```ts +for (const juicing of ctx.recipes.list("bworld:juicing")) { + ctx.recipes.register("copper_tools:squeezing", { input: juicing.input, fluid_results: juicing.fluid_results }); +} +``` + Make them from what's the same on every side, like items and their components, not from the server's [config](../misc/config.md), which clients don't have. diff --git a/tests/helpers.ts b/tests/helpers.ts index 590f4af..abf6757 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -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) }, }); diff --git a/tests/script_recipes_test.ts b/tests/script_recipes_test.ts index 43951b1..13d2034 100644 --- a/tests/script_recipes_test.ts +++ b/tests/script_recipes_test.ts @@ -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 = {}) { 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 } });`,