2021-06-26 22:02:00 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-24 09:19:26 +01:00
|
|
|
import { DS4 } from "../config";
|
2022-11-04 21:47:18 +01:00
|
|
|
import { notifications } from "../ui/notifications";
|
|
|
|
import { getGame } from "../utils/utils";
|
2021-09-19 20:12:01 +02:00
|
|
|
import { getActiveActorAndToken } from "./helpers";
|
2021-03-24 09:19:26 +01:00
|
|
|
|
2022-11-04 21:47:18 +01:00
|
|
|
import type { Check } from "../documents/actor/actor-data-properties-base";
|
2021-03-24 09:19:26 +01:00
|
|
|
/**
|
|
|
|
* Creates a macro from a check drop.
|
|
|
|
* Get an existing roll check macro if one exists, otherwise create a new one.
|
|
|
|
* @param check - The name of the check to perform.
|
|
|
|
* @param slot - The hotbar slot to use.
|
|
|
|
*/
|
|
|
|
export async function createRollCheckMacro(check: Check, slot: string): Promise<void> {
|
2021-03-29 21:20:08 +02:00
|
|
|
const macro = await getOrCreateRollCheckMacro(check);
|
2021-07-07 19:22:35 +02:00
|
|
|
getGame().user?.assignHotbarMacro(macro ?? null, slot);
|
2021-03-24 09:19:26 +01:00
|
|
|
}
|
|
|
|
|
2021-06-30 04:32:10 +02:00
|
|
|
async function getOrCreateRollCheckMacro(check: Check): Promise<Macro | undefined> {
|
2021-07-08 08:00:58 +02:00
|
|
|
const command = `game.ds4.macros.rollCheck("${check}");`;
|
2021-03-29 21:20:08 +02:00
|
|
|
|
2021-07-07 19:22:35 +02:00
|
|
|
const existingMacro = getGame().macros?.find(
|
|
|
|
(m) => m.name === DS4.i18n.checks[check] && m.data.command === command,
|
|
|
|
);
|
2021-03-29 21:20:08 +02:00
|
|
|
if (existingMacro) {
|
|
|
|
return existingMacro;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Macro.create(
|
|
|
|
{
|
|
|
|
command,
|
|
|
|
name: DS4.i18n.checks[check],
|
|
|
|
type: "script",
|
2021-04-15 20:00:37 +02:00
|
|
|
img: DS4.icons.checks[check],
|
2021-03-29 21:20:08 +02:00
|
|
|
flags: { "ds4.checkMacro": true },
|
|
|
|
},
|
2021-06-30 04:32:10 +02:00
|
|
|
{ renderSheet: false },
|
2021-03-29 21:20:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:19:26 +01:00
|
|
|
/**
|
|
|
|
* Executes the roll check macro for the given check.
|
|
|
|
*/
|
|
|
|
export async function rollCheck(check: Check): Promise<void> {
|
2021-09-19 20:12:01 +02:00
|
|
|
const { actor, token } = getActiveActorAndToken();
|
2021-03-24 09:19:26 +01:00
|
|
|
if (!actor) {
|
2021-07-07 19:22:35 +02:00
|
|
|
return notifications.warn(getGame().i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
|
2021-03-24 09:19:26 +01:00
|
|
|
}
|
2021-03-29 21:20:08 +02:00
|
|
|
|
2021-09-19 20:12:01 +02:00
|
|
|
return actor.rollCheck(check, { speaker: { token } }).catch((e) => notifications.error(e, { log: true }));
|
2021-03-24 09:19:26 +01:00
|
|
|
}
|