Fixed relinking of Features from items created on Character

This commit is contained in:
WBHarry 2025-07-31 18:03:33 +02:00
parent 0cf9e1d17c
commit 3dc4daffc3
7 changed files with 96 additions and 51 deletions

View file

@ -125,18 +125,36 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
}
if (this.actor && this.actor.type === 'character' && this.features) {
const featureUpdates = {};
for (let f of this.features) {
const feature = f.item ?? f;
const createData = foundry.utils.mergeObject(feature.toObject(), {
system: {
originItemType: this.parent.type,
originId: data._id,
identifier: feature.identifier,
subType: feature.item ? feature.type : undefined
}
}, { inplace: false });
await this.actor.createEmbeddedDocuments('Item', [createData]);
const createData = foundry.utils.mergeObject(
feature.toObject(),
{
system: {
originItemType: this.parent.type,
originId: data._id,
identifier: this.isMulticlass ? 'multiclass' : null,
subType: feature.item ? feature.type : undefined
}
},
{ inplace: false }
);
const [doc] = await this.actor.createEmbeddedDocuments('Item', [createData]);
if (!featureUpdates.features)
featureUpdates.features = this.features.map(x => (x.item ? { ...x, item: x.item.uuid } : x.uuid));
if (f.item) {
const existingFeature = featureUpdates.features.find(x => x.item === f.item.uuid);
existingFeature.item = doc.uuid;
} else {
const replaceIndex = featureUpdates.features.findIndex(x => x === f.uuid);
featureUpdates.features.splice(replaceIndex, 1, doc.uuid);
}
}
await this.updateSource(featureUpdates);
}
}