From ce81212bbe04b72fa56844529dc2f304cac32f78 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 14 Mar 2026 15:58:06 -0600 Subject: [PATCH] Add a group property on the Item data model --- langs/en-ca.json | 3 +- module/data/Item/generic.mjs | 6 ++++ module/documents/Actor.mjs | 38 ++++++++++++++++++++++++++ templates/GenericItemSheet/content.hbs | 12 ++++++++ 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/langs/en-ca.json b/langs/en-ca.json index 0377938..3e4c89f 100644 --- a/langs/en-ca.json +++ b/langs/en-ca.json @@ -59,7 +59,8 @@ "item": { "weight": "Weight", "quantity": "Quantity", - "equipped": "Equipped" + "equipped": "Equipped", + "group": "Group" } }, "Apps": { diff --git a/module/data/Item/generic.mjs b/module/data/Item/generic.mjs index b030f6f..e082582 100644 --- a/module/data/Item/generic.mjs +++ b/module/data/Item/generic.mjs @@ -2,6 +2,12 @@ export class GenericItemData extends foundry.abstract.TypeDataModel { static defineSchema() { const fields = foundry.data.fields; return { + group: new fields.StringField({ + blank: false, + trim: true, + initial: null, + nullable: true, + }), weight: new fields.NumberField({ min: 0, initial: 0, diff --git a/module/documents/Actor.mjs b/module/documents/Actor.mjs index c142baa..5a2c988 100644 --- a/module/documents/Actor.mjs +++ b/module/documents/Actor.mjs @@ -6,6 +6,11 @@ const { hasProperty } = foundry.utils; export class TAFActor extends Actor { // #region Lifecycle + /** + * This makes sure that the actor gets created with the global attributes if + * they exist, while still allowing programmatic creation through the API with + * specific attributes. + */ async _preCreate(data, options, user) { // Assign the defaults from the world setting if they exist @@ -17,8 +22,19 @@ export class TAFActor extends Actor { return super._preCreate(data, options, user); }; + + /** + * This resets the cache of the item groupings whenever a descedant document + * gets changed (created, updated, deleted) so that we keep the cache as close + * to accurate as can be possible. + */ + _onEmbeddedDocumentChange(...args) { + super._onEmbeddedDocumentChange(...args); + this.#sortedTypes = null; + }; // #endregion Lifecycle + // #region Token Attributes async modifyTokenAttribute(attribute, value, isDelta = false, isBar = true) { const attr = foundry.utils.getProperty(this.system, attribute); const current = isBar ? attr.value : attr; @@ -40,7 +56,9 @@ export class TAFActor extends Actor { return allowed !== false ? this.update(updates) : this; }; + // #endregion Token Attributes + // #region Roll Data getRollData() { /* All properties assigned during this phase of the roll data prep can potentially @@ -67,4 +85,24 @@ export class TAFActor extends Actor { return data; }; + // #endregion Roll Data + + // #region Getters + #sortedTypes = null; + get itemTypes() { + if (this.#sortedTypes) return this.#sortedTypes; + const types = {}; + for (const item of this.items) { + if (item.type !== `generic`) { + types[item.type] ??= []; + types[item.type].push(item); + } else { + const group = item.system.group ?? `Items`; + types[group] ??= []; + types[group].push(item); + }; + }; + return this.#sortedTypes = types; + }; + // #endregion Getters }; diff --git a/templates/GenericItemSheet/content.hbs b/templates/GenericItemSheet/content.hbs index ec90aca..f20bf62 100644 --- a/templates/GenericItemSheet/content.hbs +++ b/templates/GenericItemSheet/content.hbs @@ -11,6 +11,18 @@ {{disabled (not meta.editable)}} > +
+ + +