Skip to content

Commit

Permalink
Fix token effects reapplying on sheet closure, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkinabout committed Oct 1, 2024
1 parent d45ffa7 commit 0bf04a1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
Binary file added .github/readme/custom-dnd5e-camp-supplies.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,22 @@ For help and advice on the house rules, either click the info buttons in the Con

![Custom D&D 5e Negative Hit Points](./.github/readme/custom-dnd5e-negative-hit-points.png)

### Inspiration
## Award Inspiration
- Award Inspiration when a set value is rolled for an ability check, attack roll, saving throw and/or skill check.

![Custom D&D 5e Award Inspiration](./.github/readme/custom-dnd5e-award-inspiration.png)

### Prone Status
## Prone
- Rotate the token when it gains the Prone status effect.

![Custom D&D 5e Prone](./.github/readme/custom-dnd5e-prone-rotation.gif)

## Resting
### Use Camp Supplies
- When taking a Long Rest, optionally requires spending Camp Supplies.

![Custom D&D 5e Camp Supplies](./.github/readme/custom-dnd5e-camp-supplies.png)

## Counters
**Counter Types:** Add four types of counter to the character sheets: checkbox, fraction, number and success/failure.

Expand Down Expand Up @@ -100,6 +106,13 @@ For help and advice on modifying a configuration, either click the info button i
- Change the banner at the top of the sheet.
- Hide death saves, encumbrance, exhaustion, inspiration, the Manage Currency button, Legendary Actions, Legendary Resistances and Use Lair Action.

## Show Pressed Keys
- Display an icon near the cursor when the Skip Dialog, Advantage or Disadvantage keys are pressed.

![Custom D&D 5e Show Pressed Keys - Skip Dialog](./.github/readme/custom-dnd5e-show-pressed-keys-skip-dialog.png)

![Custom D&D 5e Show Pressed Keys - Advantage](./.github/readme/custom-dnd5e-show-pressed-keys-advantage.png)

## Radial Status Effects
- Display status effects in a circle around the token.

Expand Down
40 changes: 27 additions & 13 deletions scripts/house-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { CONSTANTS, SHEET_TYPE } from './constants.js'
import {
Logger,
getSetting,
getFlag,
setFlag,
registerMenu,
registerSetting,
makeBloodied,
Expand Down Expand Up @@ -311,7 +313,8 @@ 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', (actor, data, options) => {
Hooks.on('preUpdateActor', capturePreviousHp)
Hooks.on('updateActor', (actor, data, options) => {
const instantDeath = updateInstantDeath(actor, data)
updateHp(actor, data)
if (!instantDeath) {
Expand Down Expand Up @@ -489,7 +492,7 @@ function recalculateDamage (actor, amount, updates, options) {

/**
* Recalculate Healing
* Triggered by the 'preUpdateActor' hook
* Triggered by the 'updateActor' hook
* If 'Apply Negative HP' and 'Heal from 0 HP' are enabled, recalculate healing to increase HP from zero instead of the negative value
* @param {object} actor The actor
* @param {object} data The data
Expand All @@ -502,7 +505,7 @@ function recalculateHealing (actor, data) {

if (typeof currentHp === 'undefined') return

const previousHp = actor.system.attributes.hp.value
const previousHp = getFlag(actor, 'previousHp')

if (previousHp < 0 && currentHp > previousHp) {
const diff = currentHp - previousHp
Expand Down Expand Up @@ -535,7 +538,7 @@ async function rollNpcHp (token) {

/**
* Update Bloodied
* Triggered by the 'preUpdateActor' hook
* Triggered by the 'updateActor' hook
* If 'Apply Bloodied' is enabled, apply or remove the Bloodied condition and other token effects based on the HP change
* @param {object} actor The actor
* @param {object} data The data
Expand All @@ -548,7 +551,7 @@ function updateBloodied (actor, data) {

if (typeof currentHp === 'undefined') return null

const previousHp = actor.system.attributes.hp.value
const previousHp = getFlag(actor, 'previousHp')
const halfHp = Math.ceil((maxHp ?? actor.system.attributes.hp.max) * 0.5)
const deathFailures = data?.system?.attributes?.death?.failure ?? actor?.system?.attributes?.death?.failure ?? 0

Expand All @@ -565,7 +568,7 @@ function updateBloodied (actor, data) {

/**
* Update Dead
* Triggered by the 'preUpdateActor' hook
* Triggered by the 'updateActor' hook
* @param {object} actor The actor
* @param {object} data The data
*/
Expand All @@ -588,7 +591,7 @@ function updateDead (actor, data) {

/**
* Update Unconscious
* Triggered by the 'preUpdateActor' hook
* Triggered by the 'updateActor' hook
* @param {object} actor The actor
* @param {object} data The data
*/
Expand All @@ -611,7 +614,7 @@ function updateUnconscious (actor, data) {

/**
* Update Death Saves
* Triggered by the 'preUpdateActor' hook
* Triggered by the 'updateActor' hook
* @param {string} source regainHp or rest
* @param {object} actor The actor
* @param {object} data The data
Expand All @@ -627,7 +630,7 @@ function updateDeathSaves (source, actor, data) {
if (typeof currentValue === 'undefined') return

if (source === 'regainHp' && removeDeathSaves.regainHp[type] < 3 && foundry.utils.hasProperty(data, 'system.attributes.hp.value')) {
const previousHp = actor.system.attributes.hp.value
const previousHp = getFlag(actor, 'previousHp')
const newValue = (previousHp === 0) ? Math.max(currentValue - removeDeathSaves.regainHp[type], 0) : currentValue
foundry.utils.setProperty(data, `system.attributes.death.${type}`, newValue)
} else if (source === 'rest') {
Expand All @@ -646,7 +649,7 @@ function updateDeathSaves (source, actor, data) {

/**
* Update HP
* Triggered by the 'preUpdateActor' hook
* Triggered by the 'updateActor' hook
* If 'Apply Negative HP' is disabled and HP is below 0, set HP to 0.
* This will happen where 'Apply Instant Death' is enabled as negative HP is used to initially calculate whether Instant Death applies
* @param {object} actor The actor
Expand Down Expand Up @@ -693,7 +696,7 @@ function updateHpMeter (app, html, data) {

/**
* Update Instant Death
* Triggered by the 'preUpdateActor' hook and called by the 'recalculateDamage' function
* Triggered by the 'updateActor' hook and called by the 'recalculateDamage' function
* @param {object} actor The actor
* @param {object} data The data
* @returns {boolean} Whether instant death is applied
Expand All @@ -717,15 +720,15 @@ function updateInstantDeath (actor, data) {

/**
* Update Massive Damage
* Triggered by the 'preUpdateActor' hook and called by the 'recalculateDamage' function
* Triggered by the 'updateActor' hook and called by the 'recalculateDamage' function
* @param {object} actor The actor
* @param {object} data The data
* @returns {boolean} Whether instant death is applied
*/
function updateMassiveDamage (actor, data) {
if (actor.type !== 'character' || !getSetting(CONSTANTS.HIT_POINTS.SETTING.APPLY_MASSIVE_DAMAGE.KEY)) return false

const previousHp = actor.system.attributes.hp.value
const previousHp = getFlag(actor, 'previousHp')
const currentHp = data?.system?.attributes?.hp?.value

if (previousHp <= currentHp) return
Expand All @@ -742,6 +745,17 @@ function updateMassiveDamage (actor, data) {
return false
}

/**
* Capture previous HP
* @param {object} actor The actor
* @param {object} data The data
*/
function capturePreviousHp (actor, data) {
const currentHp = data?.system?.attributes?.hp?.value
if (currentHp === undefined) return
setFlag(actor, 'previousHp', actor.system.attributes.hp.value)
}

/**
* Update Token Effects
* Triggered by the 'createActiveEffect' and 'deleteActiveEffect' hooks
Expand Down

0 comments on commit 0bf04a1

Please sign in to comment.