Skip to content

Commit

Permalink
Fix durabilityUsed for items with components (#123)
Browse files Browse the repository at this point in the history
* Fix durabilityUsed for items with components

Fixes #121

Update `durabilityUsed` getter to support items with components.

* Modify `durabilityUsed` getter in `index.js` to check the `components` array for the `damage` component.
* Use the `damage` component value for `durabilityUsed` if found.
* Fall back to checking the `Damage` field in `nbt` or `metadata` if the `damage` component is not found.
* Add test cases in `test/basic.test.js` to verify `durabilityUsed` returns the correct value for items with and without the `damage` component.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/PrismarineJS/prismarine-item/issues/121?shareId=XXXX-XXXX-XXXX-XXXX).

* Update `durabilityUsed` getter to check `components` array for `damage` component

* Add a component map to avoid constantly searching the `components` array
* Use the `damage` component value for `durabilityUsed` if found
* Fall back to checking the `Damage` field in `nbt` or `metadata` if `damage` component is not found
* Add test cases to verify `durabilityUsed` returns correct value for items with and without the `damage` component

* Update test case for `durabilityUsed` with damage component

* Use `fromNotch` method to create the item
* Add `components` array with `damage` component to the item
* Verify `durabilityUsed` returns correct value for items with and without `damage` component

* Check components is defined

* Fix test
  • Loading branch information
rom1504 authored Jan 11, 2025
1 parent 5f72f30 commit 70fc068
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function loader (registryOrVersion) {
if (registry.supportFeature('itemsWithComponents')) {
this.components = []
this.removedComponents = []
this.componentMap = new Map() // Pf146
}

// Probably add a new feature to mcdata, e.g itemsCanHaveStackId
Expand Down Expand Up @@ -151,6 +152,12 @@ function loader (registryOrVersion) {
const item = new Item(networkItem.itemId, networkItem.itemCount, null, null, true)
item.components = networkItem.components
item.removedComponents = networkItem.removeComponents
item.componentMap = new Map() // Pf146
if (item.components) {
for (const component of item.components) {
item.componentMap.set(component.type, component)
}
}
return item
} else if (registry.supportFeature('itemSerializationWillOnlyUsePresent')) {
if (networkItem.present === false) return null
Expand Down Expand Up @@ -330,9 +337,17 @@ 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.componentMap && this.componentMap.has('damage')) { // Pf146
ret = this.componentMap.get('damage').data // Pdaf7
}

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)
}

Expand Down
18 changes: 18 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,3 +447,21 @@ 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 = Item.fromNotch({
itemId: 830,
itemCount: 1,
components: [
{
type: 'damage',
data: 15
}
]
})
expect(item.durabilityUsed).toBe(15)
})
})

0 comments on commit 70fc068

Please sign in to comment.