Skip to content

Commit

Permalink
1.0.36 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ironmonk88 committed Jul 19, 2022
1 parent 5ce9dee commit bba8bb0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
76 changes: 48 additions & 28 deletions alwayshp.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class AlwaysHP extends Application {
if (!a)
continue;

let resourcename = (setting("resourcename") || game.system.primaryTokenAttribute || 'attributes.hp');
let resourcename = (setting("resourcename") || (game.system?.primaryTokenAttribute ?? game.data?.primaryTokenAttribute) || 'attributes.hp');
let resource = getProperty(a, "system." + resourcename);

if (value.value == 'zero')
Expand All @@ -93,13 +93,16 @@ export class AlwaysHP extends Application {
if (active != undefined && setting("add-defeated")) {
let status = CONFIG.statusEffects.find(e => e.id === CONFIG.Combat.defeatedStatusId);
let effect = a && status ? status : CONFIG.controlIcons.defeated;
const exists = (effect.icon == undefined ? (t.document.overlayEffect == effect) : (a.effects.find(e => e.getFlag("core", "statusId") === effect.id) != undefined));
let overlay = (isNewerVersion(game.version, "9") ? t.document.overlayEffect : t.data.overlayEffect);
const exists = (effect.icon == undefined ? (overlay == effect) : (a.effects.find(e => e.getFlag("core", "statusId") === effect.id) != undefined));
if (exists != active)
await t.toggleEffect(effect, { overlay: true, active: (active == 'toggle' ? !exists : active) });
}

if (active === false && setting("clear-savingthrows")) {
a.update({ "system.attributes.death.failure": 0, "system.attributes.death.success": 0 });
a.update(isNewerVersion(game.version, "9")
? { "system.attributes.death.failure": 0, "system.attributes.death.success": 0 }
: { "data.attributes.death.failure": 0, "data.attributes.death.success": 0 });
}

log('applying damage', a, value);
Expand All @@ -119,15 +122,16 @@ export class AlwaysHP extends Application {
let actor = token.actor;
let { value, target } = amount;
let updates = {};
let resourcename = (setting("resourcename") || game.system.primaryTokenAttribute || 'attributes.hp');
let resource = getProperty(actor, "system." + resourcename);
let dataname = (isNewerVersion(game.version, "9") ? "system." : "data.");
let resourcename = (setting("resourcename") || (game.system?.primaryTokenAttribute ?? game.data?.primaryTokenAttribute) || 'attributes.hp');
let resource = getProperty((isNewerVersion(game.version, "9") ? actor : actor.data), dataname + resourcename);
if (resource instanceof Object) {
value = Math.floor(parseInt(value) * multiplier);

// Deduct damage from temp HP first
if (resource.hasOwnProperty("tempmax") && target == "max") {
const dm = (resource.tempmax ?? 0) - value;
updates["system." + resourcename + ".tempmax"] = dm;
updates[dataname + resourcename + ".tempmax"] = dm;
} else {
let dt = 0;
let tmpMax = 0;
Expand All @@ -138,7 +142,7 @@ export class AlwaysHP extends Application {

tmpMax = parseInt(resource.tempmax) || 0;

updates["system." + resourcename + ".temp"] = tmp - dt;
updates[dataname + resourcename + ".temp"] = tmp - dt;
}

// Update the Actor
Expand All @@ -147,21 +151,32 @@ export class AlwaysHP extends Application {
const dh = Math.clamped(resource.value - change, (game.system.id == 'D35E' || game.system.id == 'pf1' ? -2000 : 0), resource.max + tmpMax);
updates["system." + resourcename + ".value"] = dh;

let display = change + dt;
canvas.interface.createScrollingText(token.center, (-display).signedString(), {
anchor: CONST.TEXT_ANCHOR_POINTS.CENTER,
direction: display > 0 ? CONST.TEXT_ANCHOR_POINTS.BOTTOM : CONST.TEXT_ANCHOR_POINTS.TOP,
distance: token.h,
fontSize: 28,
stroke: 0x000000,
strokeThickness: 4,
jitter: 0.25
});
if (isNewerVersion(game.version, "9")) {
let display = change + dt;
canvas.interface.createScrollingText(token.center, (-display).signedString(), {
anchor: CONST.TEXT_ANCHOR_POINTS.CENTER,
direction: display > 0 ? CONST.TEXT_ANCHOR_POINTS.BOTTOM : CONST.TEXT_ANCHOR_POINTS.TOP,
distance: token.h,
fontSize: 28,
stroke: 0x000000,
strokeThickness: 4,
jitter: 0.25
});
} else {
token.hud.createScrollingText((-change).signedString(), {
anchor: CONST.TEXT_ANCHOR_POINTS.TOP,
fontSize: 32,
fill: (change > 0 ? 16711680 : 65280),
stroke: 0x000000,
strokeThickness: 4,
jitter: 0.25
});
}
}
}
} else {
let val = Math.floor(parseInt(resource));
updates["system." + resourcename] = (val - value);
updates[dataname + resourcename] = (val - value);
}

return await actor.update(updates);
Expand All @@ -186,6 +201,7 @@ export class AlwaysHP extends Application {
this.tokenstat = "";
this.tokentemp = "";
this.tokentooltip = "";
let dataname = (isNewerVersion(game.version, "9") ? "system." : "data.");
if (canvas.tokens.controlled.length == 0)
this.tokenname = "";
else if (canvas.tokens.controlled.length == 1) {
Expand All @@ -194,7 +210,7 @@ export class AlwaysHP extends Application {
this.tokenname = "";
else {
let resourcename = setting("resourcename");
let resource = getProperty(a, "system." + resourcename);
let resource = getProperty((isNewerVersion(game.version, "9") ? a : a.data), dataname + resourcename);

let value = this.getResValue(resource, "value", resource);
let max = this.getResValue(resource, "max");
Expand All @@ -214,7 +230,7 @@ export class AlwaysHP extends Application {
const color = [(1 - (this.valuePct / 2)), this.valuePct, 0];
this.color = `rgba(${parseInt(color[0] * 255)},${parseInt(color[1] * 255)},${parseInt(color[2] * 255)}, 0.7)`;

this.tokenname = canvas.tokens.controlled[0].name;
this.tokenname = canvas.tokens.controlled[0]?.name ?? canvas.tokens.controlled[0]?.data?.name;
this.tokenstat = value;
this.tokentemp = temp;
this.tokentooltip = `HP: ${value}, Temp: ${temp}, Max: ${max}`;
Expand All @@ -233,11 +249,12 @@ export class AlwaysHP extends Application {
if (!a)
return;

let prop = a.system.attributes.death;
let prop = (isNewerVersion(game.version, "9") ? a.system.attributes.death : a.data.data.attributes.death);
prop[save ? 'success' : 'failure'] = Math.max(0, Math.min(3, prop[save ? 'success' : 'failure'] + value));

let dataname = (isNewerVersion(game.version, "9") ? "system." : "data.");
let updates = {};
updates["system.attributes.death." + (save ? 'success' : 'failure')] = prop[save ? 'success' : 'failure'];
updates[dataname + "attributes.death." + (save ? 'success' : 'failure')] = prop[save ? 'success' : 'failure'];
canvas.tokens.controlled[0].actor.update(updates);

this.changeToken();
Expand All @@ -249,11 +266,12 @@ export class AlwaysHP extends Application {
$('.token-stats', this.element).attr('title', this.tokentooltip).html((this.tokentemp ? `<div class="stat temp">${this.tokentemp}</div>` : '') + (this.tokenstat ? `<div class="stat" style="background-color:${this.color}">${this.tokenstat}</div>` : ''));

let actor = (canvas.tokens.controlled.length == 1 ? canvas.tokens.controlled[0].actor : null);
let showST = (actor != undefined && game.system.id == "dnd5e" && actor.system.attributes.hp.value == 0 && actor?.hasPlayerOwner);
let data = (isNewerVersion(game.version, "9") ? actor?.system : actor?.data?.data);
let showST = (actor != undefined && game.system.id == "dnd5e" && data.attributes.hp.value == 0 && actor?.hasPlayerOwner);
$('.death-savingthrow', this.element).css({ display: (showST ? 'inline-block' : 'none') });
if (showST) {
$('.death-savingthrow.fail > div', this.element).each(function (idx) { $(this).toggleClass('active', idx < actor.system.attributes.death.failure) });
$('.death-savingthrow.save > div', this.element).each(function (idx) { $(this).toggleClass('active', idx < actor.system.attributes.death.success) });
$('.death-savingthrow.fail > div', this.element).each(function (idx) { $(this).toggleClass('active', idx < data.attributes.death.failure) });
$('.death-savingthrow.save > div', this.element).each(function (idx) { $(this).toggleClass('active', idx < data.attributes.death.success) });
}

$('.resource', this.element).toggle(canvas.tokens.controlled.length == 1 && this.valuePct != undefined);
Expand Down Expand Up @@ -295,8 +313,9 @@ export class AlwaysHP extends Application {
if (canvas.tokens.controlled.length == 1) {
const actor = canvas.tokens.controlled[0].actor;

let resourcename = (setting("resourcename") || game.system.primaryTokenAttribute || 'attributes.hp');
let resource = getProperty(actor, "system." + resourcename);
let dataname = (isNewerVersion(game.version, "9") ? "system." : "data.");
let resourcename = (setting("resourcename") || (game.system.primaryTokenAttribute ?? game.system.data.primaryTokenAttribute) || 'attributes.hp');
let resource = getProperty(actor, dataname + resourcename);

if (resource.hasOwnProperty("max")) {
let max = this.getResValue(resource, "max");
Expand Down Expand Up @@ -458,9 +477,10 @@ Hooks.on('controlToken', () => {

Hooks.on('updateActor', (actor, data) => {
//log('Updating actor', actor, data);
let dataname = (isNewerVersion(game.version, "9") ? "system." : "data.");
if (canvas.tokens.controlled.length == 1
&& canvas.tokens.controlled[0]?.actor.id == actor.id
&& (getProperty(data, "system.attributes.death") != undefined || getProperty(data, "system." + setting("resourcename")))) {
&& (getProperty(data, dataname + "attributes.death") != undefined || getProperty(data, dataname + setting("resourcename")))) {
game.AlwaysHP?.refreshSelected();
}
});
Expand Down
8 changes: 4 additions & 4 deletions module.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"title": "Always HP",
"description": "We're always updating HP, add a window to make it easier to adjust that",
"version": "1.0.35",
"version": "1.0.36",
"authors": [
{
"name": "IronMonk",
Expand Down Expand Up @@ -29,16 +29,16 @@
"css/alwayshp.css"
],
"url": "https://github.com/ironmonk88/always-hp",
"download": "https://github.com/ironmonk88/always-hp/archive/1.0.35.zip",
"download": "https://github.com/ironmonk88/always-hp/archive/1.0.36.zip",
"manifest": "https://github.com/ironmonk88/always-hp/releases/latest/download/module.json",
"bugs": "https://github.com/ironmonk88/always-hp/issues",
"allowBugReporter": true,
"id": "always-hp",
"compatibility": {
"minimum": "10",
"minimum": "9",
"verified": "10"
},
"name": "always-hp",
"minimumCoreVersion": "10",
"minimumCoreVersion": "9",
"compatibleCoreVersion": "10"
}
2 changes: 1 addition & 1 deletion modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const registerSettings = function () {
name: i18n("ALWAYSHP.resourcename.name"),
hint: i18n("ALWAYSHP.resourcename.hint"),
scope: "world",
default: game.system.primaryTokenAttribute || 'attributes.hp',
default: (game.system.primaryTokenAttribute ?? game.system.data?.primaryTokenAttribute) || 'attributes.hp',
type: String,
config: true
});
Expand Down

0 comments on commit bba8bb0

Please sign in to comment.