Refactor features creations

This commit is contained in:
Joaquin Pereyra 2025-07-29 22:53:50 -03:00
parent a9a6e8b1d9
commit e2b0c206d2

View file

@ -124,43 +124,17 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel {
}
if (this.actor && this.actor.type === 'character' && this.features) {
/* Actions won't get created during createEmbeddedDocuments because they need the parent to be the newly created feature.
If this can be fixed somehow, we can just create all at the same time instead of one by one.
*/
for (let listFeature of this.features) {
const feature = listFeature.item ?? listFeature;
const [doc] = await this.actor.createEmbeddedDocuments('Item', [
{
...feature,
effects: feature.effects.map(x => x.toObject()),
for (let f of this.features) {
const feature = f.item ?? f;
const createData = foundry.utils.mergeObject(feature.toObject(), {
system: {
...feature.system,
originItemType: this.parent.type,
originId: data._id,
identifier: feature.identifier,
subType: feature.item ? feature.type : undefined
}
}
]);
const actions = feature.system.actions.reduce((acc, oldAction) => {
const cls = game.system.api.models.actions.actionsTypes[oldAction.type];
const action = new cls(
{
...oldAction.toObject(),
...cls.getSourceConfig(doc)
},
{
parent: doc
}
);
acc[action.id] = action.toObject();
return acc;
}, {});
await doc.updateSource({ 'system.actions': actions });
}, { inplace: false });
await this.actor.createEmbeddedDocuments('Item', [createData]);
}
}
}