Skip to content

Commit

Permalink
fix: Update of auto apply persistent healing similar to how auto appl…
Browse files Browse the repository at this point in the history
…y persistent damage is done.
  • Loading branch information
xdy committed Jul 15, 2024
1 parent 47e1cdb commit 59070ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
34 changes: 11 additions & 23 deletions src/module/feature/damageHandler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,25 +165,15 @@ export function persistentDamage(message: ChatMessagePF2e) {
}
}

export function persistentHealing(message, enabled: boolean) {
export function persistentHealing(message) {
if (
enabled &&
shouldIHandleThisMessage(
message,
["all", "players"].includes(String(game.settings.get(MODULENAME, "applyPersistentAllow"))),
["all", "gm"].includes(String(game.settings.get(MODULENAME, "applyPersistentAllow"))),
) &&
message.flavor &&
message.rolls &&
game.combats &&
game.combats.active &&
game.combats.active.combatant &&
game.combats.active.combatant.actor &&
message.id === game.messages.contents.slice(-1, game.messages.size)[0].id &&
(message.getFlag(MODULENAME, "persistentHandled") ?? true)
game.ready &&
game.settings.get(MODULENAME, "applyPersistentAllow") !== "none" &&
message.token &&
message.isDamageRoll &&
(message.rolls[0] as Rolled<DamageRoll>)?.instances.some((i) => i.kinds.some((k) => k === "healing"))
) {
const token = game.combats.active.combatant.token;
if (token && token.isOwner) {
if (message.token && message.token.isOwner) {
const fastHealingLabel = game.i18n.localize(
`${MODULENAME}.SETTINGS.applyPersistentHealing.FastHealingLabel`,
);
Expand All @@ -192,12 +182,10 @@ export function persistentHealing(message, enabled: boolean) {
);
if ([fastHealingLabel, regenerationLabel].some((text) => message.flavor?.includes(text))) {
const healing = message.rolls.reduce((sum, current) => sum + (current.total || 1), 0) * -1;
token.actor
?.applyDamage({
damage: healing,
token: token.actor?.getActiveTokens()[0].document,
})
.then(() => message.setFlag(MODULENAME, "persistentHandled", true).then());
message.token.actor?.applyDamage({
damage: healing,
token: message.token,
});
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/module/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export const preCreateChatMessageHook = (message: ChatMessagePF2e, data: any, _o
persistentDamage(message);
}

if (game.settings.get(MODULENAME, "applyPersistentHealing")) {
persistentHealing(message);
}

if (reminderTargetingEnabled) {
proceed = reminderTargeting(message, String(game.settings.get(MODULENAME, "reminderTargeting")));
}
Expand Down Expand Up @@ -118,8 +122,6 @@ function deprecatedDyingHandlingRenderChatMessageHook(message: ChatMessagePF2e)

export function renderChatMessageHook(message: ChatMessagePF2e, jq: JQuery) {
const html = <HTMLElement>jq.get(0);
// Only acts on latest message, but can't be in createChatMessageHook as that doesn't get triggered for some reason.
persistentHealing(message, Boolean(game.settings.get(MODULENAME, "applyPersistentHealing")));

deprecatedDyingHandlingRenderChatMessageHook(message);

Expand Down

0 comments on commit 59070ba

Please sign in to comment.