Every recipe type registered from shared scripts
This commit is contained in:
+45
-19
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user