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
@@ -321,7 +321,7 @@ catch up. If it's further behind than that, it skips the rest and logs a warning
| `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. |
| `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it that has a sell value for coins. |
| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. |
| `bworld:smithing_table` | none | Upgrades tools with `smithing` recipes. |
| `bworld:smithing_table` | none | Upgrades tools with `bworld:smithing` recipes. |
| `bworld:stonecutter` | none | Cuts one item into another with `bworld:stonecutting` recipes, picked from a list. |
| `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. |
| `bworld:farmland` | none | Watered with the watering can, dries out every morning. |
+57 -12
View File
@@ -1,7 +1,7 @@
# Recipes
Recipes are data in a mod's `recipes/` folder, one per file. The base game has four types, for its crafting grid,
furnace and smithing table. Mods can make up types of their own for their machines.
Recipes are data in a mod's `recipes/` folder, one per file. The engine has three types, for the crafting grid and the
furnace. Everything else, the base game's smithing table included, has a type of its own that a mod defines.
## Crafting: `shaped`
@@ -46,15 +46,16 @@ pattern fits anywhere in the grid.
`cook_time` and `burn_time` are in ticks, 20 to a second. Two furnace recipes for the same input are an error. For fuel,
a later recipe for the same item replaces an earlier one.
## Smithing: `smithing`
## Smithing: `bworld:smithing`
For the smithing table, which upgrades a tool:
For the smithing table, which upgrades a tool. It's the base game's own type, registered by its shared script like
[any mod's](#custom-recipe-types):
```json
{
"format_version": 1,
"recipe": {
"type": "smithing",
"type": "bworld:smithing",
"tool": "bworld:wood_pickaxe",
"material": { "id": "bworld:stone", "count": 5 },
"result": "bworld:stone_pickaxe"
@@ -155,8 +156,54 @@ Use a tag rather than listing ids when other mods might have more of the same th
## Custom Recipe Types
Any `type` with a namespace is a mod's own, like `bworld:cooking` for the cooking pot. The engine only stores these
recipes. Everything else about them is up to the mod:
Any `type` with a namespace is a mod's own, like `bworld:smithing` for the smithing table. The engine only stores these
recipes. What their fields mean is up to the mod, which registers the type from its
[shared script](../concepts/sides.md#shared-scripts), like NeoForge's `RecipeType` with its serializer:
```ts
import type { ItemStack, SharedContext } from "bworld/shared";
interface SmithingRecipe {
tool: string;
material: { id: string; count: number };
result: string;
}
export function setup(ctx: SharedContext) {
ctx.recipes.register_type<SmithingRecipe, { tool: ItemStack; material: ItemStack }>("bworld:smithing", {
// the json checked and turned into what matches and the machine use
parse: (json, read) => ({
tool: read.item(json, "tool"),
material: read.stack(json, "material"),
result: read.item(json, "result"),
}),
// whether what's in the machine makes the recipe
matches: (recipe, input) =>
recipe.tool === input.tool.id && recipe.material.id === input.material.id &&
input.material.count >= recipe.material.count,
});
}
```
`parse` runs once for every recipe of the type, from files and from [scripts](#recipes-from-scripts), in load order, on
the server and every client. Throwing stops the mod loading with an error naming the recipe, so a broken one is caught
when the server starts rather than when someone uses the machine. `read.item(json, field)` and `read.stack(json, field)`
check a field the way the engine's own types are checked, an item id or `{ id, count }` of an item that exists, and
throw naming the field.
Then server, client and shared scripts look recipes up by what's in the machine, like NeoForge's
`RecipeManager.getRecipeFor`:
```ts
const recipe = ctx.recipes.find<SmithingRecipe>("bworld:smithing", { tool, material }); // or undefined
ctx.recipes.all<SmithingRecipe>("bworld:smithing"); // every one, in load order
```
`find` gives the first recipe, in load order, that `matches` says the input makes. Both give the recipes as `parse` made
them, not copies, so they aren't to be changed. A type can only be registered during `setup`, in the mod's own
namespace. Other mods can add recipes of your type too, as long as they depend on your mod.
Types that aren't registered, like `bworld:cooking` for now, are read as JSON instead:
```json
{
@@ -182,9 +229,7 @@ const recipes = ctx.recipes.list("bworld:cooking").map((json, i) => {
});
```
Throwing in `setup` stops the server from starting with the error, so a broken recipe is caught when the server starts
rather than when someone uses the machine. Other mods can add recipes of your type too, as long as they depend on your
mod.
Throwing in `setup` stops the server from starting with the error, like a broken recipe of a registered type does.
`list` returns copies, in load order. Client scripts can read every recipe with `ctx.recipes.list(type?)`, which is how
the recipe viewer shows mods' own types.
@@ -229,12 +274,12 @@ Make them from what's the same on every side, like items and their components, n
## Looking Up Recipes
Server scripts can ask about the base game's types:
Mods' registered types are looked up with [`find` and `all`](#custom-recipe-types). Server scripts can also ask about
the furnace's:
```ts
ctx.recipes.furnace_result("bworld:iron_ore"); // { output, cook_time }, or undefined
ctx.recipes.is_smeltable("bworld:iron_ore");
ctx.recipes.is_fuel("bworld:coal");
ctx.recipes.fuel_value("bworld:coal"); // burn time in ticks, 0 when it isn't fuel
ctx.recipes.smithing("bworld:wood_pickaxe", "bworld:stone"); // { result, material_count }, or undefined
```
+3 -2
View File
@@ -120,7 +120,8 @@ interface RecipeRegistry {
- Common scripts only run during loading and can't reach the world or players. They get the same `ctx` on both sides, so
they can't accidentally register different things.
- `register_type` would let `check-mods` and the loader validate mods' own recipe types, instead of each mod checking
its recipes in `setup`. `shaped`, `furnace`, `fuel` and `smithing` become short for `bworld:shaped` and so on.
its recipes in `setup`. `shaped`, `furnace` and `fuel` become short for `bworld:shaped` and so on. Shared scripts can
now register types with `ctx.recipes.register_type`, and `bworld:smithing` is one.
### The Freeze and the Registry Hash
@@ -276,7 +277,7 @@ engine assets in `assets/`.
| ---------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------- |
| Blocks, items, models, entities, recipes, textures, credits | `mods/bworld` | Done |
| Hoeing, chest, furnace, smithing table, farming, watering can, fishing | `mods/bworld/scripts/server.ts` | Done |
| Recipe types `shaped`, `furnace`, `fuel`, `smithing` | engine (`common/mod_loader.ts`, `server/game/crafting.ts`) | Phase 5 |
| Recipe types `shaped`, `furnace`, `fuel` | engine (`common/mod_loader.ts`, `server/game/crafting.ts`) | Phase 5 |
| The player's inventory screen and crafting grid | engine (`client/gui/gui_player_inventory.ts`, `server/game/crafting.ts`) | Phase 5 |
| The hotbar and crosshair | engine (`client/gui/hud.ts`) | Phase 5 |
| `/give`, `/time` and `/summon` | engine (`server/game/game_server.ts`) | Phase 5 |