From 4cd1b4692a5ae6a6f477345322441750209d0e44 Mon Sep 17 00:00:00 2001 From: Jeff Hitchcock Date: Sun, 7 Aug 2022 10:49:23 -0700 Subject: [PATCH] [#1620, #1626, #1689] Add spell config & type restriction to ItemGrant advancement --- dnd5e.css | 2 +- lang/en.json | 1 + less/items.less | 2 +- module/advancement/advancement-config.mjs | 101 +++++++++++++++++- module/advancement/advancement.mjs | 27 +++++ module/advancement/types/item-grant.mjs | 81 +++++--------- module/utils.mjs | 3 +- templates/advancement/item-grant-config.hbs | 8 +- .../parts/advancement-spell-config.hbs | 28 +++++ 9 files changed, 191 insertions(+), 62 deletions(-) create mode 100644 templates/advancement/parts/advancement-spell-config.hbs diff --git a/dnd5e.css b/dnd5e.css index 1a15c427ba..c515f9ce3d 100644 --- a/dnd5e.css +++ b/dnd5e.css @@ -1646,7 +1646,7 @@ margin-inline-start: 0.5em; } .dnd5e.sheet.item .advancement .items-list .main-controls .configuration-mode-control a { - text-align: inline-start; + text-align: start; } .dnd5e.sheet.item .advancement .items-list .main-controls .item-add { padding-block-start: 0.2em; diff --git a/lang/en.json b/lang/en.json index 1b1d699510..6ac02889d3 100644 --- a/lang/en.json +++ b/lang/en.json @@ -118,6 +118,7 @@ "DND5E.AdvancementItemGrantRecursiveWarning": "You cannot grant an item in its own advancement.", "DND5E.AdvancementItemGrantOptional": "Optional", "DND5E.AdvancementItemGrantOptionalHint": "If optional, players will be given the option to opt out of any of the following items, otherwise all of them are granted.", +"DND5E.AdvancementItemTypeInvalidWarning": "{type} items cannot be added with this advancement type.", "DND5E.AdvancementLevelHeader": "Level {level}", "DND5E.AdvancementLevelAnyHeader": "Any Level", "DND5E.AdvancementLevelNoneHeader": "No Level", diff --git a/less/items.less b/less/items.less index 6ffddcd018..499530b726 100644 --- a/less/items.less +++ b/less/items.less @@ -251,7 +251,7 @@ .configuration-mode-control { flex: 1 0; margin-inline-start: 0.5em; - a { text-align: inline-start; } + a { text-align: start; } } .item-add { padding-block-start: 0.2em; diff --git a/module/advancement/advancement-config.mjs b/module/advancement/advancement-config.mjs index bdd95d7a25..8211ca61ba 100644 --- a/module/advancement/advancement-config.mjs +++ b/module/advancement/advancement-config.mjs @@ -2,8 +2,10 @@ * Base configuration application for advancements that can be extended by other types to implement custom * editing interfaces. * - * @param {Advancement} advancement The advancement item being edited. - * @param {object} [options={}] Additional options passed to FormApplication. + * @property {Advancement} advancement The advancement item being edited. + * @param {object} [options={}] Additional options passed to FormApplication. + * @param {string} [options.dropKeyPath=null] Path within advancement configuration where dropped items are stored. + * If populated, will enable default drop & delete behavior. */ export default class AdvancementConfig extends FormApplication { constructor(advancement, options={}) { @@ -32,7 +34,8 @@ export default class AdvancementConfig extends FormApplication { width: 400, height: "auto", submitOnChange: true, - closeOnSubmit: false + closeOnSubmit: false, + dropKeyPath: null }); } @@ -52,6 +55,7 @@ export default class AdvancementConfig extends FormApplication { if ( ["class", "subclass"].includes(this.item.type) ) delete levels[0]; else levels[0] = game.i18n.localize("DND5E.AdvancementLevelAnyHeader"); return { + CONFIG: CONFIG.DND5E, data: this.advancement.data, default: { title: this.advancement.constructor.metadata.title, @@ -76,6 +80,15 @@ export default class AdvancementConfig extends FormApplication { /* -------------------------------------------- */ + activateListeners(html) { + super.activateListeners(html); + + // Remove an item from the list + if ( this.options.dropKeyPath ) html.on("click", ".item-delete", this._onItemDelete.bind(this)); + } + + /* -------------------------------------------- */ + /** @inheritdoc */ async _updateObject(event, formData) { let updates = foundry.utils.expandObject(formData).data; @@ -101,4 +114,86 @@ export default class AdvancementConfig extends FormApplication { }, {}); } + /* -------------------------------------------- */ + /* Drag & Drop for Item Pools */ + /* -------------------------------------------- */ + + /** + * Handle deleting an existing Item entry from the Advancement. + * @param {Event} event The originating click event. + * @returns {Promise} The updated parent Item after the application re-renders. + * @protected + */ + async _onItemDelete(event) { + event.preventDefault(); + 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: await this.prepareConfigurationUpdate({ + [this.options.dropKeyPath]: items.filter(uuid => uuid !== uuidToDelete) + }) }; + console.log(updates); + await this.advancement.update(updates); + this.render(); + } + + /* -------------------------------------------- */ + + /** @inheritdoc */ + _canDragDrop() { + return this.isEditable; + } + + /* -------------------------------------------- */ + + /** @inheritdoc */ + async _onDrop(event) { + if ( !this.options.dropKeyPath ) return console.error( + "AdvancementConfig#options.dropKeyPath must be configured or #_onDrop must be overridden to support" + + " drag and drop on advancement config items." + ); + + // Try to extract the data + let data; + try { + data = JSON.parse(event.dataTransfer.getData("text/plain")); + } catch(err) { + return false; + } + + if ( data.type !== "Item" ) return false; + const item = await Item.implementation.fromDropData(data); + + const verified = this._verifyDroppedItem(event, item); + if ( !verified ) return false; + + const existingItems = foundry.utils.getProperty(this.advancement.data.configuration, this.options.dropKeyPath); + + // Abort if this uuid is the parent item + if ( item.uuid === this.item.uuid ) { + return ui.notifications.warn(game.i18n.localize("DND5E.AdvancementItemGrantRecursiveWarning")); + } + + // Abort if this uuid exists already + if ( existingItems.includes(item.uuid) ) { + return ui.notifications.warn(game.i18n.localize("DND5E.AdvancementItemGrantDuplicateWarning")); + } + + await this.advancement.update({[`configuration.${this.options.dropKeyPath}`]: [...existingItems, item.uuid]}); + this.render(); + } + + /* -------------------------------------------- */ + + /** + * Called when an item is dropped to verify the Item before it is saved. + * @param {Event} event Triggering drop event. + * @param {Item5e} item The materialized Item that was dropped. + * @returns {boolean} Is the dropped Item valid? + * @protected + */ + _verifyDroppedItem(event, item) { + return true; + } + } diff --git a/module/advancement/advancement.mjs b/module/advancement/advancement.mjs index 082cb0d510..e948c2854d 100644 --- a/module/advancement/advancement.mjs +++ b/module/advancement/advancement.mjs @@ -308,4 +308,31 @@ export default class Advancement { * @abstract */ async reverse(level) { } + + /* -------------------------------------------- */ + + /** + * Helper method to prepare spell customizations. + * @param {object} spell Spell configuration object. + * @returns {object} Object of updates to apply to item. + * @protected + */ + _prepareSpellChanges(spell) { + const updates = {}; + if ( spell.ability ) updates["system.ability"] = spell.ability; + if ( spell.preparation ) updates["system.preparation.mode"] = spell.preparation; + if ( spell.uses?.max ) { + updates["system.uses.max"] = spell.uses.max; + if ( Number.isNumeric(spell.uses.max) ) updates["system.uses.value"] = parseInt(spell.uses.max); + else { + try { + const rollData = this.actor.getRollData({ deterministic: true }); + const formula = Roll.replaceFormulaData(spell.uses.max, rollData, {missing: 0}); + updates["system.uses.value"] = Roll.safeEval(formula); + } catch(e) { } + } + } + if ( spell.uses?.per ) updates["system.uses.per"] = spell.uses.per; + return updates; + } } diff --git a/module/advancement/types/item-grant.mjs b/module/advancement/types/item-grant.mjs index e1c104dbe2..50b1bf9e4f 100644 --- a/module/advancement/types/item-grant.mjs +++ b/module/advancement/types/item-grant.mjs @@ -12,7 +12,11 @@ export class ItemGrantAdvancement extends Advancement { static get metadata() { return foundry.utils.mergeObject(super.metadata, { defaults: { - configuration: { items: [], optional: false } + configuration: { + items: [], + optional: false, + spell: null + } }, order: 40, icon: "systems/dnd5e/icons/svg/item-grant.svg", @@ -25,6 +29,14 @@ export class ItemGrantAdvancement extends Advancement { }); } + /* -------------------------------------------- */ + + /** + * The item types that are supported in Item Grant. + * @type {Set} + */ + static VALID_TYPES = new Set(["feat", "spell", "consumable", "backpack", "equipment", "loot", "tool", "weapon"]); + /* -------------------------------------------- */ /* Display Methods */ /* -------------------------------------------- */ @@ -66,6 +78,7 @@ export class ItemGrantAdvancement extends Advancement { async apply(level, data, retainedData={}) { const items = []; const updates = {}; + const spellChanges = this.data.configuration.spell ? this._prepareSpellChanges(this.data.configuration.spell) : {}; for ( const [uuid, selected] of Object.entries(data) ) { if ( !selected ) continue; @@ -79,6 +92,7 @@ export class ItemGrantAdvancement extends Advancement { "flags.dnd5e.advancementOrigin": `${this.item.id}.${this.id}` }, {keepId: true}).toObject(); } + foundry.utils.mergeObject(itemData, spellChanges); items.push(itemData); // TODO: Trigger any additional advancement steps for added items @@ -129,72 +143,29 @@ export class ItemGrantConfig extends AdvancementConfig { static get defaultOptions() { return foundry.utils.mergeObject(super.defaultOptions, { dragDrop: [{ dropSelector: ".drop-target" }], + dropKeyPath: "items", template: "systems/dnd5e/templates/advancement/item-grant-config.hbs" }); } /* -------------------------------------------- */ - activateListeners(html) { - super.activateListeners(html); - - // Remove an item from the list - html.on("click", ".item-delete", this._onItemDelete.bind(this)); - } - - /* -------------------------------------------- */ - - /** - * Handle deleting an existing Item entry from the Advancement. - * @param {Event} event The originating click event. - * @returns {Promise} The promise for the updated parent Item which resolves after the application re-renders - * @private - */ - async _onItemDelete(event) { - event.preventDefault(); - const uuidToDelete = event.currentTarget.closest("[data-item-uuid]")?.dataset.itemUuid; - if ( !uuidToDelete ) return; - const items = this.advancement.data.configuration.items.filter(uuid => uuid !== uuidToDelete); - const updates = { configuration: this.prepareConfigurationUpdate({ items }) }; - await this.advancement.update(updates); - this.render(); - } - - /* -------------------------------------------- */ - /** @inheritdoc */ - _canDragDrop() { - return this.isEditable; + async getData() { + const data = super.getData(); + data.items = data.data.configuration.items.map(fromUuidSync); + data.showSpellConfig = data.items.some(i => i.type === "spell"); + return data; } /* -------------------------------------------- */ /** @inheritdoc */ - async _onDrop(event) { - // Try to extract the data - let data; - try { - data = JSON.parse(event.dataTransfer.getData("text/plain")); - } catch(err) { - return false; - } - - if ( data.type !== "Item" ) return false; - const item = await Item.implementation.fromDropData(data); - const existingItems = this.advancement.data.configuration.items; - - // Abort if this uuid is the parent item - if ( item.uuid === this.item.uuid ) { - return ui.notifications.warn(game.i18n.localize("DND5E.AdvancementItemGrantRecursiveWarning")); - } - - // Abort if this uuid exists already - if ( existingItems.includes(item.uuid) ) { - return ui.notifications.warn(game.i18n.localize("DND5E.AdvancementItemGrantDuplicateWarning")); - } - - await this.advancement.update({"configuration.items": [...existingItems, item.uuid]}); - this.render(); + _verifyDroppedItem(event, item) { + if ( this.advancement.constructor.VALID_TYPES.has(item.type) ) return true; + const type = game.i18n.localize(`ITEM.Type${item.type.capitalize()}`); + ui.notifications.warn(game.i18n.format("DND5E.AdvancementItemTypeInvalidWarning", { type })); + return false; } } diff --git a/module/utils.mjs b/module/utils.mjs index a8aaaf307a..5d071cc9e4 100644 --- a/module/utils.mjs +++ b/module/utils.mjs @@ -110,7 +110,8 @@ export async function preloadHandlebarsTemplates() { "systems/dnd5e/templates/items/parts/item-spellcasting.hbs", // Advancement Partials - "systems/dnd5e/templates/advancement/parts/advancement-controls.hbs" + "systems/dnd5e/templates/advancement/parts/advancement-controls.hbs", + "systems/dnd5e/templates/advancement/parts/advancement-spell-config.hbs" ]; const paths = {}; diff --git a/templates/advancement/item-grant-config.hbs b/templates/advancement/item-grant-config.hbs index 8b78ca2b45..abf5a08216 100644 --- a/templates/advancement/item-grant-config.hbs +++ b/templates/advancement/item-grant-config.hbs @@ -8,6 +8,10 @@

{{localize "DND5E.AdvancementItemGrantOptionalHint"}}

+ {{#if showSpellConfig}} + {{> "dnd5e.advancement-spell-config"}} + {{/if}} +
  1. {{localize "DOCUMENT.Items"}}

  2. @@ -16,7 +20,9 @@
  3. {{{dnd5e-linkForUuid this}}}
    - + + +
  4. {{/each}} diff --git a/templates/advancement/parts/advancement-spell-config.hbs b/templates/advancement/parts/advancement-spell-config.hbs new file mode 100644 index 0000000000..1abf2c2bfe --- /dev/null +++ b/templates/advancement/parts/advancement-spell-config.hbs @@ -0,0 +1,28 @@ +
    + {{log this}} + +
    + +
    +
    + +
    + +
    + +
    +
    + +
    + +
    + + +
    +