diff --git a/client/gui/mod_graphics.ts b/client/gui/mod_graphics.ts index da96d73..132c51c 100644 --- a/client/gui/mod_graphics.ts +++ b/client/gui/mod_graphics.ts @@ -6,7 +6,7 @@ // pixels each import { EverythingRegistry, type ItemRegistry } from "@/common/everything_registry.ts"; import { ItemStack } from "@/common/inventory.ts"; -import type { Color, Graphics } from "@/common/mod_api/client.ts"; +import type { Color, Graphics, TooltipLine } from "@/common/mod_api/client.ts"; import type { FluidVariant, KeyCode } from "@/common/mod_api/shared.ts"; import { point_inside_rec } from "@/common/utils.ts"; import { AssetManager } from "@/client/assets.ts"; @@ -23,13 +23,19 @@ import { } from "@/client/renderer/mod.ts"; import { draw_fluid, draw_item } from "@/client/rendering/render_utils.ts"; import { get_sprite_region } from "@/client/sprites.ts"; -import { draw_tooltip, item_tooltip, type TooltipLine } from "./tooltip.ts"; +import { draw_tooltip, item_tooltip } from "./tooltip.ts"; import { TEXT_HEIGHT, TEXT_SCALE, TextInput } from "./widgets.ts"; import { GUI_ATLAS } from "./gui_atlas.ts"; import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; const WHITE: Color = [1, 1, 1, 1]; const GREY: Color = [0.66, 0.66, 0.66, 1]; + +// a script's tooltip line, copied so it can't change after, in color when it doesn't say +function tooltip_line(line: string | TooltipLine, color: Color): TooltipLine { + if (typeof line === "string") return { text: line, color }; + return { text: String(line.text), color: line.color ?? color, icon: line.icon, priority: line.priority }; +} const INPUT_HEIGHT = 32; const FIELD_BORDER = GUI_ATLAS.text_field.border[0] * GUI_SCALE; @@ -245,25 +251,12 @@ export class ModGraphics implements Graphics { return !this.typing && InputManager.is_key_pressed(key as InputKey); } - tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number) { - this.#tooltip = { - lines: lines.map((line) => - typeof line === "string" - ? { text: line, color: WHITE } - : { text: line.text, color: line.color ?? WHITE } - ), - x: this.#x(x), - y: this.#y(y), - }; + tooltip(lines: (string | TooltipLine)[], x: number, y: number) { + this.#tooltip = { lines: lines.map((line) => tooltip_line(line, WHITE)), x: this.#x(x), y: this.#y(y) }; } - item_tooltip(id: string, x: number, y: number, extra: (string | { text: string; color?: Color })[] = []) { - const lines = [ - ...item_tooltip(new ItemStack(id, 1)), - ...extra.map((line) => - typeof line === "string" ? { text: line, color: GREY } : { text: line.text, color: line.color ?? GREY } - ), - ]; + item_tooltip(id: string, x: number, y: number, extra: (string | TooltipLine)[] = []) { + const lines = item_tooltip(new ItemStack(id, 1), extra.map((line) => tooltip_line(line, GREY))); this.#tooltip = { lines, x: this.#x(x), y: this.#y(y) }; } diff --git a/client/gui/tooltip.ts b/client/gui/tooltip.ts index e0a80fb..478ed75 100644 --- a/client/gui/tooltip.ts +++ b/client/gui/tooltip.ts @@ -1,10 +1,12 @@ // SPDX-FileCopyrightText: 2026 Paula Wisnievski // SPDX-License-Identifier: AGPL-3.0-or-later -// the box next to the mouse saying what an item is, like minecraft's: its name, the fluid in it, what it's worth, then -// its lore +// the box next to the mouse saying what an item is, like minecraft's: its name, the fluid in it, its food, mods' lines, +// then its lore import { EverythingRegistry, ItemRegistry } from "@/common/everything_registry.ts"; -import { contained_fluid, type ItemStack, sell_value } from "@/common/inventory.ts"; +import { contained_fluid, type ItemStack } from "@/common/inventory.ts"; +import { type Color, TOOLTIP_PRIORITY, type TooltipLine } from "@/common/mod_api/client.ts"; +import type { ItemStack as ApiItemStack } from "@/common/mod_api/shared.ts"; import { display_name } from "@/common/utils.ts"; import { fluid_rules } from "@/client/fluid_rules.ts"; import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "@/client/renderer/mod.ts"; @@ -14,53 +16,61 @@ import { get_sprite_region } from "@/client/sprites.ts"; import { draw_gui_sprite, GUI_SCALE } from "./gui_sprites.ts"; import { TEXT_HEIGHT, TEXT_SCALE } from "./widgets.ts"; -type Color = [number, number, number, number]; +export type { TooltipLine }; const NAME_COLOR: Color = [1, 1, 1, 1]; const LORE_COLOR: Color = [0.66, 0.66, 0.66, 1]; const FOOD_COLOR: Color = [0.85, 0.6, 0.35, 1]; const FLUID_COLOR: Color = [0.55, 0.75, 0.95, 1]; -const VALUE_COLOR: Color = [0.95, 0.8, 0.3, 1]; +const WHITE: Color = [1, 1, 1, 1]; // from the edge of the tooltip sprite to the text const PADDING = 4 * GUI_SCALE; const LINE_GAP = 4; // between a line's icon and its text const ICON_GAP = 4; -// drawn before what an item sells for -const VALUE_ICON = "bworld:copper_coin"; // from the mouse, so the cursor doesn't cover it const OFFSET = 16; -export interface TooltipLine { - text: string; - color: Color; - // a texture drawn before the text, a square as tall as the line - icon?: string; -} +// what client scripts add to every item's tooltip, in load order, see ctx.ui.add_item_tooltip. client/mods.ts fills it, +// each wrapped so one throwing is logged under its mod +export const item_tooltips: ((item: ApiItemStack, lines: TooltipLine[]) => void)[] = []; -// its name, the fluid a full container holds, the item's lore from its json, then what the server's scripts said about -// this stack -export function item_tooltip(item: ItemStack): TooltipLine[] { - const lines: TooltipLine[] = [{ text: display_name(item.type_id), color: NAME_COLOR }]; +// its name, the fluid a full container holds, its food, the lines mods add, then the item's lore from its json and what +// the server's scripts said about this stack. extra lines go under all of it unless they have a priority +export function item_tooltip(item: ItemStack, extra: TooltipLine[] = []): TooltipLine[] { + const lines: TooltipLine[] = [ + { text: display_name(item.type_id), color: NAME_COLOR, priority: TOOLTIP_PRIORITY.name }, + ]; const info = EverythingRegistry.get("items", item.type_id); const fluid = contained_fluid(item.type_id, item.data); if (fluid) { - lines.push({ text: `${fluid_rules.name(fluid)}, ${info!.fluid_container!.capacity} mB`, color: FLUID_COLOR }); + lines.push({ + text: `${fluid_rules.name(fluid)}, ${info!.fluid_container!.capacity} mB`, + color: FLUID_COLOR, + priority: TOOLTIP_PRIORITY.fluid, + }); } if (info?.food) { - lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR }); + lines.push({ + text: `Restores ${info.food.nutrition} hunger`, + color: FOOD_COLOR, + priority: TOOLTIP_PRIORITY.food, + }); } - const value = sell_value(item.type_id, item.data, fluid_rules); - if (value !== undefined) { - lines.push({ icon: VALUE_ICON, text: `${value}`, color: VALUE_COLOR }); - } - const static_lore = info?.lore; - for (const lore of [static_lore, item.lore]) { + for (const lore of [info?.lore, item.lore]) { for (const text of lore?.split("\n") ?? []) { - lines.push({ text, color: LORE_COLOR }); + lines.push({ text, color: LORE_COLOR, priority: TOOLTIP_PRIORITY.lore }); } } - return lines; + if (item_tooltips.length) { + const stack = { id: item.type_id, count: item.amount, data: item.data }; + for (const fn of item_tooltips) fn(stack, lines); + } + for (const line of extra) lines.push({ ...line, priority: line.priority ?? TOOLTIP_PRIORITY.extra }); + // sort is stable, so lines with the same priority stay in the order they were added + return lines + .filter((line) => typeof line?.text === "string") + .sort((a, b) => (a.priority ?? TOOLTIP_PRIORITY.default) - (b.priority ?? TOOLTIP_PRIORITY.default)); } // a menu's fluid slot, like modern industrialization's: the fluid, how much of the tank it fills, and what clicking it @@ -109,6 +119,6 @@ export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: num line_height * 2, ); } - draw_text(line.text, x + PADDING + icon_width(line), line_y, TEXT_SCALE, line.color); + draw_text(line.text, x + PADDING + icon_width(line), line_y, TEXT_SCALE, line.color ?? WHITE); }); } diff --git a/client/mods.ts b/client/mods.ts index 904ceef..053a816 100644 --- a/client/mods.ts +++ b/client/mods.ts @@ -27,6 +27,7 @@ import { AIR_ID } from "@/common/protocol.ts"; import type { Client } from "./client.ts"; import { code_url, fetch_verified } from "./handshake.ts"; import { GuiModScreen } from "./gui/gui_mod_screens.ts"; +import { item_tooltips } from "./gui/tooltip.ts"; import { clear_hooks, expose_engine, loading_mod } from "@/common/hooks.ts"; import { common_engine } from "@/common/engine_modules.ts"; import { client_engine } from "./engine_modules.ts"; @@ -165,6 +166,7 @@ export async function load_client_mods(listings: ModListing[], mods: Mod[]) { const recipes = register_mod_data(mods.map((mod) => ({ id: mod.manifest.id, data: mod.data }))); recipe_book = recipes; inventory_overlays.length = 0; + item_tooltips.length = 0; menu_screens.clear(); net_handlers.clear(); block_visuals.clear(); @@ -239,7 +241,7 @@ function shared_context(listing: ModListing): SharedContext { side: "client", tags: tag_api(), blocks: blocks_api(mod), - items: shared_items_api(mod), + items: shared_items_api(mod, fluid_rules), fluids: { exists: fluid_exists, same: same_fluid, @@ -309,6 +311,10 @@ function client_context(listing: ModListing): ClientContext { add_inventory_overlay(overlay) { inventory_overlays.push({ mod, overlay }); }, + add_item_tooltip(fn) { + if (typeof fn !== "function") throw new Error(`[${mod}] add_item_tooltip takes a function`); + item_tooltips.push((item, lines) => run_mod(mod, "an item tooltip", () => fn(item, lines))); + }, }, hud: not_yet("hud", "HUD elements") as ClientContext["hud"], input: not_yet("input", "keybinds") as ClientContext["input"], @@ -359,7 +365,7 @@ function client_context(listing: ModListing): ClientContext { }, }, items: { - ...shared_items_api(mod), + ...shared_items_api(mod, fluid_rules), mod(id) { known_item(id); return mod_name(id); diff --git a/common/mod_api/client.ts b/common/mod_api/client.ts index 9f05c71..57d3c27 100644 --- a/common/mod_api/client.ts +++ b/common/mod_api/client.ts @@ -72,6 +72,8 @@ export interface ClientContext { // 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; + // what one sells for with this data, or undefined if it can't be sold, like the server's ctx.items.sell_value + sell_value(id: Id, data?: unknown): number | undefined; }; // every block any mod registered blocks: { @@ -244,6 +246,28 @@ export interface MenuScreen { export type Color = [number, number, number, number]; +// a line of a tooltip, see Graphics.tooltip and ClientUi.add_item_tooltip +export interface TooltipLine { + text: string; + color?: Color; + // a texture drawn before the text, as tall as the line + icon?: Id; + // lower comes first, TOOLTIP_PRIORITY.default when not set. lines with the same priority keep the order they were + // added in: the game's first, then mods' in load order. only item tooltips are sorted + priority?: number; +} + +// where the game's own lines of an item's tooltip go, so a mod's can go between them +export const TOOLTIP_PRIORITY = { + name: 0, + fluid: 100, + food: 200, + default: 500, + lore: 1000, + // g.item_tooltip's extra lines + extra: 2000, +} as const; + // immediate mode drawing, like the debug ui, styled with the gui sprites (assets/sprites/gui.png). everything is in // gui pixels, like neoforge's: 18 of them are a slot export interface Graphics { @@ -279,10 +303,11 @@ export interface Graphics { slider(id: string, x: number, y: number, w: number, min: number, max: number): number; // false while a text input has the keyboard key_pressed(key: KeyCode): boolean; - // shown next to the mouse over everything else, the last one this frame wins - tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number): void; - // the game's tooltip for an item (its name, food, lore), with extra lines under it - item_tooltip(id: Id, x: number, y: number, extra?: (string | { text: string; color?: Color })[]): void; + // shown next to the mouse over everything else, the last one this frame wins. white unless a line says + tooltip(lines: (string | TooltipLine)[], x: number, y: number): void; + // an item's tooltip as slots show it (its name, food, lore, mods' lines), with extra lines, grey unless they say. + // they go under everything unless they have a priority + item_tooltip(id: Id, x: number, y: number, extra?: (string | TooltipLine)[]): void; // the click was used, so nothing under it (like slots) gets it consume_click(): void; // the screen underneath ignores the mouse over this area this frame, for something drawn over it @@ -316,6 +341,9 @@ export interface ClientUi { // closes the screen on top close(): void; add_inventory_overlay(overlay: InventoryOverlay): void; + // adds to every item's tooltip, like neoforge's ItemTooltipEvent. lines has the game's lines and earlier mods'. push + // to it, and the lines are sorted by priority once every function has run + add_item_tooltip(fn: (item: ItemStack, lines: TooltipLine[]) => void): void; } export interface HudRegistry { diff --git a/common/mod_api/shared.ts b/common/mod_api/shared.ts index fe3853f..b17a1c5 100644 --- a/common/mod_api/shared.ts +++ b/common/mod_api/shared.ts @@ -157,8 +157,9 @@ export interface SharedContext { name(id: Id): string; // the components its json lists and their params, a copy components(id: Id): Record; - // what one sells for, from its "base_sell_value", or undefined if it can't be sold - sell_value(id: Id): number | undefined; + // what one of it sells for with this data, or undefined if it can't be sold: the item's "base_sell_value", and a + // full fluid container's fluid on top, like the server's ctx.items.sell_value + sell_value(id: Id, data?: unknown): number | undefined; }; // every fluid any mod defines, like the server's and clients' ctx.fluids, without what rules decide readonly fluids: Pick & { diff --git a/common/utils.ts b/common/utils.ts index f482d52..5ff04a8 100644 --- a/common/utils.ts +++ b/common/utils.ts @@ -5,7 +5,8 @@ import { ID_MASK, STATE_SHIFT } from "./constants.ts"; import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "./everything_registry.ts"; import { block_item_texture, cube_face_texture } from "./block_models.ts"; import { FULL_SHAPE, type Shape } from "./block_shapes.ts"; -import type { ItemStack } from "./inventory.ts"; +import type { FluidRules } from "./fluids.ts"; +import { type ItemStack, sell_value } from "./inventory.ts"; import type { SharedContext } from "./mod_api/shared.ts"; import type { FluidInCell } from "./physics.ts"; import { list_matches } from "./tags.ts"; @@ -87,8 +88,9 @@ export function display_name(item_id: string): string { return EverythingRegistry.get("items", item_id)?.name || name_from_id(item_id); } -// ctx.items for shared scripts, and the part of the client's that's the same -export function shared_items_api(mod: string): SharedContext["items"] { +// ctx.items for shared scripts, and the part of the client's that's the same. fluids are this side's rules, which +// price full fluid containers +export function shared_items_api(mod: string, fluids: FluidRules): SharedContext["items"] { const known = (id: string) => { const item = EverythingRegistry.get("items", id); if (!item) throw new Error(`[${mod}] unknown item ${id}`); @@ -99,7 +101,7 @@ export function shared_items_api(mod: string): SharedContext["items"] { exists: (id) => EverythingRegistry.get("items", id) !== undefined, name: (id) => (known(id), display_name(id)), components: (id) => structuredClone(known(id).components ?? {}), - sell_value: (id) => known(id).base_sell_value, + sell_value: (id, data) => (known(id), sell_value(id, data, fluids)), }; } diff --git a/docs/modding/gui/index.md b/docs/modding/gui/index.md index bcd7822..b9ef192 100644 --- a/docs/modding/gui/index.md +++ b/docs/modding/gui/index.md @@ -136,18 +136,61 @@ interface Graphics { text_input(id: string, x: number, y: number, w: number): string; // what's typed in it so far key_pressed(key: string): boolean; // KeyboardEvent.code, like "KeyE". false while a text input has the keyboard - tooltip(lines: (string | { text: string; color?: Color })[], x: number, y: number): void; // over everything - item_tooltip(id: string, x: number, y: number, extra?: (string | { text: string; color?: Color })[]): void; + tooltip(lines: (string | TooltipLine)[], x: number, y: number): void; // over everything + item_tooltip(id: string, x: number, y: number, extra?: (string | TooltipLine)[]): void; consume_click(): void; // nothing under it gets this click block_mouse(x: number, y: number, w: number, h: number): void; // the screen underneath ignores the mouse there } ``` -Colors are `[r, g, b, a]` from 0 to 1. The last `tooltip` drawn in a frame is the one shown, and `item_tooltip` shows -the game's own tooltip for an item (its name, food, durability and lore) with extra lines under it. +Colors are `[r, g, b, a]` from 0 to 1. The last `tooltip` drawn in a frame is the one shown, and `item_tooltip` shows an +item's tooltip the way a slot does, see [Item Tooltips](#item-tooltips), with extra lines under it. `tooltip` lines are +white unless they say, and `item_tooltip`'s extra lines grey. Either can have an icon. `slider` is in the type but not built yet. +## Item Tooltips + +`ctx.ui.add_item_tooltip` adds lines to every item's tooltip, in slots and in `g.item_tooltip`, like NeoForge's +`ItemTooltipEvent`. The function gets the stack and the lines so far, the game's and those of mods loaded earlier, and +pushes its own: + +```ts +import { type ClientContext, TOOLTIP_PRIORITY } from "cubi/client"; + +export function setup(ctx: ClientContext) { + ctx.ui.add_item_tooltip((item, lines) => { + const value = ctx.items.sell_value(item.id, item.data); + if (value === undefined) return; + lines.push({ icon: "bworld:copper_coin", text: String(value), priority: TOOLTIP_PRIORITY.food + 1 }); + }); +} +``` + +That's the base game's sell value line. Each line is: + +```ts +interface TooltipLine { + text: string; + color?: Color; // white when not set + icon?: string; // a texture drawn before the text, as tall as the line + priority?: number; // where it goes, TOOLTIP_PRIORITY.default when not set +} +``` + +Once every function has run, the lines are sorted by priority, lowest first. Lines with the same priority keep the order +they were added in: the game's first, then mods' in load order. `TOOLTIP_PRIORITY` says where the game's own lines are, +so a mod's can go between them: + +| Priority | Value | What's there | +| --------- | ----- | ----------------------------------------------------------------------------------------------- | +| `name` | 0 | The item's name | +| `fluid` | 100 | The fluid a full container holds and how much | +| `food` | 200 | How much hunger it restores | +| `default` | 500 | Lines that don't set a priority | +| `lore` | 1000 | The item's `lore`, then what the server's [`get_lore`](../items/index.md#item-components) added | +| `extra` | 2000 | `g.item_tooltip`'s extra lines that don't set a priority | + ## Reading Game State Client scripts can read what the client knows, to show it: diff --git a/docs/modding/items/index.md b/docs/modding/items/index.md index 357680f..156538a 100644 --- a/docs/modding/items/index.md +++ b/docs/modding/items/index.md @@ -255,3 +255,7 @@ ctx.items.sell_value(id, data); // the same for a stack with this data, with a f player.give_item("bworld:carrot", 3); // what doesn't fit drops at their feet player.exchange_held({ id: "bworld:bucket", count: 1, data }); // one of the held item becomes this ``` + +Client and shared scripts have `ctx.items.sell_value(id, data)` too, worked out the same way with that side's +[fluid rules](../resources/fluids.md#rules), so a client can show what a full bottle of juice is worth, like the base +game's [tooltip line](../gui/index.md#item-tooltips) does. diff --git a/docs/modding/resources/fluids.md b/docs/modding/resources/fluids.md index 30c52a9..c831ed5 100644 --- a/docs/modding/resources/fluids.md +++ b/docs/modding/resources/fluids.md @@ -90,7 +90,7 @@ and they can only be registered during `setup`. The rules are in the shared script because both sides need them: the server for tooltips, and clients to draw the fluid. Shared scripts have `ctx.items.exists(id)`, `ctx.items.name(id)`, `ctx.items.components(id)` and -`ctx.items.sell_value(id)` for rules that read an item, like syrup named after its tree. +`ctx.items.sell_value(id, data?)` for rules that read an item, like syrup named after its tree. ## Using Fluids @@ -147,7 +147,8 @@ tooltip says what's in it and how much, like "Birch Log Syrup, 250 mB". A full one sells for its own `base_sell_value` and what its fluid is worth on top: the fluid's value for 1000 mB times how much it holds, rounded up to a whole number. A jar of maple syrup worth 40 a bucket sells for 10, and the jar's own value when it has one. It can't be sold if neither has a value. Server scripts get this from -`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by, and tooltips show it. +`ctx.items.sell_value(id, data)`, which the base game's shipping bin sells by. Client and shared scripts have it too, +and the base game shows it in [tooltips](../gui/index.md#item-tooltips). A server script fills and empties one through its [fluid handler](#fluid-handlers), and `ctx.items.fluid_capacity(id)` says how much one holds. diff --git a/mods/bworld/scripts/client.ts b/mods/bworld/scripts/client.ts index b47db26..b6fc696 100644 --- a/mods/bworld/scripts/client.ts +++ b/mods/bworld/scripts/client.ts @@ -17,6 +17,7 @@ import type { Visual, VisualBlock, } from "cubi/client"; +import { TOOLTIP_PRIORITY } from "cubi/client"; import { MINIGAMES } from "./minigames/mod.ts"; import { JUICER_CAPACITY, JUICER_PRESSES } from "./shared.ts"; import { STONECUTTING, stonecutting_options, type StonecuttingRecipe } from "./stonecutting.ts"; @@ -24,6 +25,8 @@ import { STONECUTTING, stonecutting_options, type StonecuttingRecipe } from "./s const WIDTH = 176; // like minecraft's 176x166 container screens const HEIGHT = 166; +// what an item sells for, after its food line in its tooltip +const VALUE_COLOR: Color = [0.95, 0.8, 0.3, 1]; export function setup(ctx: ClientContext) { ctx.ui.register_menu_screen("bworld:storage", chest_screen); @@ -36,6 +39,17 @@ export function setup(ctx: ClientContext) { ctx.ui.register_menu_screen("bworld:fishing_minigame", fishing_minigame_screen); ctx.render.register_visual("bworld:juicer", juicer_visual); + ctx.ui.add_item_tooltip((item, lines) => { + const value = ctx.items.sell_value(item.id, item.data); + if (value === undefined) return; + lines.push({ + icon: "bworld:copper_coin", + text: String(value), + color: VALUE_COLOR, + priority: TOOLTIP_PRIORITY.food + 1, + }); + }); + ctx.ui.register_menu_screen("bworld:cooking_pot", (menu) => ({ width: WIDTH, height: HEIGHT, diff --git a/server/game/mod_runtime.ts b/server/game/mod_runtime.ts index 2438134..c977ced 100644 --- a/server/game/mod_runtime.ts +++ b/server/game/mod_runtime.ts @@ -4,12 +4,7 @@ // runs mods' server scripts: builds each one's ServerContext and keeps what they register. // see docs/modding/. parts not built yet throw when used, saying so import { BlockRegistry, EverythingRegistry, fluid_height, ItemRegistry } from "@/common/everything_registry.ts"; -import { - Container as EngineContainer, - ItemStack as EngineItemStack, - max_stack, - sell_value, -} from "@/common/inventory.ts"; +import { Container as EngineContainer, ItemStack as EngineItemStack, max_stack } from "@/common/inventory.ts"; import { block_states, fluid_block, @@ -297,7 +292,7 @@ export class ModRuntime { side: "server", tags: tag_api(), blocks, - items: shared_items_api(mod), + items: shared_items_api(mod, this.fluid_rules), fluids: { exists: fluid_exists, same: same_fluid, @@ -998,10 +993,7 @@ export class ModRuntime { if (!item) throw new Error(`unknown item ${id}`); return item.tool?.type; }, - sell_value: (id, data) => { - if (!EverythingRegistry.get("items", id)) throw new Error(`unknown item ${id}`); - return sell_value(id, data, this.fluid_rules); - }, + sell_value: shared_items_api(mod, this.fluid_rules).sell_value, components: (id) => { const item = EverythingRegistry.get("items", id); if (!item) throw new Error(`unknown item ${id}`); diff --git a/tests/fluid_container_test.ts b/tests/fluid_container_test.ts index 94dc069..8044a83 100644 --- a/tests/fluid_container_test.ts +++ b/tests/fluid_container_test.ts @@ -80,9 +80,6 @@ Deno.test("a full fluid container sells for its own value and its fluid's, round // a jar of syrup is 2.5, and a flask of it is the flask's 2 and the syrup's 10 assertEquals([value(JAR), value(JAR, FULL)], [undefined, 3]); assertEquals([value(FLASK), value(FLASK, FULL)], [2, 12]); - const jar = new ItemStack(JAR); - jar.data = FULL; - assertEquals(item_tooltip(jar).map((line) => line.text), ["Jar", "Syrup, 250 mB", "3"]); }); Deno.test("exchange_held puts the result in the hand from a stack of one, and in the inventory from more", async () => { diff --git a/tests/helpers.ts b/tests/helpers.ts index 40437d9..caff0d7 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -13,6 +13,7 @@ import { AIR_ID, PROTOCOL_VERSION } from "@/common/protocol.ts"; import { decode_level_chunk } from "@/common/chunk_streaming.ts"; import { CHUNK_AREA, CHUNK_SIZE, ID_MASK } from "@/common/constants.ts"; import type { SavedChunk, SavedChunks, WorldSave } from "@/server/game/saves.ts"; +import { FluidRules } from "@/common/fluids.ts"; import { BlockRules } from "@/common/block_rules.ts"; import { collision_shape_at, covers_face, is_solid } from "@/common/block_shapes.ts"; import { tag_api } from "@/common/tags.ts"; @@ -259,7 +260,7 @@ export function client_of(game: GameServer, area: Area) { components: (id) => structuredClone(info(id)!.components ?? {}), solid: (id) => id !== AIR_ID && is_solid(info(id)!), }, - items: shared_items_api("bworld"), + items: shared_items_api("bworld", new FluidRules((_mod, _what, fn) => fn())), // only its blocks are looked at here fluids: { exists: () => false, same: () => false, register() {} }, recipes: { register() {}, list: () => [], register_type() {}, find: () => undefined, all: () => [] }, diff --git a/tests/juice_test.ts b/tests/juice_test.ts index ea44ad8..0962465 100644 --- a/tests/juice_test.ts +++ b/tests/juice_test.ts @@ -287,7 +287,7 @@ async function drawn_juicer() { // bworld's client script, with only what it needs for its visuals const made = new Map Visual>(); setup_client({ - ui: { register_menu_screen() {} }, + ui: { register_menu_screen() {}, add_item_tooltip() {} }, render: { register_visual: (id: string, create: never) => made.set(id, create) }, } as unknown as ClientContext); const juicer = EverythingRegistry.get_id("blocks", "bworld:juicer")!; diff --git a/tests/tooltip_test.ts b/tests/tooltip_test.ts new file mode 100644 index 0000000..9edc55e --- /dev/null +++ b/tests/tooltip_test.ts @@ -0,0 +1,91 @@ +// SPDX-FileCopyrightText: 2026 Paula Wisnievski +// SPDX-License-Identifier: AGPL-3.0-or-later + +// item tooltips: the game's lines, the ones client scripts add and where they go +import { assertEquals } from "@std/assert"; +import { item_tooltip } from "@/client/gui/tooltip.ts"; +import { load_client_mods } from "@/client/mods.ts"; +import { EverythingRegistry } from "@/common/everything_registry.ts"; +import { ItemStack } from "@/common/inventory.ts"; +import type { ModListing } from "@/common/mod_loader.ts"; +import { read_mod } from "@/common/mod.ts"; +import { load_and_check, load_order } from "@/tools/check_mods.ts"; +import { pack_mod } from "@/tools/pack_mod.ts"; +import { copy_dir } from "./helpers.ts"; + +const SNACK = "tips:snack"; + +// mods/bworld, then tips: a snack with food and lore that sells for 4, and a client script adding lines to its tooltip +async function load(client_script: string) { + const dir = Deno.makeTempDirSync({ prefix: "bworld_tooltips_" }); + copy_dir("mods/bworld", `${dir}/bworld`); + const write = (path: string, text: string) => { + Deno.mkdirSync(`${dir}/tips/${path.split("/").slice(0, -1).join("/")}`, { recursive: true }); + Deno.writeTextFileSync(`${dir}/tips/${path}`, text); + }; + write( + "manifest.json", + JSON.stringify({ + format_version: 1, + id: "tips", + name: "Tips", + version: "1.0.0", + scripts: { client: "scripts/client.ts" }, + }), + ); + write( + "items/snack.json", + JSON.stringify({ + format_version: 1, + item: { + id: SNACK, + texture: "cubi:missing", + food: { nutrition: 3, saturation_modifier: 0.5 }, + lore: "Crunchy.", + base_sell_value: 4, + }, + }), + ); + write("scripts/client.ts", client_script); + const mods = load_order(load_and_check(dir)); + const packed = await Promise.all(mods.map(async (mod) => read_mod(await pack_mod(mod), `${mod.id}.cubi`))); + Deno.removeSync(dir, { recursive: true }); + const listings: ModListing[] = mods.map((mod) => ({ + id: mod.id, + name: mod.id, + version: "1.0.0", + sha256: "", + file: "", + size: 0, + })); + EverythingRegistry.clear(); + await load_client_mods(listings, packed); + return (id: string) => item_tooltip(new ItemStack(id)).map((line) => line.text); +} + +Deno.test("a client script's lines show in item tooltips, and bworld's sell value goes after the food", async () => { + const tooltip = await load(` + import type { ClientContext } from "cubi/client"; + export function setup(ctx: ClientContext) { + ctx.ui.add_item_tooltip((item, lines) => lines.push({ text: "from tips: " + item.id })); + } + `); + assertEquals(tooltip(SNACK), ["Snack", "Restores 3 hunger", "4", "from tips: tips:snack", "Crunchy."]); + assertEquals(tooltip("bworld:stick").at(-1), "from tips: bworld:stick"); +}); + +Deno.test("tooltip lines go where their priority says, and ones with the same priority keep their order", async () => { + const tooltip = await load(` + import { type ClientContext, TOOLTIP_PRIORITY } from "cubi/client"; + export function setup(ctx: ClientContext) { + ctx.ui.add_item_tooltip((item, lines) => { + if (item.id !== "${SNACK}") return; + lines.push({ text: "last", priority: TOOLTIP_PRIORITY.lore + 1 }); + lines.push({ text: "first", priority: TOOLTIP_PRIORITY.food }); + lines.push({ text: "second", priority: TOOLTIP_PRIORITY.food }); + }); + } + `); + // after the game's own food line, then bworld's sell value at food + 1 + assertEquals(tooltip(SNACK), ["Snack", "Restores 3 hunger", "first", "second", "4", "Crunchy.", "last"]); +});