Fixed links on character deletion

This commit is contained in:
WBHarry 2025-07-23 16:07:53 +02:00
parent 6f860c9db6
commit 67eff5d512
2 changed files with 43 additions and 7 deletions

View file

@ -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;
}, {})
});
}
}
}

View file

@ -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);