Add functionality to create item macros via drag & drop

This commit is contained in:
Johannes Loher 2021-03-14 19:04:28 +01:00
parent 07bcd3a58e
commit 212295069f
9 changed files with 128 additions and 9 deletions

View file

@ -0,0 +1,54 @@
import { DS4Actor } from "../actor/actor";
import { getCanvas } from "../helpers";
import { DS4ItemData } from "../item/item-data";
import notifications from "../ui/notifications";
/**
* Create a Macro from an item drop.
* Get an existing item macro if one exists, otherwise create a new one.
* @param itemData - The item data
* @param slot - The hotbar slot to use
*/
export async function createItemMacro(itemData: DS4ItemData, slot: string): Promise<void> {
const command = `game.ds4.macros.rollItemMacro("${itemData._id}");`;
const macro =
game.macros?.entities.find((m) => m.name === itemData.name && m.data.command === command) ??
(await Macro.create(
{
command,
name: itemData.name,
type: "script",
img: itemData.img,
flags: { "ds4.itemMacro": true },
},
{ displaySheet: false },
));
game.user?.assignHotbarMacro(macro, (slot as unknown) as number); // TODO(types): adjust so that cast is not needed, slot should be an optional string
}
/**
* Execute the item macro for the given itemId.
* @param itemId
*/
export async function rollItemMacro(itemId: string): Promise<void> {
const speaker = ChatMessage.getSpeaker();
const actor = (getCanvas().tokens.get(speaker.token ?? "")?.actor ?? game.actors?.get(speaker.actor ?? "")) as
| DS4Actor
| null
| undefined;
if (!actor) {
return notifications.warn(game.i18n.localize("DS4.WarningMustControlActorToRollItemMacro"));
}
const item = actor.items?.get(itemId);
if (!item) {
return notifications.warn(
game.i18n.format("DS4.WarningControlledActorDoesNotHaveItem", {
actorName: actor.name,
actorId: actor.id,
itemId,
}),
);
}
return item.roll();
}

View file

@ -0,0 +1,5 @@
import { rollItemMacro } from "./item-macro";
export const macros = {
rollItemMacro,
};