Tags for smithing recipes

This commit is contained in:
2026-11-13 20:19:37 -03:00
parent 51b5d148bd
commit d48fa8cca5
7 changed files with 130 additions and 47 deletions
+36 -13
View File
@@ -3,14 +3,20 @@
// 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";
import type { ItemStack, RecipeReader, SharedContext } from "cubi/shared";
export const SMITHING = "bworld:smithing";
// the items any of which fit, from an item, a #tag or a list of them, and how many it takes
export interface SmithingIngredient {
items: string[];
count: number;
}
export interface SmithingRecipe {
tool: string;
material: { id: string; count: number };
addition?: { id: string; count: number };
tool: string[];
material: SmithingIngredient;
addition?: SmithingIngredient;
result: string;
}
@@ -21,26 +27,43 @@ export interface SmithingInput {
addition?: ItemStack;
}
// { id, count }, where id is a choice like a shaped key's
function read_ingredient(read: RecipeReader, value: unknown, name: string): SmithingIngredient {
const json = value as { id?: unknown; count?: unknown } | undefined;
return { items: read.ingredient(json?.id, `${name} id`), count: read.positive_int(json?.count, `${name} count`) };
}
// whether stack is enough of ingredient. no ingredient fits only an empty slot
function fits(ingredient: SmithingIngredient | undefined, stack: ItemStack | undefined): boolean {
if (!ingredient) return !stack;
return !!stack && ingredient.items.includes(stack.id) && stack.count >= ingredient.count;
}
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"),
tool: read.ingredient(json.tool, "tool"),
material: read_ingredient(read, 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);
if (json.addition !== undefined) recipe.addition = read_ingredient(read, json.addition, "addition");
for (const tool of recipe.tool) {
for (const material of recipe.material.items) {
for (const addition of recipe.addition?.items ?? [undefined]) {
const inputs = `${tool} ${material} ${addition}`;
if (seen.has(inputs)) throw new Error(`there's already one for ${tool} with ${material}`);
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),
recipe.tool.includes(input.tool.id) && fits(recipe.material, input.material) &&
fits(recipe.addition, input.addition),
});
}
+13 -4
View File
@@ -5,7 +5,16 @@
// blocks it's made in (jei's recipe catalysts), and where its items go. types no category knows get one from their
// recipes' json, see mod_category
import type { ClientContext, FluidVariant, TagApi } from "cubi/client";
import { fluid_outputs, type Ingredient, mod_recipe_inputs, outputs, type Recipe, seconds, stack } from "./recipes.ts";
import {
choice_stack,
fluid_outputs,
type Ingredient,
mod_recipe_inputs,
outputs,
type Recipe,
seconds,
stack,
} from "./recipes.ts";
// in gui pixels, a slot with its border, like minecraft's
export const SLOT = 18;
@@ -98,9 +107,9 @@ function base_categories(tags: TagApi): RecipeCategory[] {
height: 18,
catalysts: ["bworld:smithing_table"],
draw(recipe, layout) {
layout.slot({ ids: [recipe.tool], count: 1 }, 0, 0);
layout.slot(stack(recipe.material), SLOT, 0);
layout.slot(recipe.addition && stack(recipe.addition), SLOT * 2, 0);
layout.slot({ ids: tags.expand(recipe.tool), count: 1 }, 0, 0);
layout.slot(choice_stack(recipe.material, tags), SLOT, 0);
layout.slot(recipe.addition && choice_stack(recipe.addition, tags), SLOT * 2, 0);
layout.texture("bworld:arrow_full", 61, 1, ARROW, ARROW);
layout.slot({ ids: [recipe.result], count: 1 }, 90, 0);
},
+9 -3
View File
@@ -46,7 +46,8 @@ export function fluid_inputs(recipe: Recipe): FluidAmount[] {
return recipe.fluid_ingredients ?? [];
}
// what a recipe takes. shaped keys, smelting inputs and fuels can be an item or #tag, or a list any of which fits
// 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":
@@ -59,8 +60,8 @@ export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] {
case "bworld:fuel":
return [{ ids: tags.expand(recipe.item), count: 1 }];
case "bworld:smithing":
return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat(
recipe.addition ? [stack(recipe.addition)] : [],
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);
@@ -97,6 +98,11 @@ 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`;