Files
bworld/docs/modding/resources/recipes.md
T

215 lines
7.4 KiB
Markdown

# 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.
## 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: `furnace` and `fuel`
```json
{
"format_version": 1,
"recipe": {
"type": "furnace",
"input": "bworld:iron_ore",
"output": { "id": "bworld:iron_ingot", "count": 1 },
"cook_time": 200
}
}
```
```json
{
"format_version": 1,
"recipe": { "type": "fuel", "item": "bworld:coal", "burn_time": 1000 }
}
```
`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`
For the smithing table, which upgrades a tool:
```json
{
"format_version": 1,
"recipe": {
"type": "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. Upgrading uses up the tool, `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 with 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, so `mods/bworld` checks these
recipes when the server starts rather than the engine:
```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 script](#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. It's checked when the server starts, like stonecutting:
```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.
## Choices
Anywhere a recipe takes an item (`key` values, `input`, `item`), 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 `farmersdelight:cooking` for the cooking pot. The engine only stores
these recipes. Everything else about them is up to the mod:
```json
{
"format_version": 1,
"recipe": {
"type": "farmersdelight:cooking",
"ingredients": ["farmersdelight:cabbage", ["bworld:carrot", "bworld:potato", "farmersdelight:onion"]],
"result": { "id": "farmersdelight:cabbage_rolls", "count": 1 },
"cook_time": 100
}
}
```
The mod's server script reads them in `setup` and checks their fields itself:
```ts
const recipes = ctx.recipes.list("farmersdelight:cooking").map((json, i) => {
if (!Array.isArray(json.ingredients) || json.ingredients.length > 6) {
throw new Error(`cooking recipe ${i + 1}: ingredients must be 1 to 6 items`);
}
// ...check the rest, and turn it into whatever the machine needs. expand turns #tags into their items
return { ingredients: json.ingredients.map((choice) => ctx.tags.expand(choice)) };
});
```
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.
`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.
## 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 script](../concepts/sides.md#shared-scripts) 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 "bworld/shared";
export function setup(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 } });
}
}
```
Shared scripts run 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 scripts, and recipe viewers see them
like any other. That's also why it's only in shared scripts. Recipes can only be registered during `setup`, before any
server or client script sets up, and one is checked like a file would be: a broken one, or a second furnace 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 scripts 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
Server scripts can ask about the base game's types:
```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
```