Implement tags !

This commit is contained in:
2026-09-30 13:41:29 -03:00
parent dc70ada7c4
commit 342b45d593
51 changed files with 978 additions and 219 deletions
+1 -9
View File
@@ -7,15 +7,7 @@
" P "
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
"P": "#bworld:planks"
},
"result": {
"id": "bworld:bowl",
+1 -9
View File
@@ -8,15 +8,7 @@
"PPP"
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
"P": "#bworld:planks"
},
"result": {
"id": "bworld:chest",
@@ -9,15 +9,7 @@
],
"key": {
"S": "bworld:stone",
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
"P": "#bworld:planks"
},
"result": {
"id": "bworld:smithing_table",
+1 -9
View File
@@ -7,15 +7,7 @@
"P"
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
"P": "#bworld:planks"
},
"result": {
"id": "bworld:stick",
+1 -9
View File
@@ -8,15 +8,7 @@
" S"
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
],
"P": "#bworld:planks",
"S": "bworld:stick"
},
"result": {
+1 -9
View File
@@ -8,15 +8,7 @@
" S"
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
],
"P": "#bworld:planks",
"S": "bworld:stick"
},
"result": {
@@ -8,15 +8,7 @@
" S "
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
],
"P": "#bworld:planks",
"S": "bworld:stick"
},
"result": {
@@ -8,15 +8,7 @@
"S"
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
],
"P": "#bworld:planks",
"S": "bworld:stick"
},
"result": {
@@ -8,15 +8,7 @@
" PP"
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
],
"P": "#bworld:planks",
"S": "bworld:stick"
},
"result": {
+1 -9
View File
@@ -2,15 +2,7 @@
"format_version": 1,
"recipe": {
"type": "fuel",
"item": [
"bworld:log",
"bworld:spruce_log",
"bworld:birch_log",
"bworld:jungle_log",
"bworld:acacia_log",
"bworld:dark_oak_log",
"bworld:cherry_log"
],
"item": "#bworld:logs",
"burn_time": 100
}
}
+1 -9
View File
@@ -2,15 +2,7 @@
"format_version": 1,
"recipe": {
"type": "furnace",
"input": [
"bworld:log",
"bworld:spruce_log",
"bworld:birch_log",
"bworld:jungle_log",
"bworld:acacia_log",
"bworld:dark_oak_log",
"bworld:cherry_log"
],
"input": "#bworld:logs",
"output": {
"id": "bworld:coal",
"count": 1
@@ -0,0 +1,14 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:leaves",
"bworld:spruce_leaves",
"bworld:birch_leaves",
"bworld:jungle_leaves",
"bworld:acacia_leaves",
"bworld:dark_oak_leaves",
"bworld:cherry_leaves"
]
}
}
+14
View File
@@ -0,0 +1,14 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:log",
"bworld:spruce_log",
"bworld:birch_log",
"bworld:jungle_log",
"bworld:acacia_log",
"bworld:dark_oak_log",
"bworld:cherry_log"
]
}
}
@@ -0,0 +1,14 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
}
}
+14
View File
@@ -0,0 +1,14 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:log",
"bworld:spruce_log",
"bworld:birch_log",
"bworld:jungle_log",
"bworld:acacia_log",
"bworld:dark_oak_log",
"bworld:cherry_log"
]
}
}
+14
View File
@@ -0,0 +1,14 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
}
}
+11
View File
@@ -0,0 +1,11 @@
{
"format_version": 1,
"tag": {
"values": [
"#c:ingots/copper",
"#c:ingots/tin",
"#c:ingots/iron",
"#c:ingots/gold"
]
}
}
@@ -0,0 +1,8 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:copper_ingot"
]
}
}
@@ -0,0 +1,8 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:gold_ingot"
]
}
}
@@ -0,0 +1,8 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:iron_ingot"
]
}
}
+8
View File
@@ -0,0 +1,8 @@
{
"format_version": 1,
"tag": {
"values": [
"bworld:tin_ingot"
]
}
}
@@ -2,19 +2,17 @@
"format_version": 1,
"recipe": {
"type": "shaped",
"pattern": ["SPP", "SPP"],
"pattern": [
"SPP",
"SPP"
],
"key": {
"S": "bworld:stick",
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
]
"P": "#bworld:planks"
},
"result": { "id": "farmersdelight:cutting_board", "count": 1 }
"result": {
"id": "farmersdelight:cutting_board",
"count": 1
}
}
}
@@ -7,15 +7,7 @@
"S"
],
"key": {
"P": [
"bworld:planks",
"bworld:spruce_planks",
"bworld:birch_planks",
"bworld:jungle_planks",
"bworld:acacia_planks",
"bworld:dark_oak_planks",
"bworld:cherry_planks"
],
"P": "#bworld:planks",
"S": "bworld:stick"
},
"result": {
+26 -10
View File
@@ -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 }`);
+29 -31
View File
@@ -1,7 +1,7 @@
// a recipe viewer like just enough items: every item in a list next to the inventory, searchable, and clicking one (or
// R and U over any item) shows the recipes that make it and the ones that use it. it only reads recipes, so it works
// with any mod's, and draws the ones it doesn't know as their items in, then what comes out
import type { ClientContext, Color, Graphics, InventoryScreenView } from "bworld/client";
import type { ClientContext, Color, Graphics, InventoryScreenView, TagApi } from "bworld/client";
// items and slots are drawn this big
const CELL = 54;
@@ -81,7 +81,7 @@ class RecipeViewer {
return this.#recipes.filter((recipe) =>
mode === "recipes"
? outputs(recipe).some((output) => output.ids.includes(item))
: inputs(recipe).some((input) => input.ids.includes(item))
: inputs(recipe, this.ctx.tags).some((input) => input.ids.includes(item))
);
}
@@ -165,14 +165,14 @@ class RecipeViewer {
const recipes = this.#find(view.item, view.mode);
const top = y + GAP + BUTTON;
const bottom = y + height - GAP - BUTTON;
const pages = paginate(recipes, bottom - top);
const pages = paginate(recipes, bottom - top, this.ctx.tags);
view.page = Math.min(view.page, pages.length - 1);
let hovered: string | undefined;
let card_y = top;
for (const recipe of pages[view.page]) {
hovered = this.#card(g, recipe, x + GAP, card_y, width - GAP * 2) ?? hovered;
card_y += card_height(recipe);
card_y += card_height(recipe, this.ctx.tags);
}
const page_count = pages.length;
@@ -223,7 +223,7 @@ class RecipeViewer {
const letter = recipe.pattern[row]?.[column];
const choice = letter && letter !== " " && column < width_in ? recipe.key[letter] : undefined;
cell(
choice ? { ids: choice_ids(choice), count: 1 } : undefined,
choice ? { ids: this.ctx.tags.expand(choice), count: 1 } : undefined,
x + column * CELL,
top + row * CELL,
);
@@ -234,14 +234,14 @@ class RecipeViewer {
break;
}
case "furnace":
cell({ ids: choice_ids(recipe.input), count: 1 }, x, top);
cell({ ids: this.ctx.tags.expand(recipe.input), count: 1 }, x, top);
texture("bworld:fire_full", x, top + CELL);
texture("bworld:arrow_full", x + 1.5 * CELL, top + CELL / 2);
cell(stack(recipe.output), x + 3 * CELL, top + CELL / 2);
g.text(seconds(recipe.cook_time), x + 1.5 * CELL, top + CELL * 1.5, { color: LABEL_COLOR });
break;
case "fuel":
cell({ ids: choice_ids(recipe.item), count: 1 }, x, top);
cell({ ids: this.ctx.tags.expand(recipe.item), count: 1 }, x, top);
texture("bworld:fire_full", x, top + CELL);
g.text(`Burns for ${seconds(recipe.burn_time)}`, x + 1.5 * CELL, top + CELL / 2, {
color: LABEL_COLOR,
@@ -257,7 +257,7 @@ class RecipeViewer {
default: {
// a mod's own type: the items in its lists in a grid, single items (like a bowl to serve in) under
// the arrow, then what it makes, one under the other
const { grid, extras } = mod_recipe_inputs(recipe);
const { grid, extras } = mod_recipe_inputs(recipe, this.ctx.tags);
grid.slice(0, 9).forEach((ingredient, i) =>
cell(ingredient, x + (i % 3) * CELL, top + Math.floor(i / 3) * CELL)
);
@@ -281,21 +281,21 @@ class RecipeViewer {
}
// a card's height: its label, its rows of items, and a gap
function card_height(recipe: Recipe): number {
function card_height(recipe: Recipe, tags: TagApi): number {
let rows = 2;
if (recipe.type === "shaped") rows = 3;
else if (!(recipe.type in CATEGORY_NAMES)) {
rows = Math.max(2, Math.ceil(mod_recipe_inputs(recipe).grid.length / 3), outputs(recipe).length);
rows = Math.max(2, Math.ceil(mod_recipe_inputs(recipe, tags).grid.length / 3), outputs(recipe).length);
}
return LINE + Math.min(rows, 3) * CELL + GAP;
}
// as many cards on each page as fit, at least one
function paginate(recipes: Recipe[], height: number): Recipe[][] {
function paginate(recipes: Recipe[], height: number, tags: TagApi): Recipe[][] {
const pages: Recipe[][] = [[]];
let used = 0;
for (const recipe of recipes) {
const needed = card_height(recipe);
const needed = card_height(recipe, tags);
if (used + needed > height && pages.at(-1)!.length > 0) {
pages.push([]);
used = 0;
@@ -306,11 +306,6 @@ function paginate(recipes: Recipe[], height: number): Recipe[][] {
return pages;
}
// shaped keys, furnace inputs and fuels can be one item or a list any of which fits
function choice_ids(choice: string | string[]): string[] {
return typeof choice === "string" ? [choice] : choice;
}
// what a recipe makes
export function outputs(recipe: Recipe): Ingredient[] {
switch (recipe.type) {
@@ -334,50 +329,53 @@ export function outputs(recipe: Recipe): Ingredient[] {
}
}
// what a recipe takes
export function inputs(recipe: Recipe): Ingredient[] {
// what a recipe takes. shaped keys, furnace inputs and fuels can be an item or #tag, or a list any of which fits
export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] {
switch (recipe.type) {
case "shaped":
return Object.values(recipe.key as Record<string, string | string[]>).map((choice) => ({
ids: choice_ids(choice),
ids: tags.expand(choice),
count: 1,
}));
case "furnace":
return [{ ids: choice_ids(recipe.input), count: 1 }];
return [{ ids: tags.expand(recipe.input), count: 1 }];
case "fuel":
return [{ ids: choice_ids(recipe.item), count: 1 }];
return [{ ids: tags.expand(recipe.item), count: 1 }];
case "smithing":
return [{ ids: [recipe.tool], count: 1 }, stack(recipe.material)].concat(
recipe.addition ? [{ ids: [recipe.addition], count: 1 }] : [],
);
default: {
const { grid, extras } = mod_recipe_inputs(recipe);
const { grid, extras } = mod_recipe_inputs(recipe, tags);
return [...grid, ...extras];
}
}
}
// the items in a mod recipe's fields besides its type and result: each item in a list is an ingredient (a list in a
// list is one ingredient any of whose items fit), and a field that's one item is an extra, like a container
function mod_recipe_inputs(recipe: Recipe): { grid: Ingredient[]; extras: Ingredient[] } {
// the items in a mod recipe's fields besides its type and result: each item or #tag in a list is an ingredient (a list
// in a list is one ingredient any of whose items fit), and a field that's one item is an extra, like a container
function mod_recipe_inputs(recipe: Recipe, tags: TagApi): { grid: Ingredient[]; extras: Ingredient[] } {
const grid: Ingredient[] = [];
const extras: Ingredient[] = [];
const is_id = (value: unknown): value is string =>
typeof value === "string" && /^[a-z0-9_]+:[a-z0-9_/]+$/.test(value);
const ingredient = (choice: string | string[]) => ({ ids: tags.expand(choice), count: 1 });
for (const [field, value] of Object.entries(recipe)) {
if (field === "type" || field === "result" || field === "output") continue;
if (is_id(value)) {
extras.push({ ids: [value], count: 1 });
extras.push(ingredient(value));
} else if (Array.isArray(value)) {
for (const entry of value) {
if (is_id(entry)) grid.push({ ids: [entry], count: 1 });
else if (Array.isArray(entry) && entry.every(is_id)) grid.push({ ids: entry, count: 1 });
if (is_id(entry) || (Array.isArray(entry) && entry.every(is_id))) grid.push(ingredient(entry));
}
}
}
return { grid, extras };
}
// an item id or #tag
function is_id(value: unknown): value is string {
return typeof value === "string" && /^#?[a-z0-9_]+:[a-z0-9_/]+$/.test(value);
}
function stack(value: { id: string; count: number }): Ingredient {
return { ids: [value.id], count: value.count };
}