From 7785dea7fa0f59be461eef17b9b3a0114e65259e Mon Sep 17 00:00:00 2001 From: Joaquin Pereyra Date: Sat, 5 Jul 2025 13:54:25 -0300 Subject: [PATCH] REFACTOR:make more legible _preCreate --- module/data/item/base.mjs | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/module/data/item/base.mjs b/module/data/item/base.mjs index 885320e1..7bef3e02 100644 --- a/module/data/item/base.mjs +++ b/module/data/item/base.mjs @@ -55,23 +55,28 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { return data; } + /**@inheritdoc */ async _preCreate(data, options, user) { + // Skip if no initial action is required or actions already exist if (!this.constructor.metadata.hasInitialAction || !foundry.utils.isEmpty(this.actions)) return; - const actionType = { - weapon: 'attack' - }[this.constructor.metadata.type], - cls = actionsTypes[actionType] - action = new cls( - { - _id: foundry.utils.randomID(), - type: actionType, - name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name), - ...cls.getSourceConfig(this.parent) - }, - { - parent: this.parent - } - ); + + const metadataType = this.constructor.metadata.type; + const actionType = { weapon: "attack" }[metadataType]; + const ActionClass = actionsTypes[actionType]; + + const action = new ActionClass( + { + _id: foundry.utils.randomID(), + type: actionType, + name: game.i18n.localize(CONFIG.DH.ACTIONS.actionTypes[actionType].name), + ...ActionClass.getSourceConfig(this.parent) + }, + { + parent: this.parent + } + ); + this.updateSource({ actions: [action] }); } + }