Make durability work like sunlit valley
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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