From 65113862f738b53e5e4b98e0aed474dc293c5ff9 Mon Sep 17 00:00:00 2001 From: Paula Date: Sun, 1 Nov 2026 20:41:23 -0300 Subject: [PATCH] Recipe types from shared scripts and stuff --- common/mod_api/server.ts | 8 +-- common/mod_api/shared.ts | 2 + common/mod_check.ts | 17 ++--- common/mod_data.ts | 26 ++------ common/mod_loader.ts | 35 +++++----- docs/modding/blocks/components.md | 2 +- docs/modding/inventories/menus.md | 6 +- docs/modding/resources/recipes.md | 39 ++++++----- docs/modding/resources/tags.md | 2 +- docs/modding_plan.md | 6 +- mods/bworld/recipes/fuel_coal.json | 2 +- mods/bworld/recipes/fuel_log.json | 2 +- mods/bworld/recipes/smelting_coal_ore.json | 2 +- mods/bworld/recipes/smelting_cod.json | 2 +- mods/bworld/recipes/smelting_copper_ore.json | 2 +- mods/bworld/recipes/smelting_glass.json | 2 +- mods/bworld/recipes/smelting_gold_ore.json | 2 +- mods/bworld/recipes/smelting_iron_ore.json | 2 +- mods/bworld/recipes/smelting_log.json | 2 +- mods/bworld/recipes/smelting_salmon.json | 2 +- .../bworld/recipes/smelting_smooth_stone.json | 2 +- mods/bworld/recipes/smelting_stone.json | 2 +- mods/bworld/recipes/smelting_tin_ore.json | 2 +- mods/bworld/scripts/server.ts | 12 ++-- mods/bworld/scripts/shared.ts | 2 + mods/bworld/scripts/smelting.ts | 65 +++++++++++++++++++ mods/recipe_viewer/scripts/categories.ts | 6 +- mods/recipe_viewer/scripts/recipes.ts | 10 +-- server/game/mod_runtime.ts | 18 +---- tests/mod_loading_test.ts | 5 +- tests/recipe_viewer_test.ts | 18 +++-- tests/smelting_test.ts | 50 ++++++++++++++ tests/tags_test.ts | 7 +- tests/wood_test.ts | 6 +- 34 files changed, 234 insertions(+), 134 deletions(-) create mode 100644 mods/bworld/scripts/smelting.ts create mode 100644 tests/smelting_test.ts diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index f63de7b..b65fb18 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -644,10 +644,6 @@ export interface ServerFluidApi extends FluidApi { // find and all read the recipes of types shared scripts registered export interface RecipeApi extends RecipeLookup { - furnace_result(input: Id): { output: ItemStack; cook_time: number } | undefined; - fuel_value(item: Id): number; // burn time in ticks, 0 if it isn't fuel - is_fuel(item: Id): boolean; - is_smeltable(item: Id): boolean; // every recipe of a mod's own type, like "bworld:cooking", as its json. copies, in load order // deno-lint-ignore no-explicit-any list(type: Id): any[]; @@ -726,14 +722,12 @@ export type FormResult = ({ canceled: true } & Partial) | ({ canceled: fal // menus, like neoforge's: the server side of a gui, see docs/modding/inventories/menus.md -export type SlotFilter = "smeltable" | "fuel" | ((item: ItemStack) => boolean); - export interface SlotOptions { // gui pixels from the screen's top left to the slot's 18 pixel frame x: number; y: number; // what can be put in, checked on the server - filter?: SlotFilter; + filter?: (item: ItemStack) => boolean; // can only be taken from, all at once, like the furnace result output_only?: boolean; // runs when a player takes from an output_only slot, before they get it. false stops them taking it diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index 125a6b0..d6dc1f8 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -72,6 +72,8 @@ export interface RecipeType { export interface RecipeReader { item(json: Record, field: string): Id; stack(json: Record, field: string): { id: Id; count: number }; + // an item, a #tag or a list of them, any of which fits, as every item that does, like the engine's shaped keys + ingredient(json: Record, field: string): Id[]; } // the recipes of registered types, as their parse made them, in load order. not to be changed diff --git a/common/mod_check.ts b/common/mod_check.ts index 925f32a..eda2276 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -348,20 +348,11 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable - | z.infer - | z.infer - | ModRecipeJson; +export type RecipeJson = z.infer | ModRecipeJson; const engine_recipes = new Map([ ["shaped", shaped_recipe], - ["furnace", furnace_recipe], - ["fuel", fuel_recipe], ]); // a mod's own types, like "bworld:cooking" for its cooking pot, are only stored by the engine. the mod parses them, @@ -625,10 +609,10 @@ const engine_recipes = new Map([ const recipe_schema = by_kind( "type", (type) => engine_recipes.get(type) ?? (ID_PATTERN.test(type) ? json_object : undefined), - 'must be "shaped", "furnace", "fuel" or a mod\'s own type like "bworld:cooking"', + 'must be "shaped" or a mod\'s own type like "bworld:cooking"', ); -// a recipe type a mod made up, as opposed to the engine's shaped, furnace and fuel +// a recipe type a mod made up, as opposed to the engine's shaped export function is_mod_recipe(recipe: RecipeJson): recipe is ModRecipeJson { return recipe.type.includes(":"); } @@ -643,6 +627,10 @@ export function stack_problems(json: unknown): Problems { return find_problems(stack, json); } +export function choice_problems(json: unknown): Problems { + return find_problems(item_choice, json); +} + // the crafting grid's format, see server/game/crafting.ts export interface GridRecipe { width: number; diff --git a/common/mod_loader.ts b/common/mod_loader.ts index 1095b97..5929b58 100644 --- a/common/mod_loader.ts +++ b/common/mod_loader.ts @@ -8,6 +8,8 @@ import { block_from_json, BlockJson, choice_ids, + choice_list, + choice_problems, EffectJson, EntityJson, EntityModelJson, @@ -86,8 +88,6 @@ export interface ModListing { export class RecipeBook { shaped: GridRecipe[] = []; - furnace = new Map(); - fuel = new Map(); // recipes of mods' own types, by type by_type = new Map(); // the types shared scripts registered, and their recipes as parse made them, see SharedContext.recipes.register_type @@ -202,8 +202,7 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe return recipes; } -// puts a recipe in the book, from a mod's recipes/ or a shared script. throws when it's for the same furnace input as -// one already there, or its registered type's parse throws +// puts a recipe in the book, from a mod's recipes/ or a shared script. throws when its registered type's parse throws export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) { recipes.all.push(recipe); if (is_mod_recipe(recipe)) { @@ -214,20 +213,7 @@ export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) { if (type) recipes.parsed.get(recipe.type)!.push(parse_recipe(type, recipe, list.length - 1)); return; } - switch (recipe.type) { - case "shaped": - recipes.shaped.push(grid_recipe_from_json(recipe)); - break; - case "furnace": - for (const input of choice_ids(recipe.input)) { - if (recipes.furnace.has(input)) throw new Error(`two furnace recipes for ${input}`); - recipes.furnace.set(input, { output: recipe.output, cook_time: recipe.cook_time }); - } - break; - case "fuel": - for (const item of choice_ids(recipe.item)) recipes.fuel.set(item, recipe.burn_time); - break; - } + recipes.shaped.push(grid_recipe_from_json(recipe)); } // the index'th recipe of its type, turned into what its type's parse makes of it, or throwing naming it @@ -239,13 +225,24 @@ function parse_recipe(type: RecipeType, json: ModRecipeJson, i } } -// items are registered before any script sets up, so the ones recipes name can be checked +// items and tags load before any script sets up, so the ones recipes name can be checked const recipe_reader: RecipeReader = { item: (json, field) => checked_item(field, item_problems(json[field]), json[field] as string), stack(json, field) { const stack = json[field] as { id: string; count: number } | undefined; return { id: checked_item(field, stack_problems(stack), stack?.id as string), count: stack!.count }; }, + // a tag can be empty, like one of another mod's items that isn't installed, but it has to exist + ingredient(json, field) { + const choice = json[field] as string | string[]; + const problems = choice_problems(choice); + if (problems.length > 0) throw new Error(`${field} ${problems[0]}`); + for (const entry of choice_list(choice)) { + if (!entry.startsWith("#")) checked_item(field, [], entry); + else if (!Tags.exists("items", entry)) throw new Error(`${field}: there's no item tag ${entry}`); + } + return choice_ids(choice); + }, }; // the item a field names, once the field has no problems diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 9d9ddc2..2df34a7 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -320,7 +320,7 @@ catch up. If it's further behind than that, it skips the rest and logs a warning | ----------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `bworld:storage` | `rows` | A chest: `rows` × 9 slots, dropped when it's broken. | | `bworld:shipping_bin` | `rows` | Like `bworld:storage`, and every morning at 06:00 sells what's in it that has a sell value for coins. | -| `bworld:furnace` | none | Smelts with `furnace` and `fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. | +| `bworld:furnace` | none | Smelts with `bworld:smelting` and `bworld:fuel` [recipes](../resources/recipes.md). Needs `lit` and `facing` states, see `blocks/furnace.json`. | | `bworld:smithing_table` | none | Upgrades tools with `bworld:smithing` recipes. | | `bworld:stonecutter` | none | Cuts one item into another with `bworld:stonecutting` recipes, picked from a list. | | `bworld:hoeable` | `tool`, `into` | Right clicking with a tool of type `tool` turns it into `into`, like a hoe on grass. | diff --git a/docs/modding/inventories/menus.md b/docs/modding/inventories/menus.md index 9d59043..85e867a 100644 --- a/docs/modding/inventories/menus.md +++ b/docs/modding/inventories/menus.md @@ -22,8 +22,8 @@ export function setup(ctx: ServerContext) { const smelter = ctx.menus.register("copper_tools:smelter", (menu, block, player) => { const container = ctx.containers.get(block.data.container)!; menu.title = "Smelter"; - menu.add_slot(container, 0, { x: 55, y: 16, filter: "smeltable" }); - menu.add_slot(container, 1, { x: 55, y: 52, filter: "fuel" }); + menu.add_slot(container, 0, { x: 55, y: 16, filter: (item) => !!ctx.recipes.find("bworld:smelting", item.id) }); + menu.add_slot(container, 1, { x: 55, y: 52, filter: (item) => !!ctx.recipes.find("bworld:fuel", item.id) }); menu.add_slot(container, 2, { x: 115, y: 34, output_only: true }); menu.add_player_inventory(7, 84); menu.add_data("progress", () => block.data.progress); @@ -57,7 +57,7 @@ returns the slot's **menu slot index**: slots are numbered in the order they're | Option | Meaning | | ------------- | ----------------------------------------------------------------------------------------------------------------- | | `x`, `y` | Where its 18 pixel frame goes, in GUI pixels from the screen's top left. | -| `filter` | What can be put in: `"smeltable"` (has a furnace recipe), `"fuel"`, or a function `(item) => boolean`. | +| `filter` | What can be put in: a function `(item) => boolean`, like one that looks for a [recipe](../resources/recipes.md). | | `output_only` | Can only be taken from, all at once, like the furnace result. | | `on_take` | For `output_only` slots: runs when a player takes from it, before they get the item. Return `false` to stop them. | | `icon` | A texture drawn in the slot while it's empty, like Minecraft's armor slots. | diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index e1896f8..8eeafd0 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -1,7 +1,8 @@ # Recipes -Recipes are data in a mod's `recipes/` folder, one per file. The engine has three types, for the crafting grid and the -furnace. Everything else, the base game's smithing table included, has a type of its own that a mod defines. +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` @@ -22,13 +23,16 @@ For the crafting grid in the player's inventory screen: `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` +## Smelting: `bworld:smelting` and `bworld:fuel` + +For the furnace. They're the base game's own types, registered by its shared script like +[any mod's](#custom-recipe-types): ```json { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:iron_ore", "output": { "id": "bworld:iron_ingot", "count": 1 }, "cook_time": 200 @@ -39,12 +43,13 @@ pattern fits anywhere in the grid. ```json { "format_version": 1, - "recipe": { "type": "fuel", "item": "bworld:coal", "burn_time": 1000 } + "recipe": { "type": "bworld: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. +`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` @@ -137,8 +142,9 @@ furnace smelts. ## 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: +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` and fuel's `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" } @@ -189,7 +195,8 @@ export function setup(ctx: SharedContext) { 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. +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. Then server, client and shared scripts look recipes up by what's in the machine, like NeoForge's `RecipeManager.getRecipeFor`: @@ -255,7 +262,7 @@ export function setup(ctx: SharedContext) { 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 +server or client script 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 @@ -274,12 +281,10 @@ Make them from what's the same on every side, like items and their components, n ## Looking Up Recipes -Mods' registered types are looked up with [`find` and `all`](#custom-recipe-types). Server scripts can also ask about -the furnace's: +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.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.find("bworld:smelting", "bworld:iron_ore"); // { input, output, cook_time }, or undefined +ctx.recipes.find("bworld:fuel", "bworld:coal"); // { items, burn_time }, or undefined ``` diff --git a/docs/modding/resources/tags.md b/docs/modding/resources/tags.md index d754502..48d876a 100644 --- a/docs/modding/resources/tags.md +++ b/docs/modding/resources/tags.md @@ -80,7 +80,7 @@ back. Depend on the mod whose tag you change, so your file loads after its. chec ## Using Tags in Data -- **Recipes**: anywhere a recipe takes an item (shaped `key` values, furnace `input`, fuel `item`) it can take an item +- **Recipes**: anywhere a recipe takes an item (shaped `key` values, smelting `input`, fuel `item`) it can take an item tag, on its own or in a list: `"key": { "P": "#bworld:planks" }`. See [Choices](recipes.md#choices). - **Blocks**: `place_on` and `place_in` take block tags, like `"place_on": ["#bworld:logs"]` for a fungus that grows on any log. See [Placement](../blocks/index.md#placement). diff --git a/docs/modding_plan.md b/docs/modding_plan.md index c7e930d..b9c9f1b 100644 --- a/docs/modding_plan.md +++ b/docs/modding_plan.md @@ -120,8 +120,8 @@ interface RecipeRegistry { - Common scripts only run during loading and can't reach the world or players. They get the same `ctx` on both sides, so they can't accidentally register different things. - `register_type` would let `check-mods` and the loader validate mods' own recipe types, instead of each mod checking - its recipes in `setup`. `shaped`, `furnace` and `fuel` become short for `bworld:shaped` and so on. Shared scripts can - now register types with `ctx.recipes.register_type`, and `bworld:smithing` is one. + its recipes in `setup`. `shaped` becomes short for `bworld:shaped`. Shared scripts can now register types with + `ctx.recipes.register_type`, and `bworld:smithing`, `bworld:smelting` and `bworld:fuel` are. ### The Freeze and the Registry Hash @@ -277,7 +277,7 @@ engine assets in `assets/`. | ---------------------------------------------------------------------- | ------------------------------------------------------------------------ | ------- | | Blocks, items, models, entities, recipes, textures, credits | `mods/bworld` | Done | | Hoeing, chest, furnace, smithing table, farming, watering can, fishing | `mods/bworld/scripts/server.ts` | Done | -| Recipe types `shaped`, `furnace`, `fuel` | engine (`common/mod_loader.ts`, `server/game/crafting.ts`) | Phase 5 | +| Recipe type `shaped` | engine (`common/mod_loader.ts`, `server/game/crafting.ts`) | Phase 5 | | The player's inventory screen and crafting grid | engine (`client/gui/gui_player_inventory.ts`, `server/game/crafting.ts`) | Phase 5 | | The hotbar and crosshair | engine (`client/gui/hud.ts`) | Phase 5 | | `/give`, `/time` and `/summon` | engine (`server/game/game_server.ts`) | Phase 5 | diff --git a/mods/bworld/recipes/fuel_coal.json b/mods/bworld/recipes/fuel_coal.json index a03706e..ecf8d6f 100644 --- a/mods/bworld/recipes/fuel_coal.json +++ b/mods/bworld/recipes/fuel_coal.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "fuel", + "type": "bworld:fuel", "item": "bworld:coal", "burn_time": 1000 } diff --git a/mods/bworld/recipes/fuel_log.json b/mods/bworld/recipes/fuel_log.json index cf73ebb..bfed909 100644 --- a/mods/bworld/recipes/fuel_log.json +++ b/mods/bworld/recipes/fuel_log.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "fuel", + "type": "bworld:fuel", "item": "#bworld:logs", "burn_time": 100 } diff --git a/mods/bworld/recipes/smelting_coal_ore.json b/mods/bworld/recipes/smelting_coal_ore.json index 3467b75..b94bef4 100644 --- a/mods/bworld/recipes/smelting_coal_ore.json +++ b/mods/bworld/recipes/smelting_coal_ore.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:coal_ore", "output": { "id": "bworld:coal", diff --git a/mods/bworld/recipes/smelting_cod.json b/mods/bworld/recipes/smelting_cod.json index 621bf04..0b4cf8f 100644 --- a/mods/bworld/recipes/smelting_cod.json +++ b/mods/bworld/recipes/smelting_cod.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:cod", "output": { "id": "bworld:cooked_cod", diff --git a/mods/bworld/recipes/smelting_copper_ore.json b/mods/bworld/recipes/smelting_copper_ore.json index e030ff2..4a9a647 100644 --- a/mods/bworld/recipes/smelting_copper_ore.json +++ b/mods/bworld/recipes/smelting_copper_ore.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:copper_ore", "output": { "id": "bworld:copper_ingot", diff --git a/mods/bworld/recipes/smelting_glass.json b/mods/bworld/recipes/smelting_glass.json index d604f5e..d92bec3 100644 --- a/mods/bworld/recipes/smelting_glass.json +++ b/mods/bworld/recipes/smelting_glass.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:sand", "output": { "id": "bworld:glass", diff --git a/mods/bworld/recipes/smelting_gold_ore.json b/mods/bworld/recipes/smelting_gold_ore.json index 9f7e71f..44af3cb 100644 --- a/mods/bworld/recipes/smelting_gold_ore.json +++ b/mods/bworld/recipes/smelting_gold_ore.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:gold_ore", "output": { "id": "bworld:gold_ingot", diff --git a/mods/bworld/recipes/smelting_iron_ore.json b/mods/bworld/recipes/smelting_iron_ore.json index 519caf4..509cdbf 100644 --- a/mods/bworld/recipes/smelting_iron_ore.json +++ b/mods/bworld/recipes/smelting_iron_ore.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:iron_ore", "output": { "id": "bworld:iron_ingot", diff --git a/mods/bworld/recipes/smelting_log.json b/mods/bworld/recipes/smelting_log.json index 110ad59..8d5093e 100644 --- a/mods/bworld/recipes/smelting_log.json +++ b/mods/bworld/recipes/smelting_log.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "#bworld:logs", "output": { "id": "bworld:coal", diff --git a/mods/bworld/recipes/smelting_salmon.json b/mods/bworld/recipes/smelting_salmon.json index 6822e0b..506a226 100644 --- a/mods/bworld/recipes/smelting_salmon.json +++ b/mods/bworld/recipes/smelting_salmon.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:salmon", "output": { "id": "bworld:cooked_salmon", diff --git a/mods/bworld/recipes/smelting_smooth_stone.json b/mods/bworld/recipes/smelting_smooth_stone.json index 20faa24..75af617 100644 --- a/mods/bworld/recipes/smelting_smooth_stone.json +++ b/mods/bworld/recipes/smelting_smooth_stone.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:stone", "output": { "id": "bworld:smooth_stone", diff --git a/mods/bworld/recipes/smelting_stone.json b/mods/bworld/recipes/smelting_stone.json index 7f3f4d1..69f7c97 100644 --- a/mods/bworld/recipes/smelting_stone.json +++ b/mods/bworld/recipes/smelting_stone.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:cobblestone", "output": { "id": "bworld:stone", diff --git a/mods/bworld/recipes/smelting_tin_ore.json b/mods/bworld/recipes/smelting_tin_ore.json index 39358de..03f542f 100644 --- a/mods/bworld/recipes/smelting_tin_ore.json +++ b/mods/bworld/recipes/smelting_tin_ore.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "furnace", + "type": "bworld:smelting", "input": "bworld:tin_ore", "output": { "id": "bworld:tin_ingot", diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index 4c2f9ea..a21447d 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -8,6 +8,7 @@ import { register_effects } from "./effects.ts"; import { register_fishing } from "./fishing.ts"; import { stonecutting_options, stonecutting_recipes } from "./stonecutting.ts"; import { SMITHING, type SmithingRecipe } from "./smithing.ts"; +import { FUEL, type FuelRecipe, SMELTING, type SmeltingRecipe } from "./smelting.ts"; import { register_verdigris } from "./verdigris/server.ts"; import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts"; import { juicing_for, juicing_recipes } from "./juicing.ts"; @@ -219,7 +220,7 @@ function register_furnace(ctx: ServerContext) { const container = ctx.containers.get(furnace_data(block).container)!; menu.title = "Furnace"; menu.add_slot(container, 0, { x: 55, y: 16 }); - menu.add_slot(container, 1, { x: 55, y: 52, filter: "fuel" }); + menu.add_slot(container, 1, { x: 55, y: 52, filter: (item) => ctx.recipes.find(FUEL, item.id) !== undefined }); menu.add_slot(container, 2, { x: 115, y: 34, output_only: true }); menu.add_player_inventory(7, INVENTORY_Y); for (const field of ["progress", "progress_max", "fuel", "fuel_max"] as const) { @@ -250,7 +251,7 @@ function register_furnace(ctx: ServerContext) { const data = furnace_data(block); const container = ctx.containers.get(data.container)!; const input = container.get(0); - const recipe = input && ctx.recipes.furnace_result(input.id); + const recipe = input && ctx.recipes.find(SMELTING, input.id); if (data.fuel > 0) { data.fuel -= 1; @@ -261,8 +262,9 @@ function register_furnace(ctx: ServerContext) { } else { // refuel const fuel = container.get(1); - if (data.fuel === 0 && fuel && ctx.recipes.is_fuel(fuel.id)) { - data.fuel = ctx.recipes.fuel_value(fuel.id); + const burns = fuel && ctx.recipes.find(FUEL, fuel.id); + if (data.fuel === 0 && fuel && burns) { + data.fuel = burns.burn_time; data.fuel_max = data.fuel; fuel.count -= 1; } @@ -277,7 +279,7 @@ function register_furnace(ctx: ServerContext) { if (result) { result.count += recipe.output.count; } else { - container.set(2, recipe.output); + container.set(2, { ...recipe.output }); } input.count -= 1; } diff --git a/mods/bworld/scripts/shared.ts b/mods/bworld/scripts/shared.ts index c07bead..dcb5642 100644 --- a/mods/bworld/scripts/shared.ts +++ b/mods/bworld/scripts/shared.ts @@ -8,6 +8,7 @@ import type { SharedBlock, SharedContext } from "bworld/shared"; import { register_fermenting } from "./fermenting.ts"; import { register_juicing } from "./juicing.ts"; +import { register_smelting } from "./smelting.ts"; import { register_smithing } from "./smithing.ts"; import { register_verdigris_rules } from "./verdigris/shared.ts"; @@ -20,6 +21,7 @@ export function setup(ctx: SharedContext) { register_juicing(ctx); register_fermenting(ctx); register_smithing(ctx); + register_smelting(ctx); } // the sides a fence connects on, and the face of the block there it connects to diff --git a/mods/bworld/scripts/smelting.ts b/mods/bworld/scripts/smelting.ts new file mode 100644 index 0000000..666edd2 --- /dev/null +++ b/mods/bworld/scripts/smelting.ts @@ -0,0 +1,65 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// smelting and fuel recipes: the furnace cooks one item into another, burning fuel to do it. both are found by an item +// id, and registered by the shared script like smithing.ts +import type { SharedContext } from "bworld/shared"; + +export const SMELTING = "bworld:smelting"; +export const FUEL = "bworld:fuel"; + +export interface SmeltingRecipe { + // the items it smelts + input: string[]; + output: { id: string; count: number }; + // ticks + cook_time: number; +} + +export interface FuelRecipe { + items: string[]; + // ticks + burn_time: number; +} + +export function register_smelting(ctx: SharedContext) { + // deno-lint-ignore no-explicit-any + const ticks = (json: any, field: string): number => { + if (!Number.isInteger(json[field]) || json[field] < 1) throw new Error(`${field} must be at least 1 tick`); + return json[field]; + }; + + // two recipes for one item would leave one that can never be made + const smelted = new Set(); + ctx.recipes.register_type(SMELTING, { + parse(json, read) { + const recipe = { + input: read.ingredient(json, "input"), + output: read.stack(json, "output"), + cook_time: ticks(json, "cook_time"), + }; + for (const id of recipe.input) { + if (smelted.has(id)) throw new Error(`there's already one for ${id}`); + smelted.add(id); + } + return recipe; + }, + matches: (recipe, id) => recipe.input.includes(id), + }); + + // a later recipe for an item takes it out of the earlier one's items, so a mod can change how long something + // burns and find gives its recipe + const burning = new Map(); + ctx.recipes.register_type(FUEL, { + parse(json, read) { + const recipe = { items: read.ingredient(json, "item"), burn_time: ticks(json, "burn_time") }; + for (const id of recipe.items) { + const earlier = burning.get(id); + if (earlier) earlier.items = earlier.items.filter((other) => other !== id); + burning.set(id, recipe); + } + return recipe; + }, + matches: (recipe, id) => recipe.items.includes(id), + }); +} diff --git a/mods/recipe_viewer/scripts/categories.ts b/mods/recipe_viewer/scripts/categories.ts index 6e15f34..a64bc0c 100644 --- a/mods/recipe_viewer/scripts/categories.ts +++ b/mods/recipe_viewer/scripts/categories.ts @@ -66,7 +66,7 @@ function base_categories(tags: TagApi): RecipeCategory[] { }, }, { - type: "furnace", + type: "bworld:smelting", title: "Smelting", width: 82, height: 54, @@ -80,7 +80,7 @@ function base_categories(tags: TagApi): RecipeCategory[] { }, }, { - type: "fuel", + type: "bworld:fuel", title: "Fuel", width: 98, height: 36, @@ -323,8 +323,6 @@ export function make_categories(ctx: ClientContext, recipes: Recipe[]): RecipeCa export function category_name(type: string): string { const base: Record = { shaped: "Crafting", - furnace: "Smelting", - fuel: "Fuel", [FISHING]: "Fishing", [CRABPOT]: "Crab Pot", [JUICING]: "Juicing", diff --git a/mods/recipe_viewer/scripts/recipes.ts b/mods/recipe_viewer/scripts/recipes.ts index 39d35d0..6e51c58 100644 --- a/mods/recipe_viewer/scripts/recipes.ts +++ b/mods/recipe_viewer/scripts/recipes.ts @@ -17,9 +17,9 @@ export function outputs(recipe: Recipe): Ingredient[] { switch (recipe.type) { case "shaped": return [stack(recipe.result)]; - case "furnace": + case "bworld:smelting": return [stack(recipe.output)]; - case "fuel": + case "bworld:fuel": return []; case "bworld:smithing": return [{ ids: [recipe.result], count: 1 }]; @@ -46,7 +46,7 @@ export function fluid_inputs(recipe: Recipe): FluidAmount[] { return recipe.fluid_ingredients ?? []; } -// what a recipe takes. shaped keys, furnace inputs and fuels can be an item or #tag, or a list any of which fits +// what a recipe takes. shaped keys, smelting inputs and fuels can be an item or #tag, or a list any of which fits export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] { switch (recipe.type) { case "shaped": @@ -54,9 +54,9 @@ export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] { ids: tags.expand(choice), count: 1, })); - case "furnace": + case "bworld:smelting": return [{ ids: tags.expand(recipe.input), count: 1 }]; - case "fuel": + case "bworld:fuel": return [{ ids: tags.expand(recipe.item), count: 1 }]; case "bworld:smithing": return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat( diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 57b70c5..2fe86e1 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -257,7 +257,7 @@ export class ModRuntime { const ok = run_guarded(mod, "an extend_player_menu", () => { build({ add_slot: (container, slot, options) => { - added.push(menu_slot(this.game, mod, player, container, slot, options)); + added.push(menu_slot(mod, player, container, slot, options)); return menu.slots.length + added.length - 1; }, }, api); @@ -1007,13 +1007,6 @@ export class ModRuntime { interact, }, recipes: { - furnace_result: (input) => { - const recipe = game().recipes.furnace.get(input); - return recipe && { output: { ...recipe.output }, cook_time: recipe.cook_time }; - }, - fuel_value: (item) => game().recipes.fuel.get(item) ?? 0, - is_fuel: (item) => game().recipes.fuel.has(item), - is_smeltable: (item) => game().recipes.furnace.has(item), ...recipe_lookup(() => game().recipes), list: (type) => structuredClone(game().recipes.by_type.get(type) ?? []), }, @@ -1149,7 +1142,7 @@ export class ModRuntime { menu.open_data = structuredClone(value) ?? null; }, add_slot(container, slot, options) { - menu.slots.push(menu_slot(game, mod, server_player, container, slot, options)); + menu.slots.push(menu_slot(mod, server_player, container, slot, options)); return menu.slots.length - 1; }, add_fluid_slot(handler, tank, options) { @@ -1327,7 +1320,6 @@ export function new_stack(item: ItemStack): EngineItemStack { // a menu's slot from what a mod's add_slot got function menu_slot( - game: GameServer, mod: string, player: ServerPlayer, container: Container, @@ -1345,11 +1337,7 @@ function menu_slot( const { filter, on_take } = options; const guarded = (what: string, fn: () => T) => run_guarded(mod, what, fn); const added: MenuSlot = { container: engine, index: slot, x: options.x, y: options.y }; - if (filter === "smeltable") { - added.filter = (item) => game.recipes.furnace.has(item.type_id); - } else if (filter === "fuel") { - added.filter = (item) => game.recipes.fuel.has(item.type_id); - } else if (typeof filter === "function") { + if (typeof filter === "function") { added.filter = (item) => guarded("a slot filter", () => filter(item_api(item))) === true; } if (options.output_only) added.output = true; diff --git a/tests/mod_loading_test.ts b/tests/mod_loading_test.ts index ac8cbb8..ade8a8b 100644 --- a/tests/mod_loading_test.ts +++ b/tests/mod_loading_test.ts @@ -10,6 +10,7 @@ import { block_variant } from "$/common/block_models.ts"; import { load_worldgen } from "$/common/worldgen_loader.ts"; import { create_mod } from "$/tools/new_mod.ts"; import { PLAYER_MENU_CRAFTING, PROTOCOL_VERSION } from "$/common/protocol.ts"; +import { recipe_lookup } from "$/common/mod_loader.ts"; import { container_items, copy_dir, test_game } from "./helpers.ts"; // mods/bworld plus a mod made from the template, in a temp folder @@ -31,8 +32,8 @@ Deno.test("the base game loads from mods/bworld", async () => { assert(EverythingRegistry.get("items", "bworld:stone")); assert(EverythingRegistry.get("items", "bworld:coal")); assert(game.recipes.shaped.some((recipe) => recipe.result.id === "bworld:oak_planks")); - assert(game.recipes.furnace.has("bworld:iron_ore")); - assert(game.recipes.fuel.has("bworld:coal")); + assert(recipe_lookup(() => game.recipes).find("bworld:smelting", "bworld:iron_ore")); + assert(recipe_lookup(() => game.recipes).find("bworld:fuel", "bworld:coal")); assertEquals(EverythingRegistry.get("blocks", "bworld:water")?.replaceable, true); // still has its code until it's a component assert(EverythingRegistry.get("items", "bworld:copper_watering_can")?.on_create); diff --git a/tests/recipe_viewer_test.ts b/tests/recipe_viewer_test.ts index de0b44b..47343f8 100644 --- a/tests/recipe_viewer_test.ts +++ b/tests/recipe_viewer_test.ts @@ -78,12 +78,16 @@ Deno.test("the recipe viewer finds what makes and uses an item, in any recipe ty const served_in_bowls = using("bworld:bowl").map((r) => outputs(r)[0].ids[0]); assert(served_in_bowls.includes("bworld:cooked_rice")); assert(served_in_bowls.includes("bworld:tomato_sauce")); - assert(using("bworld:coal").some((r) => r.type === "fuel")); + assert(using("bworld:coal").some((r) => r.type === "bworld:fuel")); assert(using("bworld:coal").some((r) => r.type === "shaped" && outputs(r)[0].ids[0] === "bworld:stove")); assert(using("bworld:potato").includes(rolls)); // smelting and smithing - assert(using("bworld:iron_ore").some((r) => r.type === "furnace" && outputs(r)[0].ids[0] === "bworld:iron_ingot")); + assert( + using("bworld:iron_ore").some((r) => + r.type === "bworld:smelting" && outputs(r)[0].ids[0] === "bworld:iron_ingot" + ), + ); assertEquals(ids(inputs(making("bworld:stone_pickaxe")[0])), ["bworld:wood_pickaxe", "bworld:stone"]); assertEquals(category_name("bworld:smithing"), "Smithing"); }); @@ -104,8 +108,14 @@ Deno.test("recipes are found by category in jei's order, and a catalyst's uses a { category, recipes }, ) => [category.type, recipes]); const all = game.recipes.all; - assertEquals(furnace_uses.find(([type]) => type === "furnace")?.[1], all.filter((r) => r.type === "furnace")); - assertEquals(furnace_uses.find(([type]) => type === "fuel")?.[1], all.filter((r) => r.type === "fuel")); + assertEquals( + furnace_uses.find(([type]) => type === "bworld:smelting")?.[1], + all.filter((r) => r.type === "bworld:smelting"), + ); + assertEquals( + furnace_uses.find(([type]) => type === "bworld:fuel")?.[1], + all.filter((r) => r.type === "bworld:fuel"), + ); assertEquals(index.find(item("bworld:air"), "recipes"), []); }); diff --git a/tests/smelting_test.ts b/tests/smelting_test.ts new file mode 100644 index 0000000..c4e67bd --- /dev/null +++ b/tests/smelting_test.ts @@ -0,0 +1,50 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// the base game's bworld:smelting and bworld:fuel recipes, as another mod adding to them sees them +import { assertEquals, assertRejects } from "@std/assert"; +import { recipe_lookup } from "$/common/mod_loader.ts"; +import { copy_dir, test_game } from "./helpers.ts"; + +// the base game, and a mod after it with these recipes +function kiln_mod(recipes: Record) { + const dir = Deno.makeTempDirSync({ prefix: "bworld_smelting_" }); + copy_dir("mods/bworld", `${dir}/bworld`); + const write = (path: string, json: unknown) => { + Deno.mkdirSync(`${dir}/kiln/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); + Deno.writeTextFileSync(`${dir}/kiln/${path}`, JSON.stringify(json)); + }; + write("manifest.json", { + format_version: 1, + id: "kiln", + name: "Kiln", + version: "1.0.0", + dependencies: [{ id: "bworld", version: "*" }], + }); + for (const [name, recipe] of Object.entries(recipes)) write(`recipes/${name}.json`, { format_version: 1, recipe }); + return dir; +} + +Deno.test("a later fuel recipe for an item changes how long it burns, and leaves the rest of the earlier one", async () => { + const dir = kiln_mod({ coal: { type: "bworld:fuel", item: ["bworld:coal", "bworld:oak_log"], burn_time: 50 } }); + const { game } = await test_game(dir); + Deno.removeSync(dir, { recursive: true }); + const recipes = recipe_lookup(() => game.recipes); + const burn_time = (id: string) => recipes.find<{ burn_time: number }>("bworld:fuel", id)?.burn_time; + assertEquals(burn_time("bworld:coal"), 50); + assertEquals(burn_time("bworld:oak_log"), 50); + assertEquals(burn_time("bworld:spruce_log"), 100); +}); + +Deno.test("two smelting recipes for one item stop the mod loading", async () => { + const dir = kiln_mod({ + iron: { + type: "bworld:smelting", + input: "bworld:iron_ore", + output: { id: "bworld:stone", count: 1 }, + cook_time: 10, + }, + }); + await assertRejects(() => test_game(dir), Error, "there's already one for bworld:iron_ore"); + Deno.removeSync(dir, { recursive: true }); +}); diff --git a/tests/tags_test.ts b/tests/tags_test.ts index 1568e77..eeb9a42 100644 --- a/tests/tags_test.ts +++ b/tests/tags_test.ts @@ -183,7 +183,12 @@ Deno.test("check-mods finds broken tags and tags nobody defines", async () => { write("tags/fluids/maple/sap.json", { format_version: 1, tag: { values: [] } }); write("recipes/uses_missing_tag.json", { format_version: 1, - recipe: { type: "fuel", item: "#maple:nothing_here", burn_time: 100 }, + recipe: { + type: "shaped", + pattern: ["#"], + key: { "#": "#maple:nothing_here" }, + result: { id: "maple:maple_planks", count: 1 }, + }, }); // another mod's namespace, without depending on it write("tags/items/kitchen/knives.json", { format_version: 1, tag: { values: ["maple:maple_planks"] } }); diff --git a/tests/wood_test.ts b/tests/wood_test.ts index 36e789c..712d244 100644 --- a/tests/wood_test.ts +++ b/tests/wood_test.ts @@ -8,6 +8,7 @@ import { CRAFTING_RESULT_SLOT } from "$/common/protocol.ts"; import { generate_raw_chunk } from "$/common/generation.ts"; import { get_biome } from "$/common/worldgen/biomes.ts"; import { update_crafting_result } from "$/server/game/crafting.ts"; +import { recipe_lookup } from "$/common/mod_loader.ts"; import { copy_dir, test_game } from "./helpers.ts"; Deno.test("recipes that take planks take any kind, even mixed, but nothing else", async () => { @@ -30,9 +31,10 @@ Deno.test("recipes that take planks take any kind, even mixed, but nothing else" assertEquals(grid.get_item(CRAFTING_RESULT_SLOT)?.type_id, "bworld:dark_oak_planks"); // every log burns and smelts + const recipes = recipe_lookup(() => game.recipes); for (const log of ["bworld:oak_log", "bworld:spruce_log", "bworld:cherry_log"]) { - assert(game.recipes.fuel.has(log), log); - assert(game.recipes.furnace.has(log), log); + assert(recipes.find("bworld:fuel", log), log); + assert(recipes.find("bworld:smelting", log), log); } });