Better recipe api and port smithing to it
This commit is contained in:
+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() {}, list: () => [] },
|
||||
recipes: { register() {}, list: () => [], register_type() {}, find: () => undefined, all: () => [] },
|
||||
world,
|
||||
components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) },
|
||||
});
|
||||
|
||||
@@ -248,7 +248,7 @@ Deno.test("every tier of pickaxe, axe, shovel and hoe exists and can be made", a
|
||||
const { game } = await test_game("mods");
|
||||
const made = new Set([
|
||||
...game.recipes.shaped.map((recipe) => recipe.result.id),
|
||||
...game.recipes.smithing.map((recipe) => recipe.result),
|
||||
...game.recipes.all.flatMap((recipe) => recipe.type === "bworld:smithing" ? [recipe.result] : []),
|
||||
]);
|
||||
for (const tier of ["wood", "stone", "copper", "iron", "gold"]) {
|
||||
for (const tool of ["pickaxe", "axe", "shovel", "hoe"]) {
|
||||
@@ -266,7 +266,7 @@ Deno.test("every tier of watering can exists, can be made and holds more than th
|
||||
const { game } = await test_game("mods");
|
||||
const made = new Set([
|
||||
...game.recipes.shaped.map((recipe) => recipe.result.id),
|
||||
...game.recipes.smithing.map((recipe) => recipe.result),
|
||||
...game.recipes.all.flatMap((recipe) => recipe.type === "bworld:smithing" ? [recipe.result] : []),
|
||||
]);
|
||||
let last = 0;
|
||||
for (const tier of ["wood", "stone", "copper", "iron", "gold"]) {
|
||||
|
||||
@@ -85,7 +85,7 @@ Deno.test("the recipe viewer finds what makes and uses an item, in any recipe ty
|
||||
// smelting and smithing
|
||||
assert(using("bworld:iron_ore").some((r) => r.type === "furnace" && outputs(r)[0].ids[0] === "bworld:iron_ingot"));
|
||||
assertEquals(ids(inputs(making("bworld:stone_pickaxe")[0])), ["bworld:wood_pickaxe", "bworld:stone"]);
|
||||
assertEquals(category_name("smithing"), "Smithing");
|
||||
assertEquals(category_name("bworld:smithing"), "Smithing");
|
||||
});
|
||||
|
||||
Deno.test("recipes are found by category in jei's order, and a catalyst's uses are everything made in it", async () => {
|
||||
|
||||
@@ -30,6 +30,10 @@ const SEEN = (side: string) => `
|
||||
export function setup(ctx: any) {
|
||||
ctx.components?.register_item?.("testrecipes:siftable", {});
|
||||
(globalThis as any).testrecipes_${side} = ctx.recipes.list("testrecipes:sifting");
|
||||
(globalThis as any).testrecipes_${side}_parsed = {
|
||||
all: ctx.recipes.all("testrecipes:sifting"),
|
||||
sand: ctx.recipes.find("testrecipes:sifting", "testrecipes:sand"),
|
||||
};
|
||||
(globalThis as any).testrecipes_late?.();
|
||||
}
|
||||
`;
|
||||
@@ -71,16 +75,10 @@ const SIFTING = [{
|
||||
result: { id: "testrecipes:flint", count: 1 },
|
||||
}];
|
||||
|
||||
Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => {
|
||||
const dir = recipe_mod();
|
||||
const t = await test_game(dir);
|
||||
const seen = globalThis as unknown as Record<string, unknown>;
|
||||
assertEquals(seen.testrecipes_server, SIFTING);
|
||||
assertEquals(t.game.recipes.by_type.get("testrecipes:sifting") as unknown, SIFTING);
|
||||
assert(
|
||||
t.game.recipes.shaped.some((recipe) => recipe.result.id === "testrecipes:flint" && recipe.result.count === 2),
|
||||
);
|
||||
const seen = globalThis as unknown as Record<string, unknown>;
|
||||
|
||||
// packs testrecipes from dir, which it then removes, and loads it on a client like one joining would
|
||||
async function load_client(dir: string) {
|
||||
const mod = load_and_check(dir).find((m) => m.id === "testrecipes")!;
|
||||
assertEquals(mod.report.errors, []);
|
||||
const bmod = read_bmod(await pack_mod(mod), "testrecipes.bmod");
|
||||
@@ -88,9 +86,82 @@ Deno.test("recipes a shared script registers are on the server and clients, for
|
||||
const listing: ModListing = { id: "testrecipes", name: "Test", version: "1.0.0", sha256: "", file: "", size: 0 };
|
||||
EverythingRegistry.clear();
|
||||
await load_client_mods([listing], [bmod]);
|
||||
}
|
||||
|
||||
Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => {
|
||||
const dir = recipe_mod();
|
||||
const t = await test_game(dir);
|
||||
assertEquals(seen.testrecipes_server, SIFTING);
|
||||
assertEquals(t.game.recipes.by_type.get("testrecipes:sifting") as unknown, SIFTING);
|
||||
assert(
|
||||
t.game.recipes.shaped.some((recipe) => recipe.result.id === "testrecipes:flint" && recipe.result.count === 2),
|
||||
);
|
||||
|
||||
await load_client(dir);
|
||||
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 } });
|
||||
`;
|
||||
|
||||
const SAND_FILE = {
|
||||
"sand.json": {
|
||||
type: "testrecipes:sifting",
|
||||
input: "testrecipes:sand",
|
||||
result: { id: "testrecipes:flint", count: 1 },
|
||||
},
|
||||
};
|
||||
|
||||
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);
|
||||
await test_game(dir);
|
||||
await load_client(dir);
|
||||
for (const side of ["server", "client"]) {
|
||||
const parsed = seen[`testrecipes_${side}_parsed`] as { all: { input: string }[]; sand: unknown };
|
||||
// the file's, then the ones registered before the type and after it
|
||||
assertEquals(parsed.all.map((recipe) => recipe.input), [
|
||||
"testrecipes:sand",
|
||||
"testrecipes:gravel",
|
||||
"testrecipes:sand",
|
||||
]);
|
||||
assertEquals(parsed.sand, { input: "testrecipes:sand", result: { id: "testrecipes:flint", count: 1 } });
|
||||
}
|
||||
});
|
||||
|
||||
Deno.test("a recipe its type can't parse stops the mod loading, naming it", async () => {
|
||||
const dir = recipe_mod(SIFTING_TYPE, {
|
||||
"sand.json": {
|
||||
type: "testrecipes:sifting",
|
||||
input: "testrecipes:nothing",
|
||||
result: { id: "testrecipes:flint", count: 1 },
|
||||
},
|
||||
});
|
||||
await assertRejects(
|
||||
() => test_game(dir),
|
||||
Error,
|
||||
"testrecipes:sifting recipe 1: input: there's no item testrecipes:nothing",
|
||||
);
|
||||
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 });`,
|
||||
);
|
||||
await assertRejects(
|
||||
() => test_game(dir),
|
||||
Error,
|
||||
'recipe type bworld:sifting must be in the namespace "testrecipes"',
|
||||
);
|
||||
Deno.removeSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
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(
|
||||
|
||||
Reference in New Issue
Block a user