Skip to content

Commit 70fc068

Browse files
authored
Fix durabilityUsed for items with components (#123)
* 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
1 parent 5f72f30 commit 70fc068

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

index.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function loader (registryOrVersion) {
2222
if (registry.supportFeature('itemsWithComponents')) {
2323
this.components = []
2424
this.removedComponents = []
25+
this.componentMap = new Map() // Pf146
2526
}
2627

2728
// Probably add a new feature to mcdata, e.g itemsCanHaveStackId
@@ -151,6 +152,12 @@ function loader (registryOrVersion) {
151152
const item = new Item(networkItem.itemId, networkItem.itemCount, null, null, true)
152153
item.components = networkItem.components
153154
item.removedComponents = networkItem.removeComponents
155+
item.componentMap = new Map() // Pf146
156+
if (item.components) {
157+
for (const component of item.components) {
158+
item.componentMap.set(component.type, component)
159+
}
160+
}
154161
return item
155162
} else if (registry.supportFeature('itemSerializationWillOnlyUsePresent')) {
156163
if (networkItem.present === false) return null
@@ -330,9 +337,17 @@ function loader (registryOrVersion) {
330337
get durabilityUsed () {
331338
const where = registry.supportFeature('whereDurabilityIsSerialized')
332339
let ret
333-
if (where === 'Damage') ret = this.nbt?.value?.Damage?.value
334-
else if (where === 'metadata') ret = this.metadata
335-
else throw new Error('unknown durability location')
340+
341+
if (this.componentMap && this.componentMap.has('damage')) { // Pf146
342+
ret = this.componentMap.get('damage').data // Pdaf7
343+
}
344+
345+
if (ret === undefined) {
346+
if (where === 'Damage') ret = this.nbt?.value?.Damage?.value
347+
else if (where === 'metadata') ret = this.metadata
348+
else throw new Error('unknown durability location')
349+
}
350+
336351
return ret ?? (this.maxDurability ? 0 : null)
337352
}
338353

test/basic.test.js

+18
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,21 @@ describe('use Item.equal', () => {
447447
expect(Item.equal(itemOne, itemTwo)).toStrictEqual(false)
448448
})
449449
})
450+
451+
describe('durabilityUsed with damage component', () => {
452+
const Item = require('prismarine-item')('1.20.5')
453+
454+
it('should return correct durabilityUsed for item with damage component', () => {
455+
const item = Item.fromNotch({
456+
itemId: 830,
457+
itemCount: 1,
458+
components: [
459+
{
460+
type: 'damage',
461+
data: 15
462+
}
463+
]
464+
})
465+
expect(item.durabilityUsed).toBe(15)
466+
})
467+
})

0 commit comments

Comments
 (0)