2021-03-24 09:19:26 +01:00
|
|
|
import { Check } from "../actor/actor-prepared-data";
|
|
|
|
import { DS4 } from "../config";
|
|
|
|
import notifications from "../ui/notifications";
|
2021-03-29 21:20:08 +02:00
|
|
|
import { getActiveActor } from "./helpers";
|
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-03-24 09:19:26 +01:00
|
|
|
game.user?.assignHotbarMacro(macro, slot);
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:20:08 +02:00
|
|
|
async function getOrCreateRollCheckMacro(check: Check): Promise<Macro | null> {
|
|
|
|
const command = `game.ds4.macros.rollCheck("${check}");`;
|
|
|
|
|
|
|
|
const existingMacro = game.macros?.entities.find(
|
|
|
|
(m) => m.name === DS4.i18n.checks[check] && m.data.command === command,
|
|
|
|
);
|
|
|
|
if (existingMacro) {
|
|
|
|
return existingMacro;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Macro.create(
|
|
|
|
{
|
|
|
|
command,
|
|
|
|
name: DS4.i18n.checks[check],
|
|
|
|
type: "script",
|
|
|
|
// TODO: img, should be addressed in https://git.f3l.de/dungeonslayers/ds4/-/issues/79
|
|
|
|
flags: { "ds4.checkMacro": true },
|
|
|
|
},
|
|
|
|
{ displaySheet: false },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-03-29 21:20:08 +02:00
|
|
|
const actor = getActiveActor();
|
2021-03-24 09:19:26 +01:00
|
|
|
if (!actor) {
|
|
|
|
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToUseRollCheckMacro"));
|
|
|
|
}
|
2021-03-29 21:20:08 +02:00
|
|
|
|
2021-03-24 09:19:26 +01:00
|
|
|
return actor.rollCheck(check);
|
|
|
|
}
|