Files
bworld/docs/modding/resources/recipes.md
T
2026-11-14 12:18:41 -03:00

272 lines
11 KiB
Markdown

# Recipes
Recipes are data in a mod's `recipes/` folder, one per file. The engine has one type, `shaped`, for the crafting grid in
the player's inventory. Everything else, the base game's furnace and smithing table included, has a type of its own that
a mod defines.
## Crafting: `shaped`
For the crafting grid in the player's inventory screen:
```json
{
"format_version": 1,
"recipe": {
"type": "shaped",
"pattern": ["PPP", "P P", "PPP"],
"key": { "P": "bworld:oak_planks" },
"result": { "id": "bworld:chest", "count": 1 }
}
}
```
`pattern` is 1 to 3 rows of 1 to 3 characters, where a space is an empty slot. `key` maps each letter to an item. The
pattern fits anywhere in the grid.
## Smelting: `bworld:smelting` and `bworld:fuel`
For the furnace. They're the base game's own types, registered by its shared phase like
[any mod's](#custom-recipe-types):
```json
{
"format_version": 1,
"recipe": {
"type": "bworld:smelting",
"input": "bworld:iron_ore",
"output": { "id": "bworld:iron_ingot", "count": 1 },
"cook_time": 200
}
}
```
```json
{
"format_version": 1,
"recipe": { "type": "bworld:fuel", "item": "bworld:coal", "burn_time": 1000 }
}
```
`cook_time` and `burn_time` are in ticks, 20 to a second. Two smelting recipes for the same input are an error. For
fuel, a later recipe for the same item replaces an earlier one, so a mod that depends on the base game can change how
long something burns.
## Smithing: `bworld:smithing`
For the smithing table, which upgrades a tool. It's the base game's own type, registered by its shared phase like
[any mod's](#custom-recipe-types):
```json
{
"format_version": 1,
"recipe": {
"type": "bworld:smithing",
"tool": "bworld:wood_pickaxe",
"material": { "id": "bworld:stone", "count": 5 },
"result": "bworld:stone_pickaxe"
}
}
```
An optional `addition` is a third item it also takes, as `{ "id", "count" }` like the material. The tool and each `id`
are [choices](#choices), so `"material": { "id": "#bworld:cobblestones", "count": 5 }` takes 5 of any one cobblestone.
Upgrading uses up the tool and `count` of the material and the addition, and the result keeps the tool's `data`, like
its [durability](../items/tools.md#durability). Two smithing recipes that both fit the same tool, material and addition
are an error.
## Stonecutting: `bworld:stonecutting`
For the stonecutter, which cuts one item into another. It's the base game's own type, registered by its shared phase:
```json
{
"format_version": 1,
"recipe": {
"type": "bworld:stonecutting",
"input": "bworld:cobblestone",
"output": { "id": "bworld:stone_bricks", "count": 1 }
}
}
```
Every recipe that takes the item in the stonecutter's input is listed, in load order, and the player picks one. Each cut
uses up one of the input.
## Juicing: `bworld:juicing`
For the juicer, which presses an item into a fluid. The base game registers one from its
[shared phase](#recipes-from-scripts) for every item with [`bworld:juiceable`](../items/food.md#juice), making the juice
named after it, so most mods never write one. A file can add one for anything else, or make something other than juice:
```json
{
"format_version": 1,
"recipe": {
"type": "bworld:juicing",
"input": "copper_tools:maple_sap_bucket",
"fluid_results": [
{ "fluid": { "id": "copper_tools:syrup", "data": { "tree": "bworld:maple" } }, "amount": 250 }
]
}
}
```
`input` is an item, a `#tag` or a list of them, and `fluid_results` is one fluid, with an amount in mB, that one of it
makes. The juicer uses the first recipe that takes the item.
## Fermenting: `bworld:fermentable`
A fluid that ferments into another over time, like juice into wine. The base game registers one from its
[shared phase](#recipes-from-scripts) for every [juice](../items/food.md#juice), into wine from the same item. A file
can add one for anything else:
```json
{
"format_version": 1,
"recipe": {
"type": "bworld:fermentable",
"input": { "fluid": { "id": "copper_tools:syrup", "data": { "tree": "bworld:maple" } }, "amount": 1000 },
"output": { "fluid": { "id": "copper_tools:maple_liqueur" }, "amount": 500 },
"ticks": 20
}
}
```
`input` and `output` are each a fluid with an amount in mB: that much of the input ferments into that much of the
output. `ticks` is how long each mB of the input takes, 20 to a second.
The keg ferments what's in its input tank by the first recipe for that fluid, 1 mB every `ticks` ticks, while its output
tank has room for what that makes. The output comes a bit at a time, spread so each recipe's worth of input makes the
whole `output`: the example above makes 1 mB of liqueur for every 2 mB of syrup. It only ferments near a player, like a
furnace smelts.
## Choices
Anywhere a recipe takes a choice of items (`shaped` keys, and the fields a type reads with
[`read.ingredient`](#custom-recipe-types), like smelting's `input`, fuel's `item` and smithing's `tool`), an item
[tag](tags.md) means anything in it fits, and so does a list of ids and tags:
```json
"key": { "P": "#bworld:planks" }
```
```json
"item": ["bworld:coal", "#bworld:logs"]
```
In the crafting grid, each slot can hold a different one of them, so a stick can be made from oak and birch planks
together. The recipe viewer takes turns showing them.
Use a tag rather than listing ids when other mods might have more of the same thing: a mod that adds planks to
`#bworld:planks` makes them work in every recipe that takes it.
## Custom Recipe Types
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 phase](../concepts/sides.md#shared), like NeoForge's `RecipeType` with its serializer:
```ts
import type { ItemStack, SharedContext } from "cubi/shared";
interface SmithingRecipe {
tool: string;
material: { id: string; count: number };
result: string;
}
export function shared(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, "tool"),
material: read.stack(json.material, "material"),
result: read.item(json.result, "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` checks a value the way the engine's own types
are checked, throwing with the name it's given, like `"result"` or `"ingredients[0]"`:
| Reader | Reads |
| -------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| `read.item(value, name)` | An item id, of an item that exists. |
| `read.stack(value, name)` | `{ id, count }` of an item that exists. |
| `read.ingredient(value, name)` | A [choice](#choices): an item, a `#tag` or a list of them, as every item that fits. Its tags have to exist, but can be empty. |
| `read.fluid_stack(value, name)` | `{ fluid: { id, data }, amount }` of a fluid that exists, in mB. |
| `read.positive_int(value, name)` | A whole number, at least 1, like a time in ticks. |
Then server, client and shared phases 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 while the shared phase runs, in the mod's
own namespace. Other mods can add recipes of your type too, as long as they depend on your mod.
A recipe of a type no mod registers, like a misspelled one, stops the first mod with one from loading once every shared
script has set up, since nothing would ever read it.
`ctx.recipes.list(type?)` gives recipes as their JSON instead, copies in load order. It's how the recipe viewer shows
every mod's types, and how the shared phase makes recipes from another type's, see below.
## Recipes From Scripts
A recipe that follows from other data, like one for every item with a component, can be made in the mod's
[shared phase](../concepts/sides.md#shared) instead of written out as files. `ctx.recipes.register(type, json)` adds one
like a file in `recipes/` with that `type` would, with the rest of its fields from `json`:
```ts
import type { SharedContext } from "cubi/shared";
export function shared(ctx: SharedContext) {
// a sieve recipe for every item that says what it sifts into
for (const id of ctx.items.ids()) {
const sifts = ctx.items.components(id)["copper_tools:siftable"] as { into: string } | undefined;
if (sifts) ctx.recipes.register("copper_tools:sifting", { input: id, result: { id: sifts.into, count: 1 } });
}
}
```
The shared phase runs on the server and on every player's client, from the same data, so every side makes the same
recipes and nothing is sent: the crafting grid, `ctx.recipes.list` in server and client phases, and recipe viewers see
them like any other. That's also why it's only in the shared phase. Recipes can only be registered while it runs, before
any server or client phase sets up, and one is checked like a file would be: a broken one, or a second smelting recipe
for the same input, throws, which stops the mod loading.
`ctx.recipes.list(type?)` reads the recipes there are so far, copies in load order, like the client's: every mod's
`recipes/` files, and what the shared phases that set up before this one registered. Dependencies set up first, so a mod
can make recipes of its own from another mod's, like a machine of its own for every one of the base game's juicing
recipes:
```ts
for (const juicing of ctx.recipes.list("bworld:juicing")) {
ctx.recipes.register("copper_tools:squeezing", { input: juicing.input, fluid_results: juicing.fluid_results });
}
```
Make them from what's the same on every side, like items and their components, not from the server's
[config](../misc/config.md), which clients don't have.
## Looking Up Recipes
Every registered type, the base game's included, is looked up with [`find` and `all`](#custom-recipe-types). The base
game's smelting and fuel recipes are found by an item id:
```ts
ctx.recipes.find("bworld:smelting", "bworld:iron_ore"); // { input, output, cook_time }, or undefined
ctx.recipes.find("bworld:fuel", "bworld:coal"); // { items, burn_time }, or undefined
```