From fb9f3184b19043de45f1808c6cc486a3f0f1c59e Mon Sep 17 00:00:00 2001 From: Russell Larkin Date: Mon, 13 Jan 2025 16:50:12 +0000 Subject: [PATCH] Fix Remove Death Saves, #158 - Fix Remove Death Saves. - #158: Fix rest not resetting HP or resources. --- scripts/house-rules.js | 49 ++++++++++++++++++++++++++---------------- scripts/utils.js | 15 +++++++++++++ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/scripts/house-rules.js b/scripts/house-rules.js index 5f0b115..dbfd1ce 100644 --- a/scripts/house-rules.js +++ b/scripts/house-rules.js @@ -2,6 +2,7 @@ import { CONSTANTS, SHEET_TYPE } from './constants.js' import { c5eLoadTemplates, Logger, + getDefaultSetting, getSetting, registerMenu, registerSetting, @@ -333,7 +334,10 @@ function registerHooks () { Hooks.on('dnd5e.rollAbilityTest', (actor, roll, ability) => { awardInspiration('rollAbilityTest', actor, roll) }) Hooks.on('dnd5e.rollAttack', (item, roll, ability) => { awardInspiration('rollAttack', item, roll) }) Hooks.on('dnd5e.rollSkill', (actor, roll, ability) => { awardInspiration('rollSkill', actor, roll) }) - Hooks.on('preUpdateActor', capturePreviousHp) + Hooks.on('preUpdateActor', (actor, data, options, userId) => { + capturePreviousData(actor, data, options, userId) + updateDeathSaves('regainHp', actor, data, options) + }) Hooks.on('updateActor', (actor, data, options, userId) => { if (!game.user.isGM && !game.user.id !== userId) return if (data?.flags?.['custom-dnd5e']) return @@ -347,7 +351,6 @@ function registerHooks () { updateMassiveDamage(actor, data, options) recalculateHealing(actor, data, options) updateUnconscious(actor, data) - updateDeathSaves('regainHp', actor, data, options) } } }) @@ -571,7 +574,7 @@ function recalculateHealing (actor, data, options) { if (typeof currentHp === 'undefined') return - const previousHp = options.previousHp + const previousHp = options.customDnd5e.hp.value if (previousHp < 0 && currentHp > previousHp) { const diff = currentHp - previousHp @@ -718,6 +721,7 @@ function updateDeathSaves (source, actor, data, options) { if (actor.type !== 'character') return const removeDeathSaves = getSetting(CONSTANTS.DEATH_SAVES.SETTING.REMOVE_DEATH_SAVES.KEY) + const defaultRemoveDeathSaves = getDefaultSetting(CONSTANTS.DEATH_SAVES.SETTING.REMOVE_DEATH_SAVES.KEY) Logger.debug('Updating Death Saves...') @@ -727,16 +731,18 @@ function updateDeathSaves (source, actor, data, options) { if (typeof currentValue === 'undefined') return if (source === 'regainHp' && removeDeathSaves.regainHp[type] < 3 && foundry.utils.hasProperty(data, 'system.attributes.hp.value')) { - const previousHp = options.previousHp - const newValue = (previousHp === 0) ? Math.max(currentValue - removeDeathSaves.regainHp[type], 0) : currentValue - foundry.utils.setProperty(data, `system.attributes.death.${type}`, newValue) + const previousHp = options.customDnd5e.hp.value + const previousDeathValue = options.customDnd5e.death[type] + const newDeathValue = (previousHp === 0) ? Math.max(previousDeathValue - removeDeathSaves.regainHp[type], 0) : previousDeathValue + foundry.utils.setProperty(data, `system.attributes.death.${type}`, newDeathValue) } else if (source === 'rest') { const restType = (data?.longRest) ? 'longRest' : 'shortRest' - if (removeDeathSaves[restType][type] === 0) return - - const newValue = Math.max(currentValue - removeDeathSaves[restType][type], 0) - foundry.utils.setProperty(data.updateData, `system.attributes.death.${type}`, newValue) + if (removeDeathSaves[restType][type] && removeDeathSaves[restType][type] !== defaultRemoveDeathSaves[restType][type]) { + const currentDeathValue = actor.system.attributes.death[type] + const newDeathValue = Math.max(currentDeathValue - removeDeathSaves[restType][type], 0) + data.updateData[`system.attributes.death.${type}`] = newDeathValue + } } } @@ -842,7 +848,7 @@ function updateMassiveDamage (actor, data, options) { Logger.debug('Updating Massive Damage...') - const previousHp = options.previousHp + const previousHp = options.customDnd5e.hp.value const currentHp = data?.system?.attributes?.hp?.value if (previousHp <= currentHp) return @@ -862,23 +868,28 @@ function updateMassiveDamage (actor, data, options) { } /** - * Capture previous HP + * Capture previous data * @param {object} actor The actor * @param {object} data The data * @param {object} options The options * @param {string} userId The user id */ -function capturePreviousHp (actor, data, options, userId) { +function capturePreviousData (actor, data, options, userId) { if (game.user.id !== userId || !actor.isOwner) return - const currentHp = data?.system?.attributes?.hp?.value - if (currentHp === undefined) return - - Logger.debug('Capturing previous HP...') + Logger.debug('Capturing previous data...') - options.previousHp = actor.system.attributes.hp.value + options.customDnd5e = { + hp: { + value: actor.system.attributes.hp.value + }, + death: { + success: actor.system.attributes.death.success, + failure: actor.system.attributes.death.failure + } + } - Logger.debug('Previous HP captured', { previousHp: actor.system.attributes.hp.value }) + Logger.debug('Previous data captured', { previousData: options.customDnd5e }) } /** diff --git a/scripts/utils.js b/scripts/utils.js index 4e4d13e..0203be0 100644 --- a/scripts/utils.js +++ b/scripts/utils.js @@ -179,6 +179,21 @@ export function getSetting (key, defaultValue = null) { return value } +/** +* Get default setting value +* @public +* @param {string} key The setting key +* @returns {*} The default setting value +*/ +export function getDefaultSetting (key) { + try { + const defaultValue = game.settings.settings.get(`${MODULE.ID}.${key}`)?.default + return defaultValue + } catch { + Logger.debug(`Default setting '${key}' not found`) + } +} + /** * Get dnd5e setting * @public