From 67eff5d512e8a94c115fca9ad27462057264136f Mon Sep 17 00:00:00 2001 From: WBHarry Date: Wed, 23 Jul 2025 16:07:53 +0200 Subject: [PATCH] Fixed links on character deletion --- module/data/actor/character.mjs | 31 +++++++++++++++++++++++++++++++ module/data/item/base.mjs | 19 ++++++++++++------- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/module/data/actor/character.mjs b/module/data/actor/character.mjs index 4b281eeb..207b0b5e 100644 --- a/module/data/actor/character.mjs +++ b/module/data/actor/character.mjs @@ -535,5 +535,36 @@ export default class DhCharacter extends BaseDataActor { if (this.companion) { this.companion.updateLevel(1); } + + const featureLinks = [ + ...(this.ancestry?.system?.features?.map(x => ({ linkUuid: this.ancestry.uuid, feature: x })) ?? []), + ...(this.community?.system?.features?.map(x => ({ linkUuid: this.community.uuid, feature: x })) ?? []), + ...(this.class?.value?.system?.features?.map(x => ({ linkUuid: this.class.value.uuid, feature: x })) ?? []), + ...(this.class?.subclass?.system?.features?.map(x => ({ + linkUuid: this.class.subclass.uuid, + feature: x + })) ?? []) + ]; + for (let { linkUuid, feature } of featureLinks) { + await feature.update({ + 'system.itemLinks': Object.keys(CONFIG.DH.ITEM.itemLinkFeatureTypes).reduce((acc, type) => { + acc[type] = (feature.system.itemLinks[type] ?? []).filter(uuid => uuid !== linkUuid); + return acc; + }, {}) + }); + } + + const itemLinks = this.class?.value?.system?.linkedItems?.map(x => ({ + linkUuid: this.class.value.uuid, + item: x + })); + for (let { linkUuid, item } of itemLinks) { + await item.update({ + 'system.itemLinks': Object.keys(CONFIG.DH.ITEM.itemLinkItemTypes).reduce((acc, type) => { + acc[type] = (item.system.itemLinks[type] ?? []).filter(uuid => uuid !== linkUuid); + return acc; + }, {}) + }); + } } } diff --git a/module/data/item/base.mjs b/module/data/item/base.mjs index 6325195c..aebaa9cb 100644 --- a/module/data/item/base.mjs +++ b/module/data/item/base.mjs @@ -137,10 +137,10 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { ...feature, system: { ...feature.system, - itemLinks: Object.keys(feature.system.itemLinks).reduce((acc, uuid) => { - const type = feature.system.itemLinks[uuid]; - acc[uuid === options.origUuid ? this.parent.uuid : uuid] = type; - + itemLinks: Object.keys(feature.system.itemLinks).reduce((acc, type) => { + acc[type] = feature.system.itemLinks[type].map(uuid => + uuid === options.origUuid ? this.parent.uuid : uuid + ); return acc; }, {}), originItemType: this.parent.type, @@ -152,9 +152,10 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { for (let feature of this.features) { feature.update({ - 'system.itemLinks': Object.keys(feature.system.itemLinks).reduce((acc, uuid) => { - const type = feature.system.itemLinks[uuid]; - acc[uuid === options.origUuid ? this.parent.uuid : uuid] = type; + 'system.itemLinks': Object.keys(feature.system.itemLinks).reduce((acc, type) => { + acc[type] = Array.from(feature.system.itemLinks[type]).flatMap(uuid => + uuid === options.origUuid ? [uuid, this.parent.uuid] : [uuid] + ); return acc; }, {}) }); @@ -176,6 +177,8 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { for (let [type, uuidList] of linkEntries) { for (let uuid of uuidList) { const item = await foundry.utils.fromUuid(uuid); + if (!item) return; + const path = CONFIG.DH.ITEM.itemLinkFeatureTypes[type] ? 'system.features' : CONFIG.DH.ITEM.itemLinkItemTypes[type] @@ -193,6 +196,7 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { if (this.features?.length) { for (let feature of this.features) { + if (!feature) continue; await feature.update({ 'system.itemLinks': Object.keys(feature.system.itemLinks).reduce((acc, type) => { acc[type] = feature.system.itemLinks[type].filter(uuid => uuid !== this.parent.uuid); @@ -204,6 +208,7 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { if (this.linkedItems?.length) { for (let item of this.linkedItems) { + if (!item) continue; await item.update({ 'system.itemLinks': Object.keys(item.system.itemLinks).reduce((acc, type) => { acc[type] = item.system.itemLinks[type].filter(uuid => uuid !== this.parent.uuid);