Implement tags !
This commit is contained in:
@@ -72,9 +72,10 @@ const HEAT_SOURCES: Record<string, string | undefined> = {
|
||||
"farmersdelight:stove": "lit",
|
||||
};
|
||||
|
||||
// a farmersdelight:cooking recipe: the ingredients in any slots, each an item id or a list of ids any of which will do
|
||||
// a farmersdelight:cooking recipe: the ingredients in any slots. in the json each is an item id or #tag, or a list of
|
||||
// those any of which will do. here each is every item that will do
|
||||
interface CookingRecipe {
|
||||
ingredients: (string | string[])[];
|
||||
ingredients: string[][];
|
||||
result: { id: string; count: number };
|
||||
// what the meal is served in, like a bowl. without it the meal comes out as soon as it's cooked
|
||||
container?: string;
|
||||
@@ -111,6 +112,15 @@ const POT_BARS = [
|
||||
},
|
||||
];
|
||||
|
||||
// an ingredient: an item, or a #tag of items any of which will do
|
||||
function item_or_tag(ctx: ServerContext, where: string, id: unknown) {
|
||||
if (typeof id === "string" && id.startsWith("#")) {
|
||||
if (!ctx.tags.exists("items", id)) throw new Error(`${where}: there's no item tag ${id}`);
|
||||
} else if (typeof id !== "string" || !ctx.items.exists(id)) {
|
||||
throw new Error(`${where}: there's no item ${id}`);
|
||||
}
|
||||
}
|
||||
|
||||
// checked when the server starts, so a broken recipe is a load error naming it
|
||||
function load_recipes(ctx: ServerContext): CookingRecipe[] {
|
||||
return ctx.recipes.list("farmersdelight:cooking").map((json, i) => {
|
||||
@@ -126,9 +136,9 @@ function load_recipes(ctx: ServerContext): CookingRecipe[] {
|
||||
for (const ingredient of ingredients) {
|
||||
if (Array.isArray(ingredient)) {
|
||||
if (ingredient.length === 0) throw new Error(`${where}: an ingredient's list of items is empty`);
|
||||
ingredient.forEach(item);
|
||||
ingredient.forEach((id) => item_or_tag(ctx, where, id));
|
||||
} else {
|
||||
item(ingredient);
|
||||
item_or_tag(ctx, where, ingredient);
|
||||
}
|
||||
}
|
||||
item(json.result?.id);
|
||||
@@ -140,7 +150,12 @@ function load_recipes(ctx: ServerContext): CookingRecipe[] {
|
||||
if (!Number.isInteger(cook_time) || cook_time < 1) {
|
||||
throw new Error(`${where}: cook_time must be positive ticks`);
|
||||
}
|
||||
return { ingredients, result: json.result, container: json.container, cook_time };
|
||||
return {
|
||||
ingredients: ingredients.map((ingredient: string | string[]) => ctx.tags.expand(ingredient)),
|
||||
result: json.result,
|
||||
container: json.container,
|
||||
cook_time,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -156,10 +171,10 @@ function find_recipe(recipes: CookingRecipe[], container: Container): CookingRec
|
||||
}
|
||||
|
||||
// pairs every ingredient with a different item, trying each way round, like a shapeless recipe
|
||||
function matches(ingredients: (string | string[])[], items: string[], used = new Set<number>()): boolean {
|
||||
function matches(ingredients: string[][], items: string[], used = new Set<number>()): boolean {
|
||||
if (ingredients.length === 0) return true;
|
||||
const [first, ...rest] = ingredients;
|
||||
const fits = (id: string) => Array.isArray(first) ? first.includes(id) : first === id;
|
||||
const fits = (id: string) => first.includes(id);
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
if (used.has(i) || !fits(items[i])) continue;
|
||||
used.add(i);
|
||||
@@ -336,9 +351,10 @@ function load_cutting_recipes(ctx: ServerContext): CuttingRecipe[] {
|
||||
if (!Array.isArray(json.ingredients) || json.ingredients.length !== 1) {
|
||||
throw new Error(`${where}: ingredients must be one item, or one list of items any of which will do`);
|
||||
}
|
||||
const ingredient = [json.ingredients[0]].flat();
|
||||
if (ingredient.length === 0) throw new Error(`${where}: the ingredient's list of items is empty`);
|
||||
ingredient.forEach(item);
|
||||
const choice = [json.ingredients[0]].flat();
|
||||
if (choice.length === 0) throw new Error(`${where}: the ingredient's list of items is empty`);
|
||||
choice.forEach((id) => item_or_tag(ctx, where, id));
|
||||
const ingredient = ctx.tags.expand(choice);
|
||||
if (typeof json.tool !== "string") throw new Error(`${where}: tool must be a tool type, like "knife"`);
|
||||
if (!Array.isArray(json.result) || json.result.length === 0) {
|
||||
throw new Error(`${where}: result must be a list of { id, count }`);
|
||||
|
||||
Reference in New Issue
Block a user