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.
This commit is contained in:
Matt Smith 2020-10-30 10:21:58 -05:00
parent 5560fc24d4
commit b385af307a

View file

@ -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;
}