From f749f6be79749ff53056e5103415c1f69e39cac8 Mon Sep 17 00:00:00 2001 From: Paula Date: Fri, 2 Oct 2026 13:16:50 -0300 Subject: [PATCH] Effect icons and tooltip ! --- client/client.ts | 2 +- client/entity/local_player.ts | 4 ++ client/gui/gui_player_inventory.ts | 59 +++++++++++++++++++++-- client/packet_listener.ts | 3 ++ common/mod_check.ts | 1 + common/mod_data.ts | 3 ++ common/protocol.ts | 3 ++ docs/modding/entities/effects.md | 16 +++--- mods/bworld/effects/poison.json | 7 ++- mods/bworld/effects/regeneration.json | 7 ++- mods/bworld/effects/resistance.json | 3 +- mods/bworld/effects/slowness.json | 9 +++- mods/bworld/effects/speed.json | 9 +++- mods/bworld/effects/water_breathing.json | 3 +- mods/bworld/textures/poison.png | Bin 0 -> 118 bytes mods/bworld/textures/regeneration.png | Bin 0 -> 118 bytes mods/bworld/textures/resistance.png | Bin 0 -> 118 bytes mods/bworld/textures/slow.png | Bin 0 -> 232 bytes mods/bworld/textures/speed.png | Bin 0 -> 210 bytes mods/bworld/textures/water_breathing.png | Bin 0 -> 116 bytes server/game/game_server.ts | 8 +++ tests/vitals_test.ts | 13 +++++ 22 files changed, 131 insertions(+), 19 deletions(-) create mode 100644 mods/bworld/textures/poison.png create mode 100644 mods/bworld/textures/regeneration.png create mode 100644 mods/bworld/textures/resistance.png create mode 100644 mods/bworld/textures/slow.png create mode 100644 mods/bworld/textures/speed.png create mode 100644 mods/bworld/textures/water_breathing.png diff --git a/client/client.ts b/client/client.ts index d1e3169..0e238b7 100644 --- a/client/client.ts +++ b/client/client.ts @@ -215,7 +215,7 @@ export class Client { if (InputManager.is_key_pressed(options.key_inventory)) { if (!this.screen) { - this.push_screen(new GuiPlayerInventory(player.inventories, (m) => this.connection.send(m))); + this.push_screen(new GuiPlayerInventory(player, (m) => this.connection.send(m))); } else if (this.screen instanceof GuiMenuScreen) { this.pop_screen(); } diff --git a/client/entity/local_player.ts b/client/entity/local_player.ts index a22ccf1..64a54bf 100644 --- a/client/entity/local_player.ts +++ b/client/entity/local_player.ts @@ -85,6 +85,9 @@ export class LocalPlayer extends Player { food = 20; saturation = 5; air = 20; + // the effects it has that show an icon, see the inventory screen. counted down here, the server only says when + // they change + effects: { id: string; amplifier: number; duration: number }[] = []; // the attack speed attribute the server sent, and when they last attacked or switched slots, for the indicator attack_speed = 4; last_attack = -Infinity; @@ -119,6 +122,7 @@ export class LocalPlayer extends Player { } tick() { + for (const effect of this.effects) if (effect.duration > 0) effect.duration -= 1; this.#in_water = this.in_water; const was_swimming = this.swimming; const input = this.client.screen ? NO_INPUT : this.#read_input(); diff --git a/client/gui/gui_player_inventory.ts b/client/gui/gui_player_inventory.ts index f6f4078..bde4a08 100644 --- a/client/gui/gui_player_inventory.ts +++ b/client/gui/gui_player_inventory.ts @@ -1,5 +1,9 @@ import { GuiMenuScreen } from "./gui_screen.ts"; -import { ClientInventories } from "../inventory.ts"; +import type { LocalPlayer } from "../entity/local_player.ts"; +import { EverythingRegistry } from "$/common/everything_registry.ts"; +import type { EffectJson } from "$/common/mod_data.ts"; +import { TICKS_PER_SECOND } from "$/common/constants.ts"; +import { display_name } from "$/common/utils.ts"; import { ClientMessage, PLAYER_MENU_EXTRA } from "$/common/protocol.ts"; import { player_menu } from "../menu.ts"; import type { ModGraphics } from "./mod_graphics.ts"; @@ -9,16 +13,25 @@ const HEIGHT = 166; const SLOT = 18; // around the slots, like minecraft's const MARGIN = 7; +// effect icons in a row on a panel of their own, just above the inventory's +const ICON = 16; +const ICON_GAP = 2; +const ICON_MARGIN = 4; +const TIME_COLOR: [number, number, number, number] = [0.66, 0.66, 0.66, 1]; -// menu 0's screen: the crafting grid over the inventory, and any slots mods added. the recipes live on the server, -// which fills in the result +// menu 0's screen: the crafting grid over the inventory, and any slots mods added, with the player's effects above. +// the recipes live on the server, which fills in the result export class GuiPlayerInventory extends GuiMenuScreen { - constructor(inventories: ClientInventories, send: (message: ClientMessage) => void) { - super(inventories, send, player_menu(inventories), WIDTH, HEIGHT); + #player: LocalPlayer; + + constructor(player: LocalPlayer, send: (message: ClientMessage) => void) { + super(player.inventories, send, player_menu(player.inventories), WIDTH, HEIGHT); + this.#player = player; } render_background(g: ModGraphics): void { g.panel(0, 0, WIDTH, HEIGHT); + this.#render_effects(g); // the slots mods added outside the panel get one of their own, around them with the same margin const outside = this.menu.slots.slice(PLAYER_MENU_EXTRA) .filter((s) => s.x + SLOT < 0 || s.x > WIDTH || s.y + SLOT < 0 || s.y > HEIGHT); @@ -31,8 +44,44 @@ export class GuiPlayerInventory extends GuiMenuScreen { } } + // the ones with an icon, left to right in the order they were added. hovering one says what it is + #render_effects(g: ModGraphics) { + const shown = this.#player.effects.flatMap((effect) => { + const icon = EverythingRegistry.get("effects", effect.id)?.icon; + return icon ? [{ ...effect, icon }] : []; + }); + if (shown.length === 0) return; + const width = shown.length * (ICON + ICON_GAP) - ICON_GAP + ICON_MARGIN * 2; + const height = ICON + ICON_MARGIN * 2; + const top = -height - ICON_GAP; + g.panel(0, top, width, height); + const mouse = g.mouse; + shown.forEach((effect, i) => { + const x = ICON_MARGIN + i * (ICON + ICON_GAP); + const y = top + ICON_MARGIN; + g.texture(effect.icon, x, y, ICON, ICON); + if (mouse.x >= x && mouse.x < x + ICON && mouse.y >= y && mouse.y < y + ICON) { + const name = display_name(effect.id) + level(effect.amplifier); + g.tooltip([name, { text: time_left(effect.duration), color: TIME_COLOR }], mouse.x, mouse.y); + } + }); + } + render_labels(g: ModGraphics): void { g.text(this.menu.title, 26, 6); g.text("Inventory", 8, 73); } } + +// like minecraft: nothing for level 1, then II, III... +function level(amplifier: number) { + const numerals = ["", " II", " III", " IV", " V", " VI", " VII", " VIII", " IX", " X"]; + return numerals[amplifier] ?? ` ${amplifier + 1}`; +} + +// minutes and seconds, like 1:05 +function time_left(ticks: number) { + if (ticks < 0) return "Forever"; + const seconds = Math.ceil(ticks / TICKS_PER_SECOND); + return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`; +} diff --git a/client/packet_listener.ts b/client/packet_listener.ts index b91c404..aa6d11f 100644 --- a/client/packet_listener.ts +++ b/client/packet_listener.ts @@ -84,6 +84,9 @@ export class ClientPacketListener { case "attack_speed": client.player.attack_speed = message.value; break; + case "effects": + client.player.effects = message.effects; + break; case "player_leave": level.remove_entity(message.id); break; diff --git a/common/mod_check.ts b/common/mod_check.ts index c8ff573..116b82c 100644 --- a/common/mod_check.ts +++ b/common/mod_check.ts @@ -241,6 +241,7 @@ export function check_references(mods: LoadedMod[], engine_textures: Iterable 1) { errors.push(`${file}: effect ${json.id} is also defined by ${effect_owners.get(json.id)!.join(", ")}`); } + if (json.icon !== undefined) uses(file, json.icon, "texture"); } for (const { file, json } of mod.spawns) { check_namespace(file, json.entity); diff --git a/common/mod_data.ts b/common/mod_data.ts index fd8ce44..8c6f2a2 100644 --- a/common/mod_data.ts +++ b/common/mod_data.ts @@ -140,6 +140,8 @@ export interface EffectJson { category?: EffectCategory; // happens once when added instead of lasting, like instant health. its components' on_tick runs once instant?: boolean; + // a texture, shown at the top of the inventory screen while the player has it + icon?: string; // attribute modifiers while it's on, for level 1. each level adds that much again, like minecraft's speed modifiers?: { attribute: string; amount: number; operation: EffectModifierOperation }[]; components?: Record; @@ -809,6 +811,7 @@ export function validate_effect(json: unknown): Problems { problems.push('category must be "beneficial", "harmful" or "neutral"'); } if (json.instant !== undefined && typeof json.instant !== "boolean") problems.push("instant must be true or false"); + if (json.icon !== undefined) problems.push(...validate_id(json.icon, "icon")); if (json.modifiers !== undefined) { if (!Array.isArray(json.modifiers)) { problems.push("modifiers must be a list"); diff --git a/common/protocol.ts b/common/protocol.ts index 8a94a0b..0e23660 100644 --- a/common/protocol.ts +++ b/common/protocol.ts @@ -241,6 +241,9 @@ export type ServerMessage = | { type: "vitals"; health: number; max_health: number; food: number; saturation: number; air: number } // the player's own attack speed attribute, when it changes, for the attack indicator under the crosshair | { type: "attack_speed"; value: number } + // the player's own effects that show an icon, when they change. duration is in ticks, -1 for forever, and the + // client counts it down itself, so it isn't sent every tick + | { type: "effects"; effects: { id: string; amplifier: number; duration: number }[] } // a mod's server script talking to its client script, see ctx.net | { type: "mod"; channel: string; data: unknown }; diff --git a/docs/modding/entities/effects.md b/docs/modding/entities/effects.md index fb5b27d..f09cbd4 100644 --- a/docs/modding/entities/effects.md +++ b/docs/modding/entities/effects.md @@ -17,18 +17,20 @@ game's effects are defined this way, in `mods/bworld/effects`. "effect": { "id": "copper_tools:swiftness", "category": "beneficial", + "icon": "copper_tools:swiftness", "modifiers": [{ "attribute": "bworld:movement_speed", "amount": 0.1, "operation": "add_multiplied_total" }] } } ``` -| Field | Required | What it does | -| ------------ | -------- | ---------------------------------------------------------------------------------------------- | -| `id` | yes | The effect's id, in the mod's [namespace](../concepts/registries.md#identifiers). | -| `category` | no | `"beneficial"`, `"harmful"` or `"neutral"` (the default), like NeoForge's `MobEffectCategory`. | -| `instant` | no | `true` makes it happen once when added instead of lasting, like `bworld:instant_health`. | -| `modifiers` | no | [Attribute modifiers](players.md#attributes) while it's on. Each level adds `amount` again. | -| `components` | no | What it does over time, see below. | +| Field | Required | What it does | +| ------------ | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | +| `id` | yes | The effect's id, in the mod's [namespace](../concepts/registries.md#identifiers). | +| `category` | no | `"beneficial"`, `"harmful"` or `"neutral"` (the default), like NeoForge's `MobEffectCategory`. | +| `instant` | no | `true` makes it happen once when added instead of lasting, like `bworld:instant_health`. | +| `icon` | no | A texture, shown at the top of the inventory screen while the player has it, with its name, level and time left on hover. 16×16 like an item's. | +| `modifiers` | no | [Attribute modifiers](players.md#attributes) while it's on. Each level adds `amount` again. | +| `components` | no | What it does over time, see below. | The modifiers' `operation` is `"add_value"`, `"add_multiplied_base"` or `"add_multiplied_total"`. They come off when the effect ends. diff --git a/mods/bworld/effects/poison.json b/mods/bworld/effects/poison.json index 0bc0853..90b399b 100644 --- a/mods/bworld/effects/poison.json +++ b/mods/bworld/effects/poison.json @@ -3,6 +3,11 @@ "effect": { "id": "bworld:poison", "category": "harmful", - "components": { "bworld:poison": { "every": 25 } } + "icon": "bworld:poison", + "components": { + "bworld:poison": { + "every": 25 + } + } } } diff --git a/mods/bworld/effects/regeneration.json b/mods/bworld/effects/regeneration.json index 66d849d..c63edd5 100644 --- a/mods/bworld/effects/regeneration.json +++ b/mods/bworld/effects/regeneration.json @@ -3,6 +3,11 @@ "effect": { "id": "bworld:regeneration", "category": "beneficial", - "components": { "bworld:heal_over_time": { "every": 50 } } + "icon": "bworld:regeneration", + "components": { + "bworld:heal_over_time": { + "every": 50 + } + } } } diff --git a/mods/bworld/effects/resistance.json b/mods/bworld/effects/resistance.json index 13784f9..acb04a6 100644 --- a/mods/bworld/effects/resistance.json +++ b/mods/bworld/effects/resistance.json @@ -2,6 +2,7 @@ "format_version": 1, "effect": { "id": "bworld:resistance", - "category": "beneficial" + "category": "beneficial", + "icon": "bworld:resistance" } } diff --git a/mods/bworld/effects/slowness.json b/mods/bworld/effects/slowness.json index 7ef35f8..1953f14 100644 --- a/mods/bworld/effects/slowness.json +++ b/mods/bworld/effects/slowness.json @@ -3,6 +3,13 @@ "effect": { "id": "bworld:slowness", "category": "harmful", - "modifiers": [{ "attribute": "bworld:movement_speed", "amount": -0.15, "operation": "add_multiplied_total" }] + "icon": "bworld:slow", + "modifiers": [ + { + "attribute": "bworld:movement_speed", + "amount": -0.15, + "operation": "add_multiplied_total" + } + ] } } diff --git a/mods/bworld/effects/speed.json b/mods/bworld/effects/speed.json index ac66814..a929fce 100644 --- a/mods/bworld/effects/speed.json +++ b/mods/bworld/effects/speed.json @@ -3,6 +3,13 @@ "effect": { "id": "bworld:speed", "category": "beneficial", - "modifiers": [{ "attribute": "bworld:movement_speed", "amount": 0.2, "operation": "add_multiplied_total" }] + "icon": "bworld:speed", + "modifiers": [ + { + "attribute": "bworld:movement_speed", + "amount": 0.2, + "operation": "add_multiplied_total" + } + ] } } diff --git a/mods/bworld/effects/water_breathing.json b/mods/bworld/effects/water_breathing.json index 0a5d952..0208f33 100644 --- a/mods/bworld/effects/water_breathing.json +++ b/mods/bworld/effects/water_breathing.json @@ -2,6 +2,7 @@ "format_version": 1, "effect": { "id": "bworld:water_breathing", - "category": "beneficial" + "category": "beneficial", + "icon": "bworld:water_breathing" } } diff --git a/mods/bworld/textures/poison.png b/mods/bworld/textures/poison.png new file mode 100644 index 0000000000000000000000000000000000000000..bad408b198e5887949aa4fa8d0a10ab6cdf8ac7b GIT binary patch literal 118 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#S9F5M?jcysy3fAP*B6u z#WBR9H#uQ}fPs;Lfq~Dz^&jTToZZ>OFELZ2^xz7{#cVxN4yp_cVje6q&n25?1NAU? My85}Sb4q9e0O?^J%K!iX literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/regeneration.png b/mods/bworld/textures/regeneration.png new file mode 100644 index 0000000000000000000000000000000000000000..c90a217d2f15500e151a13e16c70be5774ae667a GIT binary patch literal 118 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#S9F5M?jcysy3fAP*B6u z#WBR9H#uQ}fPs;LfkDl?xR3K?&hA+%*yA&0SHhKsKxQ}I1wsrA`YTvu-2U{30QE3< My85}Sb4q9e00|8r0RR91 literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/resistance.png b/mods/bworld/textures/resistance.png new file mode 100644 index 0000000000000000000000000000000000000000..0f10b9def1dbbe9dce32b9f5b56ff40e5d459545 GIT binary patch literal 118 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#S9F5M?jcysy3fAP*B6u z#WBR9H#uQ}fPs;Lfq{w1pa1@S&)V4B+|;~c4YCfbU|h@=z{$X%uD~Mm;P*A476t}S LS3j3^P6 literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/slow.png b/mods/bworld/textures/slow.png new file mode 100644 index 0000000000000000000000000000000000000000..23461832f170bcb59dbdcbc72efbafd2f2e3215c GIT binary patch literal 232 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#S9F5M?jcysy3fAP;i!~ zi(`mIZ*sx{0fR-{DVm-CjxQ^?wfDM2mZXG)M8pMCf71zvQ*YMiv>6DNsnj`aWYq9G z^t7IhP3dq$V`C#@v_wjLMayMYW_dP=xC3kp?lraZ7gcPKu_$=3zqy(D8pEL^4xR&- zS%eK|ZIoo|HeANc&iBW@-~q$A7s@OV1v?qUN-HLrF*7rpC?1U2prt9I$M{)!<;?CB a28K2cGv0$w(zXCy$>8bg=d#Wzp$Py1B}#w* literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/speed.png b/mods/bworld/textures/speed.png new file mode 100644 index 0000000000000000000000000000000000000000..1abbd014e0b787bdf77b12fd5a60bd58e3d449ae GIT binary patch literal 210 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#S9F5M?jcysy3fAP_V_* z#WBR9H#uQ}fWf0lCxV3j_WRb{+IwB2Dxx5BUY0`p={NsRv>6DNsnj`aWQ^F$P^xsz zK;aaF!~rqBo8oc{Kd?tiC>*z)mvJPIp>fA?E&&7 zizYTQ3g*7hD=8>QXpwHvf2gpudWX%KvKfjZj0``Tb+-uZb>RiNhr!d;&t;ucLK6Vt CX+=x` literal 0 HcmV?d00001 diff --git a/mods/bworld/textures/water_breathing.png b/mods/bworld/textures/water_breathing.png new file mode 100644 index 0000000000000000000000000000000000000000..85ae241c0e730f7c5ed6e9ad64a4c41b6775ec70 GIT binary patch literal 116 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`oCO|{#S9F5M?jcysy3fAP*Ba& z#WBR9H#uQ}fPs+#5d8W7f4 ({ type: "attack_speed", value: attack_speed })); + const effects = [...player.effects.values()] + .filter((effect) => effect.show_icon) + .map(({ type, amplifier, duration }) => ({ id: type.id, amplifier, duration })); + // by when they end, which stays the same while they count down + const ends = effects.map(( + { id, amplifier, duration }, + ) => [id, amplifier, duration < 0 ? -1 : this.#tick + duration]); + sync("effects", ends, () => ({ type: "effects", effects })); const menu = player.menu; if (menu) { diff --git a/tests/vitals_test.ts b/tests/vitals_test.ts index 4a4bf11..512d966 100644 --- a/tests/vitals_test.ts +++ b/tests/vitals_test.ts @@ -196,3 +196,16 @@ Deno.test("under water they run out of air, shown in bubbles, then drown", async for (let i = 0; i < 300; i++) t.game.tick(); assert(t.player.health < MAX_HEALTH); }); + +Deno.test("the effects that show an icon are sent to the player when they change", async () => { + const t = await setup(); + const api = t.game.mods.player(t.player); + api.add_effect("bworld:speed", 100, { amplifier: 1 }); + api.add_effect("bworld:poison", 100, { show_icon: false }); + t.take(1); + t.game.tick(); + const sent = t.take(1).filter((m: Msg) => m.type === "effects"); + assertEquals(sent.at(-1).effects, [{ id: "bworld:speed", amplifier: 1, duration: 99 }]); + t.game.tick(); + assert(!t.take(1).some((m: Msg) => m.type === "effects"), "not again while they only count down"); +});