fix: sanitize stale effectIds before removing weapon augments to prevent system crashes

This commit is contained in:
CPTN Cosmo 2026-04-26 20:11:58 +02:00
parent 204573de13
commit a31f56ac49

View file

@ -197,22 +197,36 @@ export function patchIkonisSheet() {
};
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;
// Find the index of the first matching feature to remove
const idx = weaponFeatures.findIndex(f => String(f.value) === String(targetId));
if (idx !== -1) {
// Sanitize effectIds to prevent system crash if effects are missing
const feature = weaponFeatures[idx];
if (Array.isArray(feature.effectIds)) {
feature.effectIds = feature.effectIds.filter(id => this.document.effects.has(id));
// 1. Pre-emptive cleanup of all stale effectIds on the weapon.
// 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 (needsCleanup) {
console.log("DH-Ikonis | Cleaning up stale effectIds to prevent system crash...");
await this.document.update({ "system.weaponFeatures": cleanedFeatures });
// Refresh our local copy after update
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);
await this.document.update({ "system.weaponFeatures": weaponFeatures });
}
this.render(true);
};