Skip to content

Commit

Permalink
Fixed unlinked tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Haxxer committed Aug 24, 2024
1 parent ff80218 commit efaae57
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 50 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Added option for a custom sell price on items
- Added detection for when the GM is unresponsive for item piles to make changes for players
- Fixed localization issue with `ITEM-PILES.Trade`, now moved to `ITEM-PILES.PlayerList.TradeButton`
- Fixed item piles interfaces of unlinked tokens not reacting to some changes to their flags

## Version 3.0.11

Expand Down
41 changes: 26 additions & 15 deletions src/API/private-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default class PrivateAPI {
Helpers.hooks.on("deleteActor", this._onDeleteActor.bind(this));
Helpers.hooks.on("preCreateToken", this._onPreCreateToken.bind(this))
Helpers.hooks.on("preUpdateToken", this._onPreUpdateToken.bind(this));
Helpers.hooks.on("updateToken", this._onUpdateToken.bind(this));
Helpers.hooks.on("createToken", this._onCreateToken.bind(this))
Helpers.hooks.on("dropCanvasData", this._dropData.bind(this));
}
Expand All @@ -52,36 +53,36 @@ export default class PrivateAPI {
* @private
*/
static _onCreateItem(doc) {
if (!doc.parent) return;
if (!doc.parent) return true;
ItemPileStore.notifyChanges("createItem", doc.parent, doc);
if (!PileUtilities.isValidItemPile(doc.parent)) return;
if (!PileUtilities.isValidItemPile(doc.parent)) return true;
this._evaluateItemPileChange(doc.parent, {}, true);
}

/**
* @private
*/
static _onUpdateItem(doc) {
if (!doc.parent) return;
if (!PileUtilities.isValidItemPile(doc.parent)) return;
if (!doc.parent) return true;
if (!PileUtilities.isValidItemPile(doc.parent)) return true;
this._evaluateItemPileChange(doc.parent, {}, true);
}

/**
* @private
*/
static _onDeleteItem(doc) {
if (!doc.parent) return;
if (!doc.parent) return true;
ItemPileStore.notifyChanges("deleteItem", doc.parent, doc);
if (!PileUtilities.isValidItemPile(doc.parent)) return;
if (!PileUtilities.isValidItemPile(doc.parent)) return true;
this._evaluateItemPileChange(doc.parent, {}, true);
}

/**
* @private
*/
static _onUpdateActor(doc, changes) {
if (!PileUtilities.isValidItemPile(doc)) return;
if (!PileUtilities.isValidItemPile(doc)) return true;
this._evaluateItemPileChange(doc, changes);
}

Expand All @@ -90,7 +91,7 @@ export default class PrivateAPI {
*/
static _onDeleteToken(doc) {
ItemPileStore.notifyChanges("delete", doc.actor)
if (!PileUtilities.isValidItemPile(doc)) return;
if (!PileUtilities.isValidItemPile(doc)) return true;
Helpers.hooks.callAll(CONSTANTS.HOOKS.PILE.DELETE, doc);
}

Expand All @@ -117,7 +118,7 @@ export default class PrivateAPI {
foundry.utils.deepClone(foundry.utils.getProperty(sourceActor, CONSTANTS.FLAGS.PILE) ?? {})
);
}
if (!itemPileConfig?.enabled) return;
if (!itemPileConfig?.enabled) return true;
if (!doc.isLinked) {
Utilities.deleteProperty(docData, CONSTANTS.ACTOR_DELTA_PROPERTY + "." + CONSTANTS.FLAGS.SHARING);
}
Expand Down Expand Up @@ -162,18 +163,28 @@ export default class PrivateAPI {

static _onPreUpdateToken(doc, changes) {
const diff = foundry.utils.diffObject(doc, changes);
if (!foundry.utils.hasProperty(diff, "actorLink")) return;
if (!PileUtilities.isValidItemPile(doc)) return;
const flagData = PileUtilities.getActorFlagData(doc);
const cleanFlagData = PileUtilities.cleanFlagData(flagData);
changes[CONSTANTS.FLAGS.PILE] = diff.actorLink ? cleanFlagData : null;
if (!foundry.utils.hasProperty(diff, "actorLink")) return true;
if (!PileUtilities.isValidItemPile(doc)) return true;
const actor = Utilities.getActor(doc);
const changingFlags = foundry.utils.getProperty(changes, CONSTANTS.FLAGS.PILE) ?? {};
const flagData = foundry.utils.mergeObject(
PileUtilities.getActorFlagData(actor),
changingFlags
);
changes[CONSTANTS.FLAGS.PILE] = PileUtilities.cleanFlagData(flagData);
}

static _onUpdateToken(doc) {
if (doc.actorLink) return true;
if (!PileUtilities.isValidItemPile(doc)) return true;
ItemPileStore.notifyChanges("updateUnlinkedToken", doc.actor)
}

/**
* @private
*/
static _onCreateToken(doc) {
if (!PileUtilities.isValidItemPile(doc)) return;
if (!PileUtilities.isValidItemPile(doc)) return true;
const itemPileConfig = PileUtilities.getActorFlagData(doc.actor)
Helpers.hooks.callAll(CONSTANTS.HOOKS.PILE.CREATE, doc, itemPileConfig);
return this._preloadItemPileFiles(doc);
Expand Down
59 changes: 24 additions & 35 deletions src/helpers/pile-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,33 +682,6 @@ export function shouldEvaluateChange(target, changes) {
|| flags.displayOne || flags.showItemName || flags.overrideSingleItemScale;
}

function getRelevantTokensAndActor(target) {

const relevantDocument = Utilities.getDocument(target);

let documentActor;
let documentTokens = [];

if (relevantDocument instanceof Actor) {
documentActor = relevantDocument;
if (relevantDocument.token) {
documentTokens.push(relevantDocument?.token);
} else {
documentTokens = canvas.tokens.placeables.filter(token => token.document.actor === documentActor).map(token => token.document);
}
} else {
documentActor = relevantDocument.actor;
if (relevantDocument.isLinked) {
documentTokens = canvas.tokens.placeables.filter(token => token.document.actor === documentActor).map(token => token.document);
} else {
documentTokens.push(relevantDocument);
}
}

return [documentActor, documentTokens]

}

export async function updateItemPileData(target, newFlags, tokenData) {

if (!target) return;
Expand All @@ -717,7 +690,8 @@ export async function updateItemPileData(target, newFlags, tokenData) {
if (!tokenData) tokenData = {};
tokenData = foundry.utils.mergeObject(tokenData, {});

let [documentActor, documentTokens] = getRelevantTokensAndActor(target);
let documentActor = Utilities.getActor(target);
const documentTokens = documentActor.getActiveTokens();

const items = getActorItems(documentActor, { itemFilters: flagData.overrideItemFilters });
const actorCurrencies = (flagData.overrideCurrencies || []).concat(flagData.overrideSecondaryCurrencies || []);
Expand All @@ -727,9 +701,13 @@ export async function updateItemPileData(target, newFlags, tokenData) {

const currentFlagData = getActorFlagData(target, { useDefaults: false });

const cleanedFlagData = cleanFlagData(flagData, { addRemoveFlag: true, existingData: currentFlagData });
const cleanedFlagData = cleanFlagData(flagData, {
addRemoveFlag: true,
existingData: currentFlagData
});

const updates = documentTokens.map(tokenDocument => {
const sceneUpdates = documentTokens.reduce((acc, token) => {
const tokenDocument = token.document;
const overrideImage = foundry.utils.getProperty(tokenData, "texture.src")
?? foundry.utils.getProperty(tokenData, "img");
const overrideScale = foundry.utils.getProperty(tokenData, "texture.scaleX")
Expand All @@ -748,12 +726,23 @@ export async function updateItemPileData(target, newFlags, tokenData) {
[CONSTANTS.FLAGS.VERSION]: Helpers.getModuleVersion(),
...newTokenData
};
if (!tokenDocument.actorLink) documentActor = false;
return foundry.utils.mergeObject({}, data);
});
if (!tokenDocument.actorLink) {
data[CONSTANTS.ACTOR_DELTA_PROPERTY] = {
[CONSTANTS.FLAGS.PILE]: cleanedFlagData,
[CONSTANTS.FLAGS.VERSION]: Helpers.getModuleVersion(),
}
}
acc[tokenDocument.parent.id] ??= [];
acc[tokenDocument.parent.id].push(foundry.utils.mergeObject({}, data));
return acc;
}, {});

if (canvas.scene && !foundry.utils.isEmpty(updates)) {
await canvas.scene.updateEmbeddedDocuments("Token", updates, { animate: false });
if (!foundry.utils.isEmpty(sceneUpdates)) {
for (const [sceneId, updates] of Object.entries(sceneUpdates)) {
const scene = game.scenes.get(sceneId);
if (!scene) continue
await scene.updateEmbeddedDocuments("Token", updates, { animate: false });
}
}

if (documentActor) {
Expand Down
6 changes: 6 additions & 0 deletions src/stores/item-pile-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ export default class ItemPileStore {

}

updateUnlinkedToken() {
this.pileData.set(PileUtilities.getActorFlagData(this.actor));
this.shareData.set(SharingUtilities.getItemPileSharingData(this.actor));
this.refreshItems();
}

updateSource(newSource) {
this.uuid = Utilities.getUuid(newSource);
this.actor = Utilities.getActor(newSource);
Expand Down

0 comments on commit efaae57

Please sign in to comment.