fix: sanitize stale effectIds before removing weapon augments to prevent system crashes
This commit is contained in:
parent
204573de13
commit
a31f56ac49
1 changed files with 23 additions and 9 deletions
|
|
@ -197,22 +197,36 @@ export function patchIkonisSheet() {
|
||||||
};
|
};
|
||||||
|
|
||||||
Weapon.prototype._onRemoveAugment = async function(event, target) {
|
Weapon.prototype._onRemoveAugment = async function(event, target) {
|
||||||
const weaponFeatures = foundry.utils.deepClone(this.document.system.weaponFeatures || []);
|
let weaponFeatures = foundry.utils.deepClone(this.document.system.weaponFeatures || []);
|
||||||
const targetId = target.dataset.id;
|
const targetId = target.dataset.id;
|
||||||
|
|
||||||
// Find the index of the first matching feature to remove
|
// 1. Pre-emptive cleanup of all stale effectIds on the weapon.
|
||||||
const idx = weaponFeatures.findIndex(f => String(f.value) === String(targetId));
|
// This is necessary because the system crashes if it tries to remove a feature
|
||||||
|
// that points to an effect ID that no longer exists in the weapon's effects collection.
|
||||||
|
let needsCleanup = false;
|
||||||
|
const cleanedFeatures = weaponFeatures.map(f => {
|
||||||
|
if (Array.isArray(f.effectIds)) {
|
||||||
|
const originalCount = f.effectIds.length;
|
||||||
|
f.effectIds = f.effectIds.filter(id => this.document.effects.has(id));
|
||||||
|
if (f.effectIds.length !== originalCount) needsCleanup = true;
|
||||||
|
}
|
||||||
|
return f;
|
||||||
|
});
|
||||||
|
|
||||||
if (idx !== -1) {
|
if (needsCleanup) {
|
||||||
// Sanitize effectIds to prevent system crash if effects are missing
|
console.log("DH-Ikonis | Cleaning up stale effectIds to prevent system crash...");
|
||||||
const feature = weaponFeatures[idx];
|
await this.document.update({ "system.weaponFeatures": cleanedFeatures });
|
||||||
if (Array.isArray(feature.effectIds)) {
|
// Refresh our local copy after update
|
||||||
feature.effectIds = feature.effectIds.filter(id => this.document.effects.has(id));
|
weaponFeatures = foundry.utils.deepClone(this.document.system.weaponFeatures);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. Perform the actual removal
|
||||||
|
const idx = weaponFeatures.findIndex(f => String(f.value) === String(targetId));
|
||||||
|
if (idx !== -1) {
|
||||||
weaponFeatures.splice(idx, 1);
|
weaponFeatures.splice(idx, 1);
|
||||||
await this.document.update({ "system.weaponFeatures": weaponFeatures });
|
await this.document.update({ "system.weaponFeatures": weaponFeatures });
|
||||||
}
|
}
|
||||||
|
|
||||||
this.render(true);
|
this.render(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue