This commit is contained in:
François-Xavier Guillois
2023-05-17 17:39:50 +02:00
parent 96ef5c5d7f
commit 8e2e3d1ebf
9 changed files with 129 additions and 578 deletions
+5 -89
View File
@@ -14,6 +14,9 @@ import { TotemCreatureSheet } from "./sheets/creature-sheet.mjs";
import { TotemItem } from "./documents/item.mjs";
import { TotemItemSheet } from "./sheets/item-sheet.mjs";
import { TotemRoll } from "./system/roll.mjs";
import { TotemCombat } from "./system/fight.mjs";
// Import helper/utility classes and constants.
import { preloadHandlebarsTemplates, registerHandlebarsHelpers } from "./system/handlebars-manager.mjs";
import { TOTEM } from "./system/config.mjs";
@@ -31,7 +34,8 @@ Hooks.once('init', async function() {
TotemNpc,
TotemCreature,
TotemItem,
rollItemMacro
TotemRoll,
TotemCombat
};
// Add custom constants for configuration.
@@ -76,91 +80,3 @@ Hooks.once('init', async function() {
// Preload Handlebars templates.
return preloadHandlebarsTemplates();
});
/* -------------------------------------------- */
/* Handlebars Helpers */
/* -------------------------------------------- */
// If you need to add Handlebars helpers, here are a few useful examples:
Handlebars.registerHelper('concat', function() {
var outStr = '';
for (var arg in arguments) {
if (typeof arguments[arg] != 'object') {
outStr += arguments[arg];
}
}
return outStr;
});
Handlebars.registerHelper('toLowerCase', function(str) {
return str.toLowerCase();
});
/* -------------------------------------------- */
/* Ready Hook */
/* -------------------------------------------- */
Hooks.once("ready", async function() {
// Wait to register hotbar drop hook on ready so that modules could register earlier if they want to
Hooks.on("hotbarDrop", (bar, data, slot) => createItemMacro(data, slot));
});
/* -------------------------------------------- */
/* Hotbar Macros */
/* -------------------------------------------- */
/**
* Create a Macro from an Item drop.
* Get an existing item macro if one exists, otherwise create a new one.
* @param {Object} data The dropped data
* @param {number} slot The hotbar slot to use
* @returns {Promise}
*/
async function createItemMacro(data, slot) {
// First, determine if this is a valid owned item.
if (data.type !== "Item") return;
if (!data.uuid.includes('Actor.') && !data.uuid.includes('Token.')) {
return ui.notifications.warn("You can only create macro buttons for owned Items");
}
// If it is, retrieve it based on the uuid.
const item = await Item.fromDropData(data);
// Create the macro command using the uuid.
const command = `game.totem.rollItemMacro("${data.uuid}");`;
let macro = game.macros.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,
flags: { "totem.itemMacro": true }
});
}
game.user.assignHotbarMacro(macro, slot);
return false;
}
/**
* Create a Macro from an Item drop.
* Get an existing item macro if one exists, otherwise create a new one.
* @param {string} itemUuid
*/
function rollItemMacro(itemUuid) {
// Reconstruct the drop data so that we can load the item.
const dropData = {
type: 'Item',
uuid: itemUuid
};
// Load the item from the uuid.
Item.fromDropData(dropData).then(item => {
// Determine if the item loaded and if it's an owned item.
if (!item || !item.parent) {
const itemName = item?.name ?? itemUuid;
return ui.notifications.warn(`Could not find item ${itemName}. You may need to delete and recreate this macro.`);
}
// Trigger the item roll
item.roll();
});
}