diff --git a/module/applications/dialogs/itemTransfer.mjs b/module/applications/dialogs/itemTransfer.mjs index ad3cf103..42e3a727 100644 --- a/module/applications/dialogs/itemTransfer.mjs +++ b/module/applications/dialogs/itemTransfer.mjs @@ -38,13 +38,15 @@ export default class ItemTransferDialog extends HandlebarsApplicationMixin(Appli originActor ??= item?.actor; const homebrewKey = CONFIG.DH.SETTINGS.gameSettings.Homebrew; const currencySetting = game.settings.get(CONFIG.DH.id, homebrewKey).currency?.[currency] ?? null; + const max = item?.system.quantity ?? originActor.system.gold[currency] ?? 0; return { originActor, targetActor, itemImage: item?.img, currencyIcon: currencySetting?.icon, - max: item?.system.quantity ?? originActor.system.gold[currency] ?? 0, + max, + initial: targetActor.system.metadata.quantifiable?.includes(item.type) ? max : 1, title: item?.name ?? currencySetting?.label }; } diff --git a/module/applications/sheets/api/base-actor.mjs b/module/applications/sheets/api/base-actor.mjs index f009267e..e23a4426 100644 --- a/module/applications/sheets/api/base-actor.mjs +++ b/module/applications/sheets/api/base-actor.mjs @@ -298,47 +298,79 @@ export default class DHBaseActorSheet extends DHApplicationMixin(ActorSheetV2) { ); } - if (item.system.metadata.isQuantifiable) { - const actorItem = originActor.items.get(data.originId); - const quantityTransfered = await game.system.api.applications.dialogs.ItemTransferDialog.configure({ + // Perform the actual transfer, showing a dialog when doing it + const availableQuantity = Math.max(1, item.system.quantity); + const actorItem = originActor.items.get(data.originId) ?? item; + if (availableQuantity > 1) { + const quantityTransferred = await game.system.api.applications.dialogs.ItemTransferDialog.configure({ item, targetActor: this.document }); - - if (quantityTransfered) { - const existingItem = this.document.items.find(x => itemIsIdentical(x, item)); - if (existingItem) { - await existingItem.update({ - 'system.quantity': existingItem.system.quantity + quantityTransfered - }); - } else { - const createData = item.toObject(); - await this.document.createEmbeddedDocuments('Item', [ - { - ...createData, - system: { - ...createData.system, - quantity: quantityTransfered - } - } - ]); - } - - if (quantityTransfered === actorItem.system.quantity) { - await originActor.deleteEmbeddedDocuments('Item', [data.originId]); - } else { - await actorItem.update({ - 'system.quantity': actorItem.system.quantity - quantityTransfered - }); - } - } + return this.#transferItem(actorItem, quantityTransferred); } else { - await this.document.createEmbeddedDocuments('Item', [item.toObject()]); - await originActor.deleteEmbeddedDocuments('Item', [data.originId]); + return this.#transferItem(actorItem, availableQuantity); } } } + /** + * Helper to perform the actual transfer of an item to this actor, including stack/unstack logic based on target quantifiability. + * Make sure item is the actor item before calling this method or there will be issues + */ + async #transferItem(item, quantity) { + const originActor = item.actor; + const targetActor = this.document; + const allowStacking = targetActor.system.metadata.quantifiable?.includes(item.type); + + const batch = []; + + // First add/update the item to the target actor + const existing = allowStacking ? targetActor.items.find(x => itemIsIdentical(x, item)) : null; + if (existing) { + batch.push({ + action: 'update', + documentName: 'Item', + parent: targetActor, + updates: [{ '_id': existing.id, 'system.quantity': existing.system.quantity + quantity }] + }); + } else { + const itemsToCreate = []; + if (allowStacking) { + itemsToCreate.push(foundry.utils.mergeObject(item.toObject(true), { system: { quantity } })); + } else { + const createData = new Array(Math.max(1, quantity)) + .fill(0) + .map(() => foundry.utils.mergeObject(item.toObject(), { system: { quantity: 1 } })); + itemsToCreate.push(...createData); + } + batch.push({ + action: 'create', + documentName: 'Item', + parent: targetActor, + data: itemsToCreate + }); + } + + // Remove the item from the original actor (by either deleting it, or updating its quantity) + if (quantity >= item.system.quantity) { + batch.push({ + action: 'delete', + documentName: 'Item', + parent: originActor, + ids: [item.id] + }); + } else { + batch.push({ + action: 'update', + documentName: 'Item', + parent: originActor, + updates: [{ '_id': item.id, 'system.quantity': item.system.quantity - quantity }] + }); + } + + return foundry.documents.modifyBatch(batch); + } + /** * On dragStart on the item. * @param {DragEvent} event - The drag event diff --git a/module/data/actor/character.mjs b/module/data/actor/character.mjs index 13a47b45..82dd8707 100644 --- a/module/data/actor/character.mjs +++ b/module/data/actor/character.mjs @@ -19,7 +19,8 @@ export default class DhCharacter extends DhCreature { type: 'character', settingSheet: DHCharacterSettings, isNPC: false, - hasInventory: true + hasInventory: true, + quantifiable: ["loot", "consumable"] }); } diff --git a/module/data/actor/party.mjs b/module/data/actor/party.mjs index 46635237..6ccf8852 100644 --- a/module/data/actor/party.mjs +++ b/module/data/actor/party.mjs @@ -8,7 +8,8 @@ export default class DhParty extends BaseDataActor { /** @inheritdoc */ static get metadata() { return foundry.utils.mergeObject(super.metadata, { - hasInventory: true + hasInventory: true, + quantifiable: ["weapon", "armor", "loot", "consumable"] }); } diff --git a/module/data/item/armor.mjs b/module/data/item/armor.mjs index b0e4847f..d292b184 100644 --- a/module/data/item/armor.mjs +++ b/module/data/item/armor.mjs @@ -23,6 +23,7 @@ export default class DHArmor extends AttachableItem { current: new fields.NumberField({ integer: true, min: 0, initial: 0 }), max: new fields.NumberField({ required: true, integer: true, initial: 0 }) }), + quantity: new fields.NumberField({ integer: true, initial: 1, min: 0, required: true }), baseThresholds: new fields.SchemaField({ major: new fields.NumberField({ integer: true, initial: 0 }), severe: new fields.NumberField({ integer: true, initial: 0 }) diff --git a/module/data/item/consumable.mjs b/module/data/item/consumable.mjs index ab527967..89573364 100644 --- a/module/data/item/consumable.mjs +++ b/module/data/item/consumable.mjs @@ -7,7 +7,6 @@ export default class DHConsumable extends BaseDataItem { label: 'TYPES.Item.consumable', type: 'consumable', hasDescription: true, - isQuantifiable: true, isInventoryItem: true, hasActions: true }); @@ -18,7 +17,8 @@ export default class DHConsumable extends BaseDataItem { const fields = foundry.data.fields; return { ...super.defineSchema(), - consumeOnUse: new fields.BooleanField({ initial: true }) + consumeOnUse: new fields.BooleanField({ initial: true }), + quantity: new fields.NumberField({ integer: true, initial: 1, min: 0, required: true }) }; } diff --git a/module/data/item/loot.mjs b/module/data/item/loot.mjs index cdb0855e..2d5865e9 100644 --- a/module/data/item/loot.mjs +++ b/module/data/item/loot.mjs @@ -7,7 +7,6 @@ export default class DHLoot extends BaseDataItem { label: 'TYPES.Item.loot', type: 'loot', hasDescription: true, - isQuantifiable: true, isInventoryItem: true, hasActions: true }); @@ -15,8 +14,10 @@ export default class DHLoot extends BaseDataItem { /** @inheritDoc */ static defineSchema() { + const fields = foundry.data.fields; return { - ...super.defineSchema() + ...super.defineSchema(), + quantity: new fields.NumberField({ integer: true, initial: 1, min: 0, required: true }), }; } diff --git a/module/data/item/weapon.mjs b/module/data/item/weapon.mjs index 9335037c..55e70c4d 100644 --- a/module/data/item/weapon.mjs +++ b/module/data/item/weapon.mjs @@ -26,6 +26,7 @@ export default class DHWeapon extends AttachableItem { label: 'DAGGERHEART.GENERAL.Tiers.singular' }), equipped: new fields.BooleanField({ initial: false }), + quantity: new fields.NumberField({ integer: true, initial: 1, min: 0, required: true }), //SETTINGS secondary: new fields.BooleanField({ initial: false, label: 'DAGGERHEART.ITEMS.Weapon.secondaryWeapon' }), diff --git a/templates/dialogs/item-transfer.hbs b/templates/dialogs/item-transfer.hbs index 0e7df3dc..63972ed8 100644 --- a/templates/dialogs/item-transfer.hbs +++ b/templates/dialogs/item-transfer.hbs @@ -14,7 +14,7 @@