From c6d3c094b88a8ca2a6d47ba58ec72b4ccd67d425 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 2 May 2026 19:23:20 -0600 Subject: [PATCH] Add an extra paragraph to the Macro deletion dialog in order to tell people how many items use that macro (closes #81) --- langs/en-ca.json | 1 + module/documents/Macro.mjs | 39 ++++++++++++++++++++++++++++++++++++++ module/hooks/init.mjs | 2 ++ 3 files changed, 42 insertions(+) create mode 100644 module/documents/Macro.mjs diff --git a/langs/en-ca.json b/langs/en-ca.json index bf97c0c..daef559 100644 --- a/langs/en-ca.json +++ b/langs/en-ca.json @@ -64,6 +64,7 @@ "edit": "Edit", "resizable": "Resizable", "not-resizable": "Not Resizable", + "macro-is-in-use": "The Macro is used by {count} items/attributes in the world or Actors, the items will lose their ability to use the macro.", "item": { "weight": "Weight", "quantity": "Quantity", diff --git a/module/documents/Macro.mjs b/module/documents/Macro.mjs new file mode 100644 index 0000000..53d4ced --- /dev/null +++ b/module/documents/Macro.mjs @@ -0,0 +1,39 @@ +export class TAFMacro extends foundry.documents.Macro { + async deleteDialog(options, operation) { + const itemsUsingMacro = new Set(); + + // Check Items on Actors + game.actors.forEach(actor => { + actor.items.forEach(item => { + console.log(item.uuid, `owned by`, actor.uuid); + if (item.system.trigger === this.uuid) { + itemsUsingMacro.add(item.uuid); + }; + }); + }); + + // Check World Items + game.items.forEach(item => { + if (item.system.trigger === this.uuid) { + itemsUsingMacro.add(item.uuid); + }; + }); + + // Modify the dialog arguments + const type = _loc(this.constructor.metadata.label); + const question = _loc(`COMMON.AreYouSure`); + const warning = _loc(`SIDEBAR.DeleteWarning`, { type }); + let content = `

${question} ${warning}

`; + + if (itemsUsingMacro.size) { + const extraInfo = _loc( + `taf.misc.macro-is-in-use`, + { count: itemsUsingMacro.size }, + ); + content += `

${extraInfo}

`; + }; + + options.content = content; + return super.deleteDialog(options, operation); + }; +}; diff --git a/module/hooks/init.mjs b/module/hooks/init.mjs index de838f7..e898f46 100644 --- a/module/hooks/init.mjs +++ b/module/hooks/init.mjs @@ -14,6 +14,7 @@ import { PlayerData } from "../data/Actor/player.mjs"; import { TAFActor } from "../documents/Actor.mjs"; import { TAFCombatant } from "../documents/Combatant.mjs"; import { TAFItem } from "../documents/Item.mjs"; +import { TAFMacro } from "../documents/Macro.mjs"; import { TAFTokenDocument } from "../documents/Token.mjs"; // Settings @@ -33,6 +34,7 @@ Hooks.on(`init`, () => { CONFIG.Actor.documentClass = TAFActor; CONFIG.Combatant.documentClass = TAFCombatant; CONFIG.Item.documentClass = TAFItem; + CONFIG.Macro.documentClass = TAFMacro; CONFIG.Token.documentClass = TAFTokenDocument; // #endregion Documents