Skip to content

Commit

Permalink
[#1610] Apply temp HP from context menu on dice rolls (#1614)
Browse files Browse the repository at this point in the history
Co-authored-by: Jeff Hitchcock <[email protected]>
  • Loading branch information
kaelad02 and arbron authored Aug 31, 2022
1 parent 8eec01c commit 0b3735c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
"DND5E.Charges": "Charges",
"DND5E.ChatContextDamage": "Apply Damage",
"DND5E.ChatContextHealing": "Apply Healing",
"DND5E.ChatContextTempHP": "Apply Temporary HP",
"DND5E.ChatContextDoubleDamage": "Apply Double Damage",
"DND5E.ChatContextHalfDamage": "Apply Half Damage",
"DND5E.ChatFlavor": "Chat Message Flavor",
Expand Down
16 changes: 16 additions & 0 deletions module/documents/actor/actor.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,22 @@ export default class Actor5e extends Actor {
return allowed !== false ? this.update(updates, {dhp: -amount}) : this;
}

/* -------------------------------------------- */

/**
* Apply a certain amount of temporary hit point, but only if it's more than the actor currently has.
* @param {number} amount An amount of temporary hit points to set
* @returns {Promise<Actor5e>} A Promise which resolves once the temp HP has been applied
*/
async applyTempHP(amount=0) {
amount = parseInt(amount);
const hp = this.system.attributes.hp;

// Update the actor if the new amount is greater than the current
const tmp = parseInt(hp.temp) || 0;
return amount > tmp ? this.update({"system.attributes.hp.temp": amount}) : this;
}

/* -------------------------------------------- */

/**
Expand Down
22 changes: 22 additions & 0 deletions module/documents/chat-message.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export function addChatMessageContextOptions(html, options) {
condition: canApply,
callback: li => applyChatCardDamage(li, -1)
},
{
name: game.i18n.localize("DND5E.ChatContextTempHP"),
icon: '<i class="fas fa-user-clock"></i>',
condition: canApply,
callback: li => applyChatCardTemp(li)
},
{
name: game.i18n.localize("DND5E.ChatContextDoubleDamage"),
icon: '<i class="fas fa-user-injured"></i>',
Expand Down Expand Up @@ -120,6 +126,22 @@ function applyChatCardDamage(li, multiplier) {
}));
}

/* -------------------------------------------- */

/**
* Apply rolled dice as temporary hit points to the controlled token(s).
* @param {HTMLElement} li The chat entry which contains the roll data
* @returns {Promise}
*/
function applyChatCardTemp(li) {
const message = game.messages.get(li.data("messageId"));
const roll = message.rolls[0];
return Promise.all(canvas.tokens.controlled.map(t => {
const a = t.actor;
return a.applyTempHP(roll.total);
}));
}

/* -------------------------------------------- */

/**
Expand Down

0 comments on commit 0b3735c

Please sign in to comment.