Better recipe api and port smithing to it

This commit is contained in:
2026-11-01 19:21:40 -03:00
parent ad782839ec
commit c8b5f2a3bd
50 changed files with 371 additions and 120 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:gold_axe",
"material": {
"id": "bworld:cobalt_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:gold_hoe",
"material": {
"id": "bworld:cobalt_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:gold_knife",
"material": {
"id": "bworld:cobalt_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:gold_pickaxe",
"material": {
"id": "bworld:cobalt_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:gold_shovel",
"material": {
"id": "bworld:cobalt_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:gold_watering_can",
"material": {
"id": "bworld:cobalt_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:stone_axe",
"material": {
"id": "bworld:copper_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:stone_hoe",
"material": {
"id": "bworld:copper_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:stone_knife",
"material": {
"id": "bworld:copper_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:stone_pickaxe",
"material": {
"id": "bworld:copper_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:stone_shovel",
"material": {
"id": "bworld:copper_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:stone_watering_can",
"material": {
"id": "bworld:copper_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:iron_axe",
"material": {
"id": "bworld:gold_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:iron_hoe",
"material": {
"id": "bworld:gold_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:iron_knife",
"material": {
"id": "bworld:gold_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:iron_pickaxe",
"material": {
"id": "bworld:gold_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:iron_shovel",
"material": {
"id": "bworld:gold_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:iron_watering_can",
"material": {
"id": "bworld:gold_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:copper_axe",
"material": {
"id": "bworld:iron_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:copper_hoe",
"material": {
"id": "bworld:iron_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:copper_knife",
"material": {
"id": "bworld:iron_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:copper_pickaxe",
"material": {
"id": "bworld:iron_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:copper_shovel",
"material": {
"id": "bworld:iron_ingot",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:copper_watering_can",
"material": {
"id": "bworld:iron_ingot",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:wood_axe",
"material": {
"id": "bworld:stone",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:wood_hoe",
"material": {
"id": "bworld:stone",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:wood_knife",
"material": {
"id": "bworld:stone",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:wood_pickaxe",
"material": {
"id": "bworld:stone",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:wood_shovel",
"material": {
"id": "bworld:stone",
@@ -1,7 +1,7 @@
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:wood_watering_can",
"material": {
"id": "bworld:stone",
+4 -4
View File
@@ -7,6 +7,7 @@ import { register_curios } from "./curios.ts";
import { register_effects } from "./effects.ts";
import { register_fishing } from "./fishing.ts";
import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts";
import { SMITHING, type SmithingRecipe } from "./smithing.ts";
import { register_verdigris } from "./verdigris/server.ts";
import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts";
import { juicing_for, juicing_recipes } from "./juicing.ts";
@@ -292,7 +293,7 @@ function register_furnace(ctx: ServerContext) {
});
}
// smithing table: a tool, some of a material and maybe a third item make a better tool, see the smithing recipes.
// smithing table: a tool, some of a material and maybe a third item make a better tool, see smithing.ts.
// every player gets their own slots, and what's left in them goes back to the player when they close it
const TOOL = 0;
@@ -435,9 +436,8 @@ function register_smithing_table(ctx: ServerContext) {
const material = container.get(MATERIAL);
const addition = container.get(ADDITION);
if (!tool || !material) return undefined;
const recipe = ctx.recipes.smithing(tool.id, material.id, addition?.id);
if (!recipe || material.count < recipe.material_count) return undefined;
return { ...recipe, tool, material, addition };
const recipe = ctx.recipes.find<SmithingRecipe>(SMITHING, { tool, material, addition });
return recipe && { result: recipe.result, material_count: recipe.material.count, tool, material, addition };
};
// the result slot shows what taking it would give, the upgraded tool keeps the old one's data
+3 -1
View File
@@ -4,10 +4,11 @@
// the base game's block rules: how blocks are placed and how they change with the blocks beside them. they run on the
// server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor
// or a door open the moment they click it. and the rules of juice and wine, which name and color them by the item
// they're from, and the recipes that make them
// they're from, and the recipes that make them. and the base game's recipe types
import type { SharedBlock, SharedContext } from "bworld/shared";
import { register_fermenting } from "./fermenting.ts";
import { register_juicing } from "./juicing.ts";
import { register_smithing } from "./smithing.ts";
import { register_verdigris_rules } from "./verdigris/shared.ts";
export function setup(ctx: SharedContext) {
@@ -18,6 +19,7 @@ export function setup(ctx: SharedContext) {
register_juice_and_wine(ctx);
register_juicing(ctx);
register_fermenting(ctx);
register_smithing(ctx);
}
// the sides a fence connects on, and the face of the block there it connects to
+45
View File
@@ -0,0 +1,45 @@
// 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 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 "bworld/shared";
export const SMITHING = "bworld:smithing";
export interface SmithingRecipe {
tool: string;
material: { id: string; count: number };
addition?: string;
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"),
material: read.stack(json, "material"),
result: read.item(json, "result"),
};
if (json.addition !== undefined) recipe.addition = read.item(json, "addition");
const inputs = `${recipe.tool} ${recipe.material.id} ${recipe.addition}`;
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
matches: (recipe, input) =>
recipe.tool === input.tool.id && recipe.material.id === input.material.id &&
input.material.count >= recipe.material.count && recipe.addition === input.addition?.id,
});
}
+1 -2
View File
@@ -92,7 +92,7 @@ function base_categories(tags: TagApi): RecipeCategory[] {
},
},
{
type: "smithing",
type: "bworld:smithing",
title: "Smithing",
width: 108,
height: 18,
@@ -325,7 +325,6 @@ export function category_name(type: string): string {
shaped: "Crafting",
furnace: "Smelting",
fuel: "Fuel",
smithing: "Smithing",
[FISHING]: "Fishing",
[CRABPOT]: "Crab Pot",
[JUICING]: "Juicing",
+2 -2
View File
@@ -21,7 +21,7 @@ export function outputs(recipe: Recipe): Ingredient[] {
return [stack(recipe.output)];
case "fuel":
return [];
case "smithing":
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
@@ -58,7 +58,7 @@ export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] {
return [{ ids: tags.expand(recipe.input), count: 1 }];
case "fuel":
return [{ ids: tags.expand(recipe.item), count: 1 }];
case "smithing":
case "bworld:smithing":
return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat(
recipe.addition ? [{ ids: [recipe.addition], count: 1 }] : [],
);