Fluids on the recipeviewer !

This commit is contained in:
2026-10-28 13:26:56 -03:00
parent 3cc33ea10c
commit e525681328
7 changed files with 151 additions and 83 deletions
+7 -4
View File
@@ -1,9 +1,11 @@
# Recipe Viewer # Recipe Viewer
A recipe viewer like Just Enough Items, as a client script. Every inventory screen gets the list of every item on its A recipe viewer like Just Enough Items, as a client script. Every inventory screen gets the list of every item, then
right, with the page on top and a search box under it (`@mod` searches by mod, like `@farmersdelight`). every fluid, on its right, with the page on top and a search box under it (`@mod` searches by mod, like
`@farmersdelight`). Fluids are listed by id alone, without their data, so juice is there once and stands for every
juice.
- Left click an item, or press R over any item (in the list, a slot or a recipe), for the recipes that make it. - Left click an item or fluid, or press R over any (in the list, a slot or a recipe), for the recipes that make it.
- Right click, or U, for the recipes that use it. The uses of a block that makes recipes, like the furnace, are all of - Right click, or U, for the recipes that use it. The uses of a block that makes recipes, like the furnace, are all of
the recipes made in it. the recipes made in it.
@@ -17,7 +19,8 @@ get a Fishing category too: clicking one shows its seasons, biomes, hours and th
its `bworld:crabpot` component and each kind of water, showing everything that water's loot table can catch and how its `bworld:crabpot` component and each kind of water, showing everything that water's loot table can catch and how
likely each is, read with `ctx.blocks.components` and `ctx.loot.table`. The juicer gets one for each item with the likely each is, read with `ctx.blocks.components` and `ctx.loot.table`. The juicer gets one for each item with the
`bworld:juiceable` component, showing the juice one of it is pressed into and how many mB, drawn in a fluid slot with `bworld:juiceable` component, showing the juice one of it is pressed into and how many mB, drawn in a fluid slot with
`g.fluid`. `g.fluid`. Recipes say what fluids they make and take in `fluid_results` and `fluid_ingredients`, lists of
`{ fluid, amount }`, which only the viewer's own types use so far.
- `scripts/categories.ts` has a category for each recipe type, like JEI's `IRecipeCategory`: its title, size, where it's - `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 made (`MOD_CATALYSTS` for mods' types) and where its items go. Types without one are drawn from their JSON: the items
+5 -5
View File
@@ -5,7 +5,7 @@
// blocks it's made in (jei's recipe catalysts), and where its items go. types no category knows get one from their // blocks it's made in (jei's recipe catalysts), and where its items go. types no category knows get one from their
// recipes' json, see mod_category // recipes' json, see mod_category
import type { ClientContext, FluidVariant, TagApi } from "bworld/client"; import type { ClientContext, FluidVariant, TagApi } from "bworld/client";
import { type Ingredient, mod_recipe_inputs, outputs, type Recipe, seconds, stack } from "./recipes.ts"; import { fluid_outputs, type Ingredient, mod_recipe_inputs, outputs, type Recipe, seconds, stack } from "./recipes.ts";
// in gui pixels, a slot with its border, like minecraft's // in gui pixels, a slot with its border, like minecraft's
export const SLOT = 18; export const SLOT = 18;
@@ -168,7 +168,7 @@ function base_categories(tags: TagApi): RecipeCategory[] {
height: 18, height: 18,
catalysts: [JUICER], catalysts: [JUICER],
draw(recipe, layout) { draw(recipe, layout) {
const { fluid, amount } = recipe.juice as { fluid: FluidVariant; amount: number }; const [{ fluid, amount }] = fluid_outputs(recipe);
layout.slot({ ids: [recipe.input], count: 1 }, 0, 0); layout.slot({ ids: [recipe.input], count: 1 }, 0, 0);
layout.texture("bworld:arrow_full", 31, 1, ARROW, ARROW); layout.texture("bworld:arrow_full", 31, 1, ARROW, ARROW);
layout.fluid(fluid, amount, 60, 0); layout.fluid(fluid, amount, 60, 0);
@@ -179,8 +179,8 @@ function base_categories(tags: TagApi): RecipeCategory[] {
} }
// the base game's juicer, from the items with bworld:juiceable: a recipe of its own type for each, that presses one // the base game's juicer, from the items with bworld:juiceable: a recipe of its own type for each, that presses one
// of it into its juice. the item is an input like any recipe's, the juice is under "juice", which reading a recipe's // of it into its juice. the item is an input like any recipe's, the juice is its fluid result, with the item it's from as
// inputs skips. the juice's data is the item it's from, like the juicer makes it // its data like the juicer makes it
const JUICING = "bworld:juicing"; const JUICING = "bworld:juicing";
const JUICER = "bworld:juicer"; const JUICER = "bworld:juicer";
@@ -190,7 +190,7 @@ export function juicing_recipes(ctx: ClientContext): Recipe[] {
const params = ctx.items.components(id)["bworld:juiceable"] as { juice?: number } | undefined; const params = ctx.items.components(id)["bworld:juiceable"] as { juice?: number } | undefined;
if (!params?.juice) return []; if (!params?.juice) return [];
const juice = { fluid: { id: "bworld:juice", data: { item: id } }, amount: params.juice }; const juice = { fluid: { id: "bworld:juice", data: { item: id } }, amount: params.juice };
return [{ type: JUICING, input: id, juice }]; return [{ type: JUICING, input: id, fluid_results: [juice] }];
}); });
} }
+6 -4
View File
@@ -5,21 +5,23 @@
// R and U over any item) opens a screen with the recipes that make it or the ones that use it, like jei's. it only // R and U over any item) opens a screen with the recipes that make it or the ones that use it, like jei's. it only
// reads recipes, so it works with any mod's, and draws the ones it has no category for from their json // reads recipes, so it works with any mod's, and draws the ones it has no category for from their json
import type { ClientContext } from "bworld/client"; import type { ClientContext } from "bworld/client";
import { ItemList, type Mode } from "./item_list.ts"; import { type Entry, ItemList, type Mode } from "./item_list.ts";
import { RecipeIndex, RecipesScreen } from "./recipes_screen.ts"; import { RecipeIndex, RecipesScreen } from "./recipes_screen.ts";
export function setup(ctx: ClientContext) { export function setup(ctx: ClientContext) {
const index = new RecipeIndex(ctx); const index = new RecipeIndex(ctx);
const list = new ItemList(ctx); const list = new ItemList(ctx);
// like jei's IRecipesGui.show, only when there's something to show // like jei's IRecipesGui.show, only when there's something to show
const open = (item: string, mode: Mode) => { const open = (entry: Entry, mode: Mode) => {
if (index.find(item, mode).length > 0) ctx.ui.open(new RecipesScreen(ctx, index, list, item, mode)); if (index.find(entry, mode).length > 0) ctx.ui.open(new RecipesScreen(ctx, index, list, entry, mode));
}; };
// the item list on every menu's screen, right of it // the item list on every menu's screen, right of it
ctx.ui.add_inventory_overlay({ ctx.ui.add_inventory_overlay({
on_render(g, screen) { on_render(g, screen) {
const hovered = list.render(g, screen.x + screen.width, open) ?? screen.hovered_item?.id; const slot = screen.hovered_item;
const hovered = list.render(g, screen.x + screen.width, open) ??
(slot && { kind: "item", id: slot.id } satisfies Entry);
if (hovered && g.key_pressed("KeyR")) open(hovered, "recipes"); if (hovered && g.key_pressed("KeyR")) open(hovered, "recipes");
if (hovered && g.key_pressed("KeyU")) open(hovered, "uses"); if (hovered && g.key_pressed("KeyU")) open(hovered, "uses");
}, },
+49 -30
View File
@@ -1,8 +1,8 @@
// SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-FileCopyrightText: 2026 Paula Wisnievski
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
// every item, right of the inventory and of the recipes screen, like jei's ingredient list overlay: the page and its // every item and fluid, right of the inventory and of the recipes screen, like jei's ingredient list overlay: the page
// arrows on top, the items, and the search box under them // and its arrows on top, the items then the fluids, and the search box under them
import type { ClientContext, Color, FluidVariant, Graphics } from "bworld/client"; import type { ClientContext, Color, FluidVariant, Graphics } from "bworld/client";
import { SLOT } from "./categories.ts"; import { SLOT } from "./categories.ts";
@@ -18,7 +18,21 @@ const FLUID_COLOR: Color = [0.55, 0.75, 0.95, 1];
// show the recipes that make an item, or the ones that use it // show the recipes that make an item, or the ones that use it
export type Mode = "recipes" | "uses"; export type Mode = "recipes" | "uses";
export type Open = (item: string, mode: Mode) => void; // what recipes are looked up for, like jei's ingredient types. a fluid is its id alone, whatever its data, so plain
// juice stands for every juice
export type Entry = { kind: "item" | "fluid"; id: string };
export type Open = (entry: Entry, mode: Mode) => void;
// looked up by a left click for its recipes, a right click for its uses
function click(g: Graphics, open: Open, entry: Entry) {
if (g.mouse.pressed) {
g.consume_click();
open(entry, "recipes");
} else if (g.mouse.right_pressed) {
g.consume_click();
open(entry, "uses");
}
}
// one item that can be hovered for its tooltip and clicked for its recipes (right click, its uses). true when hovered // one item that can be hovered for its tooltip and clicked for its recipes (right click, its uses). true when hovered
export function item_cell( export function item_cell(
@@ -40,54 +54,54 @@ export function item_cell(
{ text: id, color: LABEL_COLOR }, { text: id, color: LABEL_COLOR },
{ text: ctx.items.mod(id), color: MOD_COLOR }, { text: ctx.items.mod(id), color: MOD_COLOR },
]); ]);
if (mouse.pressed) { click(g, open, { kind: "item", id });
g.consume_click();
open(id, "recipes");
} else if (mouse.right_pressed) {
g.consume_click();
open(id, "uses");
}
return true; return true;
} }
// an amount of a fluid in a slot, hovered for its name and amount. fluids have no recipes of their own to open // a fluid in a slot, hovered for its name and amount (none in the list) and clicked like an item. true when hovered
export function fluid_cell( export function fluid_cell(
g: Graphics, g: Graphics,
ctx: ClientContext, ctx: ClientContext,
open: Open,
fluid: FluidVariant, fluid: FluidVariant,
amount: number,
x: number, x: number,
y: number, y: number,
tooltip: string[] = [], options: { amount?: number; tooltip?: string[] } = {},
) { ): boolean {
g.rect(x, y, SLOT, SLOT, SLOT_COLOR); g.rect(x, y, SLOT, SLOT, SLOT_COLOR);
g.fluid(fluid, x, y); g.fluid(fluid, x, y);
const { mouse } = g; const { mouse } = g;
if (mouse.x < x || mouse.y < y || mouse.x >= x + SLOT || mouse.y >= y + SLOT) return; if (mouse.x < x || mouse.y < y || mouse.x >= x + SLOT || mouse.y >= y + SLOT) return false;
g.rect(x, y, SLOT, SLOT, HOVER_COLOR); g.rect(x, y, SLOT, SLOT, HOVER_COLOR);
g.tooltip( g.tooltip(
[ [
ctx.fluids.name(fluid), ctx.fluids.name(fluid),
{ text: `${amount} mB`, color: FLUID_COLOR }, ...(options.amount === undefined ? [] : [{ text: `${options.amount} mB`, color: FLUID_COLOR }]),
...tooltip.map((text) => ({ text, color: LABEL_COLOR })), ...(options.tooltip ?? []).map((text) => ({ text, color: LABEL_COLOR })),
{ text: fluid.id, color: LABEL_COLOR }, { text: fluid.id, color: LABEL_COLOR },
{ text: ctx.fluids.mod(fluid.id), color: MOD_COLOR },
], ],
mouse.x, mouse.x,
mouse.y, mouse.y,
); );
click(g, open, { kind: "fluid", id: fluid.id });
return true;
} }
export class ItemList { export class ItemList {
// kept between screens, like jei // kept between screens, like jei
page = 0; page = 0;
// set on the first frame, once every mod's items are loaded // set on the first frame, once every mod's items and fluids are loaded
#items: string[] | undefined; #entries: Entry[] | undefined;
constructor(private ctx: ClientContext) {} constructor(private ctx: ClientContext) {}
// in the space from left to the window's right edge. returns the hovered item // in the space from left to the window's right edge. returns the hovered item or fluid
render(g: Graphics, left: number, open: Open): string | undefined { render(g: Graphics, left: number, open: Open): Entry | undefined {
this.#items ??= this.ctx.items.ids().filter((id) => id !== "bworld:air"); this.#entries ??= [
...this.ctx.items.ids().filter((id) => id !== "bworld:air").map((id) => ({ kind: "item" as const, id })),
...this.ctx.fluids.ids().map((id) => ({ kind: "fluid" as const, id })),
];
left += GAP; left += GAP;
const columns = Math.floor((g.width - GAP - left) / SLOT); const columns = Math.floor((g.width - GAP - left) / SLOT);
const top = GAP + BUTTON + 2; const top = GAP + BUTTON + 2;
@@ -96,7 +110,7 @@ export class ItemList {
const width = columns * SLOT; const width = columns * SLOT;
const search = g.text_input("search", left, g.height - GAP - BUTTON, width).trim().toLowerCase(); const search = g.text_input("search", left, g.height - GAP - BUTTON, width).trim().toLowerCase();
const items = this.#items.filter((id) => matches_search(this.ctx, id, search)); const items = this.#entries.filter((entry) => matches_search(this.ctx, entry, search));
const per_page = columns * rows; const per_page = columns * rows;
const pages = Math.max(1, Math.ceil(items.length / per_page)); const pages = Math.max(1, Math.ceil(items.length / per_page));
this.page = Math.min(this.page, pages - 1); this.page = Math.min(this.page, pages - 1);
@@ -106,25 +120,30 @@ export class ItemList {
const label = `${this.page + 1}/${pages}`; const label = `${this.page + 1}/${pages}`;
g.text(label, left + (width - g.measure_text(label)) / 2, GAP + (BUTTON - g.text_height) / 2); g.text(label, left + (width - g.measure_text(label)) / 2, GAP + (BUTTON - g.text_height) / 2);
let hovered: string | undefined; let hovered: Entry | undefined;
items.slice(this.page * per_page, (this.page + 1) * per_page).forEach((id, i) => { items.slice(this.page * per_page, (this.page + 1) * per_page).forEach((entry, i) => {
const x = left + (i % columns) * SLOT; const x = left + (i % columns) * SLOT;
const y = top + Math.floor(i / columns) * SLOT; const y = top + Math.floor(i / columns) * SLOT;
if (item_cell(g, this.ctx, open, id, x, y)) hovered = id; const shown = entry.kind === "item"
? item_cell(g, this.ctx, open, entry.id, x, y)
: fluid_cell(g, this.ctx, open, { id: entry.id }, x, y);
if (shown) hovered = entry;
}); });
if (items.length === 0) g.text("Nothing found", left, top, { color: [0.8, 0.8, 0.8, 1] }); if (items.length === 0) g.text("Nothing found", left, top, { color: [0.8, 0.8, 0.8, 1] });
return hovered; return hovered;
} }
} }
// by name or id, or "@mod" for a mod's items, like jei // by name or id, or "@mod" for a mod's items and fluids, like jei
function matches_search(ctx: ClientContext, id: string, search: string): boolean { function matches_search(ctx: ClientContext, { kind, id }: Entry, search: string): boolean {
if (search === "") return true; if (search === "") return true;
const name = kind === "item" ? ctx.items.name(id) : ctx.fluids.name({ id });
const mod_name = kind === "item" ? ctx.items.mod(id) : ctx.fluids.mod(id);
return search.split(/\s+/).every((word) => { return search.split(/\s+/).every((word) => {
if (word.startsWith("@")) { if (word.startsWith("@")) {
const mod = word.slice(1); const mod = word.slice(1);
return id.split(":")[0].includes(mod) || ctx.items.mod(id).toLowerCase().includes(mod); return id.split(":")[0].includes(mod) || mod_name.toLowerCase().includes(mod);
} }
return ctx.items.name(id).toLowerCase().includes(word) || id.includes(word); return name.toLowerCase().includes(word) || id.includes(word);
}); });
} }
+13 -1
View File
@@ -3,12 +3,14 @@
// reading recipes: what each one makes and takes, for any mod's recipe type, so the viewer works with recipes it has // reading recipes: what each one makes and takes, for any mod's recipe type, so the viewer works with recipes it has
// no category for // no category for
import type { TagApi } from "bworld/client"; import type { FluidVariant, TagApi } from "bworld/client";
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
export type Recipe = any; export type Recipe = any;
// one place in a recipe: the items any of which fit, and how many // one place in a recipe: the items any of which fit, and how many
export type Ingredient = { ids: string[]; count: number }; export type Ingredient = { ids: string[]; count: number };
// an amount of a fluid in mB
export type FluidAmount = { fluid: FluidVariant; amount: number };
// what a recipe makes // what a recipe makes
export function outputs(recipe: Recipe): Ingredient[] { export function outputs(recipe: Recipe): Ingredient[] {
@@ -33,6 +35,16 @@ export function outputs(recipe: Recipe): Ingredient[] {
} }
} }
// the fluids a recipe makes and takes, from its "fluid_results" and "fluid_ingredients", which the viewer's own recipe
// types use, like juicing
export function fluid_outputs(recipe: Recipe): FluidAmount[] {
return recipe.fluid_results ?? [];
}
export function fluid_inputs(recipe: Recipe): FluidAmount[] {
return recipe.fluid_ingredients ?? [];
}
// what a recipe takes. shaped keys, furnace inputs and fuels can be an item or #tag, or a list any of which fits // 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[] { export function inputs(recipe: Recipe, tags: TagApi): Ingredient[] {
switch (recipe.type) { switch (recipe.type) {
+47 -28
View File
@@ -15,8 +15,17 @@ import {
type RecipeLayout, type RecipeLayout,
SLOT, SLOT,
} from "./categories.ts"; } from "./categories.ts";
import { BUTTON, fluid_cell, item_cell, ItemList, LABEL_COLOR, type Mode, SLOT_COLOR } from "./item_list.ts"; import {
import { type Ingredient, inputs, outputs, type Recipe } from "./recipes.ts"; BUTTON,
type Entry,
fluid_cell,
item_cell,
ItemList,
LABEL_COLOR,
type Mode,
SLOT_COLOR,
} from "./item_list.ts";
import { fluid_inputs, fluid_outputs, type Ingredient, inputs, outputs, type Recipe } from "./recipes.ts";
// jei's sizes, in gui pixels // jei's sizes, in gui pixels
const WIDTH = 198; const WIDTH = 198;
@@ -54,23 +63,28 @@ export class RecipeIndex {
return this.#categories; return this.#categories;
} }
// each category with recipes that make the item, or use it. like jei, the uses of where a category is made, like // each category with recipes that make the item or fluid, or use it. like jei, the uses of where a category is
// a furnace, are all of that category's recipes // made, like a furnace, are all of that category's recipes
find(item: string, mode: Mode): { category: RecipeCategory; recipes: Recipe[] }[] { find({ kind, id }: Entry, mode: Mode): { category: RecipeCategory; recipes: Recipe[] }[] {
const has = (recipe: Recipe) => {
if (kind === "fluid") {
const fluids = mode === "recipes" ? fluid_outputs(recipe) : fluid_inputs(recipe);
return fluids.some(({ fluid }) => fluid.id === id);
}
const items = mode === "recipes" ? outputs(recipe) : inputs(recipe, this.ctx.tags);
return items.some((item) => item.ids.includes(id));
};
return this.categories.flatMap((category) => { return this.categories.flatMap((category) => {
const of_type = this.#recipes.filter((recipe) => recipe.type === category.type); const of_type = this.#recipes.filter((recipe) => recipe.type === category.type);
const recipes = mode === "recipes" const made_in = kind === "item" && mode === "uses" && category.catalysts.includes(id);
? of_type.filter((recipe) => outputs(recipe).some((output) => output.ids.includes(item))) const recipes = made_in ? of_type : of_type.filter(has);
: category.catalysts.includes(item)
? of_type
: of_type.filter((recipe) => inputs(recipe, this.ctx.tags).some((input) => input.ids.includes(item)));
return recipes.length > 0 ? [{ category, recipes }] : []; return recipes.length > 0 ? [{ category, recipes }] : [];
}); });
} }
} }
interface View { interface View {
item: string; entry: Entry;
mode: Mode; mode: Mode;
// which of find's categories, and which page of it // which of find's categories, and which page of it
category: number; category: number;
@@ -85,16 +99,16 @@ export class RecipesScreen implements Screen {
private ctx: ClientContext, private ctx: ClientContext,
private index: RecipeIndex, private index: RecipeIndex,
private list: ItemList, private list: ItemList,
item: string, entry: Entry,
mode: Mode, mode: Mode,
) { ) {
this.#view = { item, mode, category: 0, page: 0 }; this.#view = { entry, mode, category: 0, page: 0 };
} }
on_render(g: Graphics) { on_render(g: Graphics) {
// over the world, like every screen // over the world, like every screen
g.rect(0, 0, g.width, g.height, [0, 0, 0, 0.8]); g.rect(0, 0, g.width, g.height, [0, 0, 0, 0.8]);
const results = this.index.find(this.#view.item, this.#view.mode); const results = this.index.find(this.#view.entry, this.#view.mode);
if (results.length === 0) return; if (results.length === 0) return;
const view = this.#view; const view = this.#view;
view.category = Math.min(view.category, results.length - 1); view.category = Math.min(view.category, results.length - 1);
@@ -108,7 +122,7 @@ export class RecipesScreen implements Screen {
// the panels: tabs and where it's made go under the recipes' panel's edges // the panels: tabs and where it's made go under the recipes' panel's edges
const tabs = this.#tabs(g, results, left, top); const tabs = this.#tabs(g, results, left, top);
let hovered: string | undefined; let hovered: Entry | undefined;
const catalysts = category.catalysts; const catalysts = category.catalysts;
if (catalysts.length > 0) { if (catalysts.length > 0) {
const width = CATALYST_BORDER * 2 + SLOT; const width = CATALYST_BORDER * 2 + SLOT;
@@ -117,7 +131,7 @@ export class RecipesScreen implements Screen {
g.panel(left, top, WIDTH, height); g.panel(left, top, WIDTH, height);
catalysts.forEach((id, i) => { catalysts.forEach((id, i) => {
const y = recipes_top + CATALYST_BORDER + i * SLOT; const y = recipes_top + CATALYST_BORDER + i * SLOT;
if (this.#cell(g, id, x + CATALYST_BORDER, y, { slot: true })) hovered = id; if (this.#cell(g, id, x + CATALYST_BORDER, y, { slot: true })) hovered = { kind: "item", id };
}); });
} else { } else {
g.panel(left, top, WIDTH, height); g.panel(left, top, WIDTH, height);
@@ -149,7 +163,7 @@ export class RecipesScreen implements Screen {
hovered = this.#draw_recipe(g, category, recipe, x, y) ?? hovered; hovered = this.#draw_recipe(g, category, recipe, x, y) ?? hovered;
}); });
hovered = this.list.render(g, left + WIDTH, (item, mode) => this.#open(item, mode)) ?? hovered; hovered = this.list.render(g, left + WIDTH, (entry, mode) => this.#open(entry, mode)) ?? hovered;
if (hovered && g.key_pressed("KeyR")) this.#open(hovered, "recipes"); if (hovered && g.key_pressed("KeyR")) this.#open(hovered, "recipes");
if (hovered && g.key_pressed("KeyU")) this.#open(hovered, "uses"); if (hovered && g.key_pressed("KeyU")) this.#open(hovered, "uses");
if (g.key_pressed("Backspace")) this.#view = this.#history.pop() ?? this.#view; if (g.key_pressed("Backspace")) this.#view = this.#history.pop() ?? this.#view;
@@ -157,12 +171,12 @@ export class RecipesScreen implements Screen {
if (g.key_pressed(this.ctx.options.key_inventory)) this.ctx.ui.close(); if (g.key_pressed(this.ctx.options.key_inventory)) this.ctx.ui.close();
} }
// shows the recipes for or with another item, coming back to this one with backspace. nothing happens when there // shows the recipes for or with another item or fluid, coming back to this one with backspace. nothing happens when
// aren't any // there aren't any
#open(item: string, mode: Mode) { #open(entry: Entry, mode: Mode) {
if (this.index.find(item, mode).length === 0) return; if (this.index.find(entry, mode).length === 0) return;
this.#history.push(this.#view); this.#history.push(this.#view);
this.#view = { item, mode, category: 0, page: 0 }; this.#view = { entry, mode, category: 0, page: 0 };
} }
// a tab per category, as many as fit around the selected one, each showing where it's made or what it makes. the // a tab per category, as many as fit around the selected one, each showing where it's made or what it makes. the
@@ -200,9 +214,9 @@ export class RecipesScreen implements Screen {
}; };
} }
// one recipe, its category drawing it with its top left at x, y. returns the hovered item // one recipe, its category drawing it with its top left at x, y. returns the hovered item or fluid
#draw_recipe(g: Graphics, category: RecipeCategory, recipe: Recipe, x: number, y: number): string | undefined { #draw_recipe(g: Graphics, category: RecipeCategory, recipe: Recipe, x: number, y: number): Entry | undefined {
let hovered: string | undefined; let hovered: Entry | undefined;
const layout: RecipeLayout = { const layout: RecipeLayout = {
slot: (ingredient: Ingredient | undefined, dx, dy, tooltip = []) => { slot: (ingredient: Ingredient | undefined, dx, dy, tooltip = []) => {
if (!ingredient || ingredient.ids.length === 0) { if (!ingredient || ingredient.ids.length === 0) {
@@ -212,9 +226,14 @@ export class RecipesScreen implements Screen {
const id = ingredient.ids[Math.floor(Date.now() / CYCLE_MS) % ingredient.ids.length]; const id = ingredient.ids[Math.floor(Date.now() / CYCLE_MS) % ingredient.ids.length];
const any = ingredient.ids.length > 1 ? [`Any of ${ingredient.ids.length}`] : []; const any = ingredient.ids.length > 1 ? [`Any of ${ingredient.ids.length}`] : [];
const options = { slot: true, count: ingredient.count, tooltip: [...any, ...tooltip] }; const options = { slot: true, count: ingredient.count, tooltip: [...any, ...tooltip] };
if (this.#cell(g, id, x + dx, y + dy, options)) hovered = id; if (this.#cell(g, id, x + dx, y + dy, options)) hovered = { kind: "item", id };
},
fluid: (fluid, amount, dx, dy, tooltip) => {
const open = (entry: Entry, mode: Mode) => this.#open(entry, mode);
if (fluid_cell(g, this.ctx, open, fluid, x + dx, y + dy, { amount, tooltip })) {
hovered = { kind: "fluid", id: fluid.id };
}
}, },
fluid: (fluid, amount, dx, dy, tooltip) => fluid_cell(g, this.ctx, fluid, amount, x + dx, y + dy, tooltip),
texture: (id, dx, dy, w, h) => g.texture(id, x + dx, y + dy, w, h), texture: (id, dx, dy, w, h) => g.texture(id, x + dx, y + dy, w, h),
text: (text, dx, dy) => g.text(text, x + dx, y + dy, { color: LABEL_COLOR }), text: (text, dx, dy) => g.text(text, x + dx, y + dy, { color: LABEL_COLOR }),
}; };
@@ -223,6 +242,6 @@ export class RecipesScreen implements Screen {
} }
#cell(g: Graphics, id: string, x: number, y: number, options: Parameters<typeof item_cell>[6]) { #cell(g: Graphics, id: string, x: number, y: number, options: Parameters<typeof item_cell>[6]) {
return item_cell(g, this.ctx, (item, mode) => this.#open(item, mode), id, x, y, options); return item_cell(g, this.ctx, (entry, mode) => this.#open(entry, mode), id, x, y, options);
} }
} }
+24 -11
View File
@@ -47,6 +47,9 @@ function client_ctx(recipes: any[] = []) {
} as unknown as ClientContext; } as unknown as ClientContext;
} }
const item = (id: string) => ({ kind: "item" as const, id });
const fluid = (id: string) => ({ kind: "fluid" as const, id });
// a recipe layout that keeps nothing, for the parts a test doesn't look at // a recipe layout that keeps nothing, for the parts a test doesn't look at
const ignore: RecipeLayout = { slot: () => {}, fluid: () => {}, texture: () => {}, text: () => {} }; const ignore: RecipeLayout = { slot: () => {}, fluid: () => {}, texture: () => {}, text: () => {} };
@@ -97,18 +100,20 @@ Deno.test("recipes are found by category in jei's order, and a catalyst's uses a
"farmersdelight:cooking_pot", "farmersdelight:cooking_pot",
]); ]);
const furnace_uses = index.find("bworld:furnace", "uses").map(({ category, recipes }) => [category.type, recipes]); const furnace_uses = index.find(item("bworld:furnace"), "uses").map((
{ category, recipes },
) => [category.type, recipes]);
const all = game.recipes.all; const all = game.recipes.all;
assertEquals(furnace_uses.find(([type]) => type === "furnace")?.[1], all.filter((r) => r.type === "furnace")); assertEquals(furnace_uses.find(([type]) => type === "furnace")?.[1], all.filter((r) => r.type === "furnace"));
assertEquals(furnace_uses.find(([type]) => type === "fuel")?.[1], all.filter((r) => r.type === "fuel")); assertEquals(furnace_uses.find(([type]) => type === "fuel")?.[1], all.filter((r) => r.type === "fuel"));
assertEquals(index.find("bworld:air", "recipes"), []); assertEquals(index.find(item("bworld:air"), "recipes"), []);
}); });
Deno.test("clicking a fish shows when and where it bites, and the fishing rod's uses are every fish", async () => { 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"); await test_game("mods");
const ctx = client_ctx(); const ctx = client_ctx();
const index = new RecipeIndex(ctx); const index = new RecipeIndex(ctx);
const [fishing] = index.find("bworld:red_mullet", "recipes"); const [fishing] = index.find(item("bworld:red_mullet"), "recipes");
assertEquals(fishing.category.title, "Fishing"); assertEquals(fishing.category.title, "Fishing");
assertEquals(fishing.category.catalysts, ["bworld:fishing_rod"]); assertEquals(fishing.category.catalysts, ["bworld:fishing_rod"]);
const lines: string[] = []; const lines: string[] = [];
@@ -124,13 +129,13 @@ Deno.test("clicking a fish shows when and where it bites, and the fishing rod's
// a lava fish without limits, and every fish under the rod // a lava fish without limits, and every fish under the rod
const lava_lines: string[] = []; const lava_lines: string[] = [];
const [lava] = index.find("bworld:prismfish", "recipes"); const [lava] = index.find(item("bworld:prismfish"), "recipes");
lava.category.draw(lava.recipes[0], { ...ignore, text: (text) => lava_lines.push(text) }); lava.category.draw(lava.recipes[0], { ...ignore, text: (text) => lava_lines.push(text) });
assertEquals(lava_lines, ["Seasons: all", "Biomes: all", "Time: all day", "In: Lava"]); 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); const fish = index.find(item("bworld:fishing_rod"), "uses")[0].recipes.map((recipe) => recipe.result);
assert(fish.includes("bworld:cod") && fish.includes("bworld:prismfish")); assert(fish.includes("bworld:cod") && fish.includes("bworld:prismfish"));
// a fish isn't a use of the water it lives in // a fish isn't a use of the water it lives in
assertEquals(index.find("bworld:water", "uses"), []); assertEquals(index.find(item("bworld:water"), "uses"), []);
}); });
Deno.test("a crab pot's recipes are what each bait catches in fresh and salt water, with their chances", async () => { Deno.test("a crab pot's recipes are what each bait catches in fresh and salt water, with their chances", async () => {
@@ -139,13 +144,13 @@ Deno.test("a crab pot's recipes are what each bait catches in fresh and salt wat
const index = new RecipeIndex(ctx); const index = new RecipeIndex(ctx);
// grubs are bait for both waters, under the crab pot's tab // grubs are bait for both waters, under the crab pot's tab
const [grubs] = index.find("bworld:grubs", "uses"); const [grubs] = index.find(item("bworld:grubs"), "uses");
assertEquals(grubs.category.title, "Crab Pot"); assertEquals(grubs.category.title, "Crab Pot");
assertEquals(grubs.category.catalysts, ["bworld:crabpot"]); assertEquals(grubs.category.catalysts, ["bworld:crabpot"]);
assertEquals(grubs.recipes.map((recipe) => recipe.crabpot.water), ["freshwater", "saltwater"]); assertEquals(grubs.recipes.map((recipe) => recipe.crabpot.water), ["freshwater", "saltwater"]);
// a seashroom comes from grubs in salt water, one in 16 // a seashroom comes from grubs in salt water, one in 16
const [seashroom] = index.find("bworld:red_seashroom", "recipes"); const [seashroom] = index.find(item("bworld:red_seashroom"), "recipes");
assertEquals(seashroom.recipes.length, 1); assertEquals(seashroom.recipes.length, 1);
const recipe = seashroom.recipes[0]; const recipe = seashroom.recipes[0];
assertEquals(recipe.bait, "bworld:grubs"); assertEquals(recipe.bait, "bworld:grubs");
@@ -163,7 +168,7 @@ Deno.test("a crab pot's recipes are what each bait catches in fresh and salt wat
assertEquals(slots.find(([id]) => id === "bworld:red_seashroom")?.[1], ["6.3% chance"]); assertEquals(slots.find(([id]) => id === "bworld:red_seashroom")?.[1], ["6.3% chance"]);
// worms in fresh water catch a third each of three things // worms in fresh water catch a third each of three things
const worms = index.find("bworld:worms", "uses").find((found) => found.category.title === "Crab Pot")!; const worms = index.find(item("bworld:worms"), "uses").find((found) => found.category.title === "Crab Pot")!;
const freshwater = worms.recipes.find((r) => r.crabpot.water === "freshwater"); const freshwater = worms.recipes.find((r) => r.crabpot.water === "freshwater");
assertEquals(freshwater.result.toSorted(), ["bworld:chorus_snail", "bworld:crayfish", "bworld:leech"]); assertEquals(freshwater.result.toSorted(), ["bworld:chorus_snail", "bworld:crayfish", "bworld:leech"]);
assertEquals(freshwater.crabpot.catches.map((c: { chance: number }) => c.chance), [1 / 3, 1 / 3, 1 / 3]); assertEquals(freshwater.crabpot.catches.map((c: { chance: number }) => c.chance), [1 / 3, 1 / 3, 1 / 3]);
@@ -174,7 +179,7 @@ Deno.test("each juiceable item has a juicing recipe that presses it into its own
const index = new RecipeIndex(client_ctx()); const index = new RecipeIndex(client_ctx());
// a strawberry's uses include the juicer pressing it into strawberry juice, shown in a fluid slot // a strawberry's uses include the juicer pressing it into strawberry juice, shown in a fluid slot
const juicing = index.find("bworld:strawberry", "uses").find((found) => found.category.title === "Juicing")!; const juicing = index.find(item("bworld:strawberry"), "uses").find((found) => found.category.title === "Juicing")!;
assertEquals(juicing.category.catalysts, ["bworld:juicer"]); assertEquals(juicing.category.catalysts, ["bworld:juicer"]);
assertEquals(juicing.recipes.length, 1); assertEquals(juicing.recipes.length, 1);
const slots: string[] = []; const slots: string[] = [];
@@ -191,6 +196,14 @@ Deno.test("each juiceable item has a juicing recipe that presses it into its own
assertEquals(fluids, [[{ id: "bworld:juice", data: { item: "bworld:strawberry" } }, juice]]); assertEquals(fluids, [[{ id: "bworld:juice", data: { item: "bworld:strawberry" } }, juice]]);
// the juicer's uses are every juiceable item // the juicer's uses are every juiceable item
const pressed = index.find("bworld:juicer", "uses")[0].recipes.map((recipe) => recipe.input); const pressed = index.find(item("bworld:juicer"), "uses")[0].recipes.map((recipe) => recipe.input);
assert(pressed.includes("bworld:strawberry") && pressed.includes("bworld:melon")); assert(pressed.includes("bworld:strawberry") && pressed.includes("bworld:melon"));
// juice, whatever it's from, is made by every one of them, and nothing uses it yet
const [making_juice] = index.find(fluid("bworld:juice"), "recipes");
assertEquals(making_juice.category.title, "Juicing");
assertEquals(making_juice.recipes.map((recipe) => recipe.input), pressed);
assertEquals(index.find(fluid("bworld:juice"), "uses"), []);
// a fluid isn't an item of the same id
assertEquals(index.find(item("bworld:juice"), "recipes"), []);
}); });