From ac99c757318b81057205886297cb1c630f168b26 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Thu, 19 May 2022 03:25:18 +0200 Subject: [PATCH] feat: integrate with ds4 item roll functionality --- lang/de.json | 2 ++ lang/en.json | 2 ++ src/systems/ds4.ts | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lang/de.json b/lang/de.json index 7ba4803..b74fed7 100644 --- a/lang/de.json +++ b/lang/de.json @@ -1,6 +1,8 @@ { "TICKWERK.AdvanceTicks": "Auf der Tickleiste Vorrücken", "TICKWERK.NumberOfTicks": "Anzahl an Ticks", + "TICKWERK.SettingDS4ReactToRollItemHookName": "Item Würfelwurf Integration", + "TICKWERK.SettingDS4ReactToRollItemHookHint": "Zeige den Dialog zum Vorrücken auf der Tickleiste nachdem auf ein Item gewürfelt worden ist.", "TICKWERK.StopWaiting": "Abwarten Beenden", "TICKWERK.Tick": "Tick", "TICKWERK.Wait": "Abwarten", diff --git a/lang/en.json b/lang/en.json index 667ddd4..71d808e 100644 --- a/lang/en.json +++ b/lang/en.json @@ -1,6 +1,8 @@ { "TICKWERK.AdvanceTicks": "Advance on the Tickbar", "TICKWERK.NumberOfTicks": "Number of Ticks", + "TICKWERK.SettingDS4ReactToRollItemHookName": "Item Roll Integration", + "TICKWERK.SettingDS4ReactToRollItemHookHint": "Show the dialog for advancing on the tickbar after item rolls.", "TICKWERK.StopWaiting": "Stop Waiting", "TICKWERK.Tick": "Tick", "TICKWERK.Wait": "Wait", diff --git a/src/systems/ds4.ts b/src/systems/ds4.ts index 7846aba..0cffe69 100644 --- a/src/systems/ds4.ts +++ b/src/systems/ds4.ts @@ -3,12 +3,15 @@ // SPDX-License-Identifier: MIT import { packageId } from '../constants'; +import { getGame } from '../helpers'; import type { TickwerkCombatant } from '../data/documents/combatant'; - export const registerDS4SpecificFunctionality = () => { if (CONFIG.tickwerk === undefined) CONFIG.tickwerk = {}; foundry.utils.mergeObject(CONFIG.tickwerk, { getTiebreaker, getInitiativeFormula }); + + registerRollItemSetting(); + Hooks.on('ds4.rollItem', onRollItem); }; const getTiebreaker = async (combatant: TickwerkCombatant, combatants: TickwerkCombatant[]): Promise => { @@ -79,3 +82,37 @@ interface ActorData { }; }; } + +declare global { + // eslint-disable-next-line @typescript-eslint/no-namespace + namespace Hooks { + interface StaticCallbacks { + 'ds4.rollItem': (item: Item) => void; + } + } +} + +const onRollItem = (item: Item) => { + const game = getGame(); + if (game.settings.get(packageId, 'ds4.reactToRollItemHook')) { + if (['weapon', 'spell'].includes(item.type) && item.actor?.id) { + const combatants = item.actor + .getActiveTokens(false, true) + .map((token) => game.combat?.getCombatantByToken(token.id ?? '')); + for (const combatant of combatants) { + if (combatant?.parent?.started) combatant?.advanceTicksDialog(); + } + } + } +}; + +const registerRollItemSetting = () => { + getGame().settings.register(packageId, 'ds4.reactToRollItemHook', { + name: 'TICKWERK.SettingDS4ReactToRollItemHookName', + hint: 'TICKWERK.SettingDS4ReactToRollItemHookHint', + scope: 'client', + config: true, + type: Boolean, + default: true, + }); +};