Skip to content

Commit

Permalink
Merge pull request #47 from PwQt/30-tidy-5e-sheet-rewrite-compatibility
Browse files Browse the repository at this point in the history
30 tidy 5e sheet rewrite compatibility
  • Loading branch information
PwQt authored Jan 27, 2024
2 parents db081e0 + 9a67212 commit 8cc8ff8
Show file tree
Hide file tree
Showing 15 changed files with 855 additions and 780 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
],
"scripts": {
"build": "vite build",
"build:watch": "vite build --watch",
"dev": "vite",
"eslint": "eslint .",
"prepare": "husky install",
Expand Down
116 changes: 115 additions & 1 deletion src/module.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import API from "./scripts/API/api.js";
import CONSTANTS from "./scripts/constants/constants.js";
import Logger from "./scripts/lib/Logger.js";
import { MagicItemActor } from "./scripts/magicitemactor.js";
import { MagicItemSheet } from "./scripts/magicitemsheet.js";
import { MagicItemTab } from "./scripts/magicItemtab.js";
import { MagicItem } from "./scripts/magic-item/MagicItem.js";

//CONFIG.debug.hooks = true;

Expand Down Expand Up @@ -65,18 +67,130 @@ Hooks.once("createToken", (token) => {
}
});

let tidyApi;
Hooks.once("tidy5e-sheet.ready", (api) => {
tidyApi = api;

// Register Tidy Item Sheet Tab
const magicItemsTab = new api.models.HandlebarsTab({
title: "Magic Item",
tabId: "magic-items",
path: "/modules/magic-items-2/templates/magic-item-tab.hbs",
enabled: (data) => {
return MagicItemTab.isAcceptedItemType(data.item) && MagicItemTab.isAllowedToShow();
},
getData(data) {
return new MagicItem(data.item.flags.magicitems);
},
onRender(params) {
const html = $(params.element);

if (params.data.editable) {
const magicItem = new MagicItem(params.data.item.flags.magicitems);
MagicItemTab.activateTabContentsListeners({
html: html,
item: params.data.item,
magicItem: magicItem,
});
params.element.querySelector(`.magic-items-content`).addEventListener("drop", (event) => {
MagicItemTab.onDrop({ event, item: params.data.item, magicItem: magicItem });
});
} else {
MagicItemTab.disableMagicItemTabInputs(html);
}
},
});
api.registerItemTab(magicItemsTab);

// Register character and NPC spell tab custom content
api.registerActorContent(
new api.models.HandlebarsContent({
path: `modules/${CONSTANTS.MODULE_ID}/templates/magic-item-spell-sheet.html`,
injectParams: {
position: "afterbegin",
selector: `[data-tab-contents-for="${api.constants.TAB_ID_CHARACTER_SPELLBOOK}"] .scroll-container`,
},
enabled(data) {
const actor = MagicItemActor.get(data.actor.id);
// Required for Tidy to have accurate item data
actor.buildItems();
return ["character", "npc"].includes(data.actor.type) && actor?.hasItemsSpells();
},
getData(data) {
return MagicItemActor.get(data.actor.id);
},
})
);

// Register character and NPC feature tab custom content
const npcAbilitiesTabContainerSelector = `[data-tidy-sheet-part="${api.constants.SHEET_PARTS.NPC_ABILITIES_CONTAINER}"]`;
const characterFeaturesContainerSelector = `[data-tab-contents-for="${api.constants.TAB_ID_CHARACTER_FEATURES}"] [data-tidy-sheet-part="${api.constants.SHEET_PARTS.ITEMS_CONTAINER}"]`;
const magicItemFeatureTargetSelector = [npcAbilitiesTabContainerSelector, characterFeaturesContainerSelector].join(
", "
);
api.registerActorContent(
new api.models.HandlebarsContent({
path: `modules/${CONSTANTS.MODULE_ID}/templates/magic-item-feat-sheet.html`,
injectParams: {
position: "afterbegin",
selector: magicItemFeatureTargetSelector,
},
enabled(data) {
const actor = MagicItemActor.get(data.actor.id);
// Required for Tidy to have accurate item data
actor.buildItems();
return ["character", "npc"].includes(data.actor.type) && actor?.hasItemsFeats();
},
getData(data) {
return MagicItemActor.get(data.actor.id);
},
})
);
});

// 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 html = $(element);
actor?.items
.filter((item) => item.visible)
.forEach((item) => {
let itemEl = html.find(
`[data-tidy-sheet-part="${tidyApi.constants.SHEET_PARTS.ITEM_TABLE_ROW}"][data-item-id="${item.id}"]`
);
let itemNameContainer = itemEl.find(`[data-tidy-sheet-part=${tidyApi.constants.SHEET_PARTS.ITEM_NAME}]`);
let iconHtml = tidyApi.useHandlebarsRendering(CONSTANTS.HTML.MAGIC_ITEM_ICON);
itemNameContainer.append(iconHtml);
});

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

Hooks.on(`renderItemSheet5e`, (app, html, data) => {
if (!game.user.isGM && game.settings.get(CONSTANTS.MODULE_ID, "hideFromPlayers")) {
if (tidyApi?.isTidy5eItemSheet(app)) {
return;
}

if (!MagicItemTab.isAllowedToShow()) {
return;
}

MagicItemTab.bind(app, html, data);
});

Hooks.on(`renderActorSheet5eCharacter`, (app, html, data) => {
if (tidyApi?.isTidy5eCharacterSheet(app)) {
return;
}
MagicItemSheet.bind(app, html, data);
});

Hooks.on(`renderActorSheet5eNPC`, (app, html, data) => {
if (tidyApi?.isTidy5eNpcSheet(app)) {
return;
}
MagicItemSheet.bind(app, html, data);
});

Expand Down
4 changes: 4 additions & 0 deletions src/scripts/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const CONSTANTS = {
PREFIX_LABEL: "MAGICITEMS",
PREFIX_FLAG: "magicitems",
FLAGS: {},
HTML: {
MAGIC_ITEM_ICON: '<i class="fas fa-magic" style="margin-left: 5px;" title="Magic Item"></i>',
},
};
CONSTANTS.PATH = `modules/${CONSTANTS.MODULE_NAME}/`;

export default CONSTANTS;
11 changes: 6 additions & 5 deletions src/scripts/magic-item-entry/MagicItemSpell.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { AbstractMagicItemEntry } from "./AbstractMagicItemEntry";
import { NumberUtils } from "../utils/number";

export class MagicItemSpell extends AbstractMagicItemEntry {
constructor(data) {
super(data);
this.baseLevel = parseInt(this.baseLevel);
this.level = parseInt(this.level);
this.consumption = parseInt(this.consumption);
this.upcast = this.upcast ? parseInt(this.upcast) : this.level;
this.upcastCost = this.upcastCost ? parseInt(this.upcastCost) : 1;
this.baseLevel = NumberUtils.parseIntOrGetDefault(this.baseLevel, 0);
this.level = NumberUtils.parseIntOrGetDefault(this.level, 0);
this.consumption = NumberUtils.parseIntOrGetDefault(this.consumption, 0);
this.upcast = this.upcast ? NumberUtils.parseIntOrGetDefault(this.upcast, 0) : this.level;
this.upcastCost = this.upcastCost ? NumberUtils.parseIntOrGetDefault(this.upcastCost, 0) : 1;
this.dc = this.flatDc && this.dc ? this.dc : "";
}

Expand Down
42 changes: 17 additions & 25 deletions src/scripts/magic-item/MagicItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { MagicItemFeat } from "../magic-item-entry/MagicItemFeat.js";
import { MagicItemSpell } from "../magic-item-entry/MagicItemSpell.js";
import { MagicItemTable } from "../magic-item-entry/MagicItemTable.js";
import { MagicItemHelpers } from "../magic-item-helpers.js";
import { NumberUtils } from "../utils/number.js";

export class MagicItem {
constructor(flags) {
Expand All @@ -11,7 +12,7 @@ export class MagicItem {
this.enabled = data.enabled;
this.equipped = data.equipped;
this.attuned = data.attuned;
this.charges = parseInt(data.charges);
this.charges = NumberUtils.parseIntOrGetDefault(data.charges, 0);
this.chargeType = data.chargeType;
this.rechargeable = data.rechargeable;
this.recharge = data.recharge;
Expand All @@ -23,7 +24,6 @@ export class MagicItem {
this.destroyFlavorText = data.destroyFlavorText;
this.sorting = data.sorting;
this.sortingModes = { l: "MAGICITEMS.SheetSortByLevel", a: "MAGICITEMS.SheetSortAlphabetically" };
this.updateDestroyTarget();

this.spells = Object.values(data.spells ? data.spells : {})
.filter((spell) => spell !== "null")
Expand All @@ -44,6 +44,12 @@ export class MagicItem {
this.savedSpells = this.spells.length;
this.savedFeats = this.feats.length;
this.savedTables = this.tables.length;

this.sort();

if (!this.enabled) {
this.clear();
}
}

sort() {
Expand All @@ -55,11 +61,10 @@ export class MagicItem {
}
}

updateDestroyTarget() {
this.destroyTarget =
this.chargeType === "c1"
? game.i18n.localize("MAGICITEMS.SheetObjectTarget")
: game.i18n.localize("MAGICITEMS.SheetSpellTarget");
get destroyTarget() {
return this.chargeType === "c1"
? game.i18n.localize("MAGICITEMS.SheetObjectTarget")
: game.i18n.localize("MAGICITEMS.SheetSpellTarget");
}

defaultData() {
Expand Down Expand Up @@ -313,26 +318,13 @@ export class MagicItem {
return this.items.filter((item) => item.id === itemId)[0];
}

async renderSheet(spellId) {
let spellFounded = this.findByUuid(spellId);
if (!byUuid) {
spellFounded = this.findById(spellId);
async renderSheet(itemId) {
let item = this.findByUuid(itemId);
if (!item) {
item = this.findById(itemId);
}

// let uuid = null;
// if (spellFounded.uuid) {
// uuid = spellFounded.uuid;
// } else {
// uuid = MagiItemHelpers.retrieveUuid({
// documentName: spellFounded.name,
// documentId: spellFounded.id,
// documentCollectionType: "Item",
// documentPack: spellFounded.pack,
// });
// }
// const itemTmp = await fromUuid(uuid);
// itemTmp.sheet.render(true);
await spellFounded.renderSheet();
await item.renderSheet();
}

cleanup() {
Expand Down
4 changes: 3 additions & 1 deletion src/scripts/magic-item/OwnedMagicItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ export class OwnedMagicItem extends MagicItem {
active = active && this.item.system.equipped;
}
if (this.attuned) {
let isAttuned = this.item.system.attunement ? this.item.system.attunement === 2 : this.item.system.attuned;
let isAttuned =
this.item.system.attunement === 2 ||
this.item.system.attuned === true; /* this.item.system.attuned is a legacy property; can be undefined */
active = active && isAttuned;
}
return active;
Expand Down
Loading

0 comments on commit 8cc8ff8

Please sign in to comment.