Recipe types from shared scripts and stuff

This commit is contained in:
2026-11-01 20:41:23 -03:00
parent c8b5f2a3bd
commit 65113862f7
34 changed files with 234 additions and 134 deletions
+16 -19
View File
@@ -8,6 +8,8 @@ import {
block_from_json,
BlockJson,
choice_ids,
choice_list,
choice_problems,
EffectJson,
EntityJson,
EntityModelJson,
@@ -86,8 +88,6 @@ export interface ModListing {
export class RecipeBook {
shaped: GridRecipe[] = [];
furnace = new Map<string, { output: { id: string; count: number }; cook_time: number }>();
fuel = new Map<string, number>();
// 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
@@ -202,8 +202,7 @@ 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 as
// one already there, or its registered type's parse throws
// 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) {
recipes.all.push(recipe);
if (is_mod_recipe(recipe)) {
@@ -214,20 +213,7 @@ export function add_recipe(recipes: RecipeBook, recipe: RecipeJson) {
if (type) recipes.parsed.get(recipe.type)!.push(parse_recipe(type, recipe, list.length - 1));
return;
}
switch (recipe.type) {
case "shaped":
recipes.shaped.push(grid_recipe_from_json(recipe));
break;
case "furnace":
for (const input of choice_ids(recipe.input)) {
if (recipes.furnace.has(input)) throw new Error(`two furnace recipes for ${input}`);
recipes.furnace.set(input, { output: recipe.output, cook_time: recipe.cook_time });
}
break;
case "fuel":
for (const item of choice_ids(recipe.item)) recipes.fuel.set(item, recipe.burn_time);
break;
}
recipes.shaped.push(grid_recipe_from_json(recipe));
}
// the index'th recipe of its type, turned into what its type's parse makes of it, or throwing naming it
@@ -239,13 +225,24 @@ function parse_recipe(type: RecipeType<unknown, unknown>, json: ModRecipeJson, i
}
}
// items are registered before any script sets up, so the ones recipes name can be checked
// items 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 };
},
// 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[];
const problems = choice_problems(choice);
if (problems.length > 0) throw new Error(`${field} ${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}`);
}
return choice_ids(choice);
},
};
// the item a field names, once the field has no problems