Better recipe api and port smithing to it
This commit is contained in:
+10
-2
@@ -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: {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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[];
|
||||
|
||||
@@ -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<R, I> {
|
||||
// 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<string, unknown>, field: string): Id;
|
||||
stack(json: Record<string, unknown>, 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<R>(type: Id, input: unknown): R | undefined;
|
||||
// every one of type, empty for a type nobody registered
|
||||
all<R>(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<R, I>(type: Id, rules: RecipeType<R, I>): 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
|
||||
|
||||
@@ -361,12 +361,6 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
case "fuel":
|
||||
for (const id of choice_list(json.item)) uses(file, id, "item");
|
||||
break;
|
||||
case "smithing":
|
||||
uses(file, json.tool, "item");
|
||||
uses(file, json.material.id, "item");
|
||||
if (json.addition) uses(file, json.addition, "item");
|
||||
uses(file, json.result, "item");
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (const { file, json } of mod.ores) {
|
||||
|
||||
+14
-15
@@ -603,15 +603,6 @@ const furnace_recipe = z.object({
|
||||
|
||||
const fuel_recipe = z.object({ type: z.literal("fuel"), item: item_choice, burn_time: z.int().min(1) });
|
||||
|
||||
// upgrades a tool at a smithing table, with some of a material and maybe one more item
|
||||
const smithing_recipe = z.object({
|
||||
type: z.literal("smithing"),
|
||||
tool: id,
|
||||
material: stack,
|
||||
addition: id.optional(),
|
||||
result: id,
|
||||
});
|
||||
|
||||
export interface ModRecipeJson {
|
||||
type: `${string}:${string}`;
|
||||
[field: string]: unknown;
|
||||
@@ -621,29 +612,37 @@ export type RecipeJson =
|
||||
| z.infer<typeof shaped_recipe>
|
||||
| z.infer<typeof furnace_recipe>
|
||||
| z.infer<typeof fuel_recipe>
|
||||
| z.infer<typeof smithing_recipe>
|
||||
| ModRecipeJson;
|
||||
|
||||
const engine_recipes = new Map<string, z.ZodType>([
|
||||
["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;
|
||||
|
||||
+65
-15
@@ -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<string, { output: { id: string; count: number }; cook_time: number }>();
|
||||
fuel = new Map<string, number>();
|
||||
smithing: Extract<RecipeJson, { type: "smithing" }>[] = [];
|
||||
// recipes of mods' own types, by type
|
||||
by_type = new Map<string, ModRecipeJson[]>();
|
||||
// the types shared scripts registered, and their recipes as parse made them, see SharedContext.recipes.register_type
|
||||
types = new Map<string, RecipeType<unknown, unknown>>();
|
||||
parsed = new Map<string, unknown[]>();
|
||||
// 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<unknown, unknown>, 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<R>(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: <R>(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<unknown, unknown>;
|
||||
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);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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. |
|
||||
|
||||
@@ -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<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"),
|
||||
}),
|
||||
// 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<SmithingRecipe>("bworld:smithing", { tool, material }); // or undefined
|
||||
ctx.recipes.all<SmithingRecipe>("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
|
||||
```
|
||||
|
||||
@@ -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 |
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:gold_axe",
|
||||
"material": {
|
||||
"id": "bworld:cobalt_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:gold_hoe",
|
||||
"material": {
|
||||
"id": "bworld:cobalt_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:gold_knife",
|
||||
"material": {
|
||||
"id": "bworld:cobalt_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:gold_pickaxe",
|
||||
"material": {
|
||||
"id": "bworld:cobalt_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:gold_shovel",
|
||||
"material": {
|
||||
"id": "bworld:cobalt_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:gold_watering_can",
|
||||
"material": {
|
||||
"id": "bworld:cobalt_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:stone_axe",
|
||||
"material": {
|
||||
"id": "bworld:copper_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:stone_hoe",
|
||||
"material": {
|
||||
"id": "bworld:copper_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:stone_knife",
|
||||
"material": {
|
||||
"id": "bworld:copper_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:stone_pickaxe",
|
||||
"material": {
|
||||
"id": "bworld:copper_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:stone_shovel",
|
||||
"material": {
|
||||
"id": "bworld:copper_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:stone_watering_can",
|
||||
"material": {
|
||||
"id": "bworld:copper_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:iron_axe",
|
||||
"material": {
|
||||
"id": "bworld:gold_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:iron_hoe",
|
||||
"material": {
|
||||
"id": "bworld:gold_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:iron_knife",
|
||||
"material": {
|
||||
"id": "bworld:gold_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:iron_pickaxe",
|
||||
"material": {
|
||||
"id": "bworld:gold_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:iron_shovel",
|
||||
"material": {
|
||||
"id": "bworld:gold_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:iron_watering_can",
|
||||
"material": {
|
||||
"id": "bworld:gold_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:copper_axe",
|
||||
"material": {
|
||||
"id": "bworld:iron_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:copper_hoe",
|
||||
"material": {
|
||||
"id": "bworld:iron_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:copper_knife",
|
||||
"material": {
|
||||
"id": "bworld:iron_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:copper_pickaxe",
|
||||
"material": {
|
||||
"id": "bworld:iron_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:copper_shovel",
|
||||
"material": {
|
||||
"id": "bworld:iron_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:copper_watering_can",
|
||||
"material": {
|
||||
"id": "bworld:iron_ingot",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:wood_axe",
|
||||
"material": {
|
||||
"id": "bworld:stone",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:wood_hoe",
|
||||
"material": {
|
||||
"id": "bworld:stone",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:wood_knife",
|
||||
"material": {
|
||||
"id": "bworld:stone",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:wood_pickaxe",
|
||||
"material": {
|
||||
"id": "bworld:stone",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:wood_shovel",
|
||||
"material": {
|
||||
"id": "bworld:stone",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"format_version": 1,
|
||||
"recipe": {
|
||||
"type": "smithing",
|
||||
"type": "bworld:smithing",
|
||||
"tool": "bworld:wood_watering_can",
|
||||
"material": {
|
||||
"id": "bworld:stone",
|
||||
|
||||
@@ -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<SmithingRecipe>(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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<string>();
|
||||
ctx.recipes.register_type<SmithingRecipe, SmithingInput>(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,
|
||||
});
|
||||
}
|
||||
@@ -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",
|
||||
|
||||
@@ -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 }] : [],
|
||||
);
|
||||
|
||||
@@ -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(),
|
||||
|
||||
+1
-1
@@ -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) },
|
||||
});
|
||||
|
||||
@@ -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"]) {
|
||||
|
||||
@@ -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 () => {
|
||||
|
||||
@@ -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<string, unknown>;
|
||||
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<string, unknown>;
|
||||
|
||||
// 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(
|
||||
|
||||
Reference in New Issue
Block a user