Added transfer logic

This commit is contained in:
WBHarry 2025-11-21 00:15:38 +01:00
parent e701786f45
commit b73c59dab7
9 changed files with 180 additions and 4 deletions

View file

@ -8,7 +8,6 @@ import { getDocFromElement, getDocFromElementSync } from '../../../helpers/utils
/**@typedef {import('@client/applications/_types.mjs').ApplicationClickAction} ApplicationClickAction */
const { TextEditor } = foundry.applications.ux;
export default class CharacterSheet extends DHBaseActorSheet {
/**@inheritdoc */
static DEFAULT_OPTIONS = {
@ -881,6 +880,8 @@ export default class CharacterSheet extends DHBaseActorSheet {
const item = await getDocFromElement(event.target);
const dragData = {
originActor: this.document.uuid,
originId: item.id,
type: item.documentName,
uuid: item.uuid
};
@ -894,9 +895,12 @@ export default class CharacterSheet extends DHBaseActorSheet {
// Prevent event bubbling to avoid duplicate handling
event.preventDefault();
event.stopPropagation();
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
super._onDrop(event);
this._onDropItem(event, TextEditor.getDragEventData(event));
const { cancel } = await super._onDrop(event);
if (cancel) return;
this._onDropItem(event, data);
}
async _onDropItem(event, data) {

View file

@ -441,6 +441,8 @@ export default class Party extends DHBaseActorSheet {
async _onDragStart(event) {
const item = await getDocFromElement(event.target);
const dragData = {
originActor: this.document.uuid,
originId: item.id,
type: item.documentName,
uuid: item.uuid
};
@ -453,8 +455,11 @@ export default class Party extends DHBaseActorSheet {
// Prevent event bubbling to avoid duplicate handling
event.preventDefault();
event.stopPropagation();
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
const { cancel } = await super._onDrop(event);
if (cancel) return;
const document = await foundry.utils.fromUuid(data.uuid);
if (document instanceof DhpActor && Party.ALLOWED_ACTOR_TYPES.includes(document.type)) {

View file

@ -217,6 +217,72 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) {
/* Application Drag/Drop */
/* -------------------------------------------- */
async _onDrop(event) {
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
if (data.originActor === this.document.uuid) return { cancel: true };
/* Handling transfer of inventoryItems */
let cancel = false;
const physicalActorTypes = ['character', 'party'];
if (physicalActorTypes.includes(this.document.type)) {
const originActor = data.originActor ? await foundry.utils.fromUuid(data.originActor) : null;
if (data.originId && originActor && physicalActorTypes.includes(originActor.type)) {
const dropDocument = await foundry.utils.fromUuid(data.uuid);
if (dropDocument.system.metadata.isInventoryItem) {
cancel = true;
if (dropDocument.system.metadata.isQuantifiable) {
const actorItem = originActor.items.get(data.originId);
const quantityTransfered =
actorItem.system.quantity === 1
? 1
: await game.system.api.applications.dialogs.ItemTransferDialog.configure(dropDocument);
if (quantityTransfered) {
if (quantityTransfered === actorItem.system.quantity) {
await originActor.deleteEmbeddedDocuments('Item', [data.originId]);
} else {
cancel = true;
await actorItem.update({
'system.quantity': actorItem.system.quantity - quantityTransfered
});
}
const existingItem = this.document.items.find(
x => x._stats.compendiumSource === actorItem._stats.compendiumSource
);
if (existingItem) {
cancel = true;
await existingItem.update({
'system.quantity': existingItem.system.quantity + quantityTransfered
});
} else {
const createData = dropDocument.toObject();
await this.document.createEmbeddedDocuments('Item', [
{
...createData,
system: {
...createData.system,
quantity: quantityTransfered
}
}
]);
}
} else {
cancel = true;
}
} else {
await originActor.deleteEmbeddedDocuments('Item', [data.originId]);
const createData = dropDocument.toObject();
await this.document.createEmbeddedDocuments('Item', [createData]);
}
}
}
}
return { cancel };
}
/**
* On dragStart on the item.
* @param {DragEvent} event - The drag event