From 6928e8e74fb20bb15e3c200656f4f30ba543e760 Mon Sep 17 00:00:00 2001 From: Haxxer Date: Sat, 8 Jan 2022 21:29:20 +0000 Subject: [PATCH] Fixed linked token actors not behaving linked --- scripts/lib/lib.js | 50 +++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/scripts/lib/lib.js b/scripts/lib/lib.js index 6789b2ca..d14b9aa1 100644 --- a/scripts/lib/lib.js +++ b/scripts/lib/lib.js @@ -157,31 +157,41 @@ export async function updateItemPile(inDocument, flagData, tokenData){ if(!tokenData) tokenData = {}; - if(inDocument instanceof TokenDocument && inDocument.data.actorLink){ - inDocument = inDocument?.actor; - }else if(inDocument instanceof Actor && inDocument.token){ - inDocument = inDocument?.token; + let documentActor; + let documentTokens = []; + + if(inDocument instanceof Actor){ + documentActor = inDocument; + if(inDocument.token) { + documentToken.push(inDocument?.token); + }else{ + documentTokens = canvas.tokens.placeables.filter(token => token.document.actor === documentActor).map(token => token.document); + } + }else{ + documentActor = inDocument.actor; + if(inDocument.isLinked){ + documentTokens = canvas.tokens.placeables.filter(token => token.document.actor === documentActor).map(token => token.document); + }else{ + documentTokens.push(inDocument); + } } - tokenData = foundry.utils.mergeObject(tokenData, { - "img": getItemPileTokenImage(inDocument, flagData), - "scale": getItemPileTokenScale(inDocument, flagData), + const updates = documentTokens.map(tokenDocument => { + const newTokenData = foundry.utils.mergeObject(tokenData, { + "img": getItemPileTokenImage(tokenDocument, flagData), + "scale": getItemPileTokenScale(tokenDocument, flagData), + }); + return { + "_id": tokenDocument.id, + ...newTokenData, + [`flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.FLAG_NAME}`]: flagData + } }); - if (inDocument instanceof TokenDocument) { - return inDocument.update({ - ...tokenData, - [`flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.FLAG_NAME}`]: flagData, - [`actorData.flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.FLAG_NAME}`]: flagData - }); - } + await canvas.scene.updateEmbeddedDocuments("Token", updates); - return inDocument.update({ - [`flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.FLAG_NAME}`]: flagData, - "token": { - [`flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.FLAG_NAME}`]: flagData, - ...tokenData - } + return documentActor.update({ + [`flags.${CONSTANTS.MODULE_NAME}.${CONSTANTS.FLAG_NAME}`]: flagData }); }