Skip to content

Commit

Permalink
[#4445] Fix activity consumption breaking refund & consuming slot
Browse files Browse the repository at this point in the history
Fixed bug causing the "Consume Resource" button to still appear on
chat card if activity uses were consumed. Also fixed a bug causing
spell slot to be consumed when the "Consume Resource" button is
clicked from the chat card even if slot consumption is unchecked.

Closes #4445
  • Loading branch information
arbron committed Sep 27, 2024
1 parent 9e959ef commit c3eaa13
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions module/documents/activity/mixin.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,12 @@ export default Base => class extends PseudoDocumentMixin(Base) {
if ( !item ) continue;
const itemUpdate = {};
for ( const { keyPath, delta } of changes ) {
const value = foundry.utils.getProperty(item, keyPath) - delta;
let currentValue;
if ( keyPath.startsWith("system.activities") ) {
const [id, ...kp] = keyPath.slice(18).split(".");
currentValue = foundry.utils.getProperty(item.system.activities?.get(id) ?? {}, kp.join("."));
} else currentValue = foundry.utils.getProperty(item, keyPath);
const value = currentValue - delta;
if ( !Number.isNaN(value) ) itemUpdate[keyPath] = value;
}
if ( !foundry.utils.isEmpty(itemUpdate) ) {
Expand Down Expand Up @@ -425,7 +430,12 @@ export default Base => class extends PseudoDocumentMixin(Base) {
const getDeltas = (document, updates) => {
updates = foundry.utils.flattenObject(updates);
return Object.entries(updates).map(([keyPath, value]) => {
const delta = value - foundry.utils.getProperty(document, keyPath);
let currentValue;
if ( keyPath.startsWith("system.activities") ) {
const [id, ...kp] = keyPath.slice(18).split(".");
currentValue = foundry.utils.getProperty(document.system.activities?.get(id) ?? {}, kp.join("."));
} else currentValue = foundry.utils.getProperty(document, keyPath);
const delta = value - currentValue;
if ( delta && !Number.isNaN(delta) ) return { keyPath, delta };
return null;
}).filter(_ => _);
Expand All @@ -438,6 +448,7 @@ export default Base => class extends PseudoDocumentMixin(Base) {
return obj;
}, {})
};
console.log(consumed);
if ( foundry.utils.isEmpty(consumed.actor) ) delete consumed.actor;
if ( foundry.utils.isEmpty(consumed.item) ) delete consumed.item;

Expand Down Expand Up @@ -665,7 +676,8 @@ export default Base => class extends PseudoDocumentMixin(Base) {
}

// Handle spell slot consumption
if ( ((config.consume === true) || config.consume.spellSlot) && this.requiresSpellSlot ) {
if ( ((config.consume === true) || config.consume.spellSlot)
&& this.requiresSpellSlot && this.consumption.spellSlot ) {
const mode = this.item.system.preparation.mode;
const isLeveled = ["always", "prepared"].includes(mode);
const effectiveLevel = this.item.system.level + (config.scaling ?? 0);
Expand Down

0 comments on commit c3eaa13

Please sign in to comment.