From 844744e4dc7d0163bda49e06264fe580b5984532 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Tue, 13 Jul 2021 02:03:10 +0200 Subject: [PATCH] Add the possibility to reference actor properties in the values of effects Additionally, this also adds the possibility to do math inside the values of effects. All math functions that are available for rolls are also available here. --- src/module/active-effect.ts | 24 ++++++++++++++++++++++++ src/module/hooks/init.ts | 2 ++ 2 files changed, 26 insertions(+) create mode 100644 src/module/active-effect.ts diff --git a/src/module/active-effect.ts b/src/module/active-effect.ts new file mode 100644 index 00000000..1ae6eccc --- /dev/null +++ b/src/module/active-effect.ts @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2021 Johannes Loher +// +// SPDX-License-Identifier: MIT + +import { DS4Actor } from "./actor/actor"; + +declare global { + interface DocumentClassConfig { + ActiveEffect: typeof DS4ActiveEffect; + } +} + +export class DS4ActiveEffect extends ActiveEffect { + /** @override */ + apply(actor: DS4Actor, change: foundry.data.ActiveEffectData["changes"][number]): unknown { + change.value = Roll.replaceFormulaData(change.value, actor.data); + try { + change.value = Roll.safeEval(change.value).toString(); + } catch (e) { + // this is a valid case, e.g., if the effect change simply is a string + } + return super.apply(actor, change); + } +} diff --git a/src/module/hooks/init.ts b/src/module/hooks/init.ts index 8db11fe2..881c6690 100644 --- a/src/module/hooks/init.ts +++ b/src/module/hooks/init.ts @@ -4,6 +4,7 @@ // // SPDX-License-Identifier: MIT +import { DS4ActiveEffect } from "../active-effect"; import { DS4Actor } from "../actor/actor"; import { DS4CharacterActorSheet } from "../actor/sheets/character-sheet"; import { DS4CreatureActorSheet } from "../actor/sheets/creature-sheet"; @@ -43,6 +44,7 @@ async function init() { CONFIG.Actor.documentClass = DS4Actor; CONFIG.Item.documentClass = DS4Item; + CONFIG.ActiveEffect.documentClass = DS4ActiveEffect; CONFIG.Actor.typeLabels = DS4.i18n.actorTypes; CONFIG.Item.typeLabels = DS4.i18n.itemTypes;