Skip to content

Commit

Permalink
Update durabilityUsed getter to check components array for `damag…
Browse files Browse the repository at this point in the history
…e` 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
  • Loading branch information
rom1504 committed Jan 11, 2025
1 parent f01a6eb commit 88cf0d7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 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,10 @@ 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
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 @@ -331,11 +336,8 @@ function loader (registryOrVersion) {
const where = registry.supportFeature('whereDurabilityIsSerialized')
let ret

if (this.components && this.components.length > 0) {
const damageComponent = this.components.find(component => component.type === 'damage')
if (damageComponent) {
ret = damageComponent.data
}
if (this.componentMap && this.componentMap.has('damage')) { // Pf146
ret = this.componentMap.get('damage').data // Pdaf7
}

if (ret === undefined) {
Expand Down

0 comments on commit 88cf0d7

Please sign in to comment.