Skip to content

Commit

Permalink
[#3717] Fix appended numbers not being incremented for placed tokens (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron authored Jun 12, 2024
1 parent c8ee424 commit 1a96015
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
31 changes: 29 additions & 2 deletions module/canvas/token-placement.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*
* @typedef {object} PlacementData
* @property {PrototypeToken} prototypeToken
* @property {object} index
* @property {number} index.total Index of the placement across all placements.
* @property {number} index.unique Index of the placement across placements with the same original token.
* @property {number} x
* @property {number} y
* @property {number} rotation
Expand Down Expand Up @@ -104,14 +107,20 @@ export default class TokenPlacement {
this.#createPreviews();
try {
const placements = [];
let total = 0;
const uniqueTokens = new Map();
while ( this.#currentPlacement < this.config.tokens.length - 1 ) {
this.#currentPlacement++;
const obj = canvas.tokens.preview.addChild(this.#previews[this.#currentPlacement].object);
await obj.draw();
obj.eventMode = "none";
const placement = await this.#requestPlacement();
if ( placement ) placements.push(placement);
else obj.clear();
if ( placement ) {
const actorId = placement.prototypeToken.parent.id;
uniqueTokens.set(actorId, (uniqueTokens.get(actorId) ?? -1) + 1);
placement.index = { total: total++, unique: uniqueTokens.get(actorId) };
placements.push(placement);
} else obj.clear();
}
return placements;
} finally {
Expand Down Expand Up @@ -272,4 +281,22 @@ export default class TokenPlacement {
await this.#finishPlacement(event);
this.#events.resolve(false);
}

/* -------------------------------------------- */
/* Helpers */
/* -------------------------------------------- */

/**
* Adjust the appended number on an unlinked token to account for multiple placements.
* @param {TokenDocument|object} tokenDocument Document or data object to adjust.
* @param {PlacementData} placement Placement data associated with this token document.
*/
static adjustAppendedNumber(tokenDocument, placement) {
const regex = new RegExp(/\((\d+)\)$/);
const match = tokenDocument.name?.match(regex);
if ( !match ) return;
const name = tokenDocument.name.replace(regex, `(${Number(match[1]) + placement.index.unique})`);
if ( tokenDocument instanceof TokenDocument ) tokenDocument.updateSource({ name });
else tokenDocument.name = name;
}
}
2 changes: 2 additions & 0 deletions module/data/actor/group.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,10 @@ export default class GroupActor extends ActorDataModel.mixin(CurrencyTemplate) {
});
for ( const placement of placements ) {
const actor = placement.prototypeToken.actor;
const appendNumber = !placement.prototypeToken.actorLink && placement.prototypeToken.appendNumber;
delete placement.prototypeToken;
const tokenDocument = await actor.getTokenDocument(placement);
if ( appendNumber ) TokenPlacement.adjustAppendedNumber(tokenDocument, placement);
tokensData.push(tokenDocument.toObject());
}
} finally {
Expand Down
1 change: 1 addition & 0 deletions module/data/item/fields/summons-field.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ export class SummonsData extends foundry.abstract.DataModel {
await tokenDocument.actor.createEmbeddedDocuments("ActiveEffect", newEffects, {keepId: true});
} else {
tokenDocument.delta.updateSource(actorUpdates);
if ( actor.prototypeToken.appendNumber ) TokenPlacement.adjustAppendedNumber(tokenDocument, placement);
}

return tokenDocument.toObject();
Expand Down

0 comments on commit 1a96015

Please sign in to comment.