diff --git a/client/mods.ts b/client/mods.ts index 0295b67..5786ee6 100644 --- a/client/mods.ts +++ b/client/mods.ts @@ -6,7 +6,14 @@ import { AIR } from "$/common/constants.ts"; import { BlockRegistry, EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; import type { ClientContext, ClientMenu, InventoryOverlay, KeyMappings, MenuScreen } from "$/common/mod_api/client.ts"; -import { ModListing, ModLoadError, RecipeBook, register_mod_data, shared_recipes_api } from "$/common/mod_loader.ts"; +import { + ModListing, + ModLoadError, + recipe_lookup, + RecipeBook, + register_mod_data, + shared_recipes_api, +} from "$/common/mod_loader.ts"; import { type Bmod, read_bmod } from "$/common/bmod.ts"; import { get_state_value, set_state_value, shared_items_api } from "$/common/utils.ts"; import { BlockRules } from "$/common/block_rules.ts"; @@ -237,7 +244,7 @@ function shared_context(listing: ModListing): SharedContext { fluid_rules.register(mod, id, rules); }, }, - recipes: shared_recipes_api(() => recipe_book, () => rules_setup, (message) => { + recipes: shared_recipes_api(mod, () => recipe_book, () => rules_setup, (message) => { throw new Error(`[${mod}] ${message}`); }), world: rules_world, @@ -368,6 +375,7 @@ function client_context(listing: ModListing): ClientContext { recipes: { list: (type) => structuredClone(type ? recipe_book.all.filter((recipe) => recipe.type === type) : recipe_book.all), + ...recipe_lookup(() => recipe_book), }, tags: tag_api(), render: { diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index 8c61071..ce9ae79 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -2,7 +2,18 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // what client scripts get, see docs/modding/gui/index.md -import type { FluidApi, FluidVariant, Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi } from "./shared.ts"; +import type { + FluidApi, + FluidVariant, + Id, + ItemStack, + KeyCode, + ModInfo, + Position, + RecipeLookup, + Season, + TagApi, +} from "./shared.ts"; import type { LootTableJson } from "../loot.ts"; export type { @@ -82,11 +93,12 @@ export interface ClientContext { // the table's json, a copy, or undefined when no mod has a file for it table(id: Id): LootTableJson | undefined; }; - // every recipe as its json (see docs/modding/resources/recipes.md), including mods' own types, in load order. copies + // every recipe as its json (see docs/modding/resources/recipes.md), including mods' own types, in load order. copies. + // find and all read the recipes of types shared scripts registered recipes: { // deno-lint-ignore no-explicit-any list(type?: Id): any[]; - }; + } & RecipeLookup; // every mod's block, item and entity tags tags: TagApi; // blocks drawn with code, like a shaft that turns, see docs/modding/blocks/visuals.md diff --git a/common/mod_api/server.ts b/common/mod_api/server.ts index 997996a..f63de7b 100644 --- a/common/mod_api/server.ts +++ b/common/mod_api/server.ts @@ -12,6 +12,7 @@ import type { ItemStack, ModInfo, Position, + RecipeLookup, Season, TagApi, } from "./shared.ts"; @@ -641,13 +642,12 @@ export interface ServerFluidApi extends FluidApi { interact(player: Player, handler: FluidHandler): boolean; } -export interface RecipeApi { +// 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; - // the smithing recipe for these items, whatever the material's count. addition is the optional third item - smithing(tool: Id, material: Id, addition?: Id): { result: Id; material_count: number } | undefined; // 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[]; diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index 7b9334a..125a6b0 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -55,6 +55,33 @@ export interface FluidRules { sell_value?(data: unknown): number | undefined; } +// a recipe type of a mod's own, like neoforge's RecipeType with its serializer and Recipe.matches: R is a recipe of it +// once parsed, and I what find is asked with, like the slots of the machine that makes it. see +// docs/modding/resources/recipes.md +export interface RecipeType { + // a recipe's json turned into R, checked, like a serializer's codec. throwing stops the mod loading, like a broken + // file would. it runs once for each recipe of the type, in load order, on the server and every client + // deno-lint-ignore no-explicit-any + parse(json: any, read: RecipeReader): R; + // whether the recipe is what input makes, for find + matches(recipe: R, input: I): boolean; +} + +// reads a field of a recipe's json for RecipeType.parse, checked like the engine's own recipes' fields. throws naming +// the field when it isn't one, or its item isn't registered +export interface RecipeReader { + item(json: Record, field: string): Id; + stack(json: Record, field: string): { id: Id; count: number }; +} + +// the recipes of registered types, as their parse made them, in load order. not to be changed +export interface RecipeLookup { + // the first one input makes, like neoforge's RecipeManager.getRecipeFor. undefined for a type nobody registered + find(type: Id, input: unknown): R | undefined; + // every one of type, empty for a type nobody registered + all(type: Id): readonly R[]; +} + // the registries tags are for, see docs/modding/resources/tags.md export type TagRegistry = "blocks" | "items" | "entities" | "biomes"; @@ -142,7 +169,10 @@ export interface SharedContext { // shared scripts that set up before this one registered, which are the dependencies'. like the client's // deno-lint-ignore no-explicit-any list(type?: string): any[]; - }; + // a recipe type of the mod's own, in its namespace, only during setup: its recipes, from files and register, + // are parsed by it then, and find and all read them from there on every side. see RecipeType + register_type(type: Id, rules: RecipeType): void; + } & RecipeLookup; readonly components: { // the rules of a block component, the same id the server script registers the rest of it under // deno-lint-ignore no-explicit-any diff --git a/common/mod_check.ts b/common/mod_check.ts index 3ebcdaf..925f32a 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -361,12 +361,6 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable | z.infer | z.infer - | z.infer | ModRecipeJson; const engine_recipes = new Map([ ["shaped", shaped_recipe], ["furnace", furnace_recipe], ["fuel", fuel_recipe], - ["smithing", smithing_recipe], ]); -// a mod's own types, like "bworld:cooking" for its cooking pot, are only stored by the engine. the mod's server -// script reads them with ctx.recipes.list and checks them +// a mod's own types, like "bworld:cooking" for its cooking pot, are only stored by the engine. the mod parses them, +// with ctx.recipes.register_type, or reads them with ctx.recipes.list and checks them const recipe_schema = by_kind( "type", (type) => engine_recipes.get(type) ?? (ID_PATTERN.test(type) ? json_object : undefined), - 'must be "shaped", "furnace", "fuel", "smithing" or a mod\'s own type like "bworld:cooking"', + 'must be "shaped", "furnace", "fuel" or a mod\'s own type like "bworld:cooking"', ); -// a recipe type a mod made up, as opposed to the engine's shaped, furnace, fuel and smithing +// a recipe type a mod made up, as opposed to the engine's shaped, furnace and fuel export function is_mod_recipe(recipe: RecipeJson): recipe is ModRecipeJson { return recipe.type.includes(":"); } +// the problems with an item or a stack in a mod's own recipe type, like the engine's own types' fields have, see +// RecipeReader +export function item_problems(json: unknown): Problems { + return find_problems(id, json); +} + +export function stack_problems(json: unknown): Problems { + return find_problems(stack, 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 6490efe..1095b97 100644 --- a/common/mod_loader.ts +++ b/common/mod_loader.ts @@ -16,16 +16,18 @@ import { GridRecipe, is_mod_recipe, item_from_json, + item_problems, ItemJson, ModRecipeJson, OreJson, RecipeJson, SpawnJson, + stack_problems, TagJson, TagRegistry, validate_recipe, } from "./mod_data.ts"; -import type { SharedContext } from "./mod_api/shared.ts"; +import type { RecipeLookup, RecipeReader, RecipeType, SharedContext } from "./mod_api/shared.ts"; import { resolve_tags, Tags } from "./tags.ts"; import type { LootTableJson } from "./loot.ts"; import { type BiomeJson, register_biome } from "./worldgen/biomes.ts"; @@ -86,9 +88,11 @@ export class RecipeBook { shaped: GridRecipe[] = []; furnace = new Map(); fuel = new Map(); - smithing: Extract[] = []; // 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 + types = new Map>(); + parsed = new Map(); // every recipe as its json, in load order, for recipe viewers all: RecipeJson[] = []; // each mod's ores, in load order. each mod's are their own pass, see generate_raw_chunk @@ -198,14 +202,16 @@ 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 or -// smithing as one already there +// 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 export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) { recipes.all.push(recipe); if (is_mod_recipe(recipe)) { const list = recipes.by_type.get(recipe.type) ?? []; recipes.by_type.set(recipe.type, list); list.push(recipe); + const type = recipes.types.get(recipe.type); + if (type) recipes.parsed.get(recipe.type)!.push(parse_recipe(type, recipe, list.length - 1)); return; } switch (recipe.type) { @@ -221,29 +227,56 @@ export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) { case "fuel": for (const item of choice_ids(recipe.item)) recipes.fuel.set(item, recipe.burn_time); break; - case "smithing": - if ( - recipes.smithing.some((other) => - other.tool === recipe.tool && other.material.id === recipe.material.id && - other.addition === recipe.addition - ) - ) { - throw new Error(`two smithing recipes for ${recipe.tool} with ${recipe.material.id}`); - } - recipes.smithing.push(recipe); - break; } } +// the index'th recipe of its type, turned into what its type's parse makes of it, or throwing naming it +function parse_recipe(type: RecipeType, json: ModRecipeJson, index: number): unknown { + try { + return type.parse(structuredClone(json), recipe_reader); + } catch (e) { + throw new Error(`${json.type} recipe ${index + 1}: ${(e as Error).message}`); + } +} + +// items are registered 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 }; + }, +}; + +// the item a field names, once the field has no problems +function checked_item(field: string, problems: string[], id: string): string { + if (problems.length > 0) throw new Error(`${field} ${problems[0]}`); + if (!is_registered("items", id)) throw new Error(`${field}: there's no item ${id}`); + return id; +} + +// find and all, for every side's ctx.recipes +export function recipe_lookup(recipes: () => RecipeBook): RecipeLookup { + return { + find(type: string, input: unknown) { + const rules = recipes().types.get(type); + return recipes().parsed.get(type)?.find((recipe) => rules!.matches(recipe, input)) as R | undefined; + }, + all: (type: string) => (recipes().parsed.get(type) ?? []) as R[], + }; +} + // ctx.recipes for shared scripts, the same on the server and every client: a recipe registered there is in the book // on every side without being sent, like one from the mod's recipes/. checked like a data file's. fail throws, as the // side's error for the mod. list reads the book as it is, so a script sees the recipes of the ones before it export function shared_recipes_api( + mod: string, recipes: () => RecipeBook, setting_up: () => boolean, fail: (message: string) => never, ): SharedContext["recipes"] { return { + ...recipe_lookup(recipes), register(type, json) { if (!setting_up()) fail("recipes can only be registered during setup"); const recipe = { ...structuredClone(json), type }; @@ -256,5 +289,22 @@ export function shared_recipes_api( } }, list: (type) => structuredClone(type ? recipes().all.filter((recipe) => recipe.type === type) : recipes().all), + // the recipes of the type there are so far are parsed now, and the ones registered after as they come + register_type(type, rules) { + if (!setting_up()) fail("recipe types can only be registered during setup"); + if (!type.startsWith(`${mod}:`)) fail(`recipe type ${type} must be in the namespace "${mod}"`); + const book = recipes(); + if (book.types.has(type)) fail(`recipe type ${type} is already registered`); + const type_rules = rules as RecipeType; + try { + book.parsed.set( + type, + (book.by_type.get(type) ?? []).map((json, i) => parse_recipe(type_rules, json, i)), + ); + } catch (e) { + fail((e as Error).message); + } + book.types.set(type, type_rules); + }, }; } diff --git a/docs/modding/blocks/components.md b/docs/modding/blocks/components.md index 03ae57f..9d9ddc2 100644 --- a/docs/modding/blocks/components.md +++ b/docs/modding/blocks/components.md @@ -321,7 +321,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:smithing_table` | none | Upgrades tools with `smithing` recipes. | +| `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. | | `bworld:farmland` | none | Watered with the watering can, dries out every morning. | diff --git a/docs/modding/resources/recipes.md b/docs/modding/resources/recipes.md index 6544eef..e1896f8 100644 --- a/docs/modding/resources/recipes.md +++ b/docs/modding/resources/recipes.md @@ -1,7 +1,7 @@ # 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. +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. ## Crafting: `shaped` @@ -46,15 +46,16 @@ pattern fits anywhere in the grid. `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` +## Smithing: `bworld:smithing` -For the smithing table, which upgrades a tool: +For the smithing table, which upgrades a tool. It's the base game's own type, registered by its shared script like +[any mod's](#custom-recipe-types): ```json { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:wood_pickaxe", "material": { "id": "bworld:stone", "count": 5 }, "result": "bworld:stone_pickaxe" @@ -155,8 +156,54 @@ Use a tag rather than listing ids when other mods might have more of the same th ## Custom Recipe Types -Any `type` with a namespace is a mod's own, like `bworld:cooking` for the cooking pot. The engine only stores these -recipes. Everything else about them is up to the mod: +Any `type` with a namespace is a mod's own, like `bworld:smithing` for the smithing table. The engine only stores these +recipes. What their fields mean is up to the mod, which registers the type from its +[shared script](../concepts/sides.md#shared-scripts), like NeoForge's `RecipeType` with its serializer: + +```ts +import type { ItemStack, SharedContext } from "bworld/shared"; + +interface SmithingRecipe { + tool: string; + material: { id: string; count: number }; + result: string; +} + +export function setup(ctx: SharedContext) { + ctx.recipes.register_type("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"), + }), + // whether what's in the machine makes the recipe + matches: (recipe, input) => + recipe.tool === input.tool.id && recipe.material.id === input.material.id && + input.material.count >= recipe.material.count, + }); +} +``` + +`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. + +Then server, client and shared scripts look recipes up by what's in the machine, like NeoForge's +`RecipeManager.getRecipeFor`: + +```ts +const recipe = ctx.recipes.find("bworld:smithing", { tool, material }); // or undefined +ctx.recipes.all("bworld:smithing"); // every one, in load order +``` + +`find` gives the first recipe, in load order, that `matches` says the input makes. Both give the recipes as `parse` made +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: ```json { @@ -182,9 +229,7 @@ const recipes = ctx.recipes.list("bworld:cooking").map((json, i) => { }); ``` -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. +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. @@ -229,12 +274,12 @@ Make them from what's the same on every side, like items and their components, n ## Looking Up Recipes -Server scripts can ask about the base game's types: +Mods' registered types are looked up with [`find` and `all`](#custom-recipe-types). Server scripts can also ask about +the furnace's: ```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 ``` diff --git a/docs/modding_plan.md b/docs/modding_plan.md index f3c728e..c7e930d 100644 --- a/docs/modding_plan.md +++ b/docs/modding_plan.md @@ -120,7 +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`, `fuel` and `smithing` become short for `bworld:shaped` and so on. + 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. ### The Freeze and the Registry Hash @@ -276,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`, `smithing` | engine (`common/mod_loader.ts`, `server/game/crafting.ts`) | Phase 5 | +| Recipe types `shaped`, `furnace`, `fuel` | 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/smithing_cobalt_axe.json b/mods/bworld/recipes/smithing_cobalt_axe.json index 0dd7386..7065dde 100644 --- a/mods/bworld/recipes/smithing_cobalt_axe.json +++ b/mods/bworld/recipes/smithing_cobalt_axe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:gold_axe", "material": { "id": "bworld:cobalt_ingot", diff --git a/mods/bworld/recipes/smithing_cobalt_hoe.json b/mods/bworld/recipes/smithing_cobalt_hoe.json index a13aae1..09c8bd0 100644 --- a/mods/bworld/recipes/smithing_cobalt_hoe.json +++ b/mods/bworld/recipes/smithing_cobalt_hoe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:gold_hoe", "material": { "id": "bworld:cobalt_ingot", diff --git a/mods/bworld/recipes/smithing_cobalt_knife.json b/mods/bworld/recipes/smithing_cobalt_knife.json index 216b715..97b1e61 100644 --- a/mods/bworld/recipes/smithing_cobalt_knife.json +++ b/mods/bworld/recipes/smithing_cobalt_knife.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:gold_knife", "material": { "id": "bworld:cobalt_ingot", diff --git a/mods/bworld/recipes/smithing_cobalt_pickaxe.json b/mods/bworld/recipes/smithing_cobalt_pickaxe.json index 4048884..897f034 100644 --- a/mods/bworld/recipes/smithing_cobalt_pickaxe.json +++ b/mods/bworld/recipes/smithing_cobalt_pickaxe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:gold_pickaxe", "material": { "id": "bworld:cobalt_ingot", diff --git a/mods/bworld/recipes/smithing_cobalt_shovel.json b/mods/bworld/recipes/smithing_cobalt_shovel.json index fbc99be..5582bad 100644 --- a/mods/bworld/recipes/smithing_cobalt_shovel.json +++ b/mods/bworld/recipes/smithing_cobalt_shovel.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:gold_shovel", "material": { "id": "bworld:cobalt_ingot", diff --git a/mods/bworld/recipes/smithing_cobalt_watering_can.json b/mods/bworld/recipes/smithing_cobalt_watering_can.json index c3352d4..8fc4c4e 100644 --- a/mods/bworld/recipes/smithing_cobalt_watering_can.json +++ b/mods/bworld/recipes/smithing_cobalt_watering_can.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:gold_watering_can", "material": { "id": "bworld:cobalt_ingot", diff --git a/mods/bworld/recipes/smithing_copper_axe.json b/mods/bworld/recipes/smithing_copper_axe.json index d35c992..d713ffd 100644 --- a/mods/bworld/recipes/smithing_copper_axe.json +++ b/mods/bworld/recipes/smithing_copper_axe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:stone_axe", "material": { "id": "bworld:copper_ingot", diff --git a/mods/bworld/recipes/smithing_copper_hoe.json b/mods/bworld/recipes/smithing_copper_hoe.json index 82215a8..6397daa 100644 --- a/mods/bworld/recipes/smithing_copper_hoe.json +++ b/mods/bworld/recipes/smithing_copper_hoe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:stone_hoe", "material": { "id": "bworld:copper_ingot", diff --git a/mods/bworld/recipes/smithing_copper_knife.json b/mods/bworld/recipes/smithing_copper_knife.json index fd18806..5f1c245 100644 --- a/mods/bworld/recipes/smithing_copper_knife.json +++ b/mods/bworld/recipes/smithing_copper_knife.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:stone_knife", "material": { "id": "bworld:copper_ingot", diff --git a/mods/bworld/recipes/smithing_copper_pickaxe.json b/mods/bworld/recipes/smithing_copper_pickaxe.json index 6ae97bf..c19a098 100644 --- a/mods/bworld/recipes/smithing_copper_pickaxe.json +++ b/mods/bworld/recipes/smithing_copper_pickaxe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:stone_pickaxe", "material": { "id": "bworld:copper_ingot", diff --git a/mods/bworld/recipes/smithing_copper_shovel.json b/mods/bworld/recipes/smithing_copper_shovel.json index e46d3fc..5e9b4de 100644 --- a/mods/bworld/recipes/smithing_copper_shovel.json +++ b/mods/bworld/recipes/smithing_copper_shovel.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:stone_shovel", "material": { "id": "bworld:copper_ingot", diff --git a/mods/bworld/recipes/smithing_copper_watering_can.json b/mods/bworld/recipes/smithing_copper_watering_can.json index cfa97e8..215aa2f 100644 --- a/mods/bworld/recipes/smithing_copper_watering_can.json +++ b/mods/bworld/recipes/smithing_copper_watering_can.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:stone_watering_can", "material": { "id": "bworld:copper_ingot", diff --git a/mods/bworld/recipes/smithing_gold_axe.json b/mods/bworld/recipes/smithing_gold_axe.json index 82f9be0..b3905d8 100644 --- a/mods/bworld/recipes/smithing_gold_axe.json +++ b/mods/bworld/recipes/smithing_gold_axe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:iron_axe", "material": { "id": "bworld:gold_ingot", diff --git a/mods/bworld/recipes/smithing_gold_hoe.json b/mods/bworld/recipes/smithing_gold_hoe.json index 533ff2c..8629461 100644 --- a/mods/bworld/recipes/smithing_gold_hoe.json +++ b/mods/bworld/recipes/smithing_gold_hoe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:iron_hoe", "material": { "id": "bworld:gold_ingot", diff --git a/mods/bworld/recipes/smithing_gold_knife.json b/mods/bworld/recipes/smithing_gold_knife.json index c14501a..0b609d1 100644 --- a/mods/bworld/recipes/smithing_gold_knife.json +++ b/mods/bworld/recipes/smithing_gold_knife.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:iron_knife", "material": { "id": "bworld:gold_ingot", diff --git a/mods/bworld/recipes/smithing_gold_pickaxe.json b/mods/bworld/recipes/smithing_gold_pickaxe.json index 7ef0a2b..cadea80 100644 --- a/mods/bworld/recipes/smithing_gold_pickaxe.json +++ b/mods/bworld/recipes/smithing_gold_pickaxe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:iron_pickaxe", "material": { "id": "bworld:gold_ingot", diff --git a/mods/bworld/recipes/smithing_gold_shovel.json b/mods/bworld/recipes/smithing_gold_shovel.json index 6b2ee06..e0adc73 100644 --- a/mods/bworld/recipes/smithing_gold_shovel.json +++ b/mods/bworld/recipes/smithing_gold_shovel.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:iron_shovel", "material": { "id": "bworld:gold_ingot", diff --git a/mods/bworld/recipes/smithing_gold_watering_can.json b/mods/bworld/recipes/smithing_gold_watering_can.json index ecdf14d..57cdf3d 100644 --- a/mods/bworld/recipes/smithing_gold_watering_can.json +++ b/mods/bworld/recipes/smithing_gold_watering_can.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:iron_watering_can", "material": { "id": "bworld:gold_ingot", diff --git a/mods/bworld/recipes/smithing_iron_axe.json b/mods/bworld/recipes/smithing_iron_axe.json index c34baaf..0b53047 100644 --- a/mods/bworld/recipes/smithing_iron_axe.json +++ b/mods/bworld/recipes/smithing_iron_axe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:copper_axe", "material": { "id": "bworld:iron_ingot", diff --git a/mods/bworld/recipes/smithing_iron_hoe.json b/mods/bworld/recipes/smithing_iron_hoe.json index b385617..104cb61 100644 --- a/mods/bworld/recipes/smithing_iron_hoe.json +++ b/mods/bworld/recipes/smithing_iron_hoe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:copper_hoe", "material": { "id": "bworld:iron_ingot", diff --git a/mods/bworld/recipes/smithing_iron_knife.json b/mods/bworld/recipes/smithing_iron_knife.json index 37bd0d3..ed49e45 100644 --- a/mods/bworld/recipes/smithing_iron_knife.json +++ b/mods/bworld/recipes/smithing_iron_knife.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:copper_knife", "material": { "id": "bworld:iron_ingot", diff --git a/mods/bworld/recipes/smithing_iron_pickaxe.json b/mods/bworld/recipes/smithing_iron_pickaxe.json index af3d364..1a70765 100644 --- a/mods/bworld/recipes/smithing_iron_pickaxe.json +++ b/mods/bworld/recipes/smithing_iron_pickaxe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:copper_pickaxe", "material": { "id": "bworld:iron_ingot", diff --git a/mods/bworld/recipes/smithing_iron_shovel.json b/mods/bworld/recipes/smithing_iron_shovel.json index a6b843e..955d76f 100644 --- a/mods/bworld/recipes/smithing_iron_shovel.json +++ b/mods/bworld/recipes/smithing_iron_shovel.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:copper_shovel", "material": { "id": "bworld:iron_ingot", diff --git a/mods/bworld/recipes/smithing_iron_watering_can.json b/mods/bworld/recipes/smithing_iron_watering_can.json index abc3e67..22b1599 100644 --- a/mods/bworld/recipes/smithing_iron_watering_can.json +++ b/mods/bworld/recipes/smithing_iron_watering_can.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:copper_watering_can", "material": { "id": "bworld:iron_ingot", diff --git a/mods/bworld/recipes/smithing_stone_axe.json b/mods/bworld/recipes/smithing_stone_axe.json index 381218e..ca2d99d 100644 --- a/mods/bworld/recipes/smithing_stone_axe.json +++ b/mods/bworld/recipes/smithing_stone_axe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:wood_axe", "material": { "id": "bworld:stone", diff --git a/mods/bworld/recipes/smithing_stone_hoe.json b/mods/bworld/recipes/smithing_stone_hoe.json index 95e0733..9eaa02a 100644 --- a/mods/bworld/recipes/smithing_stone_hoe.json +++ b/mods/bworld/recipes/smithing_stone_hoe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:wood_hoe", "material": { "id": "bworld:stone", diff --git a/mods/bworld/recipes/smithing_stone_knife.json b/mods/bworld/recipes/smithing_stone_knife.json index 71ed0bb..b701b7c 100644 --- a/mods/bworld/recipes/smithing_stone_knife.json +++ b/mods/bworld/recipes/smithing_stone_knife.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:wood_knife", "material": { "id": "bworld:stone", diff --git a/mods/bworld/recipes/smithing_stone_pickaxe.json b/mods/bworld/recipes/smithing_stone_pickaxe.json index febad94..0c3d989 100644 --- a/mods/bworld/recipes/smithing_stone_pickaxe.json +++ b/mods/bworld/recipes/smithing_stone_pickaxe.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:wood_pickaxe", "material": { "id": "bworld:stone", diff --git a/mods/bworld/recipes/smithing_stone_shovel.json b/mods/bworld/recipes/smithing_stone_shovel.json index 2393ed2..24c3f1f 100644 --- a/mods/bworld/recipes/smithing_stone_shovel.json +++ b/mods/bworld/recipes/smithing_stone_shovel.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:wood_shovel", "material": { "id": "bworld:stone", diff --git a/mods/bworld/recipes/smithing_stone_watering_can.json b/mods/bworld/recipes/smithing_stone_watering_can.json index 1120894..f12f9fb 100644 --- a/mods/bworld/recipes/smithing_stone_watering_can.json +++ b/mods/bworld/recipes/smithing_stone_watering_can.json @@ -1,7 +1,7 @@ { "format_version": 1, "recipe": { - "type": "smithing", + "type": "bworld:smithing", "tool": "bworld:wood_watering_can", "material": { "id": "bworld:stone", diff --git a/mods/bworld/scripts/server.ts b/mods/bworld/scripts/server.ts index f5d3331..4c2f9ea 100644 --- a/mods/bworld/scripts/server.ts +++ b/mods/bworld/scripts/server.ts @@ -7,6 +7,7 @@ import { register_curios } from "./curios.ts"; 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 { register_verdigris } from "./verdigris/server.ts"; import { type JuiceableParams, JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts"; import { juicing_for, juicing_recipes } from "./juicing.ts"; @@ -292,7 +293,7 @@ function register_furnace(ctx: ServerContext) { }); } -// smithing table: a tool, some of a material and maybe a third item make a better tool, see the smithing recipes. +// smithing table: a tool, some of a material and maybe a third item make a better tool, see smithing.ts. // every player gets their own slots, and what's left in them goes back to the player when they close it const TOOL = 0; @@ -435,9 +436,8 @@ function register_smithing_table(ctx: ServerContext) { const material = container.get(MATERIAL); const addition = container.get(ADDITION); if (!tool || !material) return undefined; - const recipe = ctx.recipes.smithing(tool.id, material.id, addition?.id); - if (!recipe || material.count < recipe.material_count) return undefined; - return { ...recipe, tool, material, addition }; + const recipe = ctx.recipes.find(SMITHING, { tool, material, addition }); + return recipe && { result: recipe.result, material_count: recipe.material.count, tool, material, addition }; }; // the result slot shows what taking it would give, the upgraded tool keeps the old one's data diff --git a/mods/bworld/scripts/shared.ts b/mods/bworld/scripts/shared.ts index 3184ed7..c07bead 100644 --- a/mods/bworld/scripts/shared.ts +++ b/mods/bworld/scripts/shared.ts @@ -4,10 +4,11 @@ // the base game's block rules: how blocks are placed and how they change with the blocks beside them. they run on the // server and on every client, so players see a fence join or a trapdoor turn the moment they place it, and a trapdoor // or a door open the moment they click it. and the rules of juice and wine, which name and color them by the item -// they're from, and the recipes that make them +// they're from, and the recipes that make them. and the base game's recipe types import type { SharedBlock, SharedContext } from "bworld/shared"; import { register_fermenting } from "./fermenting.ts"; import { register_juicing } from "./juicing.ts"; +import { register_smithing } from "./smithing.ts"; import { register_verdigris_rules } from "./verdigris/shared.ts"; export function setup(ctx: SharedContext) { @@ -18,6 +19,7 @@ export function setup(ctx: SharedContext) { register_juice_and_wine(ctx); register_juicing(ctx); register_fermenting(ctx); + register_smithing(ctx); } // the sides a fence connects on, and the face of the block there it connects to diff --git a/mods/bworld/scripts/smithing.ts b/mods/bworld/scripts/smithing.ts new file mode 100644 index 0000000..0562aa7 --- /dev/null +++ b/mods/bworld/scripts/smithing.ts @@ -0,0 +1,45 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// smithing recipes: the smithing table upgrades a tool with some of a material and maybe one more item. the type is +// registered by the shared script, so the server's menu and anything on a client find them the same way +import type { ItemStack, SharedContext } from "bworld/shared"; + +export const SMITHING = "bworld:smithing"; + +export interface SmithingRecipe { + tool: string; + material: { id: string; count: number }; + addition?: string; + result: string; +} + +// what's in the smithing table's input slots +export interface SmithingInput { + tool: ItemStack; + material: ItemStack; + addition?: ItemStack; +} + +export function register_smithing(ctx: SharedContext) { + // two recipes for the same inputs would leave one that can never be made + const seen = new Set(); + ctx.recipes.register_type(SMITHING, { + parse(json, read) { + const recipe: SmithingRecipe = { + tool: read.item(json, "tool"), + material: read.stack(json, "material"), + result: read.item(json, "result"), + }; + if (json.addition !== undefined) recipe.addition = read.item(json, "addition"); + const inputs = `${recipe.tool} ${recipe.material.id} ${recipe.addition}`; + if (seen.has(inputs)) throw new Error(`there's already one for ${recipe.tool} with ${recipe.material.id}`); + seen.add(inputs); + return recipe; + }, + // with enough of the material + matches: (recipe, input) => + recipe.tool === input.tool.id && recipe.material.id === input.material.id && + input.material.count >= recipe.material.count && recipe.addition === input.addition?.id, + }); +} diff --git a/mods/recipe_viewer/scripts/categories.ts b/mods/recipe_viewer/scripts/categories.ts index 6500ee3..6e15f34 100644 --- a/mods/recipe_viewer/scripts/categories.ts +++ b/mods/recipe_viewer/scripts/categories.ts @@ -92,7 +92,7 @@ function base_categories(tags: TagApi): RecipeCategory[] { }, }, { - type: "smithing", + type: "bworld:smithing", title: "Smithing", width: 108, height: 18, @@ -325,7 +325,6 @@ export function category_name(type: string): string { shaped: "Crafting", furnace: "Smelting", fuel: "Fuel", - smithing: "Smithing", [FISHING]: "Fishing", [CRABPOT]: "Crab Pot", [JUICING]: "Juicing", diff --git a/mods/recipe_viewer/scripts/recipes.ts b/mods/recipe_viewer/scripts/recipes.ts index 5f1b745..39d35d0 100644 --- a/mods/recipe_viewer/scripts/recipes.ts +++ b/mods/recipe_viewer/scripts/recipes.ts @@ -21,7 +21,7 @@ export function outputs(recipe: Recipe): Ingredient[] { return [stack(recipe.output)]; case "fuel": return []; - case "smithing": + case "bworld:smithing": return [{ ids: [recipe.result], count: 1 }]; default: { // one item or stack, or a list of them, like what a cutting board cuts something into or a press presses @@ -58,7 +58,7 @@ export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] { return [{ ids: tags.expand(recipe.input), count: 1 }]; case "fuel": return [{ ids: tags.expand(recipe.item), count: 1 }]; - case "smithing": + case "bworld:smithing": return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat( recipe.addition ? [{ ids: [recipe.addition], count: 1 }] : [], ); diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index fc1967b..57b70c5 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -57,7 +57,7 @@ import type { ServerContext, SlotOptions, } from "$/common/mod_api/server.ts"; -import { ModLoadError, shared_recipes_api } from "$/common/mod_loader.ts"; +import { ModLoadError, recipe_lookup, shared_recipes_api } from "$/common/mod_loader.ts"; import { argument, block as block_argument, @@ -303,7 +303,7 @@ export class ModRuntime { this.fluid_rules.register(mod, id, rules); }, }, - recipes: shared_recipes_api(() => this.game.recipes, () => this.#shared_setup, (message) => { + recipes: shared_recipes_api(mod, () => this.game.recipes, () => this.#shared_setup, (message) => { throw new ModLoadError(mod, message); }), world: { @@ -1014,12 +1014,7 @@ export class ModRuntime { 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), - smithing: (tool, material, addition) => { - const recipe = game().recipes.smithing.find((r) => - r.tool === tool && r.material.id === material && r.addition === addition - ); - return recipe && { result: recipe.result, material_count: recipe.material.count }; - }, + ...recipe_lookup(() => game().recipes), list: (type) => structuredClone(game().recipes.by_type.get(type) ?? []), }, tags: tag_api(), diff --git a/tests/helpers.ts b/tests/helpers.ts index abf6757..ccc8d50 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -262,7 +262,7 @@ export function client_of(game: GameServer, area: Area) { items: shared_items_api("bworld"), // only its blocks are looked at here fluids: { register() {} }, - recipes: { register() {}, list: () => [] }, + recipes: { register() {}, list: () => [], register_type() {}, find: () => undefined, all: () => [] }, world, components: { register_block: (id, block_rules) => rules.register("bworld", id, block_rules) }, }); diff --git a/tests/mod_loading_test.ts b/tests/mod_loading_test.ts index 708d873..ac8cbb8 100644 --- a/tests/mod_loading_test.ts +++ b/tests/mod_loading_test.ts @@ -248,7 +248,7 @@ Deno.test("every tier of pickaxe, axe, shovel and hoe exists and can be made", a const { game } = await test_game("mods"); const made = new Set([ ...game.recipes.shaped.map((recipe) => recipe.result.id), - ...game.recipes.smithing.map((recipe) => recipe.result), + ...game.recipes.all.flatMap((recipe) => recipe.type === "bworld:smithing" ? [recipe.result] : []), ]); for (const tier of ["wood", "stone", "copper", "iron", "gold"]) { for (const tool of ["pickaxe", "axe", "shovel", "hoe"]) { @@ -266,7 +266,7 @@ Deno.test("every tier of watering can exists, can be made and holds more than th const { game } = await test_game("mods"); const made = new Set([ ...game.recipes.shaped.map((recipe) => recipe.result.id), - ...game.recipes.smithing.map((recipe) => recipe.result), + ...game.recipes.all.flatMap((recipe) => recipe.type === "bworld:smithing" ? [recipe.result] : []), ]); let last = 0; for (const tier of ["wood", "stone", "copper", "iron", "gold"]) { diff --git a/tests/recipe_viewer_test.ts b/tests/recipe_viewer_test.ts index b195a35..de0b44b 100644 --- a/tests/recipe_viewer_test.ts +++ b/tests/recipe_viewer_test.ts @@ -85,7 +85,7 @@ Deno.test("the recipe viewer finds what makes and uses an item, in any recipe ty // smelting and smithing assert(using("bworld:iron_ore").some((r) => r.type === "furnace" && outputs(r)[0].ids[0] === "bworld:iron_ingot")); assertEquals(ids(inputs(making("bworld:stone_pickaxe")[0])), ["bworld:wood_pickaxe", "bworld:stone"]); - assertEquals(category_name("smithing"), "Smithing"); + assertEquals(category_name("bworld:smithing"), "Smithing"); }); Deno.test("recipes are found by category in jei's order, and a catalyst's uses are everything made in it", async () => { diff --git a/tests/script_recipes_test.ts b/tests/script_recipes_test.ts index 13d2034..5b1000e 100644 --- a/tests/script_recipes_test.ts +++ b/tests/script_recipes_test.ts @@ -30,6 +30,10 @@ const SEEN = (side: string) => ` export function setup(ctx: any) { ctx.components?.register_item?.("testrecipes:siftable", {}); (globalThis as any).testrecipes_${side} = ctx.recipes.list("testrecipes:sifting"); + (globalThis as any).testrecipes_${side}_parsed = { + all: ctx.recipes.all("testrecipes:sifting"), + sand: ctx.recipes.find("testrecipes:sifting", "testrecipes:sand"), + }; (globalThis as any).testrecipes_late?.(); } `; @@ -71,16 +75,10 @@ const SIFTING = [{ result: { id: "testrecipes:flint", count: 1 }, }]; -Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => { - const dir = recipe_mod(); - const t = await test_game(dir); - const seen = globalThis as unknown as Record; - assertEquals(seen.testrecipes_server, SIFTING); - assertEquals(t.game.recipes.by_type.get("testrecipes:sifting") as unknown, SIFTING); - assert( - t.game.recipes.shaped.some((recipe) => recipe.result.id === "testrecipes:flint" && recipe.result.count === 2), - ); +const seen = globalThis as unknown as Record; +// packs testrecipes from dir, which it then removes, and loads it on a client like one joining would +async function load_client(dir: string) { const mod = load_and_check(dir).find((m) => m.id === "testrecipes")!; assertEquals(mod.report.errors, []); const bmod = read_bmod(await pack_mod(mod), "testrecipes.bmod"); @@ -88,9 +86,82 @@ Deno.test("recipes a shared script registers are on the server and clients, for const listing: ModListing = { id: "testrecipes", name: "Test", version: "1.0.0", sha256: "", file: "", size: 0 }; EverythingRegistry.clear(); await load_client_mods([listing], [bmod]); +} + +Deno.test("recipes a shared script registers are on the server and clients, for their scripts and crafting", async () => { + const dir = recipe_mod(); + const t = await test_game(dir); + assertEquals(seen.testrecipes_server, SIFTING); + assertEquals(t.game.recipes.by_type.get("testrecipes:sifting") as unknown, SIFTING); + assert( + t.game.recipes.shaped.some((recipe) => recipe.result.id === "testrecipes:flint" && recipe.result.count === 2), + ); + + await load_client(dir); assertEquals(seen.testrecipes_client, SIFTING); }); +// sifting as a type of testrecipes' own, matched by the item put in, and a recipe registered after it +const SIFTING_TYPE = ` + ctx.recipes.register_type("testrecipes:sifting", { + parse: (json, read) => ({ input: read.item(json, "input"), result: read.stack(json, "result") }), + matches: (recipe, input) => recipe.input === input, + }); + ctx.recipes.register("testrecipes:sifting", { input: "testrecipes:sand", result: { id: "testrecipes:gravel", count: 1 } }); +`; + +const SAND_FILE = { + "sand.json": { + type: "testrecipes:sifting", + input: "testrecipes:sand", + result: { id: "testrecipes:flint", count: 1 }, + }, +}; + +Deno.test("a recipe type parses its recipes, and find gives the first one an input makes, on every side", async () => { + const dir = recipe_mod(SIFTING_TYPE, SAND_FILE); + await test_game(dir); + await load_client(dir); + for (const side of ["server", "client"]) { + const parsed = seen[`testrecipes_${side}_parsed`] as { all: { input: string }[]; sand: unknown }; + // the file's, then the ones registered before the type and after it + assertEquals(parsed.all.map((recipe) => recipe.input), [ + "testrecipes:sand", + "testrecipes:gravel", + "testrecipes:sand", + ]); + assertEquals(parsed.sand, { input: "testrecipes:sand", result: { id: "testrecipes:flint", count: 1 } }); + } +}); + +Deno.test("a recipe its type can't parse stops the mod loading, naming it", async () => { + const dir = recipe_mod(SIFTING_TYPE, { + "sand.json": { + type: "testrecipes:sifting", + input: "testrecipes:nothing", + result: { id: "testrecipes:flint", count: 1 }, + }, + }); + await assertRejects( + () => test_game(dir), + Error, + "testrecipes:sifting recipe 1: input: there's no item testrecipes:nothing", + ); + Deno.removeSync(dir, { recursive: true }); +}); + +Deno.test("a mod can only register recipe types in its own namespace", async () => { + const dir = recipe_mod( + `ctx.recipes.register_type("bworld:sifting", { parse: (json) => json, matches: () => true });`, + ); + await assertRejects( + () => test_game(dir), + Error, + 'recipe type bworld:sifting must be in the namespace "testrecipes"', + ); + Deno.removeSync(dir, { recursive: true }); +}); + Deno.test("a shared script lists the recipes there are so far, from files and the scripts before it", async () => { // a washing recipe for every sifting one, whatever made it const dir = recipe_mod(