Skip to content

Commit

Permalink
Add ability to limit dropped spells to a certain level
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron committed Jul 4, 2022
1 parent bdcfa8f commit e171758
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
"DND5E.AdvancementItemChoiceHint": "Present the player with a choice of items (such as equipment, features, or spells) that they can choose for their character at one or more levels.",
"DND5E.AdvancementItemChoiceChosen": "Chosen: {current} of {max}",
"DND5E.AdvancementItemChoiceLevelsHint": "Specify how many choices are allowed at each level.",
"DND5E.AdvancementItemChoiceSpellLevelAvailable": "Any Available Level",
"DND5E.AdvancementItemChoiceSpellLevelAvailableWarning": "Only {level} or lower spells can be chosen for this advancement.",
"DND5E.AdvancementItemChoiceSpellLevelSpecificWarning": "Only {level} spells can be chosen for this advancement.",
"DND5E.AdvancementItemChoiceSpellLevelHint": "Only allow choices from spells of this level.",
"DND5E.AdvancementItemChoiceType": "Item Type",
"DND5E.AdvancementItemChoiceTypeHint": "Restrict what Item types can be choosen.",
"DND5E.AdvancementItemChoiceTypeAny": "Anything",
Expand Down
32 changes: 32 additions & 0 deletions module/advancement/item-choice.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,22 @@ export class ItemChoiceFlow extends AdvancementFlow {

// TODO: Check to ensure the dropped item hasn't been selected at a lower level

// If spell level is restricted, ensure the spell is of the appropriate level
const spellLevel = this.advancement.data.configuration.spell?.level;
if ( type === "spell" && spellLevel ) {
if ( spellLevel === "available") {
const maxSlot = this._maxSpellSlotLevel();
if ( item.data.data.level > maxSlot ) return ui.notifications.warn(game.i18n.format(
"DND5E.AdvancementItemChoiceSpellLevelAvailableWarning", { level: CONFIG.DND5E.spellLevels[maxSlot] }
));
// TODO: Ensure the spell level is less than or equal to maximum available spell slot
} else if ( item.data.data.level !== parseInt(spellLevel) ) {
return ui.notifications.warn(game.i18n.format(
"DND5E.AdvancementItemChoiceSpellLevelSpecificWarning", { level: CONFIG.DND5E.spellLevels[spellLevel] }
));
}
}

// Mark the item as selected
this.selected.add(item.uuid);

Expand All @@ -363,6 +379,22 @@ export class ItemChoiceFlow extends AdvancementFlow {

/* -------------------------------------------- */

/**
* Determine the maximum spell slot level for the actor to which this advancement is being applied.
* @returns {number}
*/
_maxSpellSlotLevel() {
const largestSlot = Object.entries(this.advancement.actor.data.data.spells).reduce((slot, [key, data]) => {
if ( data.max === 0 ) return slot;
const level = parseInt(key.replace("spell", ""));
if ( !Number.isNaN(level) && level > slot ) return level;
return slot;
}, -1);
return Math.max(this.advancement.actor.data.data.spells.pact.level, largestSlot);
}

/* -------------------------------------------- */

/** @inheritdoc */
async _updateObject(event, formData) {
const retainedData = this.retainedData?.items.reduce((obj, i) => {
Expand Down
16 changes: 16 additions & 0 deletions templates/advancement/item-choice-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
</div>

{{#if showSpellConfig}}
<div class="form-group">
<label>{{localize "DND5E.SpellLevel"}}</label>
<div class="form-fields">
<select name="data.configuration.spell.level">
{{#select data.configuration.spell.level}}
<option value="">&mdash;</option>
{{#each CONFIG.spellLevels as |label key|}}
<option value="{{key}}">{{label}}</option>
{{/each}}
<option value="available">{{localize "DND5E.AdvancementItemChoiceSpellLevelAvailable"}}</option>
{{/select}}
</select>
</div>
<p class="hint">{{localize "DND5E.AdvancementItemChoiceSpellLevelHint"}}</p>
</div>

{{> "systems/dnd5e/templates/advancement/parts/item-spell-config.html"}}
{{/if}}

Expand Down

0 comments on commit e171758

Please sign in to comment.