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
+9 -5
View File
@@ -1,16 +1,19 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// create's recipe types: read and checked when the server starts, and shown by the recipe viewer
// create's recipe types: read and checked when the mod loads, and shown by the recipe viewer
import { assert, assertEquals, assertThrows } from "@std/assert";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { tag_api } from "$/common/tags.ts";
import type { ClientContext } from "$/common/mod_api/client.ts";
import type { RecipeReader } from "$/common/mod_api/shared.ts";
import { recipe_lookup } from "$/common/mod_loader.ts";
import {
COMPACTING,
parse_processing,
PRESSING,
processing_recipes,
processing_type,
type ProcessingRecipe,
} from "$/mods/create/scripts/recipes/processing.ts";
import { inputs, outputs } from "$/mods/recipe_viewer/scripts/recipes.ts";
import { RecipeIndex } from "$/mods/recipe_viewer/scripts/recipes_screen.ts";
@@ -24,7 +27,7 @@ const lookup = () => ({
Deno.test("create's pressing recipes are read with every item each ingredient stands for", async () => {
const { game } = await test_game("mods");
const recipes = processing_recipes(game.recipes.by_type.get(PRESSING.id) ?? [], PRESSING, lookup());
const recipes = recipe_lookup(() => game.recipes).all<ProcessingRecipe>(PRESSING.id);
const sheet = recipes.find((recipe) => recipe.results[0].id === "create:iron_sheet");
assert(sheet, "iron is pressed into sheets");
assert(sheet.ingredients[0].includes("bworld:iron_ingot"));
@@ -50,9 +53,10 @@ Deno.test("a pressing recipe that a press couldn't make says what's wrong with i
assertEquals(problems({ ingredients: ["bworld:iron_ingot"], results: [sheet], processing_time: 100 }).length, 1);
assertEquals(problems({ results: [sheet] }).length, 1);
// the server doesn't start with one, and says which
// the mod doesn't load with one, and says what's wrong
const type = processing_type(PRESSING, lookup());
assertThrows(
() => processing_recipes([{ ingredients: ["bworld:no_such_ingot"], results: [sheet] }], PRESSING, lookup()),
() => type.parse({ ingredients: ["bworld:no_such_ingot"], results: [sheet] }, {} as RecipeReader),
Error,
"bworld:no_such_ingot",
);
+1 -1
View File
@@ -261,7 +261,7 @@ export function client_of(game: GameServer, area: Area) {
},
items: shared_items_api("bworld"),
// only its blocks are looked at here
fluids: { register() {} },
fluids: { exists: () => false, same: () => false, register() {} },
recipes: { register() {}, list: () => [], register_type() {}, find: () => undefined, all: () => [] },
world,
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
+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);
});