From d41200b2c0e9a3e2ebc85f0df1345f509a6b7a13 Mon Sep 17 00:00:00 2001 From: PwQt Date: Thu, 17 Oct 2024 18:58:43 +0200 Subject: [PATCH] #185 Add localization files + fixes --- src/languages/en.json | 4 +- src/scripts/magic-item-helpers.js | 42 +++++++++++++++++ .../AbstractOwnedMagicItemEntry.js | 14 +++--- .../OwnedMagicItemSpell.js | 45 ++++++------------- .../magic-item-summon-dialog-helper.js | 9 ---- src/templates/magic-item-summon-dialog.hbs | 36 ++++++++++----- 6 files changed, 93 insertions(+), 57 deletions(-) delete mode 100644 src/scripts/magic-item-summon-dialog-helper.js diff --git a/src/languages/en.json b/src/languages/en.json index 0717956..be65670 100644 --- a/src/languages/en.json +++ b/src/languages/en.json @@ -113,5 +113,7 @@ "MAGICITEMS.ToggleActiveEffectDialogYes": "Yes", "MAGICITEMS.ToggleActiveEffectDialogNo": "No", "MAGICITEMS.ToggleActiveEffectError": "An error occured while adding active effect - please check console.", - "MAGICITEMS.ShowChargesMessage": "The magic item \"{name}\" has {chargesLeft} charges out of {chargesMax} left." + "MAGICITEMS.ShowChargesMessage": "The magic item \"{name}\" has {chargesLeft} charges out of {chargesMax} left.", + "MAGICITEMS.SummoningDialogTitle": "Summoning Options", + "MAGICITEMS.SummoningDialogButton": "Confirm summon" } diff --git a/src/scripts/magic-item-helpers.js b/src/scripts/magic-item-helpers.js index f634aae..b36a1ba 100644 --- a/src/scripts/magic-item-helpers.js +++ b/src/scripts/magic-item-helpers.js @@ -153,4 +153,46 @@ export class MagicItemHelpers { } } } + + /** + * Create details on the summoning profiles and other related options. + * Method fetched from D&D5e ability-use-dialog.mjs + * @param {Item5e} item The item. + * @returns {{ profiles: object, creatureTypes: object }|null} + */ + static createSummoningOptions(item) { + const summons = item.system.summons; + if (!summons?.profiles.length) return null; + const options = { mode: summons.mode, createSummons: true }; + const rollData = item.getRollData(); + const level = summons.relevantLevel; + options.profiles = Object.fromEntries( + summons.profiles + .map((profile) => { + if (!summons.mode && !fromUuidSync(profile.uuid)) return null; + const withinRange = (profile.level.min ?? -Infinity) <= level && level <= (profile.level.max ?? Infinity); + if (!withinRange) return null; + return [profile._id, summons.getProfileLabel(profile, rollData)]; + }) + .filter((f) => f), + ); + if (Object.values(options.profiles).every((p) => p.startsWith("1 × "))) { + Object.entries(options.profiles).forEach(([k, v]) => (options.profiles[k] = v.replace("1 × ", ""))); + } + if (Object.values(options.profiles).length <= 1) { + options.profile = Object.keys(options.profiles)[0]; + options.profiles = null; + } + if (summons.creatureSizes.size > 1) + options.creatureSizes = summons.creatureSizes.reduce((obj, k) => { + obj[k] = CONFIG.DND5E.actorSizes[k]?.label; + return obj; + }, {}); + if (summons.creatureTypes.size > 1) + options.creatureTypes = summons.creatureTypes.reduce((obj, k) => { + obj[k] = CONFIG.DND5E.creatureTypes[k]?.label; + return obj; + }, {}); + return options; + } } diff --git a/src/scripts/magic-item-owned-entry/AbstractOwnedMagicItemEntry.js b/src/scripts/magic-item-owned-entry/AbstractOwnedMagicItemEntry.js index 10ccc1a..ef2be01 100644 --- a/src/scripts/magic-item-owned-entry/AbstractOwnedMagicItemEntry.js +++ b/src/scripts/magic-item-owned-entry/AbstractOwnedMagicItemEntry.js @@ -1,7 +1,6 @@ import CONSTANTS from "../constants/constants"; import Logger from "../lib/Logger"; import { RetrieveHelpers } from "../lib/retrieve-helpers"; -import { MagicItemSummonDialogHelper } from "../magic-item-summon-dialog-helper"; export class AbstractOwnedMagicItemEntry { constructor(magicItem, item) { @@ -166,18 +165,21 @@ export class AbstractOwnedMagicItemEntry { x.render(true); } - async askSummonningMessage(summonList, creatureTypes) { - const title = game.i18n.localize("MAGICITEMS.ToggleActiveEffectDialogTitle"); + async askSummonningMessage(summonOptions) { let html = await renderTemplate( `modules/${CONSTANTS.MODULE_ID}/templates/magic-item-summon-dialog.hbs`, - new MagicItemSummonDialogHelper(true, summonList, creatureTypes), + summonOptions, ); let dialog = await foundry.applications.api.DialogV2.prompt({ - window: { title: title }, + window: { + title: game.i18n.localize("MAGICITEMS.SummoningDialogTitle"), + }, content: html, modal: true, + rejectClose: false, ok: { - label: "Confirm Summon", + label: game.i18n.localize("MAGICITEMS.SummoningDialogButton"), + icon: "fas fa-wand-magic-sparkles", callback: (event, button, dialog) => button.form.elements, }, }); diff --git a/src/scripts/magic-item-owned-entry/OwnedMagicItemSpell.js b/src/scripts/magic-item-owned-entry/OwnedMagicItemSpell.js index ec80201..67dbf1e 100644 --- a/src/scripts/magic-item-owned-entry/OwnedMagicItemSpell.js +++ b/src/scripts/magic-item-owned-entry/OwnedMagicItemSpell.js @@ -51,42 +51,25 @@ export class OwnedMagicItemSpell extends AbstractOwnedMagicItemEntry { let clonedOwnedItem = this.ownedItem; let itemUseConfiguration = {}; + const sOptions = MagicItemHelpers.createSummoningOptions(spell); if ( MagicItemHelpers.canSummon() && - (spell.system.summons?.creatureTypes?.length > 0 || spell.system.summons?.profiles?.length > 0) + (spell.system.summons?.creatureTypes?.length > 1 || spell.system.summons?.profiles?.length > 1) ) { - const summonProfilesSync = (profiles) => { - const mapProfiles = profiles.map((profile) => { - const name = profile.name?.length ? profile.name : RetrieveHelpers.getActorSync(profile.uuid).name; - const obj = { - key: profile._id, - value: name, - }; - return obj; + const summoningDialogResult = await this.askSummonningMessage(sOptions); + if (summoningDialogResult) { + foundry.utils.mergeObject(itemUseConfiguration, { + createSummons: summoningDialogResult.createSummons?.value === "on", + summonsProfile: summoningDialogResult.summonsProfile?.value, + summonsOptions: { + creatureType: summoningDialogResult.creatureType?.value, + creatureSize: summoningDialogResult.creatureSize?.value, + }, }); - return mapProfiles; - }; - const summonProfiles = summonProfilesSync(spell.system.summons?.profiles); - - const creatureTypes = new Array(); - for (const type of spell.system.summons?.creatureTypes) { - const name = CONFIG.DND5E.creatureTypes[type]; - const obj = { - key: type, - value: name.label, - }; - creatureTypes.push(obj); + } else { + Logger.info(`The summoning dialog has been dismissed, not using the item.`); + return; } - - let summonningMessageResult = await this.askSummonningMessage(summonProfiles.flat(), creatureTypes.flat()); - - foundry.utils.mergeObject(itemUseConfiguration, { - createSummons: summonningMessageResult.createSummons.value === "on", - summonsProfile: summonningMessageResult.summonsProfile.value, - summonsOptions: { - creatureType: summonningMessageResult.creatureType.value, - }, - }); } if (spell.system.level === 0 && !MagicItemHelpers.isLevelScalingSettingOn()) { diff --git a/src/scripts/magic-item-summon-dialog-helper.js b/src/scripts/magic-item-summon-dialog-helper.js deleted file mode 100644 index 7fdbf52..0000000 --- a/src/scripts/magic-item-summon-dialog-helper.js +++ /dev/null @@ -1,9 +0,0 @@ -import CONSTANTS from "./constants/constants"; - -export class MagicItemSummonDialogHelper { - constructor(createSummons, summonProfiles, creatureTypes) { - this.createSummons = createSummons; - this.summonProfiles = summonProfiles; - this.creatureTypes = creatureTypes; - } -} diff --git a/src/templates/magic-item-summon-dialog.hbs b/src/templates/magic-item-summon-dialog.hbs index 0cf43b0..f4a3f1e 100644 --- a/src/templates/magic-item-summon-dialog.hbs +++ b/src/templates/magic-item-summon-dialog.hbs @@ -1,25 +1,41 @@
+ {{#if (ne createSummons null)}}
+ {{#if profiles}}
- + {{ selectOptions profiles selected=summonsProfile }}
+ {{else}} + + {{/if}}
+ + {{#if creatureSizes}}
- + +
+ +
+
+ {{/if}} + + {{#if creatureTypes}} +
+
+ {{/if}} + {{/if}}
\ No newline at end of file