diff --git a/scripts/ikonis-sheet.js b/scripts/ikonis-sheet.js index 519c985..f74602c 100644 --- a/scripts/ikonis-sheet.js +++ b/scripts/ikonis-sheet.js @@ -197,10 +197,22 @@ export function patchIkonisSheet() { }; Weapon.prototype._onRemoveAugment = async function(event, target) { - const weaponFeatures = this.document.system.weaponFeatures || []; + const weaponFeatures = foundry.utils.deepClone(this.document.system.weaponFeatures || []); const targetId = target.dataset.id; - const newFeatures = weaponFeatures.filter(f => f.value !== targetId); - await this.document.update({ "system.weaponFeatures": newFeatures }); + + // 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)); + } + + weaponFeatures.splice(idx, 1); + await this.document.update({ "system.weaponFeatures": weaponFeatures }); + } this.render(true); };