Added LinkedItems for class. Added autoremoval of features and linkeditems when the related item is removed.

This commit is contained in:
WBHarry 2025-07-22 04:48:21 +02:00
parent 6336f2a60f
commit df8b96e9bc
16 changed files with 202 additions and 118 deletions

View file

@ -24,7 +24,7 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
hasResource: false,
isQuantifiable: false,
isInventoryItem: false,
isItemLinkable: true
isItemLinkable: false
};
}
@ -143,13 +143,27 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
}
async _preDelete() {
if (!this.actor || this.actor.type !== 'character') return;
if (this.actor && this.actor.type === 'character') {
const items = this.actor.items.filter(item => item.system.originId === this.parent.id);
if (items.length > 0)
await this.actor.deleteEmbeddedDocuments(
'Item',
items.map(x => x.id)
);
}
const items = this.actor.items.filter(item => item.system.originId === this.parent.id);
if (items.length > 0)
await this.actor.deleteEmbeddedDocuments(
'Item',
items.map(x => x.id)
);
if (this.metadata.isItemLinkable) {
const linkEntries = Object.entries(this.itemLinks);
for (let [uuid, type] of linkEntries) {
const item = await foundry.utils.fromUuid(uuid);
const path = CONFIG.DH.ITEM.itemLinkFeatureTypes[type] ? 'system.features' : 'system.linkedItems';
await item.update({
[path]: foundry.utils
.getProperty(item, path)
.filter(x => x.uuid !== this.parent.uuid)
.map(x => x.uuid)
});
}
}
}
}