Recipe types from shared scripts and stuff
This commit is contained in:
@@ -644,10 +644,6 @@ export interface ServerFluidApi extends FluidApi {
|
||||
|
||||
// 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;
|
||||
// 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[];
|
||||
@@ -726,14 +722,12 @@ export type FormResult<T> = ({ canceled: true } & Partial<T>) | ({ canceled: fal
|
||||
|
||||
// menus, like neoforge's: the server side of a gui, see docs/modding/inventories/menus.md
|
||||
|
||||
export type SlotFilter = "smeltable" | "fuel" | ((item: ItemStack) => boolean);
|
||||
|
||||
export interface SlotOptions {
|
||||
// gui pixels from the screen's top left to the slot's 18 pixel frame
|
||||
x: number;
|
||||
y: number;
|
||||
// what can be put in, checked on the server
|
||||
filter?: SlotFilter;
|
||||
filter?: (item: ItemStack) => boolean;
|
||||
// can only be taken from, all at once, like the furnace result
|
||||
output_only?: boolean;
|
||||
// runs when a player takes from an output_only slot, before they get it. false stops them taking it
|
||||
|
||||
@@ -72,6 +72,8 @@ export interface RecipeType<R, I> {
|
||||
export interface RecipeReader {
|
||||
item(json: Record<string, unknown>, field: string): Id;
|
||||
stack(json: Record<string, unknown>, field: string): { id: Id; count: number };
|
||||
// an item, a #tag or a list of them, any of which fits, as every item that does, like the engine's shaped keys
|
||||
ingredient(json: Record<string, unknown>, field: string): Id[];
|
||||
}
|
||||
|
||||
// the recipes of registered types, as their parse made them, in load order. not to be changed
|
||||
|
||||
+4
-13
@@ -348,20 +348,11 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable<st
|
||||
if (slim_texture !== undefined) uses(file, slim_texture, "texture");
|
||||
}
|
||||
}
|
||||
// mods' own types are checked by their parse when the mod loads
|
||||
for (const { file, json } of mod.recipes) {
|
||||
switch (json.type) {
|
||||
case "shaped":
|
||||
for (const id of Object.values(json.key).flatMap(choice_list)) uses(file, id, "item");
|
||||
uses(file, json.result.id, "item");
|
||||
break;
|
||||
case "furnace":
|
||||
for (const id of choice_list(json.input)) uses(file, id, "item");
|
||||
uses(file, json.output.id, "item");
|
||||
break;
|
||||
case "fuel":
|
||||
for (const id of choice_list(json.item)) uses(file, id, "item");
|
||||
break;
|
||||
}
|
||||
if (json.type !== "shaped") continue;
|
||||
for (const id of Object.values(json.key).flatMap(choice_list)) uses(file, id, "item");
|
||||
uses(file, json.result.id, "item");
|
||||
}
|
||||
for (const { file, json } of mod.ores) {
|
||||
uses(file, json.id, "block");
|
||||
|
||||
+7
-19
@@ -593,31 +593,15 @@ const shaped_recipe = z.object({
|
||||
}
|
||||
});
|
||||
|
||||
// cook_time and burn_time are in ticks
|
||||
const furnace_recipe = z.object({
|
||||
type: z.literal("furnace"),
|
||||
input: item_choice,
|
||||
output: stack,
|
||||
cook_time: z.int().min(1),
|
||||
});
|
||||
|
||||
const fuel_recipe = z.object({ type: z.literal("fuel"), item: item_choice, burn_time: z.int().min(1) });
|
||||
|
||||
export interface ModRecipeJson {
|
||||
type: `${string}:${string}`;
|
||||
[field: string]: unknown;
|
||||
}
|
||||
|
||||
export type RecipeJson =
|
||||
| z.infer<typeof shaped_recipe>
|
||||
| z.infer<typeof furnace_recipe>
|
||||
| z.infer<typeof fuel_recipe>
|
||||
| ModRecipeJson;
|
||||
export type RecipeJson = z.infer<typeof shaped_recipe> | ModRecipeJson;
|
||||
|
||||
const engine_recipes = new Map<string, z.ZodType>([
|
||||
["shaped", shaped_recipe],
|
||||
["furnace", furnace_recipe],
|
||||
["fuel", fuel_recipe],
|
||||
]);
|
||||
|
||||
// a mod's own types, like "bworld:cooking" for its cooking pot, are only stored by the engine. the mod parses them,
|
||||
@@ -625,10 +609,10 @@ const engine_recipes = new Map<string, z.ZodType>([
|
||||
const recipe_schema = by_kind(
|
||||
"type",
|
||||
(type) => engine_recipes.get(type) ?? (ID_PATTERN.test(type) ? json_object : undefined),
|
||||
'must be "shaped", "furnace", "fuel" or a mod\'s own type like "bworld:cooking"',
|
||||
'must be "shaped" or a mod\'s own type like "bworld:cooking"',
|
||||
);
|
||||
|
||||
// a recipe type a mod made up, as opposed to the engine's shaped, furnace and fuel
|
||||
// a recipe type a mod made up, as opposed to the engine's shaped
|
||||
export function is_mod_recipe(recipe: RecipeJson): recipe is ModRecipeJson {
|
||||
return recipe.type.includes(":");
|
||||
}
|
||||
@@ -643,6 +627,10 @@ export function stack_problems(json: unknown): Problems {
|
||||
return find_problems(stack, json);
|
||||
}
|
||||
|
||||
export function choice_problems(json: unknown): Problems {
|
||||
return find_problems(item_choice, json);
|
||||
}
|
||||
|
||||
// the crafting grid's format, see server/game/crafting.ts
|
||||
export interface GridRecipe {
|
||||
width: number;
|
||||
|
||||
+16
-19
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user