Fermenting recipes
This commit is contained in:
@@ -43,7 +43,7 @@ function register_compacting(ctx: SharedContext) {
|
||||
}
|
||||
|
||||
// what reading them needs, which server scripts have
|
||||
interface RecipeReader {
|
||||
export interface RecipeReader {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
recipes: { list(type: string): any[] };
|
||||
tags: TagApi;
|
||||
@@ -64,21 +64,24 @@ export function juicing_recipes(ctx: RecipeReader): JuicingRecipe[] {
|
||||
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 : [];
|
||||
const fluid = juice?.fluid?.id;
|
||||
if (
|
||||
!Array.isArray(results) || results.length !== 1 || typeof fluid !== "string" || !ctx.fluids.exists(fluid) ||
|
||||
!Number.isInteger(juice.amount) || juice.amount < 1
|
||||
) {
|
||||
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: { fluid: juice.fluid, amount: juice.amount },
|
||||
};
|
||||
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