// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later // reading recipes: what each one makes and takes, for any mod's recipe type, so the viewer works with recipes it has // no category for import type { FluidVariant, TagApi } from "cubi/client"; // deno-lint-ignore no-explicit-any export type Recipe = any; // one place in a recipe: the items any of which fit, and how many export type Ingredient = { ids: string[]; count: number }; // an amount of a fluid in mB export type FluidAmount = { fluid: FluidVariant; amount: number }; // what a recipe makes export function outputs(recipe: Recipe): Ingredient[] { switch (recipe.type) { case "shaped": return [stack(recipe.result)]; case "bworld:smelting": return [stack(recipe.output)]; case "bworld:fuel": return []; case "bworld:smithing": return [{ ids: [recipe.result], count: 1 }]; default: { // one item or stack, or a list of them, like what a cutting board cuts something into or a press presses const output = recipe.result ?? recipe.output ?? recipe.results; return [output].flat().flatMap((output): Ingredient[] => { if (typeof output === "string") return [{ ids: [output], count: 1 }]; if (output && typeof output.id === "string") return [stack(output)]; return []; }); } } } // the fluids a recipe makes and takes, from its "fluid_results" and "fluid_ingredients", like juicing, and the // { fluid, amount } among its "results", like create's export function fluid_outputs(recipe: Recipe): FluidAmount[] { const among = Array.isArray(recipe.results) ? recipe.results.filter((result: Recipe) => result?.fluid) : []; return [...recipe.fluid_results ?? [], ...among]; } export function fluid_inputs(recipe: Recipe): FluidAmount[] { return recipe.fluid_ingredients ?? []; } // what a recipe takes. shaped keys, smelting inputs, fuels and smithing inputs can be an item or #tag, or a list any of // which fits export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] { switch (recipe.type) { case "shaped": return Object.values(recipe.key as Record).map((choice) => ({ ids: tags.expand(choice), count: 1, })); case "bworld:smelting": return [{ ids: tags.expand(recipe.input), count: 1 }]; case "bworld:fuel": return [{ ids: tags.expand(recipe.item), count: 1 }]; case "bworld:smithing": return [{ ids: tags.expand(recipe.tool), count: 1 }, choice_stack(recipe.material, tags)].concat( recipe.addition ? [choice_stack(recipe.addition, tags)] : [], ); default: { const { grid, extras } = mod_recipe_inputs(recipe, tags); return [...grid, ...extras]; } } } // the items in a mod recipe's fields besides its type and results: each item or #tag in a list is an ingredient (a list // in a list is one ingredient any of whose items fit), and a field that's one item is an extra, like a container export function mod_recipe_inputs(recipe: Recipe, tags: TagApi): { grid: Ingredient[]; extras: Ingredient[] } { const grid: Ingredient[] = []; const extras: Ingredient[] = []; const ingredient = (choice: string | string[]) => ({ ids: tags.expand(choice), count: 1 }); for (const [field, value] of Object.entries(recipe)) { if (field === "type" || field === "result" || field === "output") continue; if (is_id(value)) { extras.push(ingredient(value)); } else if (Array.isArray(value)) { for (const entry of value) { if (is_id(entry) || (Array.isArray(entry) && entry.every(is_id))) grid.push(ingredient(entry)); } } } return { grid, extras }; } // an item id or #tag function is_id(value: unknown): value is string { return typeof value === "string" && /^#?[a-z0-9_]+:[a-z0-9_/]+$/.test(value); } export function stack(value: { id: string; count: number }): Ingredient { return { ids: [value.id], count: value.count }; } // { id, count } where id is an item, a #tag or a list of them, like smithing's material export function choice_stack(value: { id: string | string[]; count: number }, tags: TagApi): Ingredient { return { ids: tags.expand(value.id), count: value.count }; } export function seconds(ticks: number): string { const s = ticks / 20; return `${Number.isInteger(s) ? s : s.toFixed(1)} s`; }