Make durability work like sunlit valley

This commit is contained in:
2026-09-29 18:00:32 -03:00
parent 8fe581ea93
commit f76cf2bf38
30 changed files with 70 additions and 28 deletions
+1 -1
View File
@@ -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]) {
+2 -1
View File
@@ -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;
}
+18
View File
@@ -28,6 +28,24 @@ export class ItemStack<T = unknown | undefined> {
}
}
// 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<ItemRegistry>("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);
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "axe",
"tier": 1,
"speed": 3.5,
"durability": 190
"durability": 5
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "hoe",
"tier": 1,
"speed": 3.5,
"durability": 190
"durability": 5
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "pickaxe",
"tier": 1,
"speed": 3.5,
"durability": 190
"durability": 5
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "shovel",
"tier": 1,
"speed": 3.5,
"durability": 190
"durability": 5
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "axe",
"tier": 0,
"speed": 6,
"durability": 32
"durability": 9
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "hoe",
"tier": 0,
"speed": 6,
"durability": 32
"durability": 9
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "pickaxe",
"tier": 0,
"speed": 6,
"durability": 32
"durability": 9
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "shovel",
"tier": 0,
"speed": 6,
"durability": 32
"durability": 9
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "axe",
"tier": 2,
"speed": 4,
"durability": 250
"durability": 7
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "hoe",
"tier": 2,
"speed": 4,
"durability": 250
"durability": 7
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "pickaxe",
"tier": 2,
"speed": 4,
"durability": 250
"durability": 7
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "shovel",
"tier": 2,
"speed": 4,
"durability": 250
"durability": 7
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "axe",
"tier": 1,
"speed": 3,
"durability": 131
"durability": 3
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "hoe",
"tier": 1,
"speed": 3,
"durability": 131
"durability": 3
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "pickaxe",
"tier": 1,
"speed": 3,
"durability": 131
"durability": 3
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "shovel",
"tier": 1,
"speed": 3,
"durability": 131
"durability": 3
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "axe",
"tier": 0,
"speed": 2,
"durability": 59
"durability": 2
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "hoe",
"tier": 0,
"speed": 2,
"durability": 59
"durability": 2
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "pickaxe",
"tier": 0,
"speed": 2,
"durability": 59
"durability": 2
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "shovel",
"tier": 0,
"speed": 2,
"durability": 59
"durability": 2
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "knife",
"tier": 1,
"speed": 3.5,
"durability": 190
"durability": 5
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "knife",
"tier": 0,
"speed": 6,
"durability": 32
"durability": 9
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "knife",
"tier": 2,
"speed": 4,
"durability": 250
"durability": 7
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "knife",
"tier": 1,
"speed": 3,
"durability": 131
"durability": 3
}
}
}
+1 -1
View File
@@ -7,7 +7,7 @@
"type": "knife",
"tier": 0,
"speed": 2,
"durability": 59
"durability": 2
}
}
}
+3 -1
View File
@@ -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);
}
}
+21
View File
@@ -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<ItemRegistry>("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) => {