Register recipe api
This commit is contained in:
+66
-36
@@ -23,7 +23,9 @@ import {
|
||||
SpawnJson,
|
||||
TagJson,
|
||||
TagRegistry,
|
||||
validate_recipe,
|
||||
} from "./mod_data.ts";
|
||||
import type { 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";
|
||||
@@ -184,42 +186,10 @@ export function register_mod_data(mods: { id: string; data: ModData }[]): Recipe
|
||||
Tags.load(tags);
|
||||
|
||||
for (const { id, data } of mods) {
|
||||
const fail = (message: string): never => {
|
||||
throw new ModLoadError(id, message);
|
||||
};
|
||||
for (const recipe of data.recipes) {
|
||||
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);
|
||||
continue;
|
||||
}
|
||||
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)) fail(`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;
|
||||
case "smithing":
|
||||
if (
|
||||
recipes.smithing.some((other) =>
|
||||
other.tool === recipe.tool && other.material.id === recipe.material.id &&
|
||||
other.addition === recipe.addition
|
||||
)
|
||||
) {
|
||||
fail(`two smithing recipes for ${recipe.tool} with ${recipe.material.id}`);
|
||||
}
|
||||
recipes.smithing.push(recipe);
|
||||
break;
|
||||
}
|
||||
try {
|
||||
for (const recipe of data.recipes) add_recipe(recipes, recipe);
|
||||
} catch (e) {
|
||||
throw new ModLoadError(id, (e as Error).message);
|
||||
}
|
||||
if (data.ores.length > 0) recipes.ores.push(data.ores);
|
||||
recipes.spawns.push(...data.spawns);
|
||||
@@ -227,3 +197,63 @@ 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
|
||||
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);
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
export function shared_recipes_api(
|
||||
recipes: () => RecipeBook,
|
||||
setting_up: () => boolean,
|
||||
fail: (message: string) => never,
|
||||
): SharedContext["recipes"] {
|
||||
return {
|
||||
register(type, json) {
|
||||
if (!setting_up()) fail("recipes can only be registered during setup");
|
||||
const recipe = { ...structuredClone(json), type };
|
||||
const problems = validate_recipe(recipe);
|
||||
if (problems.length > 0) fail(`recipe of type ${type}: ${problems.join(", ")}`);
|
||||
try {
|
||||
add_recipe(recipes(), recipe as RecipeJson);
|
||||
} catch (e) {
|
||||
fail((e as Error).message);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user