Better recipe api and port smithing to it
This commit is contained in:
+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);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user