Skip to content

Commit

Permalink
Added guards to Tidy compatibility wire-up code, to prevent console e…
Browse files Browse the repository at this point in the history
…rrors when an MI actor is not found. Instead, it will return early and return false for enabled checks, to avoid triggering errors.
  • Loading branch information
kgar committed Feb 28, 2024
1 parent 91f8384 commit 795df4b
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ Hooks.once("tidy5e-sheet.ready", (api) => {
selector: `[data-tab-contents-for="${api.constants.TAB_ID_CHARACTER_SPELLBOOK}"] .scroll-container`,
},
enabled(data) {
const actor = MagicItemActor.get(data.actor.id);
const magicItemActor = MagicItemActor.get(data.actor.id);
if (!magicItemActor) {
return false;
}
// Required for Tidy to have accurate item data
actor.buildItems();
return ["character", "npc"].includes(data.actor.type) && actor?.hasItemsSpells();
magicItemActor.buildItems();
return ["character", "npc"].includes(data.actor.type) && magicItemActor.hasItemsSpells();
},
getData(data) {
return MagicItemActor.get(data.actor.id);
Expand All @@ -136,10 +139,13 @@ Hooks.once("tidy5e-sheet.ready", (api) => {
selector: magicItemFeatureTargetSelector,
},
enabled(data) {
const actor = MagicItemActor.get(data.actor.id);
const magicItemActor = MagicItemActor.get(data.actor.id);
if (!magicItemActor) {
return false;
}
// Required for Tidy to have accurate item data
actor.buildItems();
return ["character", "npc"].includes(data.actor.type) && actor?.hasItemsFeats();
magicItemActor.buildItems();
return ["character", "npc"].includes(data.actor.type) && magicItemActor.hasItemsFeats();
},
getData(data) {
return MagicItemActor.get(data.actor.id);
Expand All @@ -151,9 +157,13 @@ Hooks.once("tidy5e-sheet.ready", (api) => {
// Wire Tidy events and register iterated, data-dependent content
Hooks.on("tidy5e-sheet.renderActorSheet", (app, element, data) => {
// Place wand for visible magic items
const actor = MagicItemActor.get(data.actor.id);
const magicItemActor = MagicItemActor.get(data.actor.id);
const html = $(element);
actor?.items
if (!magicItemActor) {
return;
}

magicItemActor.items
.filter((item) => item.visible)
.forEach((item) => {
let itemEl = html.find(
Expand All @@ -165,7 +175,7 @@ Hooks.on("tidy5e-sheet.renderActorSheet", (app, element, data) => {
});

// Wire events for custom tidy actor sheet content
MagicItemSheet.handleEvents(html, actor);
MagicItemSheet.handleEvents(html, magicItemActor);
});

Hooks.on(`renderItemSheet5e`, (app, html, data) => {
Expand Down

0 comments on commit 795df4b

Please sign in to comment.