fix: address a few problems with active effect application

This commit is contained in:
Johannes Loher 2022-11-04 04:49:18 +01:00
parent 1abaf6203c
commit ab31450dd8
8 changed files with 62 additions and 23 deletions

View file

@ -79,12 +79,12 @@ export class DS4Actor extends Actor {
if (condition !== undefined && condition !== "") {
try {
const replacedCondition = Roll.replaceFormulaData(condition, {
const replacedCondition = DS4Actor.replaceFormulaData(condition, {
item: item.data,
actor: this.data,
effect: effect.data,
});
return Boolean(mathEvaluator.evaluate(replacedCondition));
return replacedCondition !== undefined ? Boolean(mathEvaluator.evaluate(replacedCondition)) : false;
} catch (error) {
logger.warn(error);
return false;
@ -95,6 +95,21 @@ export class DS4Actor extends Actor {
});
}
private static replaceFormulaData(formula: string, data: object): string | undefined {
const dataRgx = new RegExp(/@([a-z.0-9_\-]+)/gi);
try {
return formula.replace(dataRgx, (_, term) => {
const value = foundry.utils.getProperty(data, term);
if (value == null) {
throw new Error();
}
return String(value).trim();
});
} catch {
return undefined;
}
}
/**
* We override this with an empty implementation because we have our own custom way of applying
* {@link ActiveEffect}s and {@link Actor#prepareEmbeddedDocuments} calls this.