diff --git a/lang/en.json b/lang/en.json index 797608e..01eb8bb 100644 --- a/lang/en.json +++ b/lang/en.json @@ -14,6 +14,7 @@ "SIMPLE.NotifyGroupDuplicate": "Attribute group already exists.", "SIMPLE.NotifyGroupAttrDuplicate": "Attribute group already exists as an attribute.", "SIMPLE.NotifyGroupAlphanumeric": "Attribute group names may not contain spaces or periods.", + "SIMPLE.NotifyGroupReserved": "Attribute group \"{key}\" is reserved and cannot be used.", "SIMPLE.NotifyAttrDuplicate": "Attribute key already exists as a group.", "SIMPLE.ResourceMin": "Min", diff --git a/module/actor.js b/module/actor.js index 9b7843a..8288404 100644 --- a/module/actor.js +++ b/module/actor.js @@ -49,7 +49,6 @@ export class SimpleActor extends Actor { if ( !!shorthand ) { delete data.attributes; delete data.attr; - delete data.abil; delete data.groups; } return data; @@ -211,11 +210,11 @@ export class SimpleActor extends Actor { } // Non-grouped attributes. if ( data.attributes[k]?.value ) { - data.attributes[k].value = Roll.replaceFormulaData(data.attributes[k].value, data); + data.attributes[k].value = Roll.replaceFormulaData(String(data.attributes[k].value), data); } // Grouped attributes. else if ( attr ) { - data.attributes[k][attr].value = Roll.replaceFormulaData(data.attributes[k][attr].value, data); + data.attributes[k][attr].value = Roll.replaceFormulaData(String(data.attributes[k][attr].value), data); } // Duplicate values to shorthand. diff --git a/module/helper.js b/module/helper.js index 427982a..4997798 100644 --- a/module/helper.js +++ b/module/helper.js @@ -244,6 +244,12 @@ export class EntitySheetHelper { return false; } + // Check for reserved group names. + if ( ["attr", "attributes"].includes(groupName) ) { + ui.notifications.error(game.i18n.format("SIMPLE.NotifyGroupReserved", {key: groupName})); + return false; + } + // Check for whitespace or periods. if ( groupName.match(/[\s|\.]/i) ) { ui.notifications.error(game.i18n.localize("SIMPLE.NotifyGroupAlphanumeric")); @@ -481,6 +487,7 @@ export class EntitySheetHelper { static updateGroups(formData, document) { // Handle the free-form groups list const formGroups = expandObject(formData).data.groups || {}; + const documentGroups = Object.keys(document.data.data.groups || {}); const groups = Object.values(formGroups).reduce((obj, v) => { // If there are duplicate groups, collapse them. if ( Array.isArray(v["key"]) ) { @@ -488,15 +495,23 @@ export class EntitySheetHelper { } // Trim and clean up. let k = v["key"].trim(); - if ( /[\s\.]/.test(k) ) return ui.notifications.error("Group keys may not contain spaces or periods"); + // Validate groups. + let isValidGroup = true; + // Skip validation for existing/duplicate groups since they're collapsed above. + if ( !documentGroups.includes(k) ) { + isValidGroup = this.validateGroup(k, document); + } + // Delete the key and add the group to the reducer if valid. delete v["key"]; - obj[k] = v; + if (isValidGroup) obj[k] = v; return obj; }, {}); // Remove groups which are no longer used - for ( let k of Object.keys(document.data.data.groups) ) { - if ( !groups.hasOwnProperty(k) ) groups[`-=${k}`] = null; + if (groups) { + for ( let k of Object.keys(document.data.data.groups)) { + if ( !groups.hasOwnProperty(k) ) groups[`-=${k}`] = null; + } } // Re-combine formData