From 2aaab736995eb99588daaf89afee890ffc8d0388 Mon Sep 17 00:00:00 2001 From: Dapoulp <74197441+Dapoulp@users.noreply.github.com> Date: Sun, 10 Aug 2025 01:23:00 +0200 Subject: [PATCH] Fixes (#752) * Fixes * Remove comment --- lang/en.json | 3 +- module/data/action/baseAction.mjs | 20 ++++++----- module/data/action/damageAction.mjs | 3 +- module/data/chat-message/adversaryRoll.mjs | 40 +++++++++++----------- module/data/fields/actionField.mjs | 4 +-- module/dice/d20Roll.mjs | 1 - module/dice/damageRoll.mjs | 1 - module/documents/actor.mjs | 20 ++++++----- module/documents/chatMessage.mjs | 40 +++++++++++++++++++--- templates/ui/chat/parts/roll-part.hbs | 2 +- templates/ui/chat/parts/target-part.hbs | 6 ++-- 11 files changed, 87 insertions(+), 53 deletions(-) diff --git a/lang/en.json b/lang/en.json index fd278e89..7353a1c0 100755 --- a/lang/en.json +++ b/lang/en.json @@ -505,7 +505,8 @@ }, "CLASS": { "Feature": { - "rallyDice": "Bardic Rally Dice" + "rallyDice": "Bardic Rally Dice", + "short": "Rally" } }, "CONFIG": { diff --git a/module/data/action/baseAction.mjs b/module/data/action/baseAction.mjs index 954e5328..978106db 100644 --- a/module/data/action/baseAction.mjs +++ b/module/data/action/baseAction.mjs @@ -146,7 +146,6 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel else if (this.hasSave || this.hasEffect) { const roll = new CONFIG.Dice.daggerheart.DHRoll(''); roll._evaluated = true; - if(config.hasTarget) config.targetSelection = config.targets.length > 0; await CONFIG.Dice.daggerheart.DHRoll.toMessage(roll, config); } } @@ -227,15 +226,18 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel (!successCost && (!c.consumeOnSuccess || config.roll?.success)) || (successCost && c.consumeOnSuccess) ) - .map(c => { + .reduce((a, c) => { const resource = usefulResources[c.key]; - return { - key: c.key, - value: (c.total ?? c.value) * (resource.isReversed ? 1 : -1), - target: resource.target, - keyIsID: resource.keyIsID - }; - }); + if( resource ) { + a.push({ + key: c.key, + value: (c.total ?? c.value) * (resource.isReversed ? 1 : -1), + target: resource.target, + keyIsID: resource.keyIsID + }); + return a; + } + }, []); await (this.actor.system.partner ?? this.actor).modifyResource(resources); if ( diff --git a/module/data/action/damageAction.mjs b/module/data/action/damageAction.mjs index 88eec481..1627f8e8 100644 --- a/module/data/action/damageAction.mjs +++ b/module/data/action/damageAction.mjs @@ -49,8 +49,7 @@ export default class DHDamageAction extends DHBaseAction { ...systemData, roll: formulas, dialog: {}, - data: this.getRollData(), - targetSelection: systemData.targets.length > 0 + data: this.getRollData() }; if (this.hasSave) config.onSave = this.save.damageMod; if (data.system) { diff --git a/module/data/chat-message/adversaryRoll.mjs b/module/data/chat-message/adversaryRoll.mjs index 5e1835c0..14bf9141 100644 --- a/module/data/chat-message/adversaryRoll.mjs +++ b/module/data/chat-message/adversaryRoll.mjs @@ -18,14 +18,12 @@ const targetsField = () => ); export default class DHActorRoll extends foundry.abstract.TypeDataModel { - targetHook = null; static defineSchema() { return { title: new fields.StringField(), roll: new fields.ObjectField(), targets: targetsField(), - targetSelection: new fields.BooleanField({ initial: false }), hasRoll: new fields.BooleanField({ initial: false }), hasDamage: new fields.BooleanField({ initial: false }), hasHealing: new fields.BooleanField({ initial: false }), @@ -63,42 +61,45 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel { } get targetMode() { - return this.targetSelection; + return this.parent.targetSelection; } set targetMode(mode) { - this.targetSelection = mode; + if(!this.parent.isAuthor) return; + this.parent.targetSelection = mode; this.registerTargetHook(); this.updateTargets(); } get hitTargets() { - return this.currentTargets.filter(t => t.hit || !this.hasRoll || !this.targetSelection); + return this.currentTargets.filter(t => t.hit || !this.hasRoll || !this.targetMode); } async updateTargets() { if(!ui.chat.collection.get(this.parent.id)) return; let targets; - if(this.targetSelection) + if(this.targetMode) targets = this.targets; else targets = Array.from(game.user.targets).map(t => game.system.api.fields.ActionFields.TargetField.formatTarget(t)); - this.parent.setFlag(game.system.id, "targets", targets); - await this.parent.updateSource({ - system: { - targetSelection: this.targetSelection + await this.parent.update({ + flags: { + [game.system.id]: { + targets: targets, + targetMode: this.targetMode + } } - }); + }) } registerTargetHook() { if(!this.parent.isAuthor) return; - if(this.targetSelection && this.targetHook !== null) { - Hooks.off("targetToken", this.targetHook); - this.targetHook = null; - } else if (!this.targetSelection && this.targetHook === null) { - this.targetHook = Hooks.on('targetToken', foundry.utils.debounce(this.updateTargets.bind(this), 50)); + if(this.targetMode && this.parent.targetHook !== null) { + Hooks.off("targetToken", this.parent.targetHook); + return this.parent.targetHook = null; + } else if (!this.targetMode && this.parent.targetHook === null) { + return this.parent.targetHook = Hooks.on('targetToken', foundry.utils.debounce(this.updateTargets.bind(this), 50)); } } @@ -106,9 +107,8 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel { if (this.hasTarget) { this.hasHitTarget = this.targets.filter(t => t.hit === true).length > 0; this.currentTargets = this.getTargetList(); - this. registerTargetHook(); - if(this.targetSelection === true && this.hasRoll) { + if(this.targetMode === true && this.hasRoll) { this.targetShort = this.targets.reduce((a,c) => { if(c.hit) a.hit += 1; else a.miss += 1; @@ -123,7 +123,7 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel { } getTargetList() { - const targets = this.targetSelection && this.parent.isAuthor ? this.targets : (this.parent.getFlag(game.system.id, "targets") ?? this.targets), + const targets = this.targetMode && this.parent.isAuthor ? this.targets : (this.parent.getFlag(game.system.id, "targets") ?? this.targets), reactionRolls = this.parent.getFlag(game.system.id, "reactionRolls"); if(reactionRolls) { @@ -137,7 +137,7 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel { } setPendingSaves() { - this.pendingSaves = this.targetSelection + this.pendingSaves = this.targetMode ? this.targets.filter(target => target.hit && target.saved.success === null).length > 0 : this.currentTargets.filter(target => target.saved.success === null).length > 0; } diff --git a/module/data/fields/actionField.mjs b/module/data/fields/actionField.mjs index 9300a02e..e87f6c20 100644 --- a/module/data/fields/actionField.mjs +++ b/module/data/fields/actionField.mjs @@ -227,9 +227,9 @@ export function ActionMixin(Base) { } else { result = await this.item.update({ [path]: updates }, options); } - + return this.inCollection - ? foundry.utils.getProperty(result, basePath).get(this.id) + ? foundry.utils.getProperty(result, basePath)?.get(this.id) : foundry.utils.getProperty(result, basePath); } diff --git a/module/dice/d20Roll.mjs b/module/dice/d20Roll.mjs index d636fd27..91444b8a 100644 --- a/module/dice/d20Roll.mjs +++ b/module/dice/d20Roll.mjs @@ -141,7 +141,6 @@ export default class D20Roll extends DHRoll { data.type = config.roll?.type; data.difficulty = config.roll.difficulty; if (config.targets?.length) { - config.targetSelection = true; config.targets.forEach(target => { const difficulty = config.roll.difficulty ?? target.difficulty ?? target.evasion; target.hit = roll.isCritical || roll.total >= difficulty; diff --git a/module/dice/damageRoll.mjs b/module/dice/damageRoll.mjs index 34973108..e2cf6860 100644 --- a/module/dice/damageRoll.mjs +++ b/module/dice/damageRoll.mjs @@ -15,7 +15,6 @@ export default class DamageRoll extends DHRoll { const parts = config.roll.map(r => this.postEvaluate(r)); config.damage = this.unifyDamageRoll(parts); - // config.targetSelection = config.targets?.length } static postEvaluate(roll, config = {}) { diff --git a/module/documents/actor.mjs b/module/documents/actor.mjs index 910ec4f1..bc8e2b55 100644 --- a/module/documents/actor.mjs +++ b/module/documents/actor.mjs @@ -644,16 +644,20 @@ export default class DhpActor extends Actor { ); break; case 'armor': - updates.armor.resources['system.marks.value'] = Math.max( - Math.min(this.system.armor.system.marks.value + r.value, this.system.armorScore), - 0 - ); + if(this.system.armor?.system?.marks) { + updates.armor.resources['system.marks.value'] = Math.max( + Math.min(this.system.armor.system.marks.value + r.value, this.system.armorScore), + 0 + ); + } break; default: - updates.actor.resources[`system.resources.${r.key}.value`] = Math.max( - Math.min(this.system.resources[r.key].value + r.value, this.system.resources[r.key].max), - 0 - ); + if(this.system.resources?.[r.key]) { + updates.actor.resources[`system.resources.${r.key}.value`] = Math.max( + Math.min(this.system.resources[r.key].value + r.value, this.system.resources[r.key].max), + 0 + ); + } break; } } diff --git a/module/documents/chatMessage.mjs b/module/documents/chatMessage.mjs index 06a9f147..19f7a482 100644 --- a/module/documents/chatMessage.mjs +++ b/module/documents/chatMessage.mjs @@ -1,4 +1,7 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { + targetHook = null; + targetSelection = null; + async renderHTML() { const actor = game.actors.get(this.speaker.actor); const actorData = actor && this.isContentVisible ? actor : { @@ -14,6 +17,32 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { return html; } + /* -------------------------------------------- */ + + /** @inheritDoc */ + prepareData() { + if(this.isAuthor && this.targetSelection === null) + this.targetSelection = this.system.targets?.length > 0; + super.prepareData(); + + } + + /* -------------------------------------------- */ + + /** @inheritDoc */ + _onCreate(data, options, userId) { + super._onCreate(data, options, userId); + if(this.system.registerTargetHook) this.system.registerTargetHook(); + } + + /* -------------------------------------------- */ + + /** @inheritDoc */ + async _preDelete(options, user) { + if(this.targetHook !== null) Hooks.off("targetToken", this.targetHook); + return super._preDelete(options, user); + } + enrichChatMessage(html) { const elements = html.querySelectorAll('[data-perm-id]'); elements.forEach(e => { @@ -62,7 +91,7 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { } getTargetList() { - const targets = this.system.hitTargets; + const targets = this.system.hitTargets ?? []; return targets.map(target => game.canvas.tokens.documentCollection.find(t => t.actor?.uuid === target.actorId)); } @@ -134,7 +163,7 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { } consumeOnSuccess() { - if (!this.system.successConsumed && !this.system.targetSelection) { + if (!this.system.successConsumed && !this.targetSelection) { const action = this.system.action; if (action) action.consume(this.system, true); } @@ -143,12 +172,12 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { hoverTarget(event) { event.stopPropagation(); const token = canvas.tokens.get(event.currentTarget.dataset.token); - if (!token?.controlled) token._onHoverIn(event, { hoverOutOthers: true }); + if (token && !token?.controlled) token._onHoverIn(event, { hoverOutOthers: true }); } unhoverTarget(event) { const token = canvas.tokens.get(event.currentTarget.dataset.token); - if (!token?.controlled) token._onHoverOut(event); + if (token && !token?.controlled) token._onHoverOut(event); } clickTarget(event) { @@ -163,6 +192,7 @@ export default class DhpChatMessage extends foundry.documents.ChatMessage { onTargetSelection(event) { event.stopPropagation(); - this.system.targetMode = Boolean(event.target.dataset.targetHit); + if(!event.target.classList.contains("target-selected")) + this.system.targetMode = Boolean(event.target.dataset.targetHit); } } diff --git a/templates/ui/chat/parts/roll-part.hbs b/templates/ui/chat/parts/roll-part.hbs index 1d7f3d2b..cd9bb3d6 100644 --- a/templates/ui/chat/parts/roll-part.hbs +++ b/templates/ui/chat/parts/roll-part.hbs @@ -57,7 +57,7 @@ {{/if}} {{#if roll.rally.dice}}