Tooltip api
This commit is contained in:
@@ -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 () => {
|
||||
|
||||
+2
-1
@@ -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: () => [] },
|
||||
|
||||
+1
-1
@@ -287,7 +287,7 @@ async function drawn_juicer() {
|
||||
// bworld's client script, with only what it needs for its visuals
|
||||
const made = new Map<string, (block: VisualBlock, instances: Instances) => 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")!;
|
||||
|
||||
@@ -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"]);
|
||||
});
|
||||
Reference in New Issue
Block a user