diff --git a/module/utils.mjs b/module/utils.mjs index cd41ef9..1cf2d00 100644 --- a/module/utils.mjs +++ b/module/utils.mjs @@ -28,8 +28,12 @@ export default class LethalFantasyUtils { /* -------------------------------------------- */ static setHookListeners() { - Hooks.on('renderTokenHUD', async (hud, html, token) => { - if (html.find(".lethal-hp-loss-hud").length) return + Hooks.on('renderTokenHUD', async (hud, html, data) => { + if (html.querySelector(".lethal-hp-loss-hud")) return + // The token/actor is on the HUD application instance, not the third param. + // hud.token / hud.object gives the Token (PlaceableObject), which has .actor. + const hudActor = hud.token?.actor ?? hud.object?.actor + if (!hudActor) return // HP Loss Button (existing) const lossHPButton = await foundry.applications.handlebars.renderTemplate('systems/fvtt-lethal-fantasy/templates/loss-hp-hud.hbs', {}) $(html).find('div.left').append(lossHPButton); @@ -55,18 +59,13 @@ export default class LethalFantasyUtils { $(html).find('.loss-hp-hud-click').click(async (event) => { event.preventDefault(); let hpLoss = event.currentTarget.dataset.hpValue; - if (token) { - let tokenFull = canvas.tokens.placeables.find(t => t.id === token.id); - log(tokenFull, token) - let actor = tokenFull.actor; - await actor.applyDamage(Number(hpLoss)); + await hudActor.applyDamage(Number(hpLoss)); $(html).find('.hp-loss-wrap')[0].classList.remove('hp-loss-hud-active'); $(html).find('.hp-loss-wrap')[0].classList.add('hp-loss-hud-disabled'); $(html).find('.hp-loss-wrap')[1].classList.remove('hp-loss-hud-active'); $(html).find('.hp-loss-wrap')[1].classList.add('hp-loss-hud-disabled'); $(html).find('.hp-loss-wrap')[2].classList.remove('hp-loss-hud-active'); $(html).find('.hp-loss-wrap')[2].classList.add('hp-loss-hud-disabled'); - } }) // HP Gain Button (new) @@ -94,17 +93,13 @@ export default class LethalFantasyUtils { $(html).find('.gain-hp-hud-click').click(async (event) => { event.preventDefault(); let hpGain = event.currentTarget.dataset.hpValue; - if (token) { - let tokenFull = canvas.tokens.placeables.find(t => t.id === token.id); - log(tokenFull, token) - let actor = tokenFull.actor; - await actor.applyDamage(Number(hpGain)); // Positive value to add HP + await hudActor.applyDamage(Number(hpGain)); // Positive value to add HP // Clear bleeding wounds on heal — regardless of heal amount, any // healing is enough to stop bleeding (field dressing / magic / rest). - const wounds = foundry.utils.duplicate(actor.system.hp.wounds || []) + const wounds = foundry.utils.duplicate(hudActor.system.hp.wounds || []) const hadBleeding = wounds.some(w => w.description === "Bleeding") if (hadBleeding) { - await actor.update({ + await hudActor.update({ "system.hp.wounds": wounds.map(w => w.description === "Bleeding" ? { value: 0, duration: 0 } : w ) @@ -116,7 +111,6 @@ export default class LethalFantasyUtils { $(html).find('.hp-gain-wrap')[1].classList.add('hp-gain-hud-disabled'); $(html).find('.hp-gain-wrap')[2].classList.remove('hp-gain-hud-active'); $(html).find('.hp-gain-wrap')[2].classList.add('hp-gain-hud-disabled'); - } }) // Luck/Grit Buttons @@ -135,17 +129,13 @@ export default class LethalFantasyUtils { }) $(html).find('.luck-grit-btn').click(async (event) => { event.preventDefault(); - if (token) { - let tokenFull = canvas.tokens.placeables.find(t => t.id === token.id); - let actor = tokenFull.actor; - const resource = event.currentTarget.dataset.resource; - const amount = Number(event.currentTarget.dataset.amount); - const current = Number(foundry.utils.getProperty(actor.system, `${resource}.current`)) || 0; - const newValue = Math.max(0, current + amount); - await actor.update({ [`system.${resource}.current`]: newValue }); - $(html).find('.luck-grit-wrap')[0].classList.remove('luck-grit-hud-active'); - $(html).find('.luck-grit-wrap')[0].classList.add('luck-grit-hud-disabled'); - } + const resource = event.currentTarget.dataset.resource; + const amount = Number(event.currentTarget.dataset.amount); + const current = Number(foundry.utils.getProperty(hudActor.system, `${resource}.current`)) || 0; + const newValue = Math.max(0, current + amount); + await hudActor.update({ [`system.${resource}.current`]: newValue }); + $(html).find('.luck-grit-wrap')[0].classList.remove('luck-grit-hud-active'); + $(html).find('.luck-grit-wrap')[0].classList.add('luck-grit-hud-disabled'); }) }) }