Files
bworld/mods/bworld/scripts/juicing.ts
T
2026-11-04 21:51:26 -03:00

56 lines
2.3 KiB
TypeScript

// SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later
// 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 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 "cubi/shared";
import type { JuiceableParams } from "./shared.ts";
export const JUICING = "bworld:juicing";
export interface JuicingRecipe {
// the items it presses
input: string[];
// what one of them makes
juice: FluidStack;
}
// 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;
const juice = { fluid: { id: "bworld:juice", data: { item: id } }, amount: params.juice };
ctx.recipes.register(JUICING, { input: id, fluid_results: [juice] });
}
if (ctx.blocks.exists("create:mechanical_press") && ctx.blocks.exists("create:basin")) {
register_compacting(ctx);
}
}
// a create:compacting recipe for every juicing recipe there is so far, so create's press squashes the item into its
// juice in a basin under it. create's recipes take a list of item choices and give a list of results, a fluid's as
// { fluid, amount }. juicing recipes other mods' shared scripts register later aren't seen here
function register_compacting(ctx: SharedContext) {
for (const juicing of ctx.recipes.list(JUICING)) {
ctx.recipes.register("create:compacting", {
ingredients: [juicing.input],
results: juicing.fluid_results,
});
}
}