From 1bd2bbd02d180651471aad4e800f30a1ea0978e6 Mon Sep 17 00:00:00 2001 From: Dapoolp Date: Wed, 25 Jun 2025 10:26:43 +0200 Subject: [PATCH] Auto add Attack action to newly created weapon --- module/applications/config/Action.mjs | 2 +- module/applications/sheets/item.mjs | 8 ++------ module/data/item/weapon.mjs | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/module/applications/config/Action.mjs b/module/applications/config/Action.mjs index 591a7263..d178996a 100644 --- a/module/applications/config/Action.mjs +++ b/module/applications/config/Action.mjs @@ -66,7 +66,7 @@ export default class DHActionConfig extends DaggerheartSheet(ApplicationV2) { context.hasBaseDamage = !!this.action.parent.damage; context.getRealIndex = this.getRealIndex.bind(this); context.disableOption = this.disableOption.bind(this); - context.isNPC = this.action.actor.type !== 'character'; + context.isNPC = this.action.actor && this.action.actor.type !== 'character'; return context; } diff --git a/module/applications/sheets/item.mjs b/module/applications/sheets/item.mjs index 1b8c416b..3b8fcb3f 100644 --- a/module/applications/sheets/item.mjs +++ b/module/applications/sheets/item.mjs @@ -90,13 +90,11 @@ export default function DHItemMixin(Base) { } static async addAction() { - const actionType = await DHItemSheetV2.selectActionType(), - actionIndexes = this.document.system.actions.map(x => x._id.split('-')[2]).sort((a, b) => a - b); + const actionType = await DHItemSheetV2.selectActionType(); try { const cls = actionsTypes[actionType?.type] ?? actionsTypes.attack, action = new cls( { - // id: `${this.document.id}-Action-${actionIndexes.length > 0 ? actionIndexes[0] + 1 : 1}` _id: foundry.utils.randomID(), type: actionType.type, name: game.i18n.localize(SYSTEM.ACTIONS.actionTypes[actionType.type].name), @@ -107,9 +105,7 @@ export default function DHItemMixin(Base) { } ); await this.document.update({ 'system.actions': [...this.document.system.actions, action] }); - await new DHActionConfig(this.document.system.actions[this.document.system.actions.length - 1]).render( - true - ); + await new DHActionConfig(this.document.system.actions[this.document.system.actions.length - 1]).render(true); } catch (error) { console.log(error); } diff --git a/module/data/item/weapon.mjs b/module/data/item/weapon.mjs index 00ab19b3..fefe096a 100644 --- a/module/data/item/weapon.mjs +++ b/module/data/item/weapon.mjs @@ -51,6 +51,24 @@ export default class DHWeapon extends BaseDataItem { }; } + async _preCreate(data, options, user) { + const actionType = 'attack', + cls = actionsTypes.attack, + action = new cls( + { + _id: foundry.utils.randomID(), + type: actionType, + name: game.i18n.localize(SYSTEM.ACTIONS.actionTypes[actionType].name), + ...cls.getSourceConfig(this.parent) + }, + { + parent: this.parent + } + ); + this.updateSource({actions: [action]}); + return super._preCreate(data, options, user); + } + async _preUpdate(changes, options, user) { const allowed = await super._preUpdate(changes, options, user); if (allowed === false) return false;