From e11c1ad9722aaaac6c5a1264f1b7908968897401 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Tue, 15 Jul 2025 22:06:34 +0200 Subject: [PATCH] Added itemUse macro on drag to hotbar --- daggerheart.mjs | 1 + module/applications/ui/_module.mjs | 1 + module/applications/ui/hotbar.mjs | 52 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 module/applications/ui/hotbar.mjs diff --git a/daggerheart.mjs b/daggerheart.mjs index 533691f5..259b246e 100644 --- a/daggerheart.mjs +++ b/daggerheart.mjs @@ -139,6 +139,7 @@ Hooks.once('init', () => { CONFIG.Combat.documentClass = documents.DhpCombat; CONFIG.ui.combat = applications.ui.DhCombatTracker; CONFIG.ui.chat = applications.ui.DhChatLog; + CONFIG.ui.hotbar = applications.ui.DhHotbar; CONFIG.Token.rulerClass = placeables.DhTokenRuler; CONFIG.ui.resources = applications.ui.DhFearTracker; diff --git a/module/applications/ui/_module.mjs b/module/applications/ui/_module.mjs index f1c32840..6a17a61e 100644 --- a/module/applications/ui/_module.mjs +++ b/module/applications/ui/_module.mjs @@ -2,3 +2,4 @@ export { default as DhChatLog } from './chatLog.mjs'; export { default as DhCombatTracker } from './combatTracker.mjs'; export * as DhCountdowns from './countdowns.mjs'; export { default as DhFearTracker } from './fearTracker.mjs'; +export { default as DhHotbar } from './hotbar.mjs'; diff --git a/module/applications/ui/hotbar.mjs b/module/applications/ui/hotbar.mjs new file mode 100644 index 00000000..8a272383 --- /dev/null +++ b/module/applications/ui/hotbar.mjs @@ -0,0 +1,52 @@ +export default class DhHotbar extends Hotbar { + constructor(options) { + super(options); + + this.setupHooks(); + } + + static async useItem(uuid) { + const item = await fromUuid(uuid); + if (!item) { + return ui.notifications.warn('WARNING.ObjectDoesNotExist', { + format: { + name: game.i18n.localize('Document'), + identifier: uuid + } + }); + } + + await item.use({}); + } + + setupHooks() { + Hooks.on('hotbarDrop', (bar, data, slot) => { + if (['Item'].includes(data.type)) { + const item = foundry.utils.fromUuidSync(data.uuid); + if (typeof item === 'string') return true; + + switch (item.type) { + case 'ancestry': + case 'community': + case 'class': + case 'subclass': + return true; + default: + this.createItemMacro(data, slot); + return false; + } + } + }); + } + + async createItemMacro(data, slot) { + const macro = await Macro.implementation.create({ + name: `${game.i18n.localize('Display')} ${name}`, + type: CONST.MACRO_TYPES.SCRIPT, + img: 'icons/svg/book.svg', + command: `await game.system.api.applications.ui.DhHotbar.useItem("${data.uuid}");` + }); + await game.user.assignHotbarMacro(macro, slot); + return false; + } +}