bol/module/system/bol-hotbar.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-03-27 22:56:43 +02:00
import { BoLRoll } from "../controllers/bol-rolls.js";
export class BoLHotbar {
2022-09-25 21:13:15 +02:00
static async assignToHotBar( item, slot) {
let command = `game.bol.BoLHotbar.rollMacro("${item.name}", "${item.type}");`
let macro = game.macros.contents.find(m => (m.name === item.name) && (m.command === command))
if (!macro) {
macro = await Macro.create({
name: item.name,
type: "script",
img: item.img,
command: command
}, { displaySheet: false })
}
await game.user.assignHotbarMacro(macro, slot);
}
2022-03-27 22:56:43 +02:00
/**
* Create a macro when dropping an entity on the hotbar
* Item - open roll dialog for item
* Actor - open actor sheet
* Journal - open journal sheet
*/
static init( ) {
2022-09-25 21:13:15 +02:00
Hooks.on("hotbarDrop", (bar, documentData, slot) => {
2022-03-27 22:56:43 +02:00
// Create item macro if rollable item - weapon, spell, prayer, trait, or skill
if (documentData.type == "Item") {
2022-09-25 21:13:15 +02:00
let item = fromUuidSync(documentData.uuid)
if (item == undefined) {
item = this.actor.items.get(documentData.uuid)
2022-03-27 22:56:43 +02:00
}
2022-09-25 21:13:15 +02:00
if (item && (item.system.subtype === "weapon" || item.system.category === "spell")) {
this.assignToHotBar( item, slot )
return false
2022-03-27 22:56:43 +02:00
}
}
2022-09-25 21:13:15 +02:00
return true
})
2022-03-27 22:56:43 +02:00
}
/** Roll macro */
static rollMacro(itemName, itemType, bypassData) {
const speaker = ChatMessage.getSpeaker()
let actor
if (speaker.token) actor = game.actors.tokens[speaker.token]
if (!actor) actor = game.actors.get(speaker.actor)
if (!actor) {
2022-11-25 20:47:28 +01:00
return ui.notifications.warn( game.i18n.localize("BOL.ui.selectactor") )
2022-03-27 22:56:43 +02:00
}
let item = actor.items.find(it => it.name === itemName && it.type == itemType)
if (!item ) {
2022-11-25 20:47:28 +01:00
return ui.notifications.warn( game.i18n.localize("BOL.ui.itemnotfound") )
2022-03-27 22:56:43 +02:00
}
// Trigger the item roll
2022-09-25 21:13:15 +02:00
if (item.system.category === "equipment" && item.system.subtype === "weapon") {
2022-03-27 22:56:43 +02:00
return BoLRoll.weaponCheckWithWeapon( actor, item)
}
2022-09-25 21:13:15 +02:00
if (item.system.category === "spell") {
2022-03-27 22:56:43 +02:00
return BoLRoll.spellCheckWithSpell( actor, item)
}
}
}