mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-17 15:39:02 +01:00
refactor code for review comments
This commit is contained in:
parent
001abcadf4
commit
5065a609ac
6 changed files with 128 additions and 438 deletions
|
|
@ -43,163 +43,35 @@ export default function ItemAttachmentSheet(Base) {
|
||||||
const item = await Item.implementation.fromDropData(data);
|
const item = await Item.implementation.fromDropData(data);
|
||||||
if (!item) return;
|
if (!item) return;
|
||||||
|
|
||||||
await addAttachmentToItem({
|
// Call the data model's public method
|
||||||
parentItem: this.document,
|
await this.document.system.addAttachment(item);
|
||||||
droppedItem: item,
|
|
||||||
parentType: this.document.type
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static async #removeAttachment(event, target) {
|
static async #removeAttachment(event, target) {
|
||||||
await removeAttachmentFromItem({
|
// Call the data model's public method
|
||||||
parentItem: this.document,
|
await this.document.system.removeAttachment(target.dataset.uuid);
|
||||||
attachedUuid: target.dataset.uuid,
|
}
|
||||||
parentType: this.document.type
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async removeAttachmentEffectsFromActor({ parentItem, attachedUuid, parentType }) {
|
async _preparePartContext(partId, context) {
|
||||||
const actor = parentItem.parent;
|
await super._preparePartContext(partId, context);
|
||||||
if (!actor) return;
|
|
||||||
|
|
||||||
const parentUuidProperty = `${parentType}Uuid`;
|
|
||||||
const effectsToRemove = actor.effects.filter(effect => {
|
|
||||||
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
|
||||||
return attachmentSource &&
|
|
||||||
attachmentSource[parentUuidProperty] === parentItem.uuid &&
|
|
||||||
attachmentSource.itemUuid === attachedUuid;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (effectsToRemove.length > 0) {
|
|
||||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async removeAttachmentFromItem({ parentItem, attachedUuid, parentType }) {
|
|
||||||
const currentAttached = parentItem.system.attached;
|
|
||||||
|
|
||||||
// Remove the attachment from the parent item's attached array
|
if (partId === 'attachments') {
|
||||||
await parentItem.update({
|
// Keep this simple UI preparation in the mixin
|
||||||
'system.attached': currentAttached.filter(uuid => uuid !== attachedUuid)
|
const attachedUUIDs = this.document.system.attached;
|
||||||
});
|
context.attachedItems = await Promise.all(
|
||||||
|
attachedUUIDs.map(async uuid => {
|
||||||
// Remove any effects that came from this attachment
|
const item = await fromUuid(uuid);
|
||||||
await removeAttachmentEffectsFromActor({
|
return {
|
||||||
parentItem,
|
uuid: uuid,
|
||||||
attachedUuid,
|
name: item?.name || 'Unknown Item',
|
||||||
parentType
|
img: item?.img || 'icons/svg/item-bag.svg'
|
||||||
});
|
};
|
||||||
}
|
})
|
||||||
async prepareAttachmentContext(parentItem) {
|
);
|
||||||
const attachedUUIDs = parentItem.system.attached;
|
|
||||||
return await Promise.all(
|
|
||||||
attachedUUIDs.map(async uuid => {
|
|
||||||
const item = await fromUuid(uuid);
|
|
||||||
return {
|
|
||||||
uuid: uuid,
|
|
||||||
name: item?.name || 'Unknown Item',
|
|
||||||
img: item?.img || 'icons/svg/item-bag.svg'
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async addAttachmentToItem({ parentItem, droppedItem, parentType }) {
|
|
||||||
const currentAttached = parentItem.system.attached;
|
|
||||||
const newUUID = droppedItem.uuid;
|
|
||||||
|
|
||||||
if (currentAttached.includes(newUUID)) {
|
|
||||||
ui.notifications.warn(`${droppedItem.name} is already attached to this ${parentType}.`);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedAttached = [...currentAttached, newUUID];
|
return context;
|
||||||
|
|
||||||
await parentItem.update({
|
|
||||||
'system.attached': updatedAttached
|
|
||||||
});
|
|
||||||
|
|
||||||
// Copy effects from attached item to actor (only if parent item is equipped)
|
|
||||||
const actor = parentItem.parent;
|
|
||||||
if (actor && droppedItem.effects.size > 0 && parentItem.system.equipped) {
|
|
||||||
await copyAttachmentEffectsToActor({
|
|
||||||
parentItem,
|
|
||||||
attachedItem: droppedItem,
|
|
||||||
attachedUuid: newUUID,
|
|
||||||
parentType
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async copyAttachmentEffectsToActor({ parentItem, attachedItem, attachedUuid, parentType }) {
|
|
||||||
const actor = parentItem.parent;
|
|
||||||
if (!actor || !attachedItem.effects.size > 0 || !parentItem.system.equipped) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const effectsToCreate = [];
|
|
||||||
for (const effect of attachedItem.effects) {
|
|
||||||
// Copy ALL effects when item is attached - attachment-only flag only matters for non-attached items
|
|
||||||
const effectData = effect.toObject();
|
|
||||||
effectData.origin = `${parentItem.uuid}:${attachedUuid}`;
|
|
||||||
|
|
||||||
// Set up attachment source metadata with the appropriate property name
|
|
||||||
const attachmentSource = {
|
|
||||||
itemUuid: attachedUuid,
|
|
||||||
originalEffectId: effect.id
|
|
||||||
};
|
|
||||||
attachmentSource[`${parentType}Uuid`] = parentItem.uuid;
|
|
||||||
|
|
||||||
effectData.flags = {
|
|
||||||
...effectData.flags,
|
|
||||||
[CONFIG.DH.id]: {
|
|
||||||
...effectData.flags?.[CONFIG.DH.id],
|
|
||||||
[CONFIG.DH.FLAGS.itemAttachmentSource]: attachmentSource
|
|
||||||
}
|
|
||||||
};
|
|
||||||
effectsToCreate.push(effectData);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (effectsToCreate.length > 0) {
|
|
||||||
return await actor.createEmbeddedDocuments('ActiveEffect', effectsToCreate);
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
async handleAttachmentEffectsOnEquipChange({ parentItem, newEquippedStatus, parentType }) {
|
|
||||||
// Try to get the actor - it might be parentItem.parent instead of parentItem.parent.parent
|
|
||||||
const actor = parentItem.parent?.type === 'character' ? parentItem.parent : parentItem.parent?.parent;
|
|
||||||
|
|
||||||
if (!actor || !parentItem.system.attached?.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newEquippedStatus) {
|
|
||||||
// Item is being equipped - add attachment effects
|
|
||||||
for (const attachedUuid of parentItem.system.attached) {
|
|
||||||
const attachedItem = await fromUuid(attachedUuid);
|
|
||||||
if (attachedItem && attachedItem.effects.size > 0) {
|
|
||||||
this.copyAttachmentEffectsToActor({
|
|
||||||
parentItem,
|
|
||||||
attachedItem,
|
|
||||||
attachedUuid,
|
|
||||||
parentType
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Item is being unequipped - remove attachment effects
|
|
||||||
const parentUuidProperty = `${parentType}Uuid`;
|
|
||||||
const effectsToRemove = actor.effects.filter(effect => {
|
|
||||||
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
|
||||||
return attachmentSource && attachmentSource[parentUuidProperty] === parentItem.uuid;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (effectsToRemove.length > 0) {
|
|
||||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import DHBaseItemSheet from '../api/base-item.mjs';
|
import DHBaseItemSheet from '../api/base-item.mjs';
|
||||||
import ItemAttachmentSheet from '../api/item-attachment-sheet.mjs';
|
import ItemAttachmentSheet from '../api/item-attachment-sheet.mjs';
|
||||||
import { copyAttachmentEffectsToActor, removeAttachmentFromItem, prepareAttachmentContext, addAttachmentToItem } from '../../../helpers/attachmentHelper.mjs';
|
|
||||||
|
|
||||||
export default class WeaponSheet extends ItemAttachmentSheet(DHBaseItemSheet) {
|
export default class WeaponSheet extends ItemAttachmentSheet(DHBaseItemSheet) {
|
||||||
/**@inheritdoc */
|
/**@inheritdoc */
|
||||||
|
|
|
||||||
|
|
@ -8,26 +8,60 @@ export default class AttachableItem extends BaseDataItem {
|
||||||
attached: new fields.ArrayField(new fields.DocumentUUIDField({ type: "Item", nullable: true }))
|
attached: new fields.ArrayField(new fields.DocumentUUIDField({ type: "Item", nullable: true }))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async copyAttachmentEffectsToActor({ parentItem, attachedItem, attachedUuid, parentType }) {
|
async _preUpdate(changes, options, user) {
|
||||||
const actor = parentItem.parent;
|
const allowed = await super._preUpdate(changes, options, user);
|
||||||
if (!actor || !attachedItem.effects.size > 0 || !parentItem.system.equipped) {
|
if (allowed === false) return false;
|
||||||
|
|
||||||
|
// Handle equipped status changes for attachment effects
|
||||||
|
if (changes.system?.equipped !== undefined && changes.system.equipped !== this.equipped) {
|
||||||
|
await this.#handleAttachmentEffectsOnEquipChange(changes.system.equipped);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async #handleAttachmentEffectsOnEquipChange(newEquippedStatus) {
|
||||||
|
const actor = this.parent.parent?.type === 'character' ? this.parent.parent : this.parent.parent?.parent;
|
||||||
|
const parentType = this.parent.type;
|
||||||
|
|
||||||
|
if (!actor || !this.attached?.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newEquippedStatus) {
|
||||||
|
// Item is being equipped - add attachment effects
|
||||||
|
for (const attachedUuid of this.attached) {
|
||||||
|
const attachedItem = await fromUuid(attachedUuid);
|
||||||
|
if (attachedItem && attachedItem.effects.size > 0) {
|
||||||
|
await this.#copyAttachmentEffectsToActor({
|
||||||
|
attachedItem,
|
||||||
|
attachedUuid,
|
||||||
|
parentType
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Item is being unequipped - remove attachment effects
|
||||||
|
await this.#removeAllAttachmentEffects(parentType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async #copyAttachmentEffectsToActor({ attachedItem, attachedUuid, parentType }) {
|
||||||
|
const actor = this.parent.parent;
|
||||||
|
if (!actor || !attachedItem.effects.size > 0 || !this.equipped) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const effectsToCreate = [];
|
const effectsToCreate = [];
|
||||||
for (const effect of attachedItem.effects) {
|
for (const effect of attachedItem.effects) {
|
||||||
// Copy ALL effects when item is attached - attachment-only flag only matters for non-attached items
|
|
||||||
const effectData = effect.toObject();
|
const effectData = effect.toObject();
|
||||||
effectData.origin = `${parentItem.uuid}:${attachedUuid}`;
|
effectData.origin = `${this.parent.uuid}:${attachedUuid}`;
|
||||||
|
|
||||||
// Set up attachment source metadata with the appropriate property name
|
|
||||||
const attachmentSource = {
|
const attachmentSource = {
|
||||||
itemUuid: attachedUuid,
|
itemUuid: attachedUuid,
|
||||||
originalEffectId: effect.id
|
originalEffectId: effect.id
|
||||||
};
|
};
|
||||||
attachmentSource[`${parentType}Uuid`] = parentItem.uuid;
|
attachmentSource[`${parentType}Uuid`] = this.parent.uuid;
|
||||||
|
|
||||||
effectData.flags = {
|
effectData.flags = {
|
||||||
...effectData.flags,
|
...effectData.flags,
|
||||||
[CONFIG.DH.id]: {
|
[CONFIG.DH.id]: {
|
||||||
|
|
@ -41,56 +75,78 @@ export default class AttachableItem extends BaseDataItem {
|
||||||
if (effectsToCreate.length > 0) {
|
if (effectsToCreate.length > 0) {
|
||||||
return await actor.createEmbeddedDocuments('ActiveEffect', effectsToCreate);
|
return await actor.createEmbeddedDocuments('ActiveEffect', effectsToCreate);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleAttachmentEffectsOnEquipChange({ parentItem, newEquippedStatus, parentType }) {
|
async #removeAllAttachmentEffects(parentType) {
|
||||||
// Try to get the actor - it might be parentItem.parent instead of parentItem.parent.parent
|
const actor = this.parent.parent;
|
||||||
const actor = parentItem.parent?.type === 'character' ? parentItem.parent : parentItem.parent?.parent;
|
if (!actor) return;
|
||||||
|
|
||||||
if (!actor || !parentItem.system.attached?.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newEquippedStatus) {
|
const parentUuidProperty = `${parentType}Uuid`;
|
||||||
// Item is being equipped - add attachment effects
|
const effectsToRemove = actor.effects.filter(effect => {
|
||||||
for (const attachedUuid of parentItem.system.attached) {
|
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
||||||
const attachedItem = await fromUuid(attachedUuid);
|
return attachmentSource && attachmentSource[parentUuidProperty] === this.parent.uuid;
|
||||||
if (attachedItem && attachedItem.effects.size > 0) {
|
});
|
||||||
this.copyAttachmentEffectsToActor({
|
|
||||||
parentItem,
|
|
||||||
attachedItem,
|
|
||||||
attachedUuid,
|
|
||||||
parentType
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Item is being unequipped - remove attachment effects
|
|
||||||
const parentUuidProperty = `${parentType}Uuid`;
|
|
||||||
const effectsToRemove = actor.effects.filter(effect => {
|
|
||||||
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
|
||||||
return attachmentSource && attachmentSource[parentUuidProperty] === parentItem.uuid;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (effectsToRemove.length > 0) {
|
if (effectsToRemove.length > 0) {
|
||||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async _preUpdate(changes, options, user) {
|
/**
|
||||||
const allowed = await super._preUpdate(changes, options, user);
|
* Public method for adding an attachment
|
||||||
if (allowed === false) return false;
|
*/
|
||||||
|
async addAttachment(droppedItem) {
|
||||||
|
const newUUID = droppedItem.uuid;
|
||||||
|
|
||||||
// Handle equipped status changes for attachment effects
|
if (this.attached.includes(newUUID)) {
|
||||||
if (changes.system?.equipped !== undefined && changes.system.equipped !== this.equipped) {
|
ui.notifications.warn(`${droppedItem.name} is already attached to this ${this.parent.type}.`);
|
||||||
await this.handleAttachmentEffectsOnEquipChange({
|
return;
|
||||||
parentItem: this.parent,
|
}
|
||||||
newEquippedStatus: changes.system.equipped,
|
|
||||||
|
const updatedAttached = [...this.attached, newUUID];
|
||||||
|
await this.parent.update({
|
||||||
|
'system.attached': updatedAttached
|
||||||
|
});
|
||||||
|
|
||||||
|
// Copy effects if equipped
|
||||||
|
if (this.equipped && droppedItem.effects.size > 0) {
|
||||||
|
await this.#copyAttachmentEffectsToActor({
|
||||||
|
attachedItem: droppedItem,
|
||||||
|
attachedUuid: newUUID,
|
||||||
parentType: this.parent.type
|
parentType: this.parent.type
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Public method for removing an attachment
|
||||||
|
*/
|
||||||
|
async removeAttachment(attachedUuid) {
|
||||||
|
await this.parent.update({
|
||||||
|
'system.attached': this.attached.filter(uuid => uuid !== attachedUuid)
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove effects
|
||||||
|
await this.#removeAttachmentEffects(attachedUuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
async #removeAttachmentEffects(attachedUuid) {
|
||||||
|
const actor = this.parent.parent;
|
||||||
|
if (!actor) return;
|
||||||
|
|
||||||
|
const parentType = this.parent.type;
|
||||||
|
const parentUuidProperty = `${parentType}Uuid`;
|
||||||
|
const effectsToRemove = actor.effects.filter(effect => {
|
||||||
|
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
||||||
|
return attachmentSource &&
|
||||||
|
attachmentSource[parentUuidProperty] === this.parent.uuid &&
|
||||||
|
attachmentSource.itemUuid === attachedUuid;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (effectsToRemove.length > 0) {
|
||||||
|
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
import AttachableItem from './attachableItem.mjs';
|
import AttachableItem from './attachableItem.mjs';
|
||||||
import { actionsTypes } from '../action/_module.mjs';
|
import { actionsTypes } from '../action/_module.mjs';
|
||||||
import ActionField from '../fields/actionField.mjs';
|
import ActionField from '../fields/actionField.mjs';
|
||||||
import { handleAttachmentEffectsOnEquipChange } from '../../helpers/attachmentHelper.mjs';
|
|
||||||
|
|
||||||
export default class DHWeapon extends AttachableItem {
|
export default class DHWeapon extends AttachableItem {
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
|
|
@ -39,7 +38,7 @@ export default class DHWeapon extends AttachableItem {
|
||||||
actionIds: new fields.ArrayField(new fields.StringField({ required: true }))
|
actionIds: new fields.ArrayField(new fields.StringField({ required: true }))
|
||||||
})
|
})
|
||||||
),
|
),
|
||||||
attack: new ActionField({
|
attack: new ActionField({
|
||||||
initial: {
|
initial: {
|
||||||
name: 'Attack',
|
name: 'Attack',
|
||||||
img: 'icons/skills/melee/blood-slash-foam-red.webp',
|
img: 'icons/skills/melee/blood-slash-foam-red.webp',
|
||||||
|
|
|
||||||
|
|
@ -9,46 +9,6 @@ export default class DHItem extends foundry.documents.Item {
|
||||||
for (const action of this.system.actions ?? []) action.prepareData();
|
for (const action of this.system.actions ?? []) action.prepareData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritdoc
|
|
||||||
*/
|
|
||||||
async _preUpdate(changed, options, user) {
|
|
||||||
const result = await super._preUpdate(changed, options, user);
|
|
||||||
|
|
||||||
// Store the previous equipped status for attachment handling
|
|
||||||
if (changed.system?.equipped !== undefined) {
|
|
||||||
options.previousEquipped = this.system.equipped;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @inheritdoc
|
|
||||||
*/
|
|
||||||
async _onUpdate(changed, options, user) {
|
|
||||||
await super._onUpdate(changed, options, user);
|
|
||||||
|
|
||||||
// Handle attachment effects when equipped status changes
|
|
||||||
if (changed.system?.equipped !== undefined && options.previousEquipped !== changed.system.equipped) {
|
|
||||||
const newEquippedStatus = changed.system.equipped;
|
|
||||||
const parentType = this.type === 'armor' ? 'armor' : this.type === 'weapon' ? 'weapon' : null;
|
|
||||||
|
|
||||||
if (parentType) {
|
|
||||||
try {
|
|
||||||
const { handleAttachmentEffectsOnEquipChange } = await import('../helpers/attachmentHelper.mjs');
|
|
||||||
await handleAttachmentEffectsOnEquipChange({
|
|
||||||
parentItem: this,
|
|
||||||
newEquippedStatus,
|
|
||||||
parentType
|
|
||||||
});
|
|
||||||
} catch (error) {
|
|
||||||
console.error('DH | Error in handleAttachmentEffectsOnEquipChange:', error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
* @param {object} options - Options which modify the getRollData method.
|
* @param {object} options - Options which modify the getRollData method.
|
||||||
|
|
|
||||||
|
|
@ -1,196 +0,0 @@
|
||||||
/**
|
|
||||||
* Utility functions for handling item attachments and their effects
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copy all effects from an attached item to the actor when the parent item is equipped
|
|
||||||
* @param {Object} options - Configuration options
|
|
||||||
* @param {Item} options.parentItem - The item (armor/weapon) that the item is being attached to
|
|
||||||
* @param {Item} options.attachedItem - The item being attached
|
|
||||||
* @param {string} options.attachedUuid - UUID of the attached item
|
|
||||||
* @param {string} options.parentType - Type of parent item ("armor" or "weapon")
|
|
||||||
* @returns {Promise<ActiveEffect[]>} Created effects
|
|
||||||
*/
|
|
||||||
export async function copyAttachmentEffectsToActor({ parentItem, attachedItem, attachedUuid, parentType }) {
|
|
||||||
const actor = parentItem.parent;
|
|
||||||
if (!actor || !attachedItem.effects.size > 0 || !parentItem.system.equipped) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
const effectsToCreate = [];
|
|
||||||
for (const effect of attachedItem.effects) {
|
|
||||||
// Copy ALL effects when item is attached - attachment-only flag only matters for non-attached items
|
|
||||||
const effectData = effect.toObject();
|
|
||||||
effectData.origin = `${parentItem.uuid}:${attachedUuid}`;
|
|
||||||
|
|
||||||
// Set up attachment source metadata with the appropriate property name
|
|
||||||
const attachmentSource = {
|
|
||||||
itemUuid: attachedUuid,
|
|
||||||
originalEffectId: effect.id
|
|
||||||
};
|
|
||||||
attachmentSource[`${parentType}Uuid`] = parentItem.uuid;
|
|
||||||
|
|
||||||
effectData.flags = {
|
|
||||||
...effectData.flags,
|
|
||||||
[CONFIG.DH.id]: {
|
|
||||||
...effectData.flags?.[CONFIG.DH.id],
|
|
||||||
[CONFIG.DH.FLAGS.itemAttachmentSource]: attachmentSource
|
|
||||||
}
|
|
||||||
};
|
|
||||||
effectsToCreate.push(effectData);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (effectsToCreate.length > 0) {
|
|
||||||
return await actor.createEmbeddedDocuments('ActiveEffect', effectsToCreate);
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove effects from the actor that came from a specific attached item
|
|
||||||
* @param {Object} options - Configuration options
|
|
||||||
* @param {Item} options.parentItem - The item (armor/weapon) that the item was attached to
|
|
||||||
* @param {string} options.attachedUuid - UUID of the attached item being removed
|
|
||||||
* @param {string} options.parentType - Type of parent item ("armor" or "weapon")
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
export async function removeAttachmentEffectsFromActor({ parentItem, attachedUuid, parentType }) {
|
|
||||||
const actor = parentItem.parent;
|
|
||||||
if (!actor) return;
|
|
||||||
|
|
||||||
const parentUuidProperty = `${parentType}Uuid`;
|
|
||||||
const effectsToRemove = actor.effects.filter(effect => {
|
|
||||||
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
|
||||||
return attachmentSource &&
|
|
||||||
attachmentSource[parentUuidProperty] === parentItem.uuid &&
|
|
||||||
attachmentSource.itemUuid === attachedUuid;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (effectsToRemove.length > 0) {
|
|
||||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an attachment from an item and clean up its effects
|
|
||||||
* @param {Object} options - Configuration options
|
|
||||||
* @param {Item} options.parentItem - The item (armor/weapon) that the item is attached to
|
|
||||||
* @param {string} options.attachedUuid - UUID of the attached item being removed
|
|
||||||
* @param {string} options.parentType - Type of parent item ("armor" or "weapon")
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
export async function removeAttachmentFromItem({ parentItem, attachedUuid, parentType }) {
|
|
||||||
const currentAttached = parentItem.system.attached;
|
|
||||||
|
|
||||||
// Remove the attachment from the parent item's attached array
|
|
||||||
await parentItem.update({
|
|
||||||
'system.attached': currentAttached.filter(uuid => uuid !== attachedUuid)
|
|
||||||
});
|
|
||||||
|
|
||||||
// Remove any effects that came from this attachment
|
|
||||||
await removeAttachmentEffectsFromActor({
|
|
||||||
parentItem,
|
|
||||||
attachedUuid,
|
|
||||||
parentType
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle adding/removing attachment effects when a parent item is equipped/unequipped
|
|
||||||
* @param {Object} options - Configuration options
|
|
||||||
* @param {Item} options.parentItem - The item (armor/weapon) being equipped/unequipped
|
|
||||||
* @param {boolean} options.newEquippedStatus - The new equipped status
|
|
||||||
* @param {string} options.parentType - Type of parent item ("armor" or "weapon")
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
export async function handleAttachmentEffectsOnEquipChange({ parentItem, newEquippedStatus, parentType }) {
|
|
||||||
// Try to get the actor - it might be parentItem.parent instead of parentItem.parent.parent
|
|
||||||
const actor = parentItem.parent?.type === 'character' ? parentItem.parent : parentItem.parent?.parent;
|
|
||||||
|
|
||||||
if (!actor || !parentItem.system.attached?.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newEquippedStatus) {
|
|
||||||
// Item is being equipped - add attachment effects
|
|
||||||
for (const attachedUuid of parentItem.system.attached) {
|
|
||||||
const attachedItem = await fromUuid(attachedUuid);
|
|
||||||
if (attachedItem && attachedItem.effects.size > 0) {
|
|
||||||
await copyAttachmentEffectsToActor({
|
|
||||||
parentItem,
|
|
||||||
attachedItem,
|
|
||||||
attachedUuid,
|
|
||||||
parentType
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Item is being unequipped - remove attachment effects
|
|
||||||
const parentUuidProperty = `${parentType}Uuid`;
|
|
||||||
const effectsToRemove = actor.effects.filter(effect => {
|
|
||||||
const attachmentSource = effect.getFlag(CONFIG.DH.id, CONFIG.DH.FLAGS.itemAttachmentSource);
|
|
||||||
return attachmentSource && attachmentSource[parentUuidProperty] === parentItem.uuid;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (effectsToRemove.length > 0) {
|
|
||||||
await actor.deleteEmbeddedDocuments('ActiveEffect', effectsToRemove.map(e => e.id));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Prepare attachment context data for rendering
|
|
||||||
* @param {Item} parentItem - The item (armor/weapon) that has attachments
|
|
||||||
* @returns {Promise<Object[]>} Array of attachment data objects
|
|
||||||
*/
|
|
||||||
export async function prepareAttachmentContext(parentItem) {
|
|
||||||
const attachedUUIDs = parentItem.system.attached;
|
|
||||||
return await Promise.all(
|
|
||||||
attachedUUIDs.map(async uuid => {
|
|
||||||
const item = await fromUuid(uuid);
|
|
||||||
return {
|
|
||||||
uuid: uuid,
|
|
||||||
name: item?.name || 'Unknown Item',
|
|
||||||
img: item?.img || 'icons/svg/item-bag.svg'
|
|
||||||
};
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add an attachment to an item via drag and drop
|
|
||||||
* @param {Object} options - Configuration options
|
|
||||||
* @param {Item} options.parentItem - The item (armor/weapon) that the item is being attached to
|
|
||||||
* @param {Item} options.droppedItem - The item being attached
|
|
||||||
* @param {string} options.parentType - Type of parent item ("armor" or "weapon")
|
|
||||||
* @returns {Promise<void>}
|
|
||||||
*/
|
|
||||||
export async function addAttachmentToItem({ parentItem, droppedItem, parentType }) {
|
|
||||||
const currentAttached = parentItem.system.attached;
|
|
||||||
const newUUID = droppedItem.uuid;
|
|
||||||
|
|
||||||
if (currentAttached.includes(newUUID)) {
|
|
||||||
ui.notifications.warn(`${droppedItem.name} is already attached to this ${parentType}.`);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const updatedAttached = [...currentAttached, newUUID];
|
|
||||||
|
|
||||||
await parentItem.update({
|
|
||||||
'system.attached': updatedAttached
|
|
||||||
});
|
|
||||||
|
|
||||||
// Copy effects from attached item to actor (only if parent item is equipped)
|
|
||||||
const actor = parentItem.parent;
|
|
||||||
if (actor && droppedItem.effects.size > 0 && parentItem.system.equipped) {
|
|
||||||
await copyAttachmentEffectsToActor({
|
|
||||||
parentItem,
|
|
||||||
attachedItem: droppedItem,
|
|
||||||
attachedUuid: newUUID,
|
|
||||||
parentType
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue