Register recipe api
This commit is contained in:
@@ -140,6 +140,33 @@ 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.
|
||||
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user