Recipe viewer for fishing

This commit is contained in:
2026-10-11 22:58:28 -03:00
parent 3c80938e91
commit 935c0df7af
7 changed files with 113 additions and 5 deletions
+4
View File
@@ -304,6 +304,10 @@ function client_context(listing: ModListing): ClientContext {
const namespace = id.split(":")[0];
return mod_names.get(namespace) ?? namespace;
},
components: (id) => (known_item(id),
structuredClone(
EverythingRegistry.get<ItemRegistry>("items", id)?.components ?? {},
)),
},
recipes: {
list: (type) =>
+3
View File
@@ -38,6 +38,9 @@ export interface ClientContext {
name(id: Id): string;
// the mod that registered it
mod(id: Id): string;
// the components its json lists and their params, a copy, like the server's ctx.items.components. for showing
// what a component means, like when a fish can be caught
components(id: Id): Record<Id, unknown>;
};
// every recipe as its json (see docs/modding/resources/recipes.md), including mods' own types, in load order. copies
recipes: {
+1
View File
@@ -150,6 +150,7 @@ ctx.items.ids(); // every item, in load order
ctx.items.exists(id);
ctx.items.name(id); // "Cooked Rice"
ctx.items.mod(id); // the name of the mod that added it
ctx.items.components(id); // its components and their params, like the server's, to show what they mean
ctx.recipes.list(type?); // every recipe as its JSON, including mods' own types
ctx.options.key_inventory; // the key the player has bound, like Minecraft's Options key mappings
```
+3 -1
View File
@@ -11,7 +11,9 @@ The recipes open in a screen of their own, laid out like JEI's: a tab for each c
made down its left side, the category and the page between their arrows, then the recipes. Clicking items in it follows
them, Backspace goes back, and Escape or the inventory key (E) returns to the inventory.
It reads every mod's recipes with `ctx.recipes.list()`.
It reads every mod's recipes with `ctx.recipes.list()`. Fish, the items with the base game's `bworld:fish` component,
get a Fishing category too: clicking one shows its seasons, biomes, hours and the liquid it's caught in, read with
`ctx.items.components`, and the fishing rod's uses are every fish.
- `scripts/categories.ts` has a category for each recipe type, like JEI's `IRecipeCategory`: its title, size, where it's
made (`MOD_CATALYSTS` for mods' types) and where its items go. Types without one are drawn from their JSON: the items
+57
View File
@@ -98,9 +98,65 @@ function base_categories(tags: TagApi): RecipeCategory[] {
layout.slot({ ids: [recipe.result], count: 1 }, 90, 0);
},
},
{
// when and where a fish bites, see fishing_recipes
type: FISHING,
title: "Fishing",
width: 180,
height: 54,
catalysts: ["bworld:fishing_rod"],
draw(recipe, layout) {
const fish = recipe.fish as FishParams;
layout.slot({ ids: [recipe.result], count: 1 }, 0, 0);
const lines = [
// all four is "all", which also fits
`Seasons: ${
fish.seasons && fish.seasons.length < 4 ? fish.seasons.map(title_case).join(", ") : "all"
}`,
`Biomes: ${fish.biomes?.map(biome_name).join(", ") ?? "all"}`,
`Time: ${fish.time ? `${clock(fish.time.from)} to ${clock(fish.time.to)}` : "all day"}`,
`In: ${biome_name(fish.liquid ?? "bworld:water")}`,
];
lines.forEach((line, i) => layout.text(line, 24, 1 + i * 13));
},
},
];
}
// the base game's fish, the items with bworld:fish, as recipes of their own type that make them, so clicking one shows
// when and where it bites. their params are under "fish", which reading a recipe's inputs skips
const FISHING = "bworld:fishing";
interface FishParams {
seasons?: string[];
biomes?: string[];
time?: { from: number; to: number };
liquid?: string;
}
export function fishing_recipes(ctx: ClientContext): Recipe[] {
return ctx.items.ids().flatMap((id) => {
const fish = ctx.items.components(id)["bworld:fish"];
return fish ? [{ type: FISHING, result: id, fish }] : [];
});
}
// "autumn" is "Autumn", "dark_oak" is "Dark Oak"
function title_case(name: string): string {
return name.split("_").map((word) => word[0].toUpperCase() + word.slice(1)).join(" ");
}
// a biome, liquid or #biome tag by its name: "bworld:swamp" is "Swamp", "#bworld:is_ocean" is "Ocean"
function biome_name(id: string): string {
return title_case(id.replace(/^#/, "").split(":")[1].replace(/^is_/, ""));
}
// hours on a 24 hour clock, fractions as minutes: 6.5 is "6:30"
function clock(hours: number): string {
const minutes = Math.round((hours % 1) * 60);
return `${Math.floor(hours)}:${String(minutes).padStart(2, "0")}`;
}
// a mod's own type, from its recipes' json: the items in its lists in a grid, a single item (like a bowl to serve in)
// under the arrow, then what it makes, one under the other
function mod_category(type: string, tags: TagApi): RecipeCategory {
@@ -144,6 +200,7 @@ export function category_name(type: string): string {
furnace: "Smelting",
fuel: "Fuel",
smithing: "Smithing",
[FISHING]: "Fishing",
};
if (base[type]) return base[type];
const name = type.split(":")[1] ?? type;
+2 -2
View File
@@ -3,7 +3,7 @@
// many recipes as fit, one under the other. the item list stays on the right. escape or the inventory key go back to
// the inventory, and backspace to what was shown before
import type { ClientContext, Graphics, Screen } from "bworld/client";
import { make_categories, type RecipeCategory, type RecipeLayout, SLOT } from "./categories.ts";
import { fishing_recipes, make_categories, type RecipeCategory, type RecipeLayout, SLOT } from "./categories.ts";
import { BUTTON, item_cell, ItemList, LABEL_COLOR, type Mode, SLOT_COLOR } from "./item_list.ts";
import { type Ingredient, inputs, outputs, type Recipe } from "./recipes.ts";
@@ -32,7 +32,7 @@ export class RecipeIndex {
get categories(): RecipeCategory[] {
if (!this.#categories) {
this.#recipes = this.ctx.recipes.list();
this.#recipes = [...this.ctx.recipes.list(), ...fishing_recipes(this.ctx)];
this.#categories = make_categories(this.ctx, this.#recipes);
}
return this.#categories;
+43 -2
View File
@@ -4,7 +4,7 @@ import { category_name } from "../mods/recipe_viewer/scripts/categories.ts";
import { inputs as recipe_inputs, outputs } from "../mods/recipe_viewer/scripts/recipes.ts";
import { RecipeIndex } from "../mods/recipe_viewer/scripts/recipes_screen.ts";
import { tag_api } from "$/common/tags.ts";
import { EverythingRegistry } from "$/common/everything_registry.ts";
import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts";
import type { ClientContext } from "bworld/client";
import { test_game } from "./helpers.ts";
@@ -12,6 +12,15 @@ const ids = (ingredients: { ids: string[] }[]) => ingredients.map((i) => i.ids.j
// deno-lint-ignore no-explicit-any
const inputs = (recipe: any) => recipe_inputs(recipe, tag_api());
// what the recipe viewer reads of ctx.items, from the registry like the client's
function client_items() {
return {
exists: (id: string) => EverythingRegistry.get("items", id) !== undefined,
ids: () => EverythingRegistry.entries("items").map(([id]) => id),
components: (id: string) => EverythingRegistry.get<ItemRegistry>("items", id)?.components ?? {},
};
}
Deno.test("the recipe viewer finds what makes and uses an item, in any recipe type", async () => {
const { game } = await test_game("mods");
const all = game.recipes.all;
@@ -52,7 +61,7 @@ Deno.test("recipes are found by category in jei's order, and a catalyst's uses a
const ctx = {
recipes: { list: () => game.recipes.all },
tags: tag_api(),
items: { exists: (id: string) => EverythingRegistry.get("items", id) !== undefined },
items: client_items(),
} as unknown as ClientContext;
const index = new RecipeIndex(ctx);
@@ -69,3 +78,35 @@ Deno.test("recipes are found by category in jei's order, and a catalyst's uses a
assertEquals(furnace_uses.find(([type]) => type === "fuel")?.[1], all.filter((r) => r.type === "fuel"));
assertEquals(index.find("bworld:air", "recipes"), []);
});
Deno.test("clicking a fish shows when and where it bites, and the fishing rod's uses are every fish", async () => {
await test_game("mods");
const ctx = {
recipes: { list: () => [] },
tags: tag_api(),
items: client_items(),
} as unknown as ClientContext;
const index = new RecipeIndex(ctx);
const [fishing] = index.find("bworld:red_mullet", "recipes");
assertEquals(fishing.category.title, "Fishing");
assertEquals(fishing.category.catalysts, ["bworld:fishing_rod"]);
const lines: string[] = [];
const slots: string[] = [];
fishing.category.draw(fishing.recipes[0], {
slot: (ingredient) => slots.push(ingredient!.ids[0]),
texture: () => {},
text: (text) => lines.push(text),
});
assertEquals(slots, ["bworld:red_mullet"]);
assertEquals(lines, ["Seasons: Summer, Winter", "Biomes: Ocean", "Time: 6:00 to 19:00", "In: Water"]);
// a lava fish without limits, and every fish under the rod
const lava_lines: string[] = [];
const [lava] = index.find("bworld:prismfish", "recipes");
lava.category.draw(lava.recipes[0], { slot: () => {}, texture: () => {}, text: (text) => lava_lines.push(text) });
assertEquals(lava_lines, ["Seasons: all", "Biomes: all", "Time: all day", "In: Lava"]);
const fish = index.find("bworld:fishing_rod", "uses")[0].recipes.map((recipe) => recipe.result);
assert(fish.includes("bworld:cod") && fish.includes("bworld:prismfish"));
// a fish isn't a use of the water it lives in
assertEquals(index.find("bworld:water", "uses"), []);
});