Every recipe type registered from shared scripts
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,9 +3,9 @@
|
||||
|
||||
// juicing recipes: what the juicer presses an item into. the shared script registers one for every item with
|
||||
// bworld:juiceable, its own juice, and mods can add more like any recipe, as files or from their shared scripts.
|
||||
// whatever makes juice reads them, like the juicer. with create installed, each is also a create:compacting recipe,
|
||||
// so its press can make juice in a basin
|
||||
import type { FluidStack, SharedContext, TagApi } from "bworld/shared";
|
||||
// whatever makes juice finds them by the item, like the juicer. with create installed, each is also a create:compacting
|
||||
// recipe, so its press can make juice in a basin
|
||||
import type { FluidStack, SharedContext } from "bworld/shared";
|
||||
import type { JuiceableParams } from "./shared.ts";
|
||||
|
||||
export const JUICING = "bworld:juicing";
|
||||
@@ -19,6 +19,18 @@ export interface JuicingRecipe {
|
||||
|
||||
// a recipe for every juiceable item, pressing one into the juice named after it, as much as its component says
|
||||
export function register_juicing(ctx: SharedContext) {
|
||||
ctx.recipes.register_type<JuicingRecipe, string>(JUICING, {
|
||||
parse(json, read) {
|
||||
if (!Array.isArray(json.fluid_results) || json.fluid_results.length !== 1) {
|
||||
throw new Error("fluid_results must be one { fluid, amount }");
|
||||
}
|
||||
return {
|
||||
input: read.ingredient(json.input, "input"),
|
||||
juice: read.fluid_stack(json.fluid_results[0], "fluid_results[0]"),
|
||||
};
|
||||
},
|
||||
matches: (recipe, id) => recipe.input.includes(id),
|
||||
});
|
||||
for (const id of ctx.items.ids()) {
|
||||
const params = ctx.items.components(id)["bworld:juiceable"] as JuiceableParams | undefined;
|
||||
if (!params) continue;
|
||||
@@ -41,48 +53,3 @@ function register_compacting(ctx: SharedContext) {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// what reading them needs, which server scripts have
|
||||
export interface RecipeReader {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
recipes: { list(type: string): any[] };
|
||||
tags: TagApi;
|
||||
items: { exists(id: string): boolean };
|
||||
fluids: { exists(id: string): boolean };
|
||||
}
|
||||
|
||||
// every juicing recipe, in load order. a broken one throws, which stops the server from starting
|
||||
export function juicing_recipes(ctx: RecipeReader): JuicingRecipe[] {
|
||||
return ctx.recipes.list(JUICING).map((json, i) => {
|
||||
const where = `${JUICING} recipe ${i + 1}`;
|
||||
const choice: unknown = json.input;
|
||||
const entries = typeof choice === "string" ? [choice] : Array.isArray(choice) ? choice : [];
|
||||
const exists = (entry: unknown) =>
|
||||
typeof entry === "string" &&
|
||||
(entry.startsWith("#") ? ctx.tags.exists("items", entry.slice(1)) : ctx.items.exists(entry));
|
||||
if (entries.length === 0 || !entries.every(exists)) {
|
||||
throw new Error(`${where}: input must be an item, a #tag or a list of them, and they must exist`);
|
||||
}
|
||||
const results: unknown = json.fluid_results;
|
||||
const juice = Array.isArray(results) && results.length === 1 ? fluid_stack(ctx, results[0]) : undefined;
|
||||
if (!juice) {
|
||||
throw new Error(`${where}: fluid_results must be one { fluid, amount } of a fluid that exists, in mB`);
|
||||
}
|
||||
return { input: ctx.tags.expand(choice as string | string[]), juice };
|
||||
});
|
||||
}
|
||||
|
||||
// a recipe's { fluid, amount } of a fluid that exists and a whole number of mB, undefined when it isn't one
|
||||
export function fluid_stack(ctx: RecipeReader, json: unknown): FluidStack | undefined {
|
||||
const stack = json as Partial<FluidStack> | undefined;
|
||||
const id = stack?.fluid?.id;
|
||||
if (typeof id !== "string" || !ctx.fluids.exists(id) || !Number.isInteger(stack!.amount) || stack!.amount! < 1) {
|
||||
return undefined;
|
||||
}
|
||||
return { fluid: stack!.fluid!, amount: stack!.amount! };
|
||||
}
|
||||
|
||||
// the first recipe that presses id, undefined when none does
|
||||
export function juicing_for(recipes: JuicingRecipe[], id: string): JuicingRecipe | undefined {
|
||||
return recipes.find((recipe) => recipe.input.includes(id));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user