Auto add Attack action to newly created weapon

This commit is contained in:
Dapoolp 2025-06-25 10:26:43 +02:00
parent 22075e7490
commit 1bd2bbd02d
3 changed files with 21 additions and 7 deletions

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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;