foundryvtt-reve-de-dragon/module/rdd-hotbar-drop.js

99 lines
2.9 KiB
JavaScript
Raw Normal View History

2023-10-18 21:08:01 +02:00
import { TYPES } from "./item.js";
export class RdDHotbar {
2023-10-04 09:33:24 +02:00
static async createItemMacro(item, slot, armeCompetence = undefined) {
let command = `game.system.rdd.RdDHotbar.rollMacro("${item.name}", "${item.type}"` + ((armeCompetence) ? `, "${armeCompetence}");` : `);`);
2022-09-25 21:21:28 +02:00
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);
}
2023-10-04 09:33:24 +02:00
static async addToHotbar(item, slot) {
2023-10-18 21:08:01 +02:00
switch (item?.type ?? "") {
case TYPES.arme:
{
// Les armes peuvent avoir plusieurs usages
if (item.system.competence != "") {
await this.createItemMacro(item, slot, "competence")
slot++
}
if (item.system.lancer != "") {
await this.createItemMacro(item, slot, "lancer")
slot++
}
if (item.system.tir != "") {
await this.createItemMacro(item, slot, "lancer")
slot++
}
}
return
case TYPES.competence:
case TYPES.competencecreature:
default:
await this.createItemMacro(item, slot)
return
2023-10-04 09:33:24 +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
*/
2022-09-07 09:14:42 +02:00
static initDropbar() {
2022-09-25 21:21:28 +02:00
Hooks.on("hotbarDrop", (bar, documentData, slot) => {
2022-09-06 23:52:21 +02:00
2022-09-07 09:14:42 +02:00
// Create item macro if rollable item - weapon, spell, prayer, trait, or skill
if (documentData.type == "Item") {
2023-10-18 21:08:01 +02:00
const item = fromUuidSync(documentData.uuid) ?? this.actor.items.get(documentData.uuid)
2022-09-07 09:14:42 +02:00
console.log("DROP", documentData, item)
2023-10-18 21:08:01 +02:00
switch (item?.type ?? "")
{
case TYPES.arme:
case TYPES.competence:
case TYPES.competencecreature:
this.addToHotbar(item, slot)
return false
2022-09-07 09:14:42 +02:00
}
}
2022-09-06 23:52:21 +02:00
2022-09-25 21:17:47 +02:00
return true
2022-09-25 21:21:28 +02:00
})
}
/** Roll macro */
2023-10-04 09:33:24 +02:00
static rollMacro(itemName, itemType, competenceName) {
2022-09-25 21:21:28 +02:00
const speaker = ChatMessage.getSpeaker();
let actor;
if (speaker.token) actor = game.actors.tokens[speaker.token];
if (!actor) actor = game.actors.get(speaker.actor);
2023-10-18 21:08:01 +02:00
if (!actor) {
return ui.notifications.warn(`Impossible de trouver le personnage concerné`);
}
2022-09-25 21:21:28 +02:00
let item = actor?.items.find(it => it.name === itemName && it.type == itemType) ?? undefined;
if (!item) {
return ui.notifications.warn(`Impossible de trouver l'objet de cette macro`);
}
2022-09-25 21:21:28 +02:00
// Trigger the item roll
switch (item.type) {
2023-10-18 21:08:01 +02:00
case TYPES.arme:
2023-10-04 09:33:24 +02:00
return actor.rollArme(item, competenceName);
2023-10-18 21:08:01 +02:00
case TYPES.competence:
case TYPES.competencecreature:
2022-09-25 21:21:28 +02:00
return actor.rollCompetence(itemName);
}
}
}