diff --git a/module/advancement/advancement-config.mjs b/module/advancement/advancement-config.mjs index 1f58a91211..ddfe8dc4e0 100644 --- a/module/advancement/advancement-config.mjs +++ b/module/advancement/advancement-config.mjs @@ -74,7 +74,7 @@ export default class AdvancementConfig extends FormApplication { * @param {object} configuration Configuration object. * @returns {object} Modified configuration. */ - prepareConfigurationUpdate(configuration) { + async prepareConfigurationUpdate(configuration) { return configuration; } @@ -92,7 +92,7 @@ export default class AdvancementConfig extends FormApplication { /** @inheritdoc */ async _updateObject(event, formData) { let updates = foundry.utils.expandObject(formData).data; - if ( updates.configuration ) updates.configuration = this.prepareConfigurationUpdate(updates.configuration); + if ( updates.configuration ) updates.configuration = await this.prepareConfigurationUpdate(updates.configuration); await this.advancement.update(updates); this.render(); } @@ -129,9 +129,10 @@ export default class AdvancementConfig extends FormApplication { const uuidToDelete = event.currentTarget.closest("[data-item-uuid]")?.dataset.itemUuid; if ( !uuidToDelete ) return; const items = foundry.utils.getProperty(this.advancement.data.configuration, this.options.dropKeyPath); - const updates = { configuration: this.prepareConfigurationUpdate({ + const updates = { configuration: await this.prepareConfigurationUpdate({ [this.options.dropKeyPath]: items.filter(uuid => uuid !== uuidToDelete) }) }; + console.log(updates); await this.advancement.update(updates); this.render(); } diff --git a/module/advancement/types/item-choice.mjs b/module/advancement/types/item-choice.mjs index d160961957..640d668f16 100644 --- a/module/advancement/types/item-choice.mjs +++ b/module/advancement/types/item-choice.mjs @@ -149,14 +149,18 @@ export class ItemChoiceAdvancement extends Advancement { /** * Verify that the provided item can be used with this advancement based on the configuration. - * @param {Item5e} item Item that needs to be tested. - * @param {object} options - * @param {boolean} [options.warn=true] Display UI notifications with warning messages. - * @returns {boolean} Can this item be used? + * @param {Item5e} item Item that needs to be tested. + * @param {object} config + * @param {string} config.restriction Type restriction on this advancement. + * @param {string} config.spellLevel Spell level limitation. + * @param {boolean} [config.warn=true] Display UI notifications with warning messages. + * @returns {boolean} Can this item be used? */ - _validateItemType(item, { warn=true }={}) { + _validateItemType(item, { restriction, spellLevel, warn=true }={}) { + restriction ??= this.data.configuration.type; + spellLevel ??= this.data.configuration.spell?.level; + // Type restriction is set and the item type does not match the selected type - const restriction = this.data.configuration.type; if ( restriction && (restriction !== item.type) ) { const type = game.i18n.localize(`ITEM.Type${restriction.capitalize()}`); if ( warn ) ui.notifications.warn(game.i18n.format("DND5E.AdvancementItemChoiceTypeWarning", { type })); @@ -171,7 +175,7 @@ export class ItemChoiceAdvancement extends Advancement { } // If spell level is restricted, ensure the spell is of the appropriate level - const l = parseInt(this.data.configuration.spell?.level); + const l = parseInt(spellLevel); if ( (restriction === "spell") && Number.isNumeric(l) && (item.system.level !== l) ) { if ( warn ) ui.notifications.warn(game.i18n.format( "DND5E.AdvancementItemChoiceSpellLevelSpecificWarning", { level: CONFIG.DND5E.spellLevels[l] } @@ -218,8 +222,19 @@ export class ItemChoiceConfig extends AdvancementConfig { /* -------------------------------------------- */ /** @inheritdoc */ - prepareConfigurationUpdate(configuration) { + async prepareConfigurationUpdate(configuration) { if ( configuration.choices ) configuration.choices = this.constructor._cleanedObject(configuration.choices); + + // Ensure items are still valid if type restriction or spell restriction are changed + configuration.pool ??= this.advancement.data.configuration.pool; + configuration.pool = await configuration.pool.reduce(async (pool, uuid) => { + const item = await fromUuid(uuid); + if ( this.advancement._validateItemType(item, { + restriction: configuration.type, spellLevel: configuration.spell?.level ?? false, warn: false + }) ) return [...await pool, uuid]; + return pool; + }, []); + return configuration; }