SendToChat now check links validity before adding them

This commit is contained in:
Vlyan
2021-09-29 18:19:00 +02:00
parent 0616b61db8
commit f961311022

View File

@@ -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