From f76cf2bf38e9422a887390cb7e1acd59759fba24 Mon Sep 17 00:00:00 2001 From: paulaboks Date: Tue, 29 Sep 2026 18:00:32 -0300 Subject: [PATCH] Make durability work like sunlit valley --- client/gui/tooltip.ts | 2 +- common/everything_registry.ts | 3 ++- common/inventory.ts | 18 ++++++++++++++++++ mods/bworld/items/copper_axe.json | 2 +- mods/bworld/items/copper_hoe.json | 2 +- mods/bworld/items/copper_pickaxe.json | 2 +- mods/bworld/items/copper_shovel.json | 2 +- mods/bworld/items/golden_axe.json | 2 +- mods/bworld/items/golden_hoe.json | 2 +- mods/bworld/items/golden_pickaxe.json | 2 +- mods/bworld/items/golden_shovel.json | 2 +- mods/bworld/items/iron_axe.json | 2 +- mods/bworld/items/iron_hoe.json | 2 +- mods/bworld/items/iron_pickaxe.json | 2 +- mods/bworld/items/iron_shovel.json | 2 +- mods/bworld/items/stone_axe.json | 2 +- mods/bworld/items/stone_hoe.json | 2 +- mods/bworld/items/stone_pickaxe.json | 2 +- mods/bworld/items/stone_shovel.json | 2 +- mods/bworld/items/wood_axe.json | 2 +- mods/bworld/items/wood_hoe.json | 2 +- mods/bworld/items/wood_pickaxe.json | 2 +- mods/bworld/items/wood_shovel.json | 2 +- mods/farmersdelight/items/copper_knife.json | 2 +- mods/farmersdelight/items/gold_knife.json | 2 +- mods/farmersdelight/items/iron_knife.json | 2 +- mods/farmersdelight/items/stone_knife.json | 2 +- mods/farmersdelight/items/wood_knife.json | 2 +- server/game/game_server.ts | 4 +++- tests/vitals_test.ts | 21 +++++++++++++++++++++ 30 files changed, 70 insertions(+), 28 deletions(-) diff --git a/client/gui/tooltip.ts b/client/gui/tooltip.ts index 5819b02..8e01f9f 100644 --- a/client/gui/tooltip.ts +++ b/client/gui/tooltip.ts @@ -30,7 +30,7 @@ export function item_tooltip(item: ItemStack): TooltipLine[] { lines.push({ text: `Restores ${info.food.nutrition} hunger`, color: FOOD_COLOR }); } if (info?.tool) { - lines.push({ text: `Durability: ${info.tool.durability}`, color: LORE_COLOR }); + lines.push({ text: `Durability: ${item.durability()} / ${info.tool.durability}`, color: LORE_COLOR }); } const static_lore = info?.lore; for (const lore of [static_lore, item.lore]) { diff --git a/common/everything_registry.ts b/common/everything_registry.ts index adb23bd..96b3b34 100644 --- a/common/everything_registry.ts +++ b/common/everything_registry.ts @@ -157,7 +157,8 @@ export interface ToolProperties { tier: number; // how many times faster than a hand it breaks blocks of its type speed: number; - // how much use it can take. nothing uses it up yet + // how many deaths it lasts: each one takes 1 off every tool the player carried. what's used up is the stack's + // data.damage, see ItemStack.durability durability: number; } diff --git a/common/inventory.ts b/common/inventory.ts index 684a954..8050bec 100644 --- a/common/inventory.ts +++ b/common/inventory.ts @@ -28,6 +28,24 @@ export class ItemStack { } } + // what's left of a tool's durability, its max minus the damage kept in its data. undefined for non tools + durability(): number | undefined { + const tool = EverythingRegistry.get("items", this.type_id)?.tool; + if (!tool) return undefined; + const damage = (this.data as { damage?: number } | undefined)?.damage ?? 0; + return tool.durability - damage; + } + + // takes durability off a tool, true when that used it up + damage(amount: number): boolean { + const left = this.durability(); + if (left === undefined) return false; + const data = (this.data ?? {}) as { damage?: number }; + data.damage = (data.damage ?? 0) + amount; + this.data = data as T; + return left - amount <= 0; + } + clone(): ItemStack { const item = new ItemStack(this.type_id, this.amount, this.max_amount); item.data = structuredClone(this.data); diff --git a/mods/bworld/items/copper_axe.json b/mods/bworld/items/copper_axe.json index f34d2ae..c470966 100644 --- a/mods/bworld/items/copper_axe.json +++ b/mods/bworld/items/copper_axe.json @@ -7,7 +7,7 @@ "type": "axe", "tier": 1, "speed": 3.5, - "durability": 190 + "durability": 5 } } } diff --git a/mods/bworld/items/copper_hoe.json b/mods/bworld/items/copper_hoe.json index 503de34..8440ec1 100644 --- a/mods/bworld/items/copper_hoe.json +++ b/mods/bworld/items/copper_hoe.json @@ -7,7 +7,7 @@ "type": "hoe", "tier": 1, "speed": 3.5, - "durability": 190 + "durability": 5 } } } diff --git a/mods/bworld/items/copper_pickaxe.json b/mods/bworld/items/copper_pickaxe.json index 06af996..8ee3297 100644 --- a/mods/bworld/items/copper_pickaxe.json +++ b/mods/bworld/items/copper_pickaxe.json @@ -7,7 +7,7 @@ "type": "pickaxe", "tier": 1, "speed": 3.5, - "durability": 190 + "durability": 5 } } } diff --git a/mods/bworld/items/copper_shovel.json b/mods/bworld/items/copper_shovel.json index 0c6b1d6..575df14 100644 --- a/mods/bworld/items/copper_shovel.json +++ b/mods/bworld/items/copper_shovel.json @@ -7,7 +7,7 @@ "type": "shovel", "tier": 1, "speed": 3.5, - "durability": 190 + "durability": 5 } } } diff --git a/mods/bworld/items/golden_axe.json b/mods/bworld/items/golden_axe.json index 0591382..25c1d4c 100644 --- a/mods/bworld/items/golden_axe.json +++ b/mods/bworld/items/golden_axe.json @@ -7,7 +7,7 @@ "type": "axe", "tier": 0, "speed": 6, - "durability": 32 + "durability": 9 } } } diff --git a/mods/bworld/items/golden_hoe.json b/mods/bworld/items/golden_hoe.json index 3a3c75e..6cfbaf0 100644 --- a/mods/bworld/items/golden_hoe.json +++ b/mods/bworld/items/golden_hoe.json @@ -7,7 +7,7 @@ "type": "hoe", "tier": 0, "speed": 6, - "durability": 32 + "durability": 9 } } } diff --git a/mods/bworld/items/golden_pickaxe.json b/mods/bworld/items/golden_pickaxe.json index 85f49fa..470ead9 100644 --- a/mods/bworld/items/golden_pickaxe.json +++ b/mods/bworld/items/golden_pickaxe.json @@ -7,7 +7,7 @@ "type": "pickaxe", "tier": 0, "speed": 6, - "durability": 32 + "durability": 9 } } } diff --git a/mods/bworld/items/golden_shovel.json b/mods/bworld/items/golden_shovel.json index 60cd84e..8d4db31 100644 --- a/mods/bworld/items/golden_shovel.json +++ b/mods/bworld/items/golden_shovel.json @@ -7,7 +7,7 @@ "type": "shovel", "tier": 0, "speed": 6, - "durability": 32 + "durability": 9 } } } diff --git a/mods/bworld/items/iron_axe.json b/mods/bworld/items/iron_axe.json index 48cdc91..d2af27f 100644 --- a/mods/bworld/items/iron_axe.json +++ b/mods/bworld/items/iron_axe.json @@ -7,7 +7,7 @@ "type": "axe", "tier": 2, "speed": 4, - "durability": 250 + "durability": 7 } } } diff --git a/mods/bworld/items/iron_hoe.json b/mods/bworld/items/iron_hoe.json index 6094309..8349e22 100644 --- a/mods/bworld/items/iron_hoe.json +++ b/mods/bworld/items/iron_hoe.json @@ -7,7 +7,7 @@ "type": "hoe", "tier": 2, "speed": 4, - "durability": 250 + "durability": 7 } } } diff --git a/mods/bworld/items/iron_pickaxe.json b/mods/bworld/items/iron_pickaxe.json index 1d35767..52c4ac5 100644 --- a/mods/bworld/items/iron_pickaxe.json +++ b/mods/bworld/items/iron_pickaxe.json @@ -7,7 +7,7 @@ "type": "pickaxe", "tier": 2, "speed": 4, - "durability": 250 + "durability": 7 } } } diff --git a/mods/bworld/items/iron_shovel.json b/mods/bworld/items/iron_shovel.json index fdd99f6..2034ee5 100644 --- a/mods/bworld/items/iron_shovel.json +++ b/mods/bworld/items/iron_shovel.json @@ -7,7 +7,7 @@ "type": "shovel", "tier": 2, "speed": 4, - "durability": 250 + "durability": 7 } } } diff --git a/mods/bworld/items/stone_axe.json b/mods/bworld/items/stone_axe.json index b18f6d4..2679e94 100644 --- a/mods/bworld/items/stone_axe.json +++ b/mods/bworld/items/stone_axe.json @@ -7,7 +7,7 @@ "type": "axe", "tier": 1, "speed": 3, - "durability": 131 + "durability": 3 } } } diff --git a/mods/bworld/items/stone_hoe.json b/mods/bworld/items/stone_hoe.json index c516d35..bd9b931 100644 --- a/mods/bworld/items/stone_hoe.json +++ b/mods/bworld/items/stone_hoe.json @@ -7,7 +7,7 @@ "type": "hoe", "tier": 1, "speed": 3, - "durability": 131 + "durability": 3 } } } diff --git a/mods/bworld/items/stone_pickaxe.json b/mods/bworld/items/stone_pickaxe.json index 6a8d973..a2d7494 100644 --- a/mods/bworld/items/stone_pickaxe.json +++ b/mods/bworld/items/stone_pickaxe.json @@ -7,7 +7,7 @@ "type": "pickaxe", "tier": 1, "speed": 3, - "durability": 131 + "durability": 3 } } } diff --git a/mods/bworld/items/stone_shovel.json b/mods/bworld/items/stone_shovel.json index 90251ea..4e74343 100644 --- a/mods/bworld/items/stone_shovel.json +++ b/mods/bworld/items/stone_shovel.json @@ -7,7 +7,7 @@ "type": "shovel", "tier": 1, "speed": 3, - "durability": 131 + "durability": 3 } } } diff --git a/mods/bworld/items/wood_axe.json b/mods/bworld/items/wood_axe.json index d407feb..3f18861 100644 --- a/mods/bworld/items/wood_axe.json +++ b/mods/bworld/items/wood_axe.json @@ -7,7 +7,7 @@ "type": "axe", "tier": 0, "speed": 2, - "durability": 59 + "durability": 2 } } } diff --git a/mods/bworld/items/wood_hoe.json b/mods/bworld/items/wood_hoe.json index 3bb0a6b..b6d90e2 100644 --- a/mods/bworld/items/wood_hoe.json +++ b/mods/bworld/items/wood_hoe.json @@ -7,7 +7,7 @@ "type": "hoe", "tier": 0, "speed": 2, - "durability": 59 + "durability": 2 } } } diff --git a/mods/bworld/items/wood_pickaxe.json b/mods/bworld/items/wood_pickaxe.json index 6122127..d13f596 100644 --- a/mods/bworld/items/wood_pickaxe.json +++ b/mods/bworld/items/wood_pickaxe.json @@ -7,7 +7,7 @@ "type": "pickaxe", "tier": 0, "speed": 2, - "durability": 59 + "durability": 2 } } } diff --git a/mods/bworld/items/wood_shovel.json b/mods/bworld/items/wood_shovel.json index 5b6b62f..1aede3a 100644 --- a/mods/bworld/items/wood_shovel.json +++ b/mods/bworld/items/wood_shovel.json @@ -7,7 +7,7 @@ "type": "shovel", "tier": 0, "speed": 2, - "durability": 59 + "durability": 2 } } } diff --git a/mods/farmersdelight/items/copper_knife.json b/mods/farmersdelight/items/copper_knife.json index 4766077..ba71850 100644 --- a/mods/farmersdelight/items/copper_knife.json +++ b/mods/farmersdelight/items/copper_knife.json @@ -7,7 +7,7 @@ "type": "knife", "tier": 1, "speed": 3.5, - "durability": 190 + "durability": 5 } } } diff --git a/mods/farmersdelight/items/gold_knife.json b/mods/farmersdelight/items/gold_knife.json index 2e12c7c..34336b3 100644 --- a/mods/farmersdelight/items/gold_knife.json +++ b/mods/farmersdelight/items/gold_knife.json @@ -7,7 +7,7 @@ "type": "knife", "tier": 0, "speed": 6, - "durability": 32 + "durability": 9 } } } diff --git a/mods/farmersdelight/items/iron_knife.json b/mods/farmersdelight/items/iron_knife.json index 9d874e7..5ac9705 100644 --- a/mods/farmersdelight/items/iron_knife.json +++ b/mods/farmersdelight/items/iron_knife.json @@ -7,7 +7,7 @@ "type": "knife", "tier": 2, "speed": 4, - "durability": 250 + "durability": 7 } } } diff --git a/mods/farmersdelight/items/stone_knife.json b/mods/farmersdelight/items/stone_knife.json index 0e7ff48..437a810 100644 --- a/mods/farmersdelight/items/stone_knife.json +++ b/mods/farmersdelight/items/stone_knife.json @@ -7,7 +7,7 @@ "type": "knife", "tier": 1, "speed": 3, - "durability": 131 + "durability": 3 } } } diff --git a/mods/farmersdelight/items/wood_knife.json b/mods/farmersdelight/items/wood_knife.json index bc363b8..db5075e 100644 --- a/mods/farmersdelight/items/wood_knife.json +++ b/mods/farmersdelight/items/wood_knife.json @@ -7,7 +7,7 @@ "type": "knife", "tier": 0, "speed": 2, - "durability": 59 + "durability": 2 } } } diff --git a/server/game/game_server.ts b/server/game/game_server.ts index 7c15b5b..66b5eee 100644 --- a/server/game/game_server.ts +++ b/server/game/game_server.ts @@ -1252,7 +1252,8 @@ export class GameServer { } } - // like minecraft: everything they carried falls out where they died, and they start over at spawn + // like minecraft: everything they carried falls out where they died, and they start over at spawn. tools lose 1 + // durability first, and ones that run out break instead of dropping #kill(player: ServerPlayer, cause: string) { const { x, y, z } = player; this.close_screen(player); @@ -1260,6 +1261,7 @@ export class GameServer { const item = player.inventory.get_item(i); if (item) { player.inventory.set_item(i, undefined); + if (item.damage(1)) continue; this.pop_item(Math.floor(x), Math.floor(Math.max(y, 0)), Math.floor(z), item); } } diff --git a/tests/vitals_test.ts b/tests/vitals_test.ts index 5d0567b..bfb14ca 100644 --- a/tests/vitals_test.ts +++ b/tests/vitals_test.ts @@ -2,6 +2,8 @@ import { assert, assertAlmostEquals, assertEquals } from "@std/assert"; import { MAX_HEALTH, Vitals } from "$/server/game/vitals.ts"; import { test_game } from "./helpers.ts"; +import { EverythingRegistry, type ItemRegistry } from "$/common/everything_registry.ts"; +import { ItemStack } from "$/common/inventory.ts"; // deno-lint-ignore no-explicit-any type Msg = any; @@ -147,6 +149,25 @@ Deno.test("dying drops everything, tells everyone, and respawns with full health assertEquals(t.game.item_entities().map((e) => e.item.to_data()), [{ id: "bworld:coal", count: 5 }]); }); +Deno.test("dying takes 1 durability off every tool carried, and ones that run out break", async () => { + const t = await setup(); + const max = EverythingRegistry.get("items", "bworld:iron_pickaxe")!.tool!.durability; + const pickaxe = new ItemStack("bworld:iron_pickaxe"); + pickaxe.damage(max - 2); + t.player.inventory.set_item(0, pickaxe); + t.player.inventory.set_item(1, new ItemStack("bworld:coal", 5)); + + t.game.damage_player(t.player, 25, "fall"); + const dropped = t.game.item_entities().map((e) => e.item); + assertEquals(dropped.map((item) => item.durability()), [1, undefined]); + assertEquals(dropped[1].to_data(), { id: "bworld:coal", count: 5 }); + + for (const e of t.game.item_entities()) e.remove(); + t.player.inventory.set_item(0, dropped[0]); + t.game.damage_player(t.player, 25, "fall"); + assertEquals(t.game.item_entities(), []); +}); + Deno.test("mods can stop or change damage, and hurt, heal and feed players", async () => { const t = await setup(); t.game.mods.before.player_damage.for_mod("test").subscribe((event) => {