Every recipe type registered from shared scripts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-01 21:00:44 -03:00
co-authored by Claude Opus 5.5
parent 65113862f7
commit a7e81b9944
25 changed files with 334 additions and 371 deletions
+5 -1
View File
@@ -48,7 +48,7 @@ export class FluidRules {
api(): FluidApi {
return {
ids: () => EverythingRegistry.entries("fluids").map(([id]) => id),
exists: (id) => EverythingRegistry.get("fluids", id) !== undefined,
exists: fluid_exists,
name: (fluid) => this.name(fluid),
color: (fluid) => this.color(fluid),
same: same_fluid,
@@ -68,6 +68,10 @@ export class FluidRules {
}
}
export function fluid_exists(id: string): boolean {
return EverythingRegistry.get("fluids", id) !== undefined;
}
// whether they're one fluid, see FluidApi.same
export function same_fluid(a: FluidVariant, b: FluidVariant): boolean {
return a.id === b.id && same_data(a.data, b.data);
+13 -7
View File
@@ -67,13 +67,17 @@ export interface RecipeType<R, I> {
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
// reads a value from a recipe's json for RecipeType.parse, checked like the engine's own recipes' fields. name is where
// it is, like "result" or "ingredients[0]", for the error thrown when it isn't one, or what it names 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 };
item(value: unknown, name: string): Id;
stack(value: unknown, name: string): { id: Id; count: number };
// an item, a #tag or a list of them, any of which fits, as every item that does, like the engine's shaped keys
ingredient(json: Record<string, unknown>, field: string): Id[];
ingredient(value: unknown, name: string): Id[];
// { fluid, amount } in mB
fluid_stack(value: unknown, name: string): FluidStack;
// a whole number, at least 1, like a time in ticks
positive_int(value: unknown, name: string): number;
}
// the recipes of registered types, as their parse made them, in load order. not to be changed
@@ -156,7 +160,8 @@ export interface SharedContext {
// what one sells for, from its "base_sell_value", or undefined if it can't be sold
sell_value(id: Id): number | undefined;
};
readonly fluids: {
// every fluid any mod defines, like the server's and clients' ctx.fluids, without what rules decide
readonly fluids: Pick<FluidApi, "exists" | "same"> & {
// the rules of one of the mod's fluids, which the server and every client name and color it by
register(id: Id, rules: FluidRules): void;
};
@@ -172,7 +177,8 @@ export interface SharedContext {
// 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
// are parsed by it then, and find and all read them from there on every side. a recipe of a type no mod
// registers stops its mod loading once every shared script has set up. see RecipeType
register_type<R, I>(type: Id, rules: RecipeType<R, I>): void;
} & RecipeLookup;
readonly components: {
+1 -1
View File
@@ -605,7 +605,7 @@ const engine_recipes = new Map<string, z.ZodType>([
]);
// 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
// with ctx.recipes.register_type
const recipe_schema = by_kind(
"type",
(type) => engine_recipes.get(type) ?? (ID_PATTERN.test(type) ? json_object : undefined),
+45 -19
View File
@@ -29,7 +29,8 @@ import {
TagRegistry,
validate_recipe,
} from "./mod_data.ts";
import type { RecipeLookup, RecipeReader, RecipeType, SharedContext } from "./mod_api/shared.ts";
import type { FluidStack, RecipeLookup, RecipeReader, RecipeType, SharedContext } from "./mod_api/shared.ts";
import { fluid_exists } from "./fluids.ts";
import { resolve_tags, Tags } from "./tags.ts";
import type { LootTableJson } from "./loot.ts";
import { type BiomeJson, register_biome } from "./worldgen/biomes.ts";
@@ -88,8 +89,9 @@ export interface ModListing {
export class RecipeBook {
shaped: GridRecipe[] = [];
// recipes of mods' own types, by type
// recipes of mods' own types, by type, and the mod with the first one of each, for the error when nothing registers it
by_type = new Map<string, ModRecipeJson[]>();
first_used_by = new Map<string, string>();
// 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[]>();
@@ -191,7 +193,7 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
for (const { id, data } of mods) {
try {
for (const recipe of data.recipes) add_recipe(recipes, recipe);
for (const recipe of data.recipes) add_recipe(recipes, recipe, id);
} catch (e) {
throw new ModLoadError(id, (e as Error).message);
}
@@ -202,12 +204,13 @@ 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 its registered type's parse throws
export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) {
// puts mod's recipe in the book, from its recipes/ or its shared script. throws when its registered type's parse throws
export function add_recipe(recipes: RecipeBook, recipe: RecipeJson, mod: string) {
recipes.all.push(recipe);
if (is_mod_recipe(recipe)) {
const list = recipes.by_type.get(recipe.type) ?? [];
recipes.by_type.set(recipe.type, list);
if (!recipes.first_used_by.has(recipe.type)) recipes.first_used_by.set(recipe.type, mod);
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));
@@ -225,33 +228,56 @@ function parse_recipe(type: RecipeType<unknown, unknown>, json: ModRecipeJson, i
}
}
// items and tags load before any script sets up, so the ones recipes name can be checked
// items, fluids and tags load before any script sets up, so the ones recipes name can be checked
const recipe_reader: RecipeReader = {
item: (json, field) => checked_item(field, item_problems(json[field]), json[field] as string),
stack(json, field) {
const stack = json[field] as { id: string; count: number } | undefined;
return { id: checked_item(field, stack_problems(stack), stack?.id as string), count: stack!.count };
item: (value, name) => checked_item(name, item_problems(value), value as string),
stack(value, name) {
const stack = value as { id: string; count: number } | undefined;
return { id: checked_item(name, stack_problems(stack), stack?.id as string), count: stack!.count };
},
// a tag can be empty, like one of another mod's items that isn't installed, but it has to exist
ingredient(json, field) {
const choice = json[field] as string | string[];
ingredient(value, name) {
const choice = value as string | string[];
const problems = choice_problems(choice);
if (problems.length > 0) throw new Error(`${field} ${problems[0]}`);
if (problems.length > 0) throw new Error(`${name} ${problems[0]}`);
for (const entry of choice_list(choice)) {
if (!entry.startsWith("#")) checked_item(field, [], entry);
else if (!Tags.exists("items", entry)) throw new Error(`${field}: there's no item tag ${entry}`);
if (!entry.startsWith("#")) checked_item(name, [], entry);
else if (!Tags.exists("items", entry)) throw new Error(`${name}: there's no item tag ${entry}`);
}
return choice_ids(choice);
},
fluid_stack(value, name) {
const stack = value as Partial<FluidStack> | undefined;
const id = stack?.fluid?.id;
if (typeof id !== "string") throw new Error(`${name} must be { fluid: { id, data }, amount }`);
if (!fluid_exists(id)) throw new Error(`${name}: there's no fluid ${id}`);
return { fluid: stack!.fluid!, amount: recipe_reader.positive_int(stack!.amount, `${name} amount`) };
},
positive_int(value, name) {
if (!Number.isInteger(value) || (value as number) < 1) {
throw new Error(`${name} must be a whole number, at least 1`);
}
return value as number;
},
};
// 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}`);
function checked_item(name: string, problems: string[], id: string): string {
if (problems.length > 0) throw new Error(`${name} ${problems[0]}`);
if (!is_registered("items", id)) throw new Error(`${name}: there's no item ${id}`);
return id;
}
// once every shared script has set up: a recipe of a type no mod registered, which nothing would read, is an error
// for the first mod with one, like a misspelled type
export function check_recipe_types(recipes: RecipeBook) {
for (const [type, mod] of recipes.first_used_by) {
if (!recipes.types.has(type)) {
throw new ModLoadError(mod, `it has ${type} recipes, but no mod registers that type`);
}
}
}
// find and all, for every side's ctx.recipes
export function recipe_lookup(recipes: () => RecipeBook): RecipeLookup {
return {
@@ -280,7 +306,7 @@ export function shared_recipes_api(
const problems = validate_recipe(recipe);
if (problems.length > 0) fail(`recipe of type ${type}: ${problems.join(", ")}`);
try {
add_recipe(recipes(), recipe as RecipeJson);
add_recipe(recipes(), recipe as RecipeJson, mod);
} catch (e) {
fail((e as Error).message);
}