From b385af307a2f78235af86917cb996fc497323fc6 Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Fri, 30 Oct 2020 10:21:58 -0500 Subject: [PATCH] 22: Refactor roll attributes in helper class - Updated `rollData` in helper.js to use actor as the data source rather than the object. This allows for items to reference attributes by assuming the parent actor is the source of truth and item attributes would be referenced as either `@item.attr` or `@items.ITEMNAME.attr` - Refactored formula replacement to prevent values from being replaced with 0s unintentionally. --- module/helper.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/module/helper.js b/module/helper.js index 689b279..ccdf109 100644 --- a/module/helper.js +++ b/module/helper.js @@ -181,7 +181,8 @@ export class EntitySheetHelper { const label = button.closest(".attribute").querySelector(".attribute-label")?.value; const chatLabel = label ?? button.parentElement.querySelector(".attribute-key").value; const shorthand = game.settings.get("worldbuilding", "macroShorthand"); - const rollData = this.object.getRollData(); + // Use the actor for rollData so that formulas are always in reference to the parent actor. + const rollData = this.actor.getRollData(); let formula = button.closest(".attribute").querySelector(".attribute-value")?.value; // If there's a formula, attempt to roll it. @@ -531,9 +532,14 @@ export class EntitySheetHelper { let dataRgx = new RegExp(/@([a-z.0-9_\-]+)/gi); let rollFormula = formula.replace(dataRgx, (match, term) => { let value = getProperty(data, term); - if ( value === null ) return "0"; - if ( String(value).includes('@') ) return value; - else return `@${term}`; + // If there was a value returned, trim and return it. + if ( value ) { + return String(value).trim(); + } + // Otherwise, return either the missing replacement value, or the original @attr string for later replacement. + else { + return missing != null ? missing : `@${term}`; + } }); return rollFormula; }