From b1aca218c213a6ab10a99b8814aac73fc2cd4baa Mon Sep 17 00:00:00 2001 From: WBHarry Date: Tue, 22 Jul 2025 13:22:18 +0200 Subject: [PATCH] Fixes links upon being embedded --- .../characterCreation/characterCreation.mjs | 55 +++++++++++++++---- module/data/actor/character.mjs | 2 +- module/data/item/base.mjs | 21 ++++++- 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/module/applications/characterCreation/characterCreation.mjs b/module/applications/characterCreation/characterCreation.mjs index b8759cc5..b48c3dec 100644 --- a/module/applications/characterCreation/characterCreation.mjs +++ b/module/applications/characterCreation/characterCreation.mjs @@ -205,7 +205,11 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl _getSetupTabs(tabs) { for (const v of Object.values(tabs)) { - v.active = this.tabGroups[v.group] ? this.tabGroups[v.group] === v.id : v.active; + v.active = this.tabGroups[v.group] + ? this.tabGroups[v.group] === v.id + : this.tabGroups.primary !== 'equipment' + ? v.active + : false; v.cssClass = v.active ? 'active' : ''; switch (v.id) { @@ -242,6 +246,16 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl marker.classList.remove('active'); } } + + if (tab === 'equipment') { + this.tabGroups.setup = null; + this.element.querySelector('section[data-group="setup"].active')?.classList?.remove?.('active'); + } else { + this.tabGroups.setup = 'domainCards'; + this.element + .querySelector('section[data-group="setup"][data-tab="domainCards"]') + ?.classList?.add?.('active'); + } } } @@ -266,7 +280,7 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl async _preparePartContext(partId, context) { switch (partId) { case 'footer': - context.isLastTab = this.tabGroups.setup === 'domainCards'; + context.isLastTab = this.tabGroups.setup === 'domainCards' || this.tabGroups.primary !== 'setup'; switch (this.tabGroups.setup) { case null: case 'ancestry': @@ -353,13 +367,18 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl ); context.armor = { ...this.equipment.armor, - suggestion: { ...suggestions.armor, taken: suggestions.armor?.uuid === this.equipment.armor?.uuid }, + suggestion: { + ...suggestions.armor, + uuid: suggestions.armor?.uuid, + taken: suggestions.armor?.uuid === this.equipment.armor?.uuid + }, compendium: 'armors' }; context.primaryWeapon = { ...this.equipment.primaryWeapon, suggestion: { ...suggestions.primaryWeapon, + uuid: suggestions.primaryWeapon?.uuid, taken: suggestions.primaryWeapon?.uuid === this.equipment.primaryWeapon?.uuid }, compendium: 'weapons' @@ -368,6 +387,7 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl ...this.equipment.secondaryWeapon, suggestion: { ...suggestions.secondaryWeapon, + uuid: suggestions.secondaryWeapon?.uuid, taken: suggestions.secondaryWeapon?.uuid === this.equipment.secondaryWeapon?.uuid }, disabled: this.equipment.primaryWeapon?.system?.burden === burden.twoHanded.value, @@ -421,18 +441,29 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl async getEquipmentSuggestions(choiceA, choiceB) { if (!this.setup.class.uuid) return { inventory: { take: [] } }; - const { inventory, characterGuide } = this.setup.class.system; return { - armor: characterGuide.suggestedArmor ?? null, - primaryWeapon: characterGuide.suggestedPrimaryWeapon ?? null, - secondaryWeapon: characterGuide.suggestedSecondaryWeapon - ? { ...characterGuide.suggestedSecondaryWeapon, uuid: characterGuide.suggestedSecondaryWeapon.uuid } + armor: this.setup.class.system.suggestedArmor ?? null, + primaryWeapon: this.setup.class.system.suggestedPrimaryWeapon ?? null, + secondaryWeapon: this.setup.class.system.suggestedSecondaryWeapon + ? { + ...this.setup.class.system.suggestedSecondaryWeapon, + uuid: this.setup.class.system.suggestedSecondaryWeapon.uuid + } : null, inventory: { - take: inventory.take ?? [], + take: this.setup.class.system.take ?? [], choiceA: - inventory.choiceA?.map(x => ({ ...x, uuid: x.uuid, selected: x.uuid === choiceA?.uuid })) ?? [], - choiceB: inventory.choiceB?.map(x => ({ ...x, uuid: x.uuid, selected: x.uuid === choiceB?.uuid })) ?? [] + this.setup.class.system.choiceA?.map(x => ({ + ...x, + uuid: x.uuid, + selected: x.uuid === choiceA?.uuid + })) ?? [], + choiceB: + this.setup.class.system.choiceB?.map(x => ({ + ...x, + uuid: x.uuid, + selected: x.uuid === choiceB?.uuid + })) ?? [] } }; } @@ -535,7 +566,7 @@ export default class DhCharacterCreation extends HandlebarsApplicationMixin(Appl await this.character.createEmbeddedDocuments('Item', [this.equipment.inventory.choiceA]); if (this.equipment.inventory.choiceB.uuid) await this.character.createEmbeddedDocuments('Item', [this.equipment.inventory.choiceB]); - await this.character.createEmbeddedDocuments('Item', this.setup.class.system.inventory.take); + await this.character.createEmbeddedDocuments('Item', this.setup.class.system.take); await this.character.update({ system: { diff --git a/module/data/actor/character.mjs b/module/data/actor/character.mjs index 2b5ee3c5..9adea9b3 100644 --- a/module/data/actor/character.mjs +++ b/module/data/actor/character.mjs @@ -359,7 +359,7 @@ export default class DhCharacter extends BaseDataActor { classFeatures.push(item); } else if (item.system.originItemType === CONFIG.DH.ITEM.featureTypes.subclass.id) { const subclassState = this.class.subclass.system.featureState; - const subType = item.system.subType; + const subType = item.system.itemLinks[this.class.subclass.uuid]; if ( subType === CONFIG.DH.ITEM.itemLinkFeatureTypes.foundation || (subType === CONFIG.DH.ITEM.itemLinkFeatureTypes.specialization && subclassState >= 2) || diff --git a/module/data/item/base.mjs b/module/data/item/base.mjs index 73a7eb00..31e2a631 100644 --- a/module/data/item/base.mjs +++ b/module/data/item/base.mjs @@ -6,6 +6,7 @@ * @property {boolean} hasDescription - Indicates whether items of this type have description field * @property {boolean} isQuantifiable - Indicates whether items of this type have quantity field * @property {boolean} isInventoryItem- Indicates whether items of this type is a Inventory Item + * @property {boolean} isItemLinkable - Indicates whether items of this type can have links to other items. */ import ItemLinksField from '../fields/itemLinksField.mjs'; @@ -123,9 +124,11 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { this.updateSource({ actions: [action] }); } + + options.origUuid = data.uuid; } - _onCreate(data) { + _onCreate(data, options) { if (!this.actor || this.actor.type !== 'character' || !this.features) return; this.actor.createEmbeddedDocuments( @@ -134,12 +137,28 @@ export default class BaseDataItem extends foundry.abstract.TypeDataModel { ...feature, system: { ...feature.system, + itemLinks: Object.keys(feature.system.itemLinks).reduce((acc, uuid) => { + const type = feature.system.itemLinks[uuid]; + acc[uuid === options.origUuid ? this.parent.uuid : uuid] = type; + + return acc; + }, {}), originItemType: this.parent.type, originId: data._id, identifier: feature.identifier } })) ); + + for (let feature of this.features) { + feature.update({ + 'system.itemLinks': Object.keys(feature.system.itemLinks).reduce((acc, uuid) => { + const type = feature.system.itemLinks[uuid]; + acc[uuid === options.origUuid ? this.parent.uuid : uuid] = type; + return acc; + }, {}) + }); + } } async _preDelete() {