diff --git a/index.js b/index.js index f224181..f1c72f8 100644 --- a/index.js +++ b/index.js @@ -330,9 +330,20 @@ function loader (registryOrVersion) { get durabilityUsed () { const where = registry.supportFeature('whereDurabilityIsSerialized') let ret - if (where === 'Damage') ret = this.nbt?.value?.Damage?.value - else if (where === 'metadata') ret = this.metadata - else throw new Error('unknown durability location') + + if (this.components && this.components.length > 0) { + const damageComponent = this.components.find(component => component.type === 'damage') + if (damageComponent) { + ret = damageComponent.data + } + } + + if (ret === undefined) { + if (where === 'Damage') ret = this.nbt?.value?.Damage?.value + else if (where === 'metadata') ret = this.metadata + else throw new Error('unknown durability location') + } + return ret ?? (this.maxDurability ? 0 : null) } diff --git a/test/basic.test.js b/test/basic.test.js index e703588..9ba236d 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -447,3 +447,41 @@ describe('use Item.equal', () => { expect(Item.equal(itemOne, itemTwo)).toStrictEqual(false) }) }) + +describe('durabilityUsed with damage component', () => { + const Item = require('prismarine-item')('1.20.5') + + it('should return correct durabilityUsed for item with damage component', () => { + const item = new Item(830, 1, 0, { + type: 'compound', + name: '', + value: { + Damage: { + type: 'int', + value: 0 + } + } + }) + item.components = [ + { + type: 'damage', + data: 15 + } + ] + expect(item.durabilityUsed).toBe(15) + }) + + it('should return correct durabilityUsed for item without damage component', () => { + const item = new Item(830, 1, 0, { + type: 'compound', + name: '', + value: { + Damage: { + type: 'int', + value: 10 + } + } + }) + expect(item.durabilityUsed).toBe(10) + }) +})