boilerplate
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Extend the base Actor document by defining a custom roll data structure which is ideal for the Simple system.
|
||||
* @extends {Actor}
|
||||
*/
|
||||
export class TotemActor extends Actor {
|
||||
|
||||
/** @override */
|
||||
prepareData() {
|
||||
// Prepare data for the actor. Calling the super version of this executes
|
||||
// the following, in order: data reset (to clear active effects),
|
||||
// prepareBaseData(), prepareEmbeddedDocuments() (including active effects),
|
||||
// prepareDerivedData().
|
||||
super.prepareData();
|
||||
}
|
||||
|
||||
/** @override */
|
||||
prepareBaseData() {
|
||||
// Data modifications in this step occur before processing embedded
|
||||
// documents or derived data.
|
||||
}
|
||||
|
||||
/**
|
||||
* @override
|
||||
* Augment the basic actor data with additional dynamic data. Typically,
|
||||
* you'll want to handle most of your calculated/derived data in this step.
|
||||
* Data calculated in this step should generally not exist in template.json
|
||||
* (such as ability modifiers rather than ability scores) and should be
|
||||
* available both inside and outside of character sheets (such as if an actor
|
||||
* is queried and has a roll executed directly from it).
|
||||
*/
|
||||
prepareDerivedData() {
|
||||
const actorData = this;
|
||||
const systemData = actorData.system;
|
||||
const flags = actorData.flags.totem || {};
|
||||
|
||||
// Make separate methods for each Actor type (character, npc, etc.) to keep
|
||||
// things organized.
|
||||
this._prepareCharacterData(actorData);
|
||||
this._prepareNpcData(actorData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare Character type specific data
|
||||
*/
|
||||
_prepareCharacterData(actorData) {
|
||||
if (actorData.type !== 'character') return;
|
||||
|
||||
// Make modifications to data here. For example:
|
||||
const systemData = actorData.system;
|
||||
|
||||
// Loop through ability scores, and add their modifiers to our sheet output.
|
||||
for (let [key, ability] of Object.entries(systemData.abilities)) {
|
||||
// Calculate the modifier using d20 rules.
|
||||
ability.mod = Math.floor((ability.value - 10) / 2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare NPC type specific data.
|
||||
*/
|
||||
_prepareNpcData(actorData) {
|
||||
if (actorData.type !== 'npc') return;
|
||||
|
||||
// Make modifications to data here. For example:
|
||||
const systemData = actorData.system;
|
||||
systemData.xp = (systemData.cr * systemData.cr) * 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override getRollData() that's supplied to rolls.
|
||||
*/
|
||||
getRollData() {
|
||||
const data = super.getRollData();
|
||||
|
||||
// Prepare character roll data.
|
||||
this._getCharacterRollData(data);
|
||||
this._getNpcRollData(data);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare character roll data.
|
||||
*/
|
||||
_getCharacterRollData(data) {
|
||||
if (this.type !== 'character') return;
|
||||
|
||||
// Copy the ability scores to the top level, so that rolls can use
|
||||
// formulas like `@str.mod + 4`.
|
||||
if (data.abilities) {
|
||||
for (let [k, v] of Object.entries(data.abilities)) {
|
||||
data[k] = foundry.utils.deepClone(v);
|
||||
}
|
||||
}
|
||||
|
||||
// Add level for easier access, or fall back to 0.
|
||||
if (data.attributes.level) {
|
||||
data.lvl = data.attributes.level.value ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare NPC roll data.
|
||||
*/
|
||||
_getNpcRollData(data) {
|
||||
if (this.type !== 'npc') return;
|
||||
|
||||
// Process additional NPC data here.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* Extend the basic Item with some very simple modifications.
|
||||
* @extends {Item}
|
||||
*/
|
||||
export class TotemItem extends Item {
|
||||
/**
|
||||
* Augment the basic Item data model with additional dynamic data.
|
||||
*/
|
||||
prepareData() {
|
||||
// As with the actor class, items are documents that can have their data
|
||||
// preparation methods overridden (such as prepareBaseData()).
|
||||
super.prepareData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare a data object which is passed to any Roll formulas which are created related to this Item
|
||||
* @private
|
||||
*/
|
||||
getRollData() {
|
||||
// If present, return the actor's roll data.
|
||||
if ( !this.actor ) return null;
|
||||
const rollData = this.actor.getRollData();
|
||||
// Grab the item's system data as well.
|
||||
rollData.item = foundry.utils.deepClone(this.system);
|
||||
|
||||
return rollData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle clickable rolls.
|
||||
* @param {Event} event The originating click event
|
||||
* @private
|
||||
*/
|
||||
async roll() {
|
||||
const item = this;
|
||||
|
||||
// Initialize chat data.
|
||||
const speaker = ChatMessage.getSpeaker({ actor: this.actor });
|
||||
const rollMode = game.settings.get('core', 'rollMode');
|
||||
const label = `[${item.type}] ${item.name}`;
|
||||
|
||||
// If there's no roll data, send a chat message.
|
||||
if (!this.system.formula) {
|
||||
ChatMessage.create({
|
||||
speaker: speaker,
|
||||
rollMode: rollMode,
|
||||
flavor: label,
|
||||
content: item.system.description ?? ''
|
||||
});
|
||||
}
|
||||
// Otherwise, create a roll and send a chat message from it.
|
||||
else {
|
||||
// Retrieve roll data.
|
||||
const rollData = this.getRollData();
|
||||
|
||||
// Invoke the roll and submit it to chat.
|
||||
const roll = new Roll(rollData.item.formula, rollData);
|
||||
// If you need to store the value first, uncomment the next line.
|
||||
// let result = await roll.roll({async: true});
|
||||
roll.toMessage({
|
||||
speaker: speaker,
|
||||
rollMode: rollMode,
|
||||
flavor: label,
|
||||
});
|
||||
return roll;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
export const TOTEM = {};
|
||||
|
||||
/**
|
||||
* The set of Ability Scores used within the sytem.
|
||||
* @type {Object}
|
||||
*/
|
||||
TOTEM.abilities = {
|
||||
"str": "TOTEM.AbilityStr",
|
||||
"dex": "TOTEM.AbilityDex",
|
||||
"con": "TOTEM.AbilityCon",
|
||||
"int": "TOTEM.AbilityInt",
|
||||
"wis": "TOTEM.AbilityWis",
|
||||
"cha": "TOTEM.AbilityCha"
|
||||
};
|
||||
|
||||
TOTEM.abilityAbbreviations = {
|
||||
"str": "TOTEM.AbilityStrAbbr",
|
||||
"dex": "TOTEM.AbilityDexAbbr",
|
||||
"con": "TOTEM.AbilityConAbbr",
|
||||
"int": "TOTEM.AbilityIntAbbr",
|
||||
"wis": "TOTEM.AbilityWisAbbr",
|
||||
"cha": "TOTEM.AbilityChaAbbr"
|
||||
};
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Manage Active Effect instances through the Actor Sheet via effect control buttons.
|
||||
* @param {MouseEvent} event The left-click event on the effect control
|
||||
* @param {Actor|Item} owner The owning document which manages this effect
|
||||
*/
|
||||
export function onManageActiveEffect(event, owner) {
|
||||
event.preventDefault();
|
||||
const a = event.currentTarget;
|
||||
const li = a.closest("li");
|
||||
const effect = li.dataset.effectId ? owner.effects.get(li.dataset.effectId) : null;
|
||||
switch ( a.dataset.action ) {
|
||||
case "create":
|
||||
return owner.createEmbeddedDocuments("ActiveEffect", [{
|
||||
label: "New Effect",
|
||||
icon: "icons/svg/aura.svg",
|
||||
origin: owner.uuid,
|
||||
"duration.rounds": li.dataset.effectType === "temporary" ? 1 : undefined,
|
||||
disabled: li.dataset.effectType === "inactive"
|
||||
}]);
|
||||
case "edit":
|
||||
return effect.sheet.render(true);
|
||||
case "delete":
|
||||
return effect.delete();
|
||||
case "toggle":
|
||||
return effect.update({disabled: !effect.disabled});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the data structure for Active Effects which are currently applied to an Actor or Item.
|
||||
* @param {ActiveEffect[]} effects The array of Active Effect instances to prepare sheet data for
|
||||
* @return {object} Data for rendering
|
||||
*/
|
||||
export function prepareActiveEffectCategories(effects) {
|
||||
|
||||
// Define effect header categories
|
||||
const categories = {
|
||||
temporary: {
|
||||
type: "temporary",
|
||||
label: "Temporary Effects",
|
||||
effects: []
|
||||
},
|
||||
passive: {
|
||||
type: "passive",
|
||||
label: "Passive Effects",
|
||||
effects: []
|
||||
},
|
||||
inactive: {
|
||||
type: "inactive",
|
||||
label: "Inactive Effects",
|
||||
effects: []
|
||||
}
|
||||
};
|
||||
|
||||
// Iterate over active effects, classifying them into categories
|
||||
for ( let e of effects ) {
|
||||
e._getSourceName(); // Trigger a lookup for the source name
|
||||
if ( e.disabled ) categories.inactive.effects.push(e);
|
||||
else if ( e.isTemporary ) categories.temporary.effects.push(e);
|
||||
else categories.passive.effects.push(e);
|
||||
}
|
||||
return categories;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Define a set of template paths to pre-load
|
||||
* Pre-loaded templates are compiled and cached for fast access when rendering
|
||||
* @return {Promise}
|
||||
*/
|
||||
export const preloadHandlebarsTemplates = async function() {
|
||||
return loadTemplates([
|
||||
|
||||
// Actor partials.
|
||||
"systems/totem/templates/actor/parts/actor-features.html",
|
||||
"systems/totem/templates/actor/parts/actor-items.html",
|
||||
"systems/totem/templates/actor/parts/actor-spells.html",
|
||||
"systems/totem/templates/actor/parts/actor-effects.html",
|
||||
]);
|
||||
};
|
||||
@@ -0,0 +1,229 @@
|
||||
import {onManageActiveEffect, prepareActiveEffectCategories} from "../helpers/effects.mjs";
|
||||
|
||||
/**
|
||||
* Extend the basic ActorSheet with some very simple modifications
|
||||
* @extends {ActorSheet}
|
||||
*/
|
||||
export class TotemActorSheet extends ActorSheet {
|
||||
|
||||
/** @override */
|
||||
static get defaultOptions() {
|
||||
return mergeObject(super.defaultOptions, {
|
||||
classes: ["totem", "sheet", "actor"],
|
||||
template: "systems/totem/templates/actor/actor-sheet.html",
|
||||
width: 600,
|
||||
height: 600,
|
||||
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "features" }]
|
||||
});
|
||||
}
|
||||
|
||||
/** @override */
|
||||
get template() {
|
||||
return `systems/totem/templates/actor/actor-${this.actor.type}-sheet.html`;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
getData() {
|
||||
// Retrieve the data structure from the base sheet. You can inspect or log
|
||||
// the context variable to see the structure, but some key properties for
|
||||
// sheets are the actor object, the data object, whether or not it's
|
||||
// editable, the items array, and the effects array.
|
||||
const context = super.getData();
|
||||
|
||||
// Use a safe clone of the actor data for further operations.
|
||||
const actorData = this.actor.toObject(false);
|
||||
|
||||
// Add the actor's data to context.data for easier access, as well as flags.
|
||||
context.system = actorData.system;
|
||||
context.flags = actorData.flags;
|
||||
|
||||
// Prepare character data and items.
|
||||
if (actorData.type == 'character') {
|
||||
this._prepareItems(context);
|
||||
this._prepareCharacterData(context);
|
||||
}
|
||||
|
||||
// Prepare NPC data and items.
|
||||
if (actorData.type == 'npc') {
|
||||
this._prepareItems(context);
|
||||
}
|
||||
|
||||
// Add roll data for TinyMCE editors.
|
||||
context.rollData = context.actor.getRollData();
|
||||
|
||||
// Prepare active effects
|
||||
context.effects = prepareActiveEffectCategories(this.actor.effects);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Organize and classify Items for Character sheets.
|
||||
*
|
||||
* @param {Object} actorData The actor to prepare.
|
||||
*
|
||||
* @return {undefined}
|
||||
*/
|
||||
_prepareCharacterData(context) {
|
||||
// Handle ability scores.
|
||||
for (let [k, v] of Object.entries(context.system.abilities)) {
|
||||
v.label = game.i18n.localize(CONFIG.TOTEM.abilities[k]) ?? k;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Organize and classify Items for Character sheets.
|
||||
*
|
||||
* @param {Object} actorData The actor to prepare.
|
||||
*
|
||||
* @return {undefined}
|
||||
*/
|
||||
_prepareItems(context) {
|
||||
// Initialize containers.
|
||||
const gear = [];
|
||||
const features = [];
|
||||
const spells = {
|
||||
0: [],
|
||||
1: [],
|
||||
2: [],
|
||||
3: [],
|
||||
4: [],
|
||||
5: [],
|
||||
6: [],
|
||||
7: [],
|
||||
8: [],
|
||||
9: []
|
||||
};
|
||||
|
||||
// Iterate through items, allocating to containers
|
||||
for (let i of context.items) {
|
||||
i.img = i.img || DEFAULT_TOKEN;
|
||||
// Append to gear.
|
||||
if (i.type === 'item') {
|
||||
gear.push(i);
|
||||
}
|
||||
// Append to features.
|
||||
else if (i.type === 'feature') {
|
||||
features.push(i);
|
||||
}
|
||||
// Append to spells.
|
||||
else if (i.type === 'spell') {
|
||||
if (i.system.spellLevel != undefined) {
|
||||
spells[i.system.spellLevel].push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Assign and return
|
||||
context.gear = gear;
|
||||
context.features = features;
|
||||
context.spells = spells;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
activateListeners(html) {
|
||||
super.activateListeners(html);
|
||||
|
||||
// Render the item sheet for viewing/editing prior to the editable check.
|
||||
html.find('.item-edit').click(ev => {
|
||||
const li = $(ev.currentTarget).parents(".item");
|
||||
const item = this.actor.items.get(li.data("itemId"));
|
||||
item.sheet.render(true);
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------
|
||||
// Everything below here is only needed if the sheet is editable
|
||||
if (!this.isEditable) return;
|
||||
|
||||
// Add Inventory Item
|
||||
html.find('.item-create').click(this._onItemCreate.bind(this));
|
||||
|
||||
// Delete Inventory Item
|
||||
html.find('.item-delete').click(ev => {
|
||||
const li = $(ev.currentTarget).parents(".item");
|
||||
const item = this.actor.items.get(li.data("itemId"));
|
||||
item.delete();
|
||||
li.slideUp(200, () => this.render(false));
|
||||
});
|
||||
|
||||
// Active Effect management
|
||||
html.find(".effect-control").click(ev => onManageActiveEffect(ev, this.actor));
|
||||
|
||||
// Rollable abilities.
|
||||
html.find('.rollable').click(this._onRoll.bind(this));
|
||||
|
||||
// Drag events for macros.
|
||||
if (this.actor.isOwner) {
|
||||
let handler = ev => this._onDragStart(ev);
|
||||
html.find('li.item').each((i, li) => {
|
||||
if (li.classList.contains("inventory-header")) return;
|
||||
li.setAttribute("draggable", true);
|
||||
li.addEventListener("dragstart", handler, false);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle creating a new Owned Item for the actor using initial data defined in the HTML dataset
|
||||
* @param {Event} event The originating click event
|
||||
* @private
|
||||
*/
|
||||
async _onItemCreate(event) {
|
||||
event.preventDefault();
|
||||
const header = event.currentTarget;
|
||||
// Get the type of item to create.
|
||||
const type = header.dataset.type;
|
||||
// Grab any data associated with this control.
|
||||
const data = duplicate(header.dataset);
|
||||
// Initialize a default name.
|
||||
const name = `New ${type.capitalize()}`;
|
||||
// Prepare the item object.
|
||||
const itemData = {
|
||||
name: name,
|
||||
type: type,
|
||||
system: data
|
||||
};
|
||||
// Remove the type from the dataset since it's in the itemData.type prop.
|
||||
delete itemData.system["type"];
|
||||
|
||||
// Finally, create the item!
|
||||
return await Item.create(itemData, {parent: this.actor});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle clickable rolls.
|
||||
* @param {Event} event The originating click event
|
||||
* @private
|
||||
*/
|
||||
_onRoll(event) {
|
||||
event.preventDefault();
|
||||
const element = event.currentTarget;
|
||||
const dataset = element.dataset;
|
||||
|
||||
// Handle item rolls.
|
||||
if (dataset.rollType) {
|
||||
if (dataset.rollType == 'item') {
|
||||
const itemId = element.closest('.item').dataset.itemId;
|
||||
const item = this.actor.items.get(itemId);
|
||||
if (item) return item.roll();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle rolls that supply the formula directly.
|
||||
if (dataset.roll) {
|
||||
let label = dataset.label ? `[ability] ${dataset.label}` : '';
|
||||
let roll = new Roll(dataset.roll, this.actor.getRollData());
|
||||
roll.toMessage({
|
||||
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
|
||||
flavor: label,
|
||||
rollMode: game.settings.get('core', 'rollMode'),
|
||||
});
|
||||
return roll;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/**
|
||||
* Extend the basic ItemSheet with some very simple modifications
|
||||
* @extends {ItemSheet}
|
||||
*/
|
||||
export class TotemItemSheet extends ItemSheet {
|
||||
|
||||
/** @override */
|
||||
static get defaultOptions() {
|
||||
return mergeObject(super.defaultOptions, {
|
||||
classes: ["totem", "sheet", "item"],
|
||||
width: 520,
|
||||
height: 480,
|
||||
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }]
|
||||
});
|
||||
}
|
||||
|
||||
/** @override */
|
||||
get template() {
|
||||
const path = "systems/totem/templates/item";
|
||||
// Return a single sheet for all item types.
|
||||
// return `${path}/item-sheet.html`;
|
||||
|
||||
// Alternatively, you could use the following return statement to do a
|
||||
// unique item sheet by type, like `weapon-sheet.html`.
|
||||
return `${path}/item-${this.item.type}-sheet.html`;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
getData() {
|
||||
// Retrieve base data structure.
|
||||
const context = super.getData();
|
||||
|
||||
// Use a safe clone of the item data for further operations.
|
||||
const itemData = context.item;
|
||||
|
||||
// Retrieve the roll data for TinyMCE editors.
|
||||
context.rollData = {};
|
||||
let actor = this.object?.parent ?? null;
|
||||
if (actor) {
|
||||
context.rollData = actor.getRollData();
|
||||
}
|
||||
|
||||
// Add the actor's data to context.data for easier access, as well as flags.
|
||||
context.system = itemData.system;
|
||||
context.flags = itemData.flags;
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
/* -------------------------------------------- */
|
||||
|
||||
/** @override */
|
||||
activateListeners(html) {
|
||||
super.activateListeners(html);
|
||||
|
||||
// Everything below here is only needed if the sheet is editable
|
||||
if (!this.isEditable) return;
|
||||
|
||||
// Roll handlers, click handlers, etc. would go here.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// Import document classes.
|
||||
import { TotemActor } from "./documents/actor.mjs";
|
||||
import { TotemItem } from "./documents/item.mjs";
|
||||
// Import sheet classes.
|
||||
import { TotemActorSheet } from "./sheets/actor-sheet.mjs";
|
||||
import { TotemItemSheet } from "./sheets/item-sheet.mjs";
|
||||
// Import helper/utility classes and constants.
|
||||
import { preloadHandlebarsTemplates } from "./helpers/templates.mjs";
|
||||
import { TOTEM } from "./helpers/config.mjs";
|
||||
|
||||
/* -------------------------------------------- */
|
||||
/* Init Hook */
|
||||
/* -------------------------------------------- */
|
||||
|
||||
Hooks.once('init', async function() {
|
||||
|
||||
// Add utility classes to the global game object so that they're more easily
|
||||
// accessible in global contexts.
|
||||
game.totem = {
|
||||
TotemActor,
|
||||
TotemItem,
|
||||
rollItemMacro
|
||||
};
|
||||
|
||||
// Add custom constants for configuration.
|
||||
CONFIG.TOTEM = TOTEM;
|
||||
|
||||
/**
|
||||
* Set an initiative formula for the system
|
||||
* @type {String}
|
||||
*/
|
||||
CONFIG.Combat.initiative = {
|
||||
formula: "1d20 + @abilities.dex.mod",
|
||||
decimals: 2
|
||||
};
|
||||
|
||||
// Define custom Document classes
|
||||
CONFIG.Actor.documentClass = TotemActor;
|
||||
CONFIG.Item.documentClass = TotemItem;
|
||||
|
||||
// Register sheet application classes
|
||||
Actors.unregisterSheet("core", ActorSheet);
|
||||
Actors.registerSheet("totem", TotemActorSheet, { makeDefault: true });
|
||||
Items.unregisterSheet("core", ItemSheet);
|
||||
Items.registerSheet("totem", TotemItemSheet, { makeDefault: true });
|
||||
|
||||
// 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();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user