Skip to content

Commit

Permalink
fix: V12 TAH tooltip fix
Browse files Browse the repository at this point in the history
Was trying to generate tooltips on effects that did not have tooltip generation code.

Put various things in try/catch to try to improve TAH stability.

Swapped status effects for TAH and Auto-bloodied to use the new actor.toggleStatusEffect method.
  • Loading branch information
draconas1 committed Aug 10, 2024
1 parent eb807d4 commit 024dbc2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 46 deletions.
15 changes: 1 addition & 14 deletions module/hooks/auto-bloodied-dead.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,7 @@ export async function setBloodiedDeadOnHPChange(actor, change, options, userId)
DnD4eTools.log(false, `Actor does not have status ${statusToCheck}. Applying it`)
}

const status = CONFIG.statusEffects.find(x => x.id === statusToCheck)

const effect = {
...status,
"name" : game.i18n.localize(status.label),
"statuses" : [statusToCheck],
"flags": {
"core": {
"overlay": overlay
}
}
}
delete effect.id
await ActiveEffect.create(effect, { parent : actor })
await actor.toggleStatusEffect(statusToCheck, { active: true, overlay: overlay })
}

function findEffectIds(statusToCheck, actor) {
Expand Down
89 changes: 59 additions & 30 deletions module/integrations/tah/action-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,16 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {


async #buildPowers() {
if (this.version >= 3) {
return this.#buildPowersV3()
try {
if (this.version >= 3) {
return this.#buildPowersV3()
}
else return this.#buildPowersV2()
}
catch (e) {
this.#logError(e, null)
}
else return this.#buildPowersV2()

}

async #buildPowersV3() {
Expand Down Expand Up @@ -320,29 +326,34 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
* @private
*/
async #buildBasicGroupsOfActionsByType(itemTypeLookup, actionTypeId, optFilter) {
const mapByItemType = new Map()
const filter = optFilter ?? ((itemData) => true)
try {
const mapByItemType = new Map()
const filter = optFilter ?? ((itemData) => true)


for (const itemData of this.actor.items) {
const type = itemData.type
if (itemTypeLookup[type] && filter(itemData)) {
const typeMap = mapByItemType.get(type) ?? new Map()
typeMap.set(itemData.id, itemData)
mapByItemType.set(type, typeMap)
for (const itemData of this.actor.items) {
const type = itemData.type
if (itemTypeLookup[type] && filter(itemData)) {
const typeMap = mapByItemType.get(type) ?? new Map()
typeMap.set(itemData.id, itemData)
mapByItemType.set(type, typeMap)
}
}
}

for (const [type, typeMap] of mapByItemType) {
const groupId = type
const groupData = {id: groupId, type: 'system'}
for (const [type, typeMap] of mapByItemType) {
const groupId = type
const groupData = {id: groupId, type: 'system'}

// Get actions
const actions = await Promise.all([...typeMap].map(
async ([itemId, itemData]) => this.#buildActionFromItem(actionTypeId, itemData))
)
// Get actions
const actions = await Promise.all([...typeMap].map(
async ([itemId, itemData]) => this.#buildActionFromItem(actionTypeId, itemData))
)

this.addActions(actions, groupData)
this.addActions(actions, groupData)
}
}
catch (e) {
this.#logError(e, null)
}
}

Expand Down Expand Up @@ -374,12 +385,14 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
: ''
const cssClass = `toggle${active}`
const img = coreModule.api.Utils.getImage(condition)
const tooltip = await this.#getTooltip(this.i18n(condition.description))
return {
id,
name,
encodedValue,
img,
cssClass
cssClass,
tooltip
}
}))

Expand Down Expand Up @@ -470,11 +483,6 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
const encodedValue = [actionType, id].join(this.delimiter)
const img = coreModule.api.Utils.getImage(entity)
const tooltip = await this.#getTooltip(entity)
/*
Some good stuff here in dnd5e around tooltipping that we could make use of in a future version
Keeping the async keyword in because of future tooltips.
*/
return {
id,
name,
Expand All @@ -498,20 +506,34 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
* @returns {Promise} The tooltip
*/
async #getTooltipV3 (tooltipData) {
if (!tooltipData) return ''
if (this.tooltipsSetting === 'none') return ''
if (typeof tooltipData === 'string') return tooltipData

const name = coreModule.api.Utils.i18n(tooltipData.name)

if (this.tooltipsSetting === 'nameOnly') return name


if (!this.actor) return ''
if (!tooltipData.type) return ''

const html = await this.dnd4e.tokenBarHooks.generateItemTooltip(this.actor, tooltipData)
const finalhtml = this.#buildHorribleNestedDiv(html, ["tah-4etooltip"])
return finalhtml
if (typeof tooltipData.getChatData == 'function') {
try {
const html = await this.dnd4e.tokenBarHooks.generateItemTooltip(this.actor, tooltipData)
const finalhtml = this.#buildHorribleNestedDiv(html, ["tah-4etooltip"])
return finalhtml
}
catch (e) {
this.#logError(e, tooltipData)
return ''
}
}
else if (tooltipData.description) {
return this.#buildHorribleNestedDiv(tooltipData.description, ["tah-4etooltip"])
}
else {
return ''
}
}

#buildHorribleNestedDiv(html, divClasses) {
Expand All @@ -520,6 +542,13 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
return `${divHeads}${html}${divTails}`
}

#logError(error, context) {
coreModule.api.Logger.error(error)
if (context) {
coreModule.api.Logger.error(JSON.stringify(context))
}
}

/**
* Get tooltip
* @param {object} tooltipData The tooltip data
Expand Down
4 changes: 2 additions & 2 deletions module/integrations/tah/roll-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
if (effect?.disabled) { await effect.delete() }

isRightClick
? await token.toggleEffect(condition, { overlay: true })
: await token.toggleEffect(condition)
? await actor.toggleStatusEffect(actionId, { overlay: true })
: await actor.toggleStatusEffect(actionId)
}

Hooks.callAll('forceUpdateTokenActionHud')
Expand Down

0 comments on commit 024dbc2

Please sign in to comment.