Skip to content

Commit

Permalink
Fix Remove Death Saves, #158
Browse files Browse the repository at this point in the history
- Fix Remove Death Saves.
- #158: Fix rest not resetting HP or resources.
  • Loading branch information
Larkinabout committed Jan 13, 2025
1 parent e0c954a commit fb9f318
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
49 changes: 30 additions & 19 deletions scripts/house-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CONSTANTS, SHEET_TYPE } from './constants.js'
import {
c5eLoadTemplates,
Logger,
getDefaultSetting,
getSetting,
registerMenu,
registerSetting,
Expand Down Expand Up @@ -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
Expand All @@ -347,7 +351,6 @@ function registerHooks () {
updateMassiveDamage(actor, data, options)
recalculateHealing(actor, data, options)
updateUnconscious(actor, data)
updateDeathSaves('regainHp', actor, data, options)
}
}
})
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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...')

Expand All @@ -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
}
}
}

Expand Down Expand Up @@ -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
Expand All @@ -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 })
}

/**
Expand Down
15 changes: 15 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit fb9f318

Please sign in to comment.