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) { 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. for (let f of this.features) {
If this can be fixed somehow, we can just create all at the same time instead of one by one. const feature = f.item ?? f;
*/ const createData = foundry.utils.mergeObject(feature.toObject(), {
for (let listFeature of this.features) { system: {
const feature = listFeature.item ?? listFeature; originItemType: this.parent.type,
const [doc] = await this.actor.createEmbeddedDocuments('Item', [ originId: data._id,
{ identifier: feature.identifier,
...feature, subType: feature.item ? feature.type : undefined
effects: feature.effects.map(x => x.toObject()),
system: {
...feature.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 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 });
} }
} }
} }