Skip to content

Commit

Permalink
#185 Add localization files + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
PwQt committed Oct 17, 2024
1 parent 1d7574a commit d41200b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 57 deletions.
4 changes: 3 additions & 1 deletion src/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
42 changes: 42 additions & 0 deletions src/scripts/magic-item-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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,
},
});
Expand Down
45 changes: 14 additions & 31 deletions src/scripts/magic-item-owned-entry/OwnedMagicItemSpell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
9 changes: 0 additions & 9 deletions src/scripts/magic-item-summon-dialog-helper.js

This file was deleted.

36 changes: 26 additions & 10 deletions src/templates/magic-item-summon-dialog.hbs
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
<form id="summon-form">
{{#if (ne createSummons null)}}
<div class="form-group">
<label class="checkbox">
<input type="checkbox" name="createSummons" {{checked createSummons}} />
Place Summons
<input type="checkbox" name="createSummons" {{ checked createSummons }}>
{{ localize "DND5E.Summoning.Action.Place" }}
</label>
{{#if profiles}}
<div class="form-fields">
<select name="summonsProfile" aria-label="Summons Profile">
{{#each summonProfiles as |profile|}}
<option value="{{profile.key}}">{{profile.value}}</option>
{{/each}}
<select name="summonsProfile" aria-label="{{ localize 'DND5E.Summoning.Profile.Label' }}">
{{ selectOptions profiles selected=summonsProfile }}
</select>
</div>
{{else}}
<input type="hidden" name="summonsProfile" value="{{ profile }}">
{{/if}}
</div>

{{#if creatureSizes}}
<div class="form-group">
<label>Creature Type</label>
<label>{{ localize "DND5E.Size" }}</label>
<div class="form-fields">
<select name="creatureSize">
{{ selectOptions creatureSizes }}
</select>
</div>
</div>
{{/if}}

{{#if creatureTypes}}
<div class="form-group">
<label>{{ localize "DND5E.CreatureType" }}</label>
<div class="form-fields">
<select name="creatureType">
{{#each creatureTypes as |type|}}
<option value="{{type.key}}">{{type.value}}</option>
{{/each}}
{{ selectOptions creatureTypes }}
</select>
</div>
</div>
{{/if}}
{{/if}}
</form>

0 comments on commit d41200b

Please sign in to comment.