From d4754e511f308478e7e7075bb1e7f3c479cf9edb Mon Sep 17 00:00:00 2001 From: WBHarry Date: Sat, 7 Jun 2025 17:52:51 +0200 Subject: [PATCH] Moved methods into TypedModelData --- .../applications/sheets/daggerheart-sheet.mjs | 2 +- module/applications/sheets/items/class.mjs | 14 ++-- module/applications/sheets/pc.mjs | 2 +- module/data/item/class.mjs | 35 ++++++++- module/data/item/subclass.mjs | 39 +++++++++- module/documents/item.mjs | 78 ------------------- .../global/partials/feature-section-item.hbs | 4 +- 7 files changed, 83 insertions(+), 91 deletions(-) diff --git a/module/applications/sheets/daggerheart-sheet.mjs b/module/applications/sheets/daggerheart-sheet.mjs index 635d2434..4810b0a7 100644 --- a/module/applications/sheets/daggerheart-sheet.mjs +++ b/module/applications/sheets/daggerheart-sheet.mjs @@ -27,7 +27,7 @@ export default function DhpApplicationMixin(Base) { async _prepareContext(_options, objectPath = 'document') { const context = await super._prepareContext(_options); - context.source = this[objectPath].toObject(); + context.source = this[objectPath]; context.fields = this[objectPath].schema.fields; context.systemFields = this[objectPath].system ? this[objectPath].system.schema.fields : {}; diff --git a/module/applications/sheets/items/class.mjs b/module/applications/sheets/items/class.mjs index e8c69ea3..b0d8bd1d 100644 --- a/module/applications/sheets/items/class.mjs +++ b/module/applications/sheets/items/class.mjs @@ -11,8 +11,8 @@ export default class ClassSheet extends DaggerheartSheet(ItemSheetV2) { actions: { removeSubclass: this.removeSubclass, viewSubclass: this.viewSubclass, - removeFeature: this.removeFeature, - viewFeature: this.viewFeature, + deleteFeature: this.deleteFeature, + editFeature: this.editFeature, removeItem: this.removeItem, viewItem: this.viewItem, removePrimaryWeapon: this.removePrimaryWeapon, @@ -153,13 +153,13 @@ export default class ClassSheet extends DaggerheartSheet(ItemSheetV2) { subclass.sheet.render(true); } - static async removeFeature(_, button) { + static async deleteFeature(_, button) { await this.document.update({ - 'system.features': this.document.system.features.filter(x => x.uuid !== button.dataset.feature) + 'system.features': this.document.system.features.map(x => x.uuid).filter(x => x !== button.dataset.feature) }); } - static async viewFeature(_, button) { + static async editFeature(_, button) { const feature = await fromUuid(button.dataset.feature); feature.sheet.render(true); } @@ -198,11 +198,11 @@ export default class ClassSheet extends DaggerheartSheet(ItemSheetV2) { const item = await fromUuid(data.uuid); if (item.type === 'subclass') { await this.document.update({ - 'system.subclasses': item.uuid + 'system.subclasses': [...this.document.system.subclasses.map(x => x.uuid), item.uuid] }); } else if (item.type === 'feature') { await this.document.update({ - 'system.features': [...this.document.system.features, item.uuid] + 'system.features': [...this.document.system.features.map(x => x.uuid), item.uuid] }); } else if (item.type === 'weapon') { if (event.currentTarget.classList.contains('primary-weapon-section')) { diff --git a/module/applications/sheets/pc.mjs b/module/applications/sheets/pc.mjs index 71d1c34a..7433e3f3 100644 --- a/module/applications/sheets/pc.mjs +++ b/module/applications/sheets/pc.mjs @@ -361,7 +361,7 @@ export default class PCSheet extends DaggerheartSheet(ActorSheetV2) { } mapAdvancementFeatures(actor, config) { - if (!actor.system.subclass) return { foundation: null, advancements: [] }; + if (!actor.system.class.value || !actor.system.class.subclass) return { foundation: null, advancements: [] }; const { subclass, multiclassSubclass } = actor.system.subclassFeatures; diff --git a/module/data/item/class.mjs b/module/data/item/class.mjs index 45cfa652..423ad8a5 100644 --- a/module/data/item/class.mjs +++ b/module/data/item/class.mjs @@ -1,4 +1,3 @@ -import { getTier } from '../../helpers/utils.mjs'; import BaseDataItem from './base.mjs'; import ForeignDocumentUUIDField from '../fields/foreignDocumentUUIDField.mjs'; @@ -52,4 +51,38 @@ export default class DHClass extends BaseDataItem { isMulticlass: new fields.BooleanField({ initial: false }) }; } + + async _preCreate(data, options, user) { + const allowed = await super._preCreate(data, options, user); + if (allowed === false) return; + + if (this.actor?.type === 'pc') { + const path = data.system.isMulticlass ? 'system.multiclass.value' : 'system.class.value'; + if (foundry.utils.getProperty(this.actor, path)) { + ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.ClassAlreadySelected')); + return false; + } + } + } + + _onCreate(data, options, userId) { + super._onCreate(data, options, userId); + if (options.parent?.type === 'pc') { + const path = `system.${data.system.isMulticlass ? 'multiclass.value' : 'class.value'}`; + options.parent.update({ [path]: `${options.parent.uuid}.Item.${data._id}` }); + } + } + + _onDelete(options, userId) { + super._onDelete(options, userId); + + if (options.parent?.type === 'pc') { + const path = `system.${this.isMulticlass ? 'multiclass' : 'class'}`; + options.parent.update({ + [`${path}.value`]: null + }); + + foundry.utils.getProperty(options.parent, `${path}.subclass`)?.delete(); + } + } } diff --git a/module/data/item/subclass.mjs b/module/data/item/subclass.mjs index d0095954..8fbacee8 100644 --- a/module/data/item/subclass.mjs +++ b/module/data/item/subclass.mjs @@ -1,4 +1,3 @@ -import { getTier } from '../../helpers/utils.mjs'; import BaseDataItem from './base.mjs'; export default class DHSubclass extends BaseDataItem { @@ -59,4 +58,42 @@ export default class DHSubclass extends BaseDataItem { isMulticlass: new fields.BooleanField({ initial: false }) }; } + + async _preCreate(data, options, user) { + const allowed = await super._preCreate(data, options, user); + if (allowed === false) return; + + if (this.actor?.type === 'pc') { + const path = data.system.isMulticlass ? 'system.multiclass' : 'system.class'; + const classData = foundry.utils.getProperty(this.actor, path); + if (!classData.value) { + ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.MissingClass')); + return false; + } else if (classData.subclass) { + ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.SubclassAlreadySelected')); + return false; + } else if (classData.value.system.subclasses.every(x => x.uuid !== `Item.${data._id}`)) { + ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.SubclassNotInClass')); + return false; + } + } + } + + _onCreate(data, options, userId) { + super._onCreate(data, options, userId); + + if (options.parent?.type === 'pc') { + const path = `system.${data.system.isMulticlass ? 'multiclass.subclass' : 'class.subclass'}`; + options.parent.update({ [path]: `${options.parent.uuid}.Item.${data._id}` }); + } + } + + _onDelete(options, userId) { + super._onDelete(options, userId); + + if (options.parent?.type === 'pc') { + const path = `system.${this.isMulticlass ? 'multiclass.subclass' : 'class.subclass'}`; + options.parent.update({ [path]: null }); + } + } } diff --git a/module/documents/item.mjs b/module/documents/item.mjs index fa666d30..65dafc51 100644 --- a/module/documents/item.mjs +++ b/module/documents/item.mjs @@ -1,84 +1,6 @@ export default class DhpItem extends Item { - async _preCreate(data, changes, user) { - const allowed = await super._preCreate(data, changes, user); - if (allowed === false) return; - - if (data.type === 'class') { - if (this.parent?.type === 'pc') { - const path = data.system.isMulticlass ? 'system.multiclass.value' : 'system.class.value'; - if (foundry.utils.getProperty(this.parent, path)) { - ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.ClassAlreadySelected')); - return false; - } - } - } - - if (data.type === 'subclass') { - if (this.parent?.type === 'pc') { - const path = data.system.isMulticlass ? 'system.multiclass' : 'system.class'; - const classData = foundry.utils.getProperty(this.parent, path); - if (!classData.value) { - ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.MissingClass')); - return false; - } else if (classData.subclass) { - ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.SubclassAlreadySelected')); - return false; - } else if ( - classData.value.system.subclasses.every(x => `${this.parent.uuid}.${x.uuid}` !== this.uuid) - ) { - ui.notifications.error(game.i18n.localize('DAGGERHEART.Item.Errors.SubclassNotInClass')); - return false; - } - } - } - } - - static async _onCreateOperation(documents, operation, user) { - await super._onCreateOperation(documents, operation, user); - for (var document of documents) { - if (document.type === 'class') { - if (operation.parent?.type === 'pc') { - const path = `system.${document.system.isMulticlass ? 'multiclass.value' : 'class.value'}`; - await operation.parent.update({ [path]: document.uuid }); - } - } else if (document.type === 'subclass') { - if (operation.parent?.type === 'pc') { - const path = `system.${document.system.isMulticlass ? 'multiclass.subclass' : 'class.subclass'}`; - await operation.parent.update({ [path]: document.uuid }); - } - } - } - } - - static async _onDeleteOperation(documents, operation, user) { - await super._onDeleteOperation(documents, operation, user); - for (var document of documents) { - if (document.type === 'class') { - if (operation.parent?.type === 'pc') { - const path = `system.${document.system.isMulticlass ? 'multiclass' : 'class'}`; - await operation.parent.update({ - [path]: { - class: null, - subclass: null - } - }); - } - } else if (document.type === 'subclass') { - if (operation.parent?.type === 'pc') { - const path = `system.${document.system.isMulticlass ? 'multiclass.subclass' : 'class.subclass'}`; - await operation.parent.update({ [path]: null }); - } - } - } - } - prepareData() { super.prepareData(); - - if (this.type === 'class') { - // Bad. Make this better. - // this.system.domains = CONFIG.daggerheart.DOMAIN.classDomainMap[Object.keys(CONFIG.daggerheart.DOMAIN.classDomainMap).find(x => x === this.name.toLowerCase())]; - } } /** diff --git a/templates/sheets/global/partials/feature-section-item.hbs b/templates/sheets/global/partials/feature-section-item.hbs index adc029e9..ebaabefe 100644 --- a/templates/sheets/global/partials/feature-section-item.hbs +++ b/templates/sheets/global/partials/feature-section-item.hbs @@ -9,7 +9,7 @@ @@ -17,7 +17,7 @@