Every recipe type registered from shared scripts

This commit is contained in:
2026-11-03 15:34:41 -03:00
committed by paula
parent 65113862f7
commit b329a61d96
25 changed files with 334 additions and 371 deletions
+3
View File
@@ -109,6 +109,9 @@ ctx.fluids.same(birch, { id: "copper_tools:syrup" }); // false, the data is diff
there as plain `copper_tools:syrup`. Client scripts also have `ctx.fluids.mod(id)`, the name of the mod that added it,
like `ctx.items.mod`.
Shared scripts have `ctx.fluids.exists` and `ctx.fluids.same` too, which don't depend on rules, like a
[recipe type](recipes.md#custom-recipe-types) that's found by a fluid.
## Containers
An item with `fluid_container` holds one fluid, full or empty, like a bucket or a bottle:
+20 -40
View File
@@ -74,8 +74,7 @@ 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:
For the stonecutter, which cuts one item into another. It's the base game's own type, registered by its shared script:
```json
{
@@ -96,7 +95,7 @@ uses up one of the input.
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:
juice:
```json
{
@@ -118,7 +117,7 @@ makes. The juicer uses the first recipe that takes the item.
A fluid that ferments into another over time, like juice into wine. The base game registers one from its
[shared script](#recipes-from-scripts) for every [juice](../items/food.md#juice), into wine from the same item. A file
can add one for anything else. It's checked when the server starts, like juicing:
can add one for anything else:
```json
{
@@ -179,9 +178,9 @@ 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"),
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) =>
@@ -193,10 +192,16 @@ export function setup(ctx: SharedContext) {
`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. `read.ingredient(json, field)` reads a [choice](#choices) of items, an item, a `#tag` or a list
of them, as every item that fits. Its tags have to exist, but can be empty.
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 scripts look recipes up by what's in the machine, like NeoForge's
`RecipeManager.getRecipeFor`:
@@ -210,36 +215,11 @@ ctx.recipes.all<SmithingRecipe>("bworld:smithing"); // every one, in load order
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:
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.
```json
{
"format_version": 1,
"recipe": {
"type": "bworld:cooking",
"ingredients": ["bworld:cabbage", ["bworld:carrot", "bworld:potato", "bworld:onion"]],
"result": { "id": "bworld: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("bworld: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, 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.
`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 a shared script makes recipes from another type's, see below.
## Recipes From Scripts