Skip to content

Commit

Permalink
Simplify hooks, fix invisible estimates
Browse files Browse the repository at this point in the history
  • Loading branch information
mclemente committed Oct 5, 2023
1 parent d0d2c0b commit 0227d99
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 49 deletions.
11 changes: 2 additions & 9 deletions healthEstimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,15 @@ Hooks.once("i18nInit", function () {
game.healthEstimate = new HealthEstimate();
});

Hooks.once("setup", function () {
game.healthEstimate.setup();
});

Hooks.once("ready", function () {
if (game.settings.get("healthEstimate", "core.outputChat") && game.user.isGM) {
Hooks.on("updateActor", HealthEstimateHooks.onUpdateActor);
}
});
Hooks.once("setup", () => game.healthEstimate.setup());

//Canvas
Hooks.once("canvasReady", HealthEstimateHooks.onceCanvasReady);
Hooks.on("canvasReady", HealthEstimateHooks.onCanvasReady);
Hooks.on("createToken", HealthEstimateHooks.onCreateToken);

//Actor
Hooks.on("updateActor", HealthEstimateHooks.onUpdateActor);
Hooks.on("deleteActor", HealthEstimateHooks.deleteActor);
Hooks.on("deleteActiveEffect", HealthEstimateHooks.deleteActiveEffect);

Expand Down
53 changes: 28 additions & 25 deletions module/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export class HealthEstimateHooks {
Hooks.on("refreshToken", HealthEstimateHooks.refreshToken);
if (game.healthEstimate.alwaysShow) {
canvas.tokens?.placeables.forEach((token) => game.healthEstimate._handleOverlay(token, true));
Hooks.on("updateActor", HealthEstimateHooks.alwaysOnUpdateActor);
}
if (game.healthEstimate.scaleToZoom) Hooks.on("canvasPan", HealthEstimateHooks.onCanvasPan);
Hooks.on("canvasInit", HealthEstimateHooks.canvasInit);
Expand Down Expand Up @@ -61,31 +60,35 @@ export class HealthEstimateHooks {
// ACTOR //
///////////

static alwaysOnUpdateActor(actor, updates, options, userId) {
//Get all the tokens on the off-chance there's two tokens of the same linked actor.
const tokens = canvas.tokens?.placeables.filter((token) => token.actor?.id === actor.id);
// Call the _handleOverlay method for each token.
tokens?.forEach((token) => {
game.healthEstimate._handleOverlay(token, true);
});
}

static onUpdateActor(actor, data, options, userId) {
// Find a single token associated with the updated actor.
const token = canvas.tokens?.placeables.find((token) => {
if (options?.syntheticActorUpdate) return token?.id === actor.token.id;
return token?.actor.id === actor.id;
});
if (token) {
const tokenId = token?.id;
const tokenHP = game.healthEstimate.actorsCurrentHP?.[tokenId];
if (
tokenId &&
tokenHP &&
!game.healthEstimate.breakOverlayRender(token) &&
!game.healthEstimate.hideEstimate(token)
) {
outputStageChange(token);
if (game.healthEstimate.alwaysShow) {
//Get all the tokens on the off-chance there's two tokens of the same linked actor.
const tokens = canvas.tokens?.placeables.filter((token) => {
if (options?.syntheticActorUpdate) return token?.id === actor.token.id;
return token.actor?.id === actor.id;
});
// Call the _handleOverlay method for each token.
tokens?.forEach((token) => {
game.healthEstimate._handleOverlay(token, true);
});
}
if (game.healthEstimate.outputChat && game.user.isGM) {
// Find a single token associated with the updated actor.
const token = canvas.tokens?.placeables.find((token) => {
if (options?.syntheticActorUpdate) return token?.id === actor.token.id;
return token?.actor.id === actor.id;
});
if (token) {
const tokenId = token?.id;
const tokenHP = game.healthEstimate.actorsCurrentHP?.[tokenId];
if (
tokenId &&
tokenHP &&
!game.healthEstimate.breakOverlayRender(token) &&
!game.healthEstimate.hideEstimate(token)
) {
outputStageChange(token);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion module/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class HealthEstimate {
!token?.actor ||
this.breakOverlayRender(token) ||
(!game.user.isGM && this.hideEstimate(token)) ||
!token.isVisible
(!token.isVisible && !this.alwaysShow)
)
return;

Expand Down Expand Up @@ -328,6 +328,7 @@ export class HealthEstimate {
this.NPCsJustDie = sGet("core.NPCsJustDie");
this.deathMarker = sGet("core.deathMarker");
this.scaleToZoom = sGet("core.menuSettings.scaleToZoom");
this.outputChat = sGet("core.outputChat");

this.smoothGradient = sGet("core.menuSettings.smoothGradient");

Expand Down
18 changes: 4 additions & 14 deletions module/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ export const registerSettings = function () {
type: Boolean,
default: false,
onChange: (value) => {
if (value && game.user.isGM) {
Hooks.on("updateActor", HealthEstimateHooks.onUpdateActor);
} else if (game.user.isGM) {
Hooks.off("updateActor", HealthEstimateHooks.onUpdateActor);
}
game.healthEstimate.outputChat = value;
},
});
let warning = " ";
Expand Down Expand Up @@ -104,15 +100,9 @@ export const registerSettings = function () {
default: false,
onChange: (value) => {
game.healthEstimate.alwaysShow = value;
if (value) {
canvas.tokens?.placeables.forEach((token) => game.healthEstimate._handleOverlay(token, true));
Hooks.on("updateActor", HealthEstimateHooks.alwaysOnUpdateActor);
} else {
canvas.tokens?.placeables.forEach((token) =>
game.healthEstimate._handleOverlay(token, game.healthEstimate.showCondition(token.hover))
);
Hooks.off("updateActor", HealthEstimateHooks.alwaysOnUpdateActor);
}
canvas.tokens?.placeables.forEach((token) =>
game.healthEstimate._handleOverlay(token, value || game.healthEstimate.showCondition(token.hover))
);
},
});
addMenuSetting("core.combatOnly", {
Expand Down

0 comments on commit 0227d99

Please sign in to comment.