Skip to content

Commit

Permalink
Merge branch 'hotbar' into 'master'
Browse files Browse the repository at this point in the history
Implement Hotbar support for Items.

See merge request hooking/foundry-vtt---pathfinder-2e!19
  • Loading branch information
shaun-newsome committed Feb 18, 2020
2 parents f0ed876 + 5152cd4 commit d8018d9
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
57 changes: 56 additions & 1 deletion scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ Hooks.once("init", () => {



game.pf2e = {
rollItemMacro,
};

// Pre-load templates
loadTemplates([

Expand Down Expand Up @@ -382,4 +386,55 @@ Hooks.on("canvasInit", async () => {
return spaces * canvas.dimensions.distance;

}
});
});


/* -------------------------------------------- */
/* Hotbar Macros */
/* -------------------------------------------- */

Hooks.on("hotbarDrop", (bar, data, slot) => {
if ( data.type !== "Item" ) return;
createItemMacro(data.data, slot);
return false;
});

/**
* Create a Macro from an Item drop.
* Get an existing item macro if one exists, otherwise create a new one.
* @param {Object} item The item data
* @param {number} slot The hotbar slot to use
* @returns {Promise}
*/
async function createItemMacro(item, slot) {
const command = `game.pf2e.rollItemMacro("${item._id}");`;
let macro = game.macros.entities.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: {"pf2e.itemMacro": true}
}, {displaySheet: false});
}
game.user.assignHotbarMacro(macro, slot);
}

/**
* Create a Macro from an Item drop.
* Get an existing item macro if one exists, otherwise create a new one.
* @param {string} itemName
* @return {Promise}
*/
function rollItemMacro(itemId) {
const speaker = ChatMessage.getSpeaker();
let actor;
if ( speaker.token ) actor = game.actors.tokens[speaker.token];
if ( !actor ) actor = game.actors.get(speaker.actor);
const item = actor ? actor.items.find(i => i._id === itemId) : null;
if ( !item ) return ui.notifications.warn(`Your controlled Actor does not have an item with ID ${itemId}`);

// Trigger the item roll
return item.roll();
}
2 changes: 1 addition & 1 deletion scripts/item/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ItemPF2e extends Item {
const nearestItem = event.currentTarget.closest(".item")
const templateData = {
actor: this.actor,
tokenId: token ? `${token.scene._id}.${token._id}` : null,
tokenId: token ? `${token.scene._id}.${token.id}` : null,
item: this.data,
data: this.getChatData(),
contextualItemData: nearestItem.dataset
Expand Down

0 comments on commit d8018d9

Please sign in to comment.