From 5df1d908a9b5f4e7fb651df46066751c98028c26 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Tue, 29 Jul 2025 22:20:12 +0200 Subject: [PATCH] Fixed so that features get their actions and effects --- module/data/actor/character.mjs | 18 ++++++----- module/data/item/base.mjs | 55 +++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 25 deletions(-) diff --git a/module/data/actor/character.mjs b/module/data/actor/character.mjs index ec6e5c7c..da4feb5f 100644 --- a/module/data/actor/character.mjs +++ b/module/data/actor/character.mjs @@ -397,14 +397,16 @@ export default class DhCharacter extends BaseDataActor { } else if (item.system.originItemType === CONFIG.DH.ITEM.featureTypes.class.id) { classFeatures.push(item); } else if (item.system.originItemType === CONFIG.DH.ITEM.featureTypes.subclass.id) { - const subclassState = this.class.subclass.system.featureState; - const subType = item.system.subType; - if ( - subType === CONFIG.DH.ITEM.featureSubTypes.foundation || - (subType === CONFIG.DH.ITEM.featureSubTypes.specialization && subclassState >= 2) || - (subType === CONFIG.DH.ITEM.featureSubTypes.mastery && subclassState >= 3) - ) { - subclassFeatures.push(item); + if (this.class.subclass) { + const subclassState = this.class.subclass.system.featureState; + const subType = item.system.subType; + if ( + subType === CONFIG.DH.ITEM.featureSubTypes.foundation || + (subType === CONFIG.DH.ITEM.featureSubTypes.specialization && subclassState >= 2) || + (subType === CONFIG.DH.ITEM.featureSubTypes.mastery && subclassState >= 3) + ) { + subclassFeatures.push(item); + } } } else if (item.system.originItemType === CONFIG.DH.ITEM.featureTypes.companion.id) { companionFeatures.push(item); diff --git a/module/data/item/base.mjs b/module/data/item/base.mjs index 5b95a810..fba5a232 100644 --- a/module/data/item/base.mjs +++ b/module/data/item/base.mjs @@ -122,26 +122,47 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { this.updateSource({ actions: [action] }); } - } - _onCreate(data, _, userId) { - if (userId !== game.user.id) return; + 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 docs = await this.actor.createEmbeddedDocuments('Item', [ + { + ...feature, + 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 + } + } + ]); - if (!this.actor || this.actor.type !== 'character' || !this.features) return; + 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(docs[0]) + }, + { + parent: docs[0] + } + ); - this.actor.createEmbeddedDocuments( - 'Item', - this.features.map(feature => ({ - ...(feature.item ?? feature), - system: { - ...(feature.item?.system ?? feature.system), - originItemType: this.parent.type, - originId: data._id, - identifier: feature.identifier, - subType: feature.item ? feature.type : undefined - } - })) - ); + acc[action.id] = action.toObject(); + + return acc; + }, {}); + + await docs[0].updateSource({ 'system.actions': actions }); + } + } } async _preDelete() {