2021-06-26 22:02:00 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Johannes Loher
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-02-16 13:32:04 +01:00
|
|
|
import { isCheck } from "../actor/actor-data-properties-base";
|
2021-07-07 19:22:35 +02:00
|
|
|
import { getGame } from "../helpers";
|
2021-03-14 19:04:28 +01:00
|
|
|
import { DS4Item } from "../item/item";
|
2021-04-15 20:00:37 +02:00
|
|
|
import { createRollCheckMacro } from "../macros/roll-check";
|
2021-03-16 08:25:56 +01:00
|
|
|
import { createRollItemMacro } from "../macros/roll-item";
|
2021-03-14 19:04:28 +01:00
|
|
|
import notifications from "../ui/notifications";
|
|
|
|
|
2021-03-16 07:42:00 +01:00
|
|
|
export default function registerForHotbarDropHook(): void {
|
2021-06-30 03:53:52 +02:00
|
|
|
Hooks.on("hotbarDrop", async (hotbar: Hotbar, data: HotbarDropData, slot: string) => {
|
2021-03-14 19:04:28 +01:00
|
|
|
switch (data.type) {
|
|
|
|
case "Item": {
|
2021-06-30 03:53:52 +02:00
|
|
|
if (!isItemDropData(data) || !("data" in data)) {
|
2021-07-07 19:22:35 +02:00
|
|
|
return notifications.warn(
|
|
|
|
getGame().i18n.localize("DS4.WarningMacrosCanOnlyBeCreatedForOwnedItems"),
|
|
|
|
);
|
2021-03-14 19:04:28 +01:00
|
|
|
}
|
2021-06-30 03:53:52 +02:00
|
|
|
const itemData = data.data;
|
2021-03-14 19:04:28 +01:00
|
|
|
|
|
|
|
if (!DS4Item.rollableItemTypes.includes(itemData.type)) {
|
|
|
|
return notifications.warn(
|
2021-07-07 19:22:35 +02:00
|
|
|
getGame().i18n.format("DS4.WarningItemIsNotRollable", {
|
2021-03-14 19:04:28 +01:00
|
|
|
name: itemData.name,
|
|
|
|
id: itemData._id,
|
|
|
|
type: itemData.type,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2021-04-15 20:00:37 +02:00
|
|
|
return createRollItemMacro(itemData, slot);
|
|
|
|
}
|
|
|
|
case "Check": {
|
|
|
|
if (!("data" in data) || typeof data.data !== "string" || !isCheck(data.data)) {
|
2021-07-07 19:22:35 +02:00
|
|
|
return notifications.warn(getGame().i18n.localize("DS4.WarningInvalidCheckDropped"));
|
2021-04-15 20:00:37 +02:00
|
|
|
}
|
|
|
|
return createRollCheckMacro(data.data, slot);
|
2021-03-14 19:04:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-06-30 03:53:52 +02:00
|
|
|
|
|
|
|
type HotbarDropData = ActorSheet.DropData.Item | ({ type: string } & Partial<Record<string, unknown>>);
|
|
|
|
|
|
|
|
function isItemDropData(dropData: HotbarDropData): dropData is ActorSheet.DropData.Item {
|
|
|
|
return dropData.type === "Item";
|
|
|
|
}
|