Skip to content

Commit 9e54d8e

Browse files
committed
Fix invalid item Documents preventing all physical item updates
1 parent 64b66a7 commit 9e54d8e

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/module/item/physical/helpers.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,28 +264,31 @@ function handleHPChange(item: PhysicalItemPF2e, changed: DeepPartial<PhysicalIte
264264
}
265265
}
266266

267-
// Get a clone of the item, through an actor clone if owned
268-
const actorSource = item.actor?.toObject();
269-
const changedSource = item.clone(fu.deepClone(changed), { keepId: true }).toObject();
270-
const itemIndex = actorSource?.items.findIndex((i) => i._id === item._id);
271-
if (itemIndex === -1) return;
272-
actorSource?.items.splice(itemIndex ?? 0, 1, changedSource);
273-
const actorClone = actorSource ? new ActorProxyPF2e(actorSource) : null;
274-
const itemClone = actorClone?.inventory.get(item.id, { strict: true }) ?? item.clone(changed, { keepId: true });
275-
276-
// Adjust current HP proportionally if max HP changed
277-
const maxHPDifference = itemClone.system.hp.max - item.system.hp.max;
278-
if (maxHPDifference !== 0) {
279-
changed.system = fu.mergeObject(changed.system ?? {}, {
280-
hp: { value: Math.max(item.system.hp.value + maxHPDifference, 0) },
281-
});
282-
}
267+
// Catch invalid items in the actor clone preventing other items from updating
268+
try {
269+
// Get a clone of the item, through an actor clone if owned
270+
const actorSource = item.actor?.toObject();
271+
const changedSource = item.clone(fu.deepClone(changed), { keepId: true }).toObject();
272+
const itemIndex = actorSource?.items.findIndex((i) => i._id === item._id);
273+
if (itemIndex === -1) return;
274+
actorSource?.items.splice(itemIndex ?? 0, 1, changedSource);
275+
const actorClone = actorSource ? new ActorProxyPF2e(actorSource) : null;
276+
const itemClone = actorClone?.inventory.get(item.id, { strict: true }) ?? item.clone(changed, { keepId: true });
277+
278+
// Adjust current HP proportionally if max HP changed
279+
const maxHPDifference = itemClone.system.hp.max - item.system.hp.max;
280+
if (maxHPDifference !== 0) {
281+
changed.system = fu.mergeObject(changed.system ?? {}, {
282+
hp: { value: Math.max(item.system.hp.value + maxHPDifference, 0) },
283+
});
284+
}
283285

284-
// Final overage check
285-
const newValue = changed.system?.hp?.value ?? itemClone.system.hp.value;
286-
if (newValue > itemClone.system.hp.max) {
287-
changed.system = fu.mergeObject(changed.system ?? {}, { hp: { value: itemClone.system.hp.max } });
288-
}
286+
// Final overage check
287+
const newValue = changed.system?.hp?.value ?? itemClone.system.hp.value;
288+
if (newValue > itemClone.system.hp.max) {
289+
changed.system = fu.mergeObject(changed.system ?? {}, { hp: { value: itemClone.system.hp.max } });
290+
}
291+
} catch {}
289292
}
290293

291294
/** Add and adjust properties on an item's bulk data object */

0 commit comments

Comments
 (0)