47 lines
1.8 KiB
TypeScript
47 lines
1.8 KiB
TypeScript
// SPDX-FileCopyrightText: 2026 Paula Wisnievski
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
// smithing recipes: the smithing table upgrades a tool with some of a material and maybe some of one more item. the
|
|
// type is registered by the shared script, so the server's menu and anything on a client find them the same way
|
|
import type { ItemStack, SharedContext } from "cubi/shared";
|
|
|
|
export const SMITHING = "bworld:smithing";
|
|
|
|
export interface SmithingRecipe {
|
|
tool: string;
|
|
material: { id: string; count: number };
|
|
addition?: { id: string; count: number };
|
|
result: string;
|
|
}
|
|
|
|
// what's in the smithing table's input slots
|
|
export interface SmithingInput {
|
|
tool: ItemStack;
|
|
material: ItemStack;
|
|
addition?: ItemStack;
|
|
}
|
|
|
|
export function register_smithing(ctx: SharedContext) {
|
|
// two recipes for the same inputs would leave one that can never be made
|
|
const seen = new Set<string>();
|
|
ctx.recipes.register_type<SmithingRecipe, SmithingInput>(SMITHING, {
|
|
parse(json, read) {
|
|
const recipe: SmithingRecipe = {
|
|
tool: read.item(json.tool, "tool"),
|
|
material: read.stack(json.material, "material"),
|
|
result: read.item(json.result, "result"),
|
|
};
|
|
if (json.addition !== undefined) recipe.addition = read.stack(json.addition, "addition");
|
|
const inputs = `${recipe.tool} ${recipe.material.id} ${recipe.addition?.id}`;
|
|
if (seen.has(inputs)) throw new Error(`there's already one for ${recipe.tool} with ${recipe.material.id}`);
|
|
seen.add(inputs);
|
|
return recipe;
|
|
},
|
|
// with enough of the material and the addition
|
|
matches: (recipe, input) =>
|
|
recipe.tool === input.tool.id && recipe.material.id === input.material.id &&
|
|
input.material.count >= recipe.material.count && recipe.addition?.id === input.addition?.id &&
|
|
(input.addition?.count ?? 0) >= (recipe.addition?.count ?? 0),
|
|
});
|
|
}
|