Better recipe api and port smithing to it
This commit is contained in:
@@ -2,7 +2,18 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
// what client scripts get, see docs/modding/gui/index.md
|
||||
import type { FluidApi, FluidVariant, Id, ItemStack, KeyCode, ModInfo, Position, Season, TagApi } from "./shared.ts";
|
||||
import type {
|
||||
FluidApi,
|
||||
FluidVariant,
|
||||
Id,
|
||||
ItemStack,
|
||||
KeyCode,
|
||||
ModInfo,
|
||||
Position,
|
||||
RecipeLookup,
|
||||
Season,
|
||||
TagApi,
|
||||
} from "./shared.ts";
|
||||
import type { LootTableJson } from "../loot.ts";
|
||||
|
||||
export type {
|
||||
@@ -82,11 +93,12 @@ export interface ClientContext {
|
||||
// the table's json, a copy, or undefined when no mod has a file for it
|
||||
table(id: Id): LootTableJson | undefined;
|
||||
};
|
||||
// every recipe as its json (see docs/modding/resources/recipes.md), including mods' own types, in load order. copies
|
||||
// every recipe as its json (see docs/modding/resources/recipes.md), including mods' own types, in load order. copies.
|
||||
// find and all read the recipes of types shared scripts registered
|
||||
recipes: {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
list(type?: Id): any[];
|
||||
};
|
||||
} & RecipeLookup;
|
||||
// every mod's block, item and entity tags
|
||||
tags: TagApi;
|
||||
// blocks drawn with code, like a shaft that turns, see docs/modding/blocks/visuals.md
|
||||
|
||||
@@ -12,6 +12,7 @@ import type {
|
||||
ItemStack,
|
||||
ModInfo,
|
||||
Position,
|
||||
RecipeLookup,
|
||||
Season,
|
||||
TagApi,
|
||||
} from "./shared.ts";
|
||||
@@ -641,13 +642,12 @@ export interface ServerFluidApi extends FluidApi {
|
||||
interact(player: Player, handler: FluidHandler): boolean;
|
||||
}
|
||||
|
||||
export interface RecipeApi {
|
||||
// find and all read the recipes of types shared scripts registered
|
||||
export interface RecipeApi extends RecipeLookup {
|
||||
furnace_result(input: Id): { output: ItemStack; cook_time: number } | undefined;
|
||||
fuel_value(item: Id): number; // burn time in ticks, 0 if it isn't fuel
|
||||
is_fuel(item: Id): boolean;
|
||||
is_smeltable(item: Id): boolean;
|
||||
// the smithing recipe for these items, whatever the material's count. addition is the optional third item
|
||||
smithing(tool: Id, material: Id, addition?: Id): { result: Id; material_count: number } | undefined;
|
||||
// every recipe of a mod's own type, like "bworld:cooking", as its json. copies, in load order
|
||||
// deno-lint-ignore no-explicit-any
|
||||
list(type: Id): any[];
|
||||
|
||||
@@ -55,6 +55,33 @@ export interface FluidRules {
|
||||
sell_value?(data: unknown): number | undefined;
|
||||
}
|
||||
|
||||
// a recipe type of a mod's own, like neoforge's RecipeType with its serializer and Recipe.matches: R is a recipe of it
|
||||
// once parsed, and I what find is asked with, like the slots of the machine that makes it. see
|
||||
// docs/modding/resources/recipes.md
|
||||
export interface RecipeType<R, I> {
|
||||
// a recipe's json turned into R, checked, like a serializer's codec. throwing stops the mod loading, like a broken
|
||||
// file would. it runs once for each recipe of the type, in load order, on the server and every client
|
||||
// deno-lint-ignore no-explicit-any
|
||||
parse(json: any, read: RecipeReader): R;
|
||||
// whether the recipe is what input makes, for find
|
||||
matches(recipe: R, input: I): boolean;
|
||||
}
|
||||
|
||||
// reads a field of a recipe's json for RecipeType.parse, checked like the engine's own recipes' fields. throws naming
|
||||
// the field when it isn't one, or its item isn't registered
|
||||
export interface RecipeReader {
|
||||
item(json: Record<string, unknown>, field: string): Id;
|
||||
stack(json: Record<string, unknown>, field: string): { id: Id; count: number };
|
||||
}
|
||||
|
||||
// the recipes of registered types, as their parse made them, in load order. not to be changed
|
||||
export interface RecipeLookup {
|
||||
// the first one input makes, like neoforge's RecipeManager.getRecipeFor. undefined for a type nobody registered
|
||||
find<R>(type: Id, input: unknown): R | undefined;
|
||||
// every one of type, empty for a type nobody registered
|
||||
all<R>(type: Id): readonly R[];
|
||||
}
|
||||
|
||||
// the registries tags are for, see docs/modding/resources/tags.md
|
||||
export type TagRegistry = "blocks" | "items" | "entities" | "biomes";
|
||||
|
||||
@@ -142,7 +169,10 @@ export interface SharedContext {
|
||||
// shared scripts that set up before this one registered, which are the dependencies'. like the client's
|
||||
// deno-lint-ignore no-explicit-any
|
||||
list(type?: string): any[];
|
||||
};
|
||||
// a recipe type of the mod's own, in its namespace, only during setup: its recipes, from files and register,
|
||||
// are parsed by it then, and find and all read them from there on every side. see RecipeType
|
||||
register_type<R, I>(type: Id, rules: RecipeType<R, I>): void;
|
||||
} & RecipeLookup;
|
||||
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
|
||||
|
||||
@@ -361,12 +361,6 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
case "fuel":
|
||||
for (const id of choice_list(json.item)) uses(file, id, "item");
|
||||
break;
|
||||
case "smithing":
|
||||
uses(file, json.tool, "item");
|
||||
uses(file, json.material.id, "item");
|
||||
if (json.addition) uses(file, json.addition, "item");
|
||||
uses(file, json.result, "item");
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (const { file, json } of mod.ores) {
|
||||
|
||||
+14
-15
@@ -603,15 +603,6 @@ const furnace_recipe = z.object({
|
||||
|
||||
const fuel_recipe = z.object({ type: z.literal("fuel"), item: item_choice, burn_time: z.int().min(1) });
|
||||
|
||||
// upgrades a tool at a smithing table, with some of a material and maybe one more item
|
||||
const smithing_recipe = z.object({
|
||||
type: z.literal("smithing"),
|
||||
tool: id,
|
||||
material: stack,
|
||||
addition: id.optional(),
|
||||
result: id,
|
||||
});
|
||||
|
||||
export interface ModRecipeJson {
|
||||
type: `${string}:${string}`;
|
||||
[field: string]: unknown;
|
||||
@@ -621,29 +612,37 @@ export type RecipeJson =
|
||||
| z.infer<typeof shaped_recipe>
|
||||
| z.infer<typeof furnace_recipe>
|
||||
| z.infer<typeof fuel_recipe>
|
||||
| z.infer<typeof smithing_recipe>
|
||||
| ModRecipeJson;
|
||||
|
||||
const engine_recipes = new Map<string, z.ZodType>([
|
||||
["shaped", shaped_recipe],
|
||||
["furnace", furnace_recipe],
|
||||
["fuel", fuel_recipe],
|
||||
["smithing", smithing_recipe],
|
||||
]);
|
||||
|
||||
// a mod's own types, like "bworld:cooking" for its cooking pot, are only stored by the engine. the mod's server
|
||||
// script reads them with ctx.recipes.list and checks them
|
||||
// a mod's own types, like "bworld:cooking" for its cooking pot, are only stored by the engine. the mod parses them,
|
||||
// with ctx.recipes.register_type, or reads them with ctx.recipes.list and checks them
|
||||
const recipe_schema = by_kind(
|
||||
"type",
|
||||
(type) => engine_recipes.get(type) ?? (ID_PATTERN.test(type) ? json_object : undefined),
|
||||
'must be "shaped", "furnace", "fuel", "smithing" or a mod\'s own type like "bworld:cooking"',
|
||||
'must be "shaped", "furnace", "fuel" or a mod\'s own type like "bworld:cooking"',
|
||||
);
|
||||
|
||||
// a recipe type a mod made up, as opposed to the engine's shaped, furnace, fuel and smithing
|
||||
// a recipe type a mod made up, as opposed to the engine's shaped, furnace and fuel
|
||||
export function is_mod_recipe(recipe: RecipeJson): recipe is ModRecipeJson {
|
||||
return recipe.type.includes(":");
|
||||
}
|
||||
|
||||
// the problems with an item or a stack in a mod's own recipe type, like the engine's own types' fields have, see
|
||||
// RecipeReader
|
||||
export function item_problems(json: unknown): Problems {
|
||||
return find_problems(id, json);
|
||||
}
|
||||
|
||||
export function stack_problems(json: unknown): Problems {
|
||||
return find_problems(stack, json);
|
||||
}
|
||||
|
||||
// the crafting grid's format, see server/game/crafting.ts
|
||||
export interface GridRecipe {
|
||||
width: number;
|
||||
|
||||
+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