Better recipe api and port smithing to it
This commit is contained in:
@@ -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. |
|
||||
|
||||
@@ -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
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user