Register recipe api
This commit is contained in:
@@ -111,6 +111,8 @@ export interface SharedContext {
|
||||
// every item any mod registered, like the client's ctx.items, for rules that read one, like a juice named after the
|
||||
// fruit it's from
|
||||
readonly items: {
|
||||
// every item, in load order, like the server's ctx.items.ids
|
||||
ids(): Id[];
|
||||
exists(id: Id): boolean;
|
||||
// what players see, like "Cooked Rice"
|
||||
name(id: Id): string;
|
||||
@@ -123,6 +125,14 @@ export interface SharedContext {
|
||||
// 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;
|
||||
};
|
||||
readonly recipes: {
|
||||
// adds a recipe of type, like "shaped" or "farmersdelight:cooking", as a file in the mod's recipes/ with that
|
||||
// json would, only during setup. for recipes made from other data, like one for every item with a component.
|
||||
// shared scripts run on the server and every client alike, so they all have it without it being sent. it's
|
||||
// there for server and client scripts, which set up after, and it throws when it's broken like a file would
|
||||
// stop the mod loading
|
||||
register(type: string, json: Record<string, unknown>): void;
|
||||
};
|
||||
readonly components: {
|
||||
// the rules of a block component, the same id the server script registers the rest of it under
|
||||
// deno-lint-ignore no-explicit-any
|
||||
|
||||
+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);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -95,6 +95,7 @@ export function shared_items_api(mod: string): SharedContext["items"] {
|
||||
return item;
|
||||
};
|
||||
return {
|
||||
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
|
||||
exists: (id) => EverythingRegistry.get("items", id) !== undefined,
|
||||
name: (id) => (known(id), display_name(id)),
|
||||
components: (id) => structuredClone(known(id).components ?? {}),
|
||||
|
||||
Reference in New Issue
Block a user