Add functionality to create item macros via drag & drop
This commit is contained in:
parent
07bcd3a58e
commit
212295069f
9 changed files with 128 additions and 9 deletions
|
@ -10,6 +10,8 @@ import { registerSystemSettings } from "./settings";
|
|||
import { migration } from "./migrations";
|
||||
import registerHandlebarsHelpers from "./handlebars/handlebars-helpers";
|
||||
import registerHandlebarsPartials from "./handlebars/handlebars-partials";
|
||||
import { macros } from "./macros/macros";
|
||||
import registerHooks from "./hooks/hooks";
|
||||
|
||||
Hooks.once("init", async () => {
|
||||
console.log(`DS4 | Initializing the DS4 Game System\n${DS4.ASCII}`);
|
||||
|
@ -20,6 +22,7 @@ Hooks.once("init", async () => {
|
|||
DS4,
|
||||
createCheckRoll,
|
||||
migration,
|
||||
macros,
|
||||
};
|
||||
|
||||
CONFIG.DS4 = DS4;
|
||||
|
@ -43,6 +46,8 @@ Hooks.once("init", async () => {
|
|||
|
||||
await registerHandlebarsPartials();
|
||||
registerHandlebarsHelpers();
|
||||
|
||||
registerHooks();
|
||||
});
|
||||
|
||||
/**
|
||||
|
|
6
src/module/helpers.ts
Normal file
6
src/module/helpers.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
export function getCanvas(): Canvas {
|
||||
if (!(canvas instanceof Canvas) || !canvas.ready) {
|
||||
throw new Error(game.i18n.localize("DS4.ErrorCanvasIsNotInitialized"));
|
||||
}
|
||||
return canvas;
|
||||
}
|
5
src/module/hooks/hooks.ts
Normal file
5
src/module/hooks/hooks.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import registerHotbarHook from "./hotbar-drop";
|
||||
|
||||
export default function registerHooks(): void {
|
||||
registerHotbarHook();
|
||||
}
|
28
src/module/hooks/hotbar-drop.ts
Normal file
28
src/module/hooks/hotbar-drop.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import { DS4Item } from "../item/item";
|
||||
import { DS4ItemData } from "../item/item-data";
|
||||
import { createItemMacro } from "../macros/item-macro";
|
||||
import notifications from "../ui/notifications";
|
||||
|
||||
export default function registerHotbarHook(): void {
|
||||
Hooks.on("hotbarDrop", async (hotbar: Hotbar, data: { type: string } & Record<string, unknown>, slot: string) => {
|
||||
switch (data.type) {
|
||||
case "Item": {
|
||||
if (!("data" in data)) {
|
||||
return notifications.warn(game.i18n.localize("DS4.WarningMacrosCanOnlyBeCreatedForOwnedItems"));
|
||||
}
|
||||
const itemData = data.data as DS4ItemData;
|
||||
|
||||
if (!DS4Item.rollableItemTypes.includes(itemData.type)) {
|
||||
return notifications.warn(
|
||||
game.i18n.format("DS4.WarningItemIsNotRollable", {
|
||||
name: itemData.name,
|
||||
id: itemData._id,
|
||||
type: itemData.type,
|
||||
}),
|
||||
);
|
||||
}
|
||||
await createItemMacro(itemData, slot);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
|
@ -2,7 +2,7 @@ import { DS4Actor } from "../actor/actor";
|
|||
import { DS4 } from "../config";
|
||||
import { createCheckRoll } from "../rolls/check-factory";
|
||||
import notifications from "../ui/notifications";
|
||||
import { AttackType, DS4ItemData } from "./item-data";
|
||||
import { AttackType, DS4ItemData, ItemType } from "./item-data";
|
||||
|
||||
/**
|
||||
* The Item class for DS4
|
||||
|
@ -40,6 +40,13 @@ export class DS4Item extends Item<DS4ItemData> {
|
|||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* The list of item types that are rollable.
|
||||
*/
|
||||
static get rollableItemTypes(): ItemType[] {
|
||||
return ["weapon", "spell"];
|
||||
}
|
||||
|
||||
/**
|
||||
* Roll a check for a action with this item.
|
||||
*/
|
||||
|
@ -71,8 +78,8 @@ export class DS4Item extends Item<DS4ItemData> {
|
|||
}
|
||||
|
||||
if (!this.data.data.equipped) {
|
||||
throw new Error(
|
||||
game.i18n.format("DS4.ErrorItemMustBeEquippedToBeRolled", {
|
||||
return notifications.warn(
|
||||
game.i18n.format("DS4.WarningItemMustBeEquippedToBeRolled", {
|
||||
name: this.name,
|
||||
id: this.id,
|
||||
type: this.data.type,
|
||||
|
@ -104,8 +111,8 @@ export class DS4Item extends Item<DS4ItemData> {
|
|||
}
|
||||
|
||||
if (!this.data.data.equipped) {
|
||||
throw new Error(
|
||||
game.i18n.format("DS4.ErrorItemMustBeEquippedToBeRolled", {
|
||||
return notifications.warn(
|
||||
game.i18n.format("DS4.WarningItemMustBeEquippedToBeRolled", {
|
||||
name: this.name,
|
||||
id: this.id,
|
||||
type: this.data.type,
|
||||
|
@ -158,7 +165,6 @@ export class DS4Item extends Item<DS4ItemData> {
|
|||
}
|
||||
return `${selectedAttackType}Attack` as const;
|
||||
},
|
||||
render: () => undefined, // TODO(types): This is actually optional, remove when types are updated )
|
||||
options: { jQuery: true },
|
||||
});
|
||||
return answer;
|
||||
|
|
54
src/module/macros/item-macro.ts
Normal file
54
src/module/macros/item-macro.ts
Normal 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();
|
||||
}
|
5
src/module/macros/macros.ts
Normal file
5
src/module/macros/macros.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { rollItemMacro } from "./item-macro";
|
||||
|
||||
export const macros = {
|
||||
rollItemMacro,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue