fix: resolve hud actor from hud.token not render context param
Release Creation / build (release) Successful in 41s

V2 renderTokenHUD passes (hud, html, data) where data is a render
context object — not a TokenDocument.  data.actor was undefined,
and data?.token?.actor was also undefined.  Use hud.token.actor
(or hud.object.actor) instead, which is the real PlaceableObject
with proper actor resolution.

Also fix html.find() → html.querySelector() for V2 HTMLElement.
This commit is contained in:
2026-06-12 19:01:54 +02:00
parent 2570bf707e
commit 1b53bf9152
+17 -27
View File
@@ -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');
})
})
}