From cc2cf1205c053324904cbbce9d6b2c9d0f01ee17 Mon Sep 17 00:00:00 2001 From: WBHarry Date: Tue, 30 Jun 2026 11:50:39 +0200 Subject: [PATCH] Moved getWorldActor function to utils and updated logic --- module/data/fields/action/summonField.mjs | 26 +++-------------------- module/helpers/utils.mjs | 21 ++++++++++++++++++ 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/module/data/fields/action/summonField.mjs b/module/data/fields/action/summonField.mjs index 64b42267..6c35c779 100644 --- a/module/data/fields/action/summonField.mjs +++ b/module/data/fields/action/summonField.mjs @@ -1,4 +1,4 @@ -import { itemAbleRollParse, triggerChatRollFx } from '../../../helpers/utils.mjs'; +import { getWorldActor, itemAbleRollParse, triggerChatRollFx } from '../../../helpers/utils.mjs'; import FormulaField from '../formulaField.mjs'; const fields = foundry.data.fields; @@ -42,9 +42,9 @@ export default class DHSummonField extends fields.ArrayField { const count = roll.total; if (!roll.isDeterministic) rolls.push(roll); - const actor = await DHSummonField.getWorldActor(await foundry.utils.fromUuid(summon.actorUUID)); + const actor = await getWorldActor(await foundry.utils.fromUuid(summon.actorUUID)); /* Extending summon data in memory so it's available in actionField.toChat. Think it's harmless, but ugly. Could maybe find a better way. */ - summon.actor = actor.toObject(); + summon.actor = actor; const countNumber = Number.parseInt(count); for (let i = 0; i < countNumber; i++) { @@ -62,26 +62,6 @@ export default class DHSummonField extends fields.ArrayField { DHSummonField.handleSummon(summonData, this.actor); } - /* Check for any available instances of the actor present in the world if we're missing artwork in the compendium. If none exists, create one. */ - static async getWorldActor(baseActor) { - const dataType = game.system.api.data.actors[`Dh${baseActor.type.capitalize()}`]; - if (baseActor.inCompendium) { - const worldActorCopy = game.actors.find(x => x.name === baseActor.name); - if ( - worldActorCopy && ( - baseActor.img === worldActorCopy.img || - (dataType && baseActor.img === dataType.DEFAULT_ICON) - ) - ) { - return worldActorCopy; - } - - return await game.system.api.documents.DhpActor.create(baseActor.toObject()); - } - - return baseActor; - } - static async handleSummon(summonData, actionActor) { await CONFIG.ux.TokenManager.createTokensWithPreview(summonData, { elevation: actionActor.token?.elevation }); diff --git a/module/helpers/utils.mjs b/module/helpers/utils.mjs index 6467edd7..8e21a668 100644 --- a/module/helpers/utils.mjs +++ b/module/helpers/utils.mjs @@ -889,4 +889,25 @@ export async function triggerChatRollFx(rolls, options = { whisper: false, blind export function shouldUseHopeFearAutomation(options = { gmAsPlayer: true }) { const { hopeFear } = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Automation); return (!game.user.isGM || options.gmAsPlayer) ? hopeFear.players : hopeFear.gm; +} + +export async function getWorldActor(baseActor) { + if (baseActor.inCompendium) { + const worldActorCopy = + game.actors.find(x => x._stats.compendiumSource === baseActor.uuid && x.name === baseActor.name); + + if (worldActorCopy) + return worldActorCopy; + + const baseActorData = baseActor.toObject(); + return await game.system.api.documents.DhpActor.create({ + ...baseActorData, + _stats: { + ...baseActorData._stats, + compendiumSource: baseActor.uuid + } + }); + } + + return baseActor.toObject(); } \ No newline at end of file