From b06d6930a3df1998701c03410bcb5ed6efae147c Mon Sep 17 00:00:00 2001 From: Jeff Hitchcock Date: Tue, 6 Aug 2024 09:56:39 -0700 Subject: [PATCH] Adjust dice to avoid deprecated global terms --- dnd5e.mjs | 5 ----- module/data/item/fields/summons-field.mjs | 1 - module/dice/aggregate-damage-rolls.mjs | 4 ++-- module/dice/d20-roll.mjs | 2 ++ module/dice/damage-roll.mjs | 4 +++- module/dice/simplify-roll-formula.mjs | 10 +++++++--- module/documents/chat-message.mjs | 8 +++++--- module/documents/item.mjs | 4 ++-- module/utils.mjs | 1 - 9 files changed, 21 insertions(+), 18 deletions(-) diff --git a/dnd5e.mjs b/dnd5e.mjs index 30f43ee8d6..f07c98fd3c 100644 --- a/dnd5e.mjs +++ b/dnd5e.mjs @@ -52,11 +52,6 @@ Hooks.once("init", function() { // TODO: Remove when v11 support is dropped. CONFIG.compatibility.excludePatterns.push(/filePicker|select/); - CONFIG.compatibility.excludePatterns.push(/foundry\.dice\.terms/); - CONFIG.compatibility.excludePatterns.push( - /aggregateDamageRoll|configureDamage|preprocessFormula|simplifyRollFormula/ - ); - CONFIG.compatibility.excludePatterns.push(/core\.sourceId/); // Record Configuration Values CONFIG.DND5E = DND5E; diff --git a/module/data/item/fields/summons-field.mjs b/module/data/item/fields/summons-field.mjs index 9564edcb10..59565761b8 100644 --- a/module/data/item/fields/summons-field.mjs +++ b/module/data/item/fields/summons-field.mjs @@ -317,7 +317,6 @@ export class SummonsData extends foundry.abstract.DataModel { // Template actor (linked) found in world, create a copy for this user's item. return actor.clone({ "flags.dnd5e.summonedCopy": true, - "flags.core.sourceId": actor.uuid, "_stats.compendiumSource": actor.uuid }, {save: true}); } diff --git a/module/dice/aggregate-damage-rolls.mjs b/module/dice/aggregate-damage-rolls.mjs index 8b97ca51d2..51774c86dd 100644 --- a/module/dice/aggregate-damage-rolls.mjs +++ b/module/dice/aggregate-damage-rolls.mjs @@ -60,14 +60,14 @@ function chunkTerms(terms, type) { for ( let term of terms ) { // Plus or minus operators split chunks - if ( (term instanceof OperatorTerm) && ["+", "-"].includes(term.operator) ) { + if ( (term instanceof foundry.dice.terms.OperatorTerm) && ["+", "-"].includes(term.operator) ) { if ( currentChunk ) pushChunk(); if ( term.operator === "-" ) negative = !negative; continue; } // All other terms get added to the current chunk - term = RollTerm.fromData(foundry.utils.deepClone(term.toJSON())); + term = foundry.dice.terms.RollTerm.fromData(foundry.utils.deepClone(term.toJSON())); currentChunk ??= { terms: [], negative, type: null }; currentChunk.terms.push(term); const flavor = term.flavor?.toLowerCase().trim(); diff --git a/module/dice/d20-roll.mjs b/module/dice/d20-roll.mjs index 03dcd6043d..2bbd90d934 100644 --- a/module/dice/d20-roll.mjs +++ b/module/dice/d20-roll.mjs @@ -1,3 +1,5 @@ +const { Die, NumericTerm, OperatorTerm } = foundry.dice.terms; + /** * A type of Roll specific to a d20-based check, save, or attack roll in the 5e system. * @param {string} formula The string formula to parse diff --git a/module/dice/damage-roll.mjs b/module/dice/damage-roll.mjs index 0f6b09fa60..a2e067eae4 100644 --- a/module/dice/damage-roll.mjs +++ b/module/dice/damage-roll.mjs @@ -1,3 +1,5 @@ +const { DiceTerm, FunctionTerm, NumericTerm, OperatorTerm, ParentheticalTerm } = foundry.dice.terms; + /** * A type of Roll specific to a damage (or healing) roll in the 5e system. * @param {string} formula The string formula to parse @@ -89,7 +91,7 @@ export default class DamageRoll extends Roll { } // Merge any parenthetical terms followed by string terms - else if ( (term instanceof ParentheticalTerm || term instanceof MathTerm) && (nextTerm instanceof StringTerm) + else if ( (term instanceof ParentheticalTerm || term instanceof FunctionTerm) && (nextTerm instanceof StringTerm) && nextTerm.term.match(/^d[0-9]*$/)) { if ( term.isDeterministic ) { const newFormula = `${term.evaluate().total}${nextTerm.term}`; diff --git a/module/dice/simplify-roll-formula.mjs b/module/dice/simplify-roll-formula.mjs index c3673160f0..d736e67b2f 100644 --- a/module/dice/simplify-roll-formula.mjs +++ b/module/dice/simplify-roll-formula.mjs @@ -1,3 +1,7 @@ +const { + Coin, DiceTerm, Die, FunctionTerm, NumericTerm, OperatorTerm, ParentheticalTerm, RollTerm +} = foundry.dice.terms; + /** * A standardized helper function for simplifying the constant parts of a multipart roll formula. * @@ -196,8 +200,8 @@ function _expandParentheticalTerms(terms) { /* -------------------------------------------- */ /** - * A helper function to group terms into PoolTerms, DiceTerms, MathTerms, and NumericTerms. - * MathTerms are included as NumericTerms if they are deterministic. + * A helper function to group terms into PoolTerms, DiceTerms, FunctionTerms, and NumericTerms. + * FunctionTerms are included as NumericTerms if they are deterministic. * @param {RollTerm[]} terms An array of roll terms. * @returns {object} An object mapping term types to arrays containing roll terms of that type. */ @@ -208,7 +212,7 @@ function _groupTermsByType(terms) { return terms.reduce((obj, term, i) => { let type; if ( term instanceof DiceTerm ) type = DiceTerm; - else if ( (term instanceof MathTerm) && (term.isDeterministic) ) type = NumericTerm; + else if ( (term instanceof FunctionTerm) && (term.isDeterministic) ) type = NumericTerm; else type = term.constructor; const key = `${type.name.charAt(0).toLowerCase()}${type.name.substring(1)}s`; diff --git a/module/documents/chat-message.mjs b/module/documents/chat-message.mjs index 363ec9e4d7..4f395c77de 100644 --- a/module/documents/chat-message.mjs +++ b/module/documents/chat-message.mjs @@ -481,9 +481,11 @@ export default class ChatMessage5e extends ChatMessage { const aggregate = { type: roll.options.type, total: roll.total, constant: 0, dice: [] }; for ( let i = roll.terms.length - 1; i >= 0; ) { const term = roll.terms[i--]; - if ( !(term instanceof NumericTerm) && !(term instanceof DiceTerm) ) continue; + if ( !(term instanceof foundry.dice.terms.NumericTerm) && !(term instanceof foundry.dice.terms.DiceTerm) ) { + continue; + } const value = term.total; - if ( term instanceof DiceTerm ) aggregate.dice.push(...term.results.map(r => ({ + if ( term instanceof foundry.dice.terms.DiceTerm ) aggregate.dice.push(...term.results.map(r => ({ result: term.getResultLabel(r), classes: term.getResultCSS(r).filterJoin(" ") }))); let multiplier = 1; @@ -492,7 +494,7 @@ export default class ChatMessage5e extends ChatMessage { if ( operator.operator === "-" ) multiplier *= -1; operator = roll.terms[--i]; } - if ( term instanceof NumericTerm ) aggregate.constant += value * multiplier; + if ( term instanceof foundry.dice.terms.NumericTerm ) aggregate.constant += value * multiplier; } return aggregate; diff --git a/module/documents/item.mjs b/module/documents/item.mjs index 289332fc41..5e0a46519c 100644 --- a/module/documents/item.mjs +++ b/module/documents/item.mjs @@ -1682,10 +1682,10 @@ export default class Item5e extends SystemDocumentMixin(Item) { // Attempt to simplify by combining like dice terms let simplified = false; - if ( (s.terms[0] instanceof Die) && (s.terms.length === 1) ) { + if ( (s.terms[0] instanceof foundry.dice.terms.Die) && (s.terms.length === 1) ) { const d0 = p0.terms[0]; const s0 = s.terms[0]; - if ( (d0 instanceof Die) && (d0.faces === s0.faces) && d0.modifiers.equals(s0.modifiers) ) { + if ( (d0 instanceof foundry.dice.terms.Die) && (d0.faces === s0.faces) && d0.modifiers.equals(s0.modifiers) ) { d0.number += s0.number; parts[0] = p0.formula; simplified = true; diff --git a/module/utils.mjs b/module/utils.mjs index 2610fdf6ca..480d54d10f 100644 --- a/module/utils.mjs +++ b/module/utils.mjs @@ -752,7 +752,6 @@ function _synchronizeActorSpells(actor, spellsMap) { Object.assign(spellData.system, {preparation, uses}); spellData.system.save.dc = save.dc; foundry.utils.setProperty(spellData, "_stats.compendiumSource", source.uuid); - foundry.utils.setProperty(spellData, "flags.core.sourceId", source.uuid); // Record spells to be deleted and created toDelete.push(spell.id);