From f9613110228162e8008e5f9f45fd080373981401 Mon Sep 17 00:00:00 2001 From: Vlyan Date: Wed, 29 Sep 2021 18:19:00 +0200 Subject: [PATCH] SendToChat now check links validity before adding them --- system/scripts/helpers.js | 50 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/system/scripts/helpers.js b/system/scripts/helpers.js index 859f51e..164c0e1 100644 --- a/system/scripts/helpers.js +++ b/system/scripts/helpers.js @@ -527,10 +527,21 @@ export class HelpersL5r5e { let link = null; if (object.data.flags.core?.sourceId) { link = object.data.flags.core?.sourceId.replace(/(\w+)\.(.+)/, "@$1[$2]"); - } else if (object.pack) { + if (!HelpersL5r5e.isLinkValid(link)) { + link = null; + } + } + if (!link && object.pack) { link = `@Compendium[${object.pack}.${object.id}]{${object.name}}`; - } else if (!object.actor) { + if (!HelpersL5r5e.isLinkValid(link)) { + link = null; + } + } + if (!link && !object.actor) { link = object.link; + if (!HelpersL5r5e.isLinkValid(link)) { + link = null; + } } // Send to Chat @@ -539,6 +550,41 @@ export class HelpersL5r5e { }); } + /** + * Check if the link is valid (format "@Item[L5RCoreIte000042]{Amigasa}" / "@Compendium[l5r5e.core-peculiarities-distinctions.L5RCoreDis000002]{Ambidextrie}") + * @param {string} link + * @return {boolean} + */ + static isLinkValid(link) { + const [type, target] = link.replace(/@(\w+)\[([^\]]+)\].*/, "$1|$2").split("|"); + + // Get a matched World document + // "@Item[L5RCoreIte000042]{Amigasa}" + if (CONST.ENTITY_TYPES.includes(type)) { + const collection = game.collections.get(type); + const document = /^[a-zA-Z0-9]{16}$/.test(target) ? collection.get(target) : collection.getName(target); + return !!document; + } + + // Get a matched Compendium entity + // "@Compendium[l5r5e.core-peculiarities-distinctions.L5RCoreDis000002]{Ambidextrie}" + if (type === "Compendium") { + // Get the linked Entity + const [scope, packName, id] = target.split("."); + const pack = game.packs.get(`${scope}.${packName}`); + if (!pack) { + return false; + } + // If the pack is indexed, check, if not assume it's ok + if (pack.index.size) { + const index = pack.index.find((i) => i._id === id || i.name === id); + return !!index; + } + } + + return true; + } + /** * Return the RollMode for this ChatData * @param {object} chatData