ctx.recipes.list api on shared side
This commit is contained in:
@@ -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<string, unknown>): 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
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
+1
-1
@@ -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) },
|
||||
});
|
||||
|
||||
@@ -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 } });`,
|
||||
|
||||
Reference in New Issue
Block a user