fix: resolve hud actor from hud.token not render context param
Release Creation / build (release) Successful in 41s
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:
+17
-27
@@ -28,8 +28,12 @@ export default class LethalFantasyUtils {
|
|||||||
/* -------------------------------------------- */
|
/* -------------------------------------------- */
|
||||||
static setHookListeners() {
|
static setHookListeners() {
|
||||||
|
|
||||||
Hooks.on('renderTokenHUD', async (hud, html, token) => {
|
Hooks.on('renderTokenHUD', async (hud, html, data) => {
|
||||||
if (html.find(".lethal-hp-loss-hud").length) return
|
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)
|
// HP Loss Button (existing)
|
||||||
const lossHPButton = await foundry.applications.handlebars.renderTemplate('systems/fvtt-lethal-fantasy/templates/loss-hp-hud.hbs', {})
|
const lossHPButton = await foundry.applications.handlebars.renderTemplate('systems/fvtt-lethal-fantasy/templates/loss-hp-hud.hbs', {})
|
||||||
$(html).find('div.left').append(lossHPButton);
|
$(html).find('div.left').append(lossHPButton);
|
||||||
@@ -55,18 +59,13 @@ export default class LethalFantasyUtils {
|
|||||||
$(html).find('.loss-hp-hud-click').click(async (event) => {
|
$(html).find('.loss-hp-hud-click').click(async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let hpLoss = event.currentTarget.dataset.hpValue;
|
let hpLoss = event.currentTarget.dataset.hpValue;
|
||||||
if (token) {
|
await hudActor.applyDamage(Number(hpLoss));
|
||||||
let tokenFull = canvas.tokens.placeables.find(t => t.id === token.id);
|
|
||||||
log(tokenFull, token)
|
|
||||||
let actor = tokenFull.actor;
|
|
||||||
await actor.applyDamage(Number(hpLoss));
|
|
||||||
$(html).find('.hp-loss-wrap')[0].classList.remove('hp-loss-hud-active');
|
$(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')[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.remove('hp-loss-hud-active');
|
||||||
$(html).find('.hp-loss-wrap')[1].classList.add('hp-loss-hud-disabled');
|
$(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.remove('hp-loss-hud-active');
|
||||||
$(html).find('.hp-loss-wrap')[2].classList.add('hp-loss-hud-disabled');
|
$(html).find('.hp-loss-wrap')[2].classList.add('hp-loss-hud-disabled');
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// HP Gain Button (new)
|
// HP Gain Button (new)
|
||||||
@@ -94,17 +93,13 @@ export default class LethalFantasyUtils {
|
|||||||
$(html).find('.gain-hp-hud-click').click(async (event) => {
|
$(html).find('.gain-hp-hud-click').click(async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
let hpGain = event.currentTarget.dataset.hpValue;
|
let hpGain = event.currentTarget.dataset.hpValue;
|
||||||
if (token) {
|
await hudActor.applyDamage(Number(hpGain)); // Positive value to add HP
|
||||||
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
|
|
||||||
// Clear bleeding wounds on heal — regardless of heal amount, any
|
// Clear bleeding wounds on heal — regardless of heal amount, any
|
||||||
// healing is enough to stop bleeding (field dressing / magic / rest).
|
// 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")
|
const hadBleeding = wounds.some(w => w.description === "Bleeding")
|
||||||
if (hadBleeding) {
|
if (hadBleeding) {
|
||||||
await actor.update({
|
await hudActor.update({
|
||||||
"system.hp.wounds": wounds.map(w =>
|
"system.hp.wounds": wounds.map(w =>
|
||||||
w.description === "Bleeding" ? { value: 0, duration: 0 } : 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')[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.remove('hp-gain-hud-active');
|
||||||
$(html).find('.hp-gain-wrap')[2].classList.add('hp-gain-hud-disabled');
|
$(html).find('.hp-gain-wrap')[2].classList.add('hp-gain-hud-disabled');
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Luck/Grit Buttons
|
// Luck/Grit Buttons
|
||||||
@@ -135,17 +129,13 @@ export default class LethalFantasyUtils {
|
|||||||
})
|
})
|
||||||
$(html).find('.luck-grit-btn').click(async (event) => {
|
$(html).find('.luck-grit-btn').click(async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
if (token) {
|
const resource = event.currentTarget.dataset.resource;
|
||||||
let tokenFull = canvas.tokens.placeables.find(t => t.id === token.id);
|
const amount = Number(event.currentTarget.dataset.amount);
|
||||||
let actor = tokenFull.actor;
|
const current = Number(foundry.utils.getProperty(hudActor.system, `${resource}.current`)) || 0;
|
||||||
const resource = event.currentTarget.dataset.resource;
|
const newValue = Math.max(0, current + amount);
|
||||||
const amount = Number(event.currentTarget.dataset.amount);
|
await hudActor.update({ [`system.${resource}.current`]: newValue });
|
||||||
const current = Number(foundry.utils.getProperty(actor.system, `${resource}.current`)) || 0;
|
$(html).find('.luck-grit-wrap')[0].classList.remove('luck-grit-hud-active');
|
||||||
const newValue = Math.max(0, current + amount);
|
$(html).find('.luck-grit-wrap')[0].classList.add('luck-grit-hud-disabled');
|
||||||
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');
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user