From 1a96015ba1767d5831a94936415ee524c92e992f Mon Sep 17 00:00:00 2001 From: Jeff Hitchcock Date: Wed, 12 Jun 2024 05:18:26 -0700 Subject: [PATCH] [#3717] Fix appended numbers not being incremented for placed tokens (#3727) --- module/canvas/token-placement.mjs | 31 +++++++++++++++++++++-- module/data/actor/group.mjs | 2 ++ module/data/item/fields/summons-field.mjs | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/module/canvas/token-placement.mjs b/module/canvas/token-placement.mjs index e1c6f20af9..876abbbb39 100644 --- a/module/canvas/token-placement.mjs +++ b/module/canvas/token-placement.mjs @@ -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 @@ -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 { @@ -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; + } } diff --git a/module/data/actor/group.mjs b/module/data/actor/group.mjs index e29b011408..7c652abc52 100644 --- a/module/data/actor/group.mjs +++ b/module/data/actor/group.mjs @@ -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 { diff --git a/module/data/item/fields/summons-field.mjs b/module/data/item/fields/summons-field.mjs index d62d5783f9..1a90f9da19 100644 --- a/module/data/item/fields/summons-field.mjs +++ b/module/data/item/fields/summons-field.mjs @@ -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();