Make durability work like sunlit valley
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user