Simplify macro creation for formula attributes

This commit is contained in:
Andrew 2020-10-19 18:30:53 -07:00
parent 6bf056b3d4
commit 5560fc24d4
2 changed files with 5 additions and 31 deletions

View file

@ -6,42 +6,17 @@
* @returns {Promise} * @returns {Promise}
*/ */
export async function createWorldbuildingMacro(data, slot) { export async function createWorldbuildingMacro(data, slot) {
const item = data; const command = `const roll = new Roll("${data.roll}", actor ? actor.getRollData() : {});
roll.toMessage({speaker, flavor: "${data.label}"});`;
// Create the macro command
const command = `game.worldbuilding.rollAttrMacro("${item.label}", "${item.roll}");`;
let macro = game.macros.entities.find(m => (m.name === item.label) && (m.command === command)); let macro = game.macros.entities.find(m => (m.name === item.label) && (m.command === command));
if (!macro) { if (!macro) {
macro = await Macro.create({ macro = await Macro.create({
name: item.label, name: data.label,
type: "script", type: "script",
command: command, command: command,
flags: { "worldbuilding.attrMacro": true } flags: { "worldbuilding.attrMacro": true }
}); });
} }
game.user.assignHotbarMacro(macro, slot); game.user.assignHotbarMacro(macro, slot);
return false; return false;
} }
/**
* 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}
*/
export function rollAttrMacro(attrName, attrFormula) {
let actor;
// Get the speaker and actor if not provided.
const speaker = ChatMessage.getSpeaker({ actor: this.actor });
if (speaker.token) actor = game.actors.tokens[speaker.token];
if (!actor) actor = game.actors.get(speaker.actor);
// Create the roll.
let r = new Roll(attrFormula, actor.getRollData());
r.roll().toMessage({
user: game.user._id,
speaker: speaker,
flavor: attrName
});
}

View file

@ -9,7 +9,7 @@ import { SimpleActor } from "./actor.js";
import { SimpleItemSheet } from "./item-sheet.js"; import { SimpleItemSheet } from "./item-sheet.js";
import { SimpleActorSheet } from "./actor-sheet.js"; import { SimpleActorSheet } from "./actor-sheet.js";
import { preloadHandlebarsTemplates } from "./templates.js"; import { preloadHandlebarsTemplates } from "./templates.js";
import { createWorldbuildingMacro, rollAttrMacro } from "./macro.js"; import { createWorldbuildingMacro } from "./macro.js";
/* -------------------------------------------- */ /* -------------------------------------------- */
/* Foundry VTT Initialization */ /* Foundry VTT Initialization */
@ -32,8 +32,7 @@ Hooks.once("init", async function() {
game.worldbuilding = { game.worldbuilding = {
SimpleActor, SimpleActor,
createWorldbuildingMacro, createWorldbuildingMacro
rollAttrMacro,
}; };
// Define custom Entity classes // Define custom Entity classes