diff --git a/module/applications/sheets/actors/character.mjs b/module/applications/sheets/actors/character.mjs index 540a8392..2ec176ae 100644 --- a/module/applications/sheets/actors/character.mjs +++ b/module/applications/sheets/actors/character.mjs @@ -754,8 +754,6 @@ export default class CharacterSheet extends DHBaseActorSheet { await config.resourceUpdates.updateResources(); } - //TODO: redo toggleEquipItem method - /** * Toggles the equipped state of an item (armor or weapon). * @type {ApplicationClickAction} @@ -830,12 +828,13 @@ export default class CharacterSheet extends DHBaseActorSheet { */ static async #toggleVault(_event, button) { const doc = await getDocFromElement(button); - const { available } = this.document.system.loadoutSlot; - if (doc.system.inVault && !available && !doc.system.loadoutIgnore) { - return ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.loadoutMaxReached')); - } - - await doc?.update({ 'system.inVault': !doc.system.inVault }); + const changedData = await this.document.toggleDomainCardVault(doc); + const removedData = changedData.filter(x => !x.add); + this.document.update({ + 'system.sidebarFavorites': [ + ...this.document.system.sidebarFavorites.filter(x => removedData.every(r => r.item.id !== x.id)) + ] + }); } /** diff --git a/module/applications/sheets/api/application-mixin.mjs b/module/applications/sheets/api/application-mixin.mjs index 6e1827d0..0c0de075 100644 --- a/module/applications/sheets/api/application-mixin.mjs +++ b/module/applications/sheets/api/application-mixin.mjs @@ -554,6 +554,7 @@ export default function DHApplicationMixin(Base) { callback: async (target, _event) => { const doc = await getDocFromElement(target); if (doc.type === 'domainCard') { + this.document.setFavoriteItem(doc, false); } else { this.document.update({ 'system.sidebarFavorites': this.document.system.sidebarFavorites.filter( diff --git a/module/data/item/domainCard.mjs b/module/data/item/domainCard.mjs index 2c272f75..327dafce 100644 --- a/module/data/item/domainCard.mjs +++ b/module/data/item/domainCard.mjs @@ -72,26 +72,6 @@ export default class DHDomainCard extends BaseDataItem { /* -------------------------------------------- */ - /**@inheritdoc */ - async _preUpdate(data, options, user) { - const allowed = await super._preUpdate(data, options, user); - if (allowed === false) return; - - if (this.parent.parent?.type === 'character') { - if ( - data.system?.inVault && - !this.inVault && - this.parent.parent.system.sidebarFavorites.find(x => x?.id === this.parent.id) - ) { - this.parent.parent.update({ - 'system.sidebarFavorites': this.parent.parent.system.sidebarFavorites.filter( - x => x.id !== this.parent.id - ) - }); - } - } - } - /**@inheritdoc */ async _preCreate(data, options, user) { const allowed = await super._preCreate(data, options, user); diff --git a/module/documents/actor.mjs b/module/documents/actor.mjs index 639938b3..c04d886b 100644 --- a/module/documents/actor.mjs +++ b/module/documents/actor.mjs @@ -993,6 +993,18 @@ export default class DhpActor extends Actor { return allTokens; } + async toggleDomainCardVault(card, options = { render: true }) { + const { render } = options; + const { available } = this.system.loadoutSlot; + + if (card.system.inVault && !available && !card.system.loadoutIgnore) { + return ui.notifications.warn(game.i18n.localize('DAGGERHEART.UI.Notifications.loadoutMaxReached')); + } + + await card?.update({ 'system.inVault': !card.system.inVault }, { render }); + return [{ item: card, add: !card.system.inVault }]; + } + async unequipBeforeEquip(itemToEquip, options = { render: true }) { const { render } = options; @@ -1030,7 +1042,7 @@ export default class DhpActor extends Actor { await item.update({ 'system.equipped': equip }, { render }); }; - if (item.system.equipped) { + if (item.system.equipped && [undefined, false].includes(options.equip)) { await updateAndAddChangedItem(item, false); return changedItems; } @@ -1051,7 +1063,7 @@ export default class DhpActor extends Actor { ); } - const unequippedItems = await this.unequipBeforeEquip(item, { render }); + const unequippedItems = await this.unequipBeforeEquip(item, { render: false }); changedItems.push(...unequippedItems.map(x => ({ item: x, add: false }))); await updateAndAddChangedItem(item, true); break; @@ -1060,11 +1072,18 @@ export default class DhpActor extends Actor { return changedItems; } + /* This is very convoluted, and there is almost certainly a better way to do it. I couldn't get it working any better way atm though. */ async setFavoriteItem(item, setFavorited) { const favoritesToRemove = []; const favoritesToAdd = []; - if (item.type === 'weapon') { - const changedData = await this.toggleEquipItem(item, { render: false }); + if (['weapon', 'armor'].includes(item.type)) { + const changedData = await this.toggleEquipItem(item, { render: false, equip: setFavorited }); + for (const data of changedData) { + if (data.add) favoritesToAdd.push(data.item); + else favoritesToRemove.push(data.item); + } + } else if (item.type === 'domainCard') { + const changedData = await this.toggleDomainCardVault(item, { render: false }); for (const data of changedData) { if (data.add) favoritesToAdd.push(data.item); else favoritesToRemove.push(data.item);