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
+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);