Sell value on the tooltip !

This commit is contained in:
2026-10-04 00:21:27 -03:00
parent 681692bdce
commit 67c84e3504
5 changed files with 58 additions and 18 deletions
+34 -4
View File
@@ -1,8 +1,10 @@
// the box next to the mouse saying what an item is, like minecraft's: its name, then its lore
// the box next to the mouse saying what an item is, like minecraft's: its name, what it's worth, then its lore
import { EverythingRegistry, ItemRegistry } from "$/common/everything_registry.ts";
import type { ItemStack } from "$/common/inventory.ts";
import { display_name } from "$/common/utils.ts";
import { canvas, draw_text, measure_text } from "$/client/renderer/mod.ts";
import { canvas, draw_text, draw_texture_region, measure_text, type Texture } from "$/client/renderer/mod.ts";
import { AssetManager } from "$/client/assets.ts";
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";
@@ -11,15 +13,22 @@ type Color = [number, number, number, number];
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 VALUE_COLOR: Color = [0.95, 0.8, 0.3, 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;
}
// its name, the item's lore from its json, then what the server's scripts said about this stack
@@ -29,6 +38,9 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
if (info?.food) {
lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR });
}
if (info?.base_sell_value !== undefined) {
lines.push({ icon: VALUE_ICON, text: `${info.base_sell_value}`, color: VALUE_COLOR });
}
const static_lore = info?.lore;
for (const lore of [static_lore, item.lore]) {
for (const text of lore?.split("\n") ?? []) {
@@ -41,7 +53,9 @@ export function item_tooltip(item: ItemStack): TooltipLine[] {
// below and right of the mouse, moved back inside the window when it would go past its edge
export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: number) {
const line_height = TEXT_HEIGHT * TEXT_SCALE;
const width = Math.max(...lines.map((line) => measure_text(line.text, TEXT_SCALE))) + PADDING * 2;
const icon_width = (line: TooltipLine) => line.icon ? line_height + ICON_GAP : 0;
const width = Math.max(...lines.map((line) => icon_width(line) + measure_text(line.text, TEXT_SCALE))) +
PADDING * 2;
const height = lines.length * line_height + (lines.length - 1) * LINE_GAP + PADDING * 2;
let x = mouse_x + OFFSET;
@@ -52,7 +66,23 @@ export function draw_tooltip(lines: TooltipLine[], mouse_x: number, mouse_y: num
y = Math.max(0, y);
draw_gui_sprite("tooltip", x, y, width, height);
const atlas = AssetManager.instance.get<Texture>("bworld:textures");
lines.forEach((line, i) => {
draw_text(line.text, x + PADDING, y + PADDING + i * (line_height + LINE_GAP), TEXT_SCALE, line.color);
const line_y = y + PADDING + i * (line_height + LINE_GAP);
if (line.icon) {
const { x: sx, y: sy, width: sw, height: sh } = get_sprite_region(line.icon);
draw_texture_region(
atlas,
sx,
sy,
sw,
sh,
x + PADDING - line_height / 2,
line_y - line_height / 2,
line_height * 2,
line_height * 2,
);
}
draw_text(line.text, x + PADDING + icon_width(line), line_y, TEXT_SCALE, line.color);
});
}
+2
View File
@@ -211,6 +211,8 @@ export interface ItemRegistry<T = unknown | undefined> {
tool?: ToolProperties;
// 1 for tools and 64 for everything else when not set
max_stack?: number;
// what one is worth when sold
base_sell_value?: number;
lore?: string;
food?: FoodProperties;
worn?: WornProperties;
+6
View File
@@ -105,6 +105,7 @@ export interface ItemJson {
tool?: Omit<ToolProperties, "tier" | "speed"> & Partial<ToolProperties>;
places?: string;
max_stack?: number;
base_sell_value?: number;
food?: FoodProperties;
// how it looks worn on a player
worn?: WornProperties;
@@ -341,6 +342,7 @@ export function item_to_json(id: string, item: ItemRegistry): ItemJson {
if (item.tool !== undefined) json.tool = item.tool;
if (item.block_id !== undefined) json.places = item.block_id;
if (item.max_stack !== undefined) json.max_stack = item.max_stack;
if (item.base_sell_value !== undefined) json.base_sell_value = item.base_sell_value;
if (item.food !== undefined) json.food = item.food;
if (item.worn !== undefined) json.worn = item.worn;
if (item.name !== undefined) json.name = item.name;
@@ -355,6 +357,7 @@ export function item_from_json(json: ItemJson): ItemRegistry {
if (json.tool !== undefined) item.tool = { tier: 0, speed: 2, ...json.tool };
if (json.places !== undefined) item.block_id = json.places;
if (json.max_stack !== undefined) item.max_stack = json.max_stack;
if (json.base_sell_value !== undefined) item.base_sell_value = json.base_sell_value;
if (json.food !== undefined) item.food = json.food;
if (json.worn !== undefined) item.worn = json.worn;
if (json.name !== undefined) item.name = json.name;
@@ -711,6 +714,9 @@ export function validate_item(json: unknown): Problems {
if (json.max_stack !== undefined && (!Number.isInteger(json.max_stack) || (json.max_stack as number) < 1)) {
problems.push("max_stack must be a positive whole number");
}
if (json.base_sell_value !== undefined && !is_whole(json.base_sell_value)) {
problems.push("base_sell_value must be a whole number of 0 or more");
}
if (json.tool !== undefined) {
const tool = json.tool;
if (!is_object(tool) || typeof tool.type !== "string") {
+14 -13
View File
@@ -39,19 +39,20 @@ down to 0 is gone.
## Properties
| Field | Default | Meaning |
| --------------- | ------------------- | ----------------------------------------------------------------------------- |
| `id` | required | The item's id. |
| `texture` | required | The texture it's drawn with. |
| `name` | from the id | What players see. `copper_tools:copper_ingot` is "Copper Ingot" when not set. |
| `lore` | none | Tooltip text under the name. `\n` starts a new line. |
| `max_stack` | `64`, `1` for tools | How many fit in one slot. |
| `places` | none | A block it places when used on a block, like seeds. |
| `tool` | none | Makes it a [tool](tools.md). |
| `food` | none | Makes it [edible](food.md). |
| `data_textures` | none | Other textures, picked by the stack's `data`, see below. |
| `worn` | none | How it looks worn on a player, see [below](#worn-models). |
| `components` | none | [Item components](#item-components) and their parameters. |
| Field | Default | Meaning |
| ----------------- | ------------------- | ----------------------------------------------------------------------------- |
| `id` | required | The item's id. |
| `texture` | required | The texture it's drawn with. |
| `name` | from the id | What players see. `copper_tools:copper_ingot` is "Copper Ingot" when not set. |
| `lore` | none | Tooltip text under the name. `\n` starts a new line. |
| `max_stack` | `64`, `1` for tools | How many fit in one slot. |
| `base_sell_value` | none | What one is worth when sold, a whole number of 0 or more. |
| `places` | none | A block it places when used on a block, like seeds. |
| `tool` | none | Makes it a [tool](tools.md). |
| `food` | none | Makes it [edible](food.md). |
| `data_textures` | none | Other textures, picked by the stack's `data`, see below. |
| `worn` | none | How it looks worn on a player, see [below](#worn-models). |
| `components` | none | [Item components](#item-components) and their parameters. |
### Textures From Data
+2 -1
View File
@@ -2,6 +2,7 @@
"format_version": 1,
"item": {
"id": "bworld:stick",
"texture": "bworld:stick"
"texture": "bworld:stick",
"base_sell_value": 1
}
}