Register recipe api

This commit is contained in:
2026-10-28 23:56:29 -03:00
parent 5d64b801a3
commit 43521b3675
9 changed files with 235 additions and 42 deletions
+3 -1
View File
@@ -96,7 +96,9 @@ Shared scripts hold a block component's rules, the code Minecraft runs on both s
(`state_for_placement`), how it changes with the blocks beside it (`on_neighbor_change`) and what right clicking it
changes (`on_interact`, like a trapdoor opening). See [Block Components](../blocks/components.md#rules). The server
script registers the rest of the same component, like its `on_break` or an `on_interact` that opens a screen, under the
same id. They also hold [fluids' rules](../resources/fluids.md#rules), which name and color a fluid by its data.
same id. They also hold [fluids' rules](../resources/fluids.md#rules), which name and color a fluid by its data, and
[recipes made in code](../resources/recipes.md#recipes-from-scripts), which every side has without the server sending
them.
On the server they run on the real world. On a client they run on what that client shows, when its player places, breaks
or right clicks a block, so a fence joins its neighbors the moment it's placed. What they change there is a guess, and
+27
View File
@@ -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: