diff --git a/system/scripts/actors/base-sheet.js b/system/scripts/actors/base-sheet.js index f43f3d4..dff2209 100644 --- a/system/scripts/actors/base-sheet.js +++ b/system/scripts/actors/base-sheet.js @@ -61,7 +61,7 @@ export class BaseSheetL5r5e extends ActorSheet { item.entity !== "Item" || !["item", "armor", "weapon", "technique", "peculiarity", "advancement"].includes(item.data.type) ) { - return Promise.resolve(); + return; } // Check if technique is allowed for this character @@ -80,10 +80,16 @@ export class BaseSheetL5r5e extends ActorSheet { }, }, }).render(true); - return Promise.resolve(); + return; } - // TODO dropped a item with same id as one owned, add qte instead + // Dropped a item with same "id" as one owned, add qte instead + if (item.data.data.quantity && this.actor.data.items) { + const tmpItem = this.actor.data.items.find((e) => e.name === item.name && e.type === item.type); + if (tmpItem && this._modifyQuantity(tmpItem._id, 1)) { + return; + } + } // Babele and properties specific if (item.data.data.properties && typeof Babele !== "undefined") { @@ -191,8 +197,15 @@ export class BaseSheetL5r5e extends ActorSheet { * Delete a generic item with sub type * @private */ - _deleteSubItem(event, type) { + _deleteSubItem(event) { const itemId = $(event.currentTarget).data("item-id"); + + // Remove 1 qty if possible + const tmpItem = this.actor.getOwnedItem(itemId); + if (tmpItem && tmpItem.data.data.quantity > 1 && this._modifyQuantity(tmpItem._id, -1)) { + return; + } + return this.actor.deleteOwnedItem(itemId); } @@ -211,4 +224,22 @@ export class BaseSheetL5r5e extends ActorSheet { }); } } + + /** + * Add or subtract a quantity to a owned item + * @private + */ + _modifyQuantity(itemId, add) { + const tmpItem = this.actor.getOwnedItem(itemId); + if (tmpItem) { + tmpItem.data.data.quantity = Math.max(1, tmpItem.data.data.quantity + add); + tmpItem.update({ + data: { + quantity: tmpItem.data.data.quantity, + }, + }); + return true; + } + return false; + } } diff --git a/system/scripts/actors/twenty-questions-dialog.js b/system/scripts/actors/twenty-questions-dialog.js index 4b5aeee..5e150f6 100644 --- a/system/scripts/actors/twenty-questions-dialog.js +++ b/system/scripts/actors/twenty-questions-dialog.js @@ -132,7 +132,7 @@ export class TwentyQuestionsDialog extends FormApplication { /** * Handle dropped items */ - _onDropItem(type, event) { + async _onDropItem(type, event) { if (!["item", "technique", "peculiarity"].includes(type)) { return; } @@ -142,17 +142,11 @@ export class TwentyQuestionsDialog extends FormApplication { return; } - // Try to extract the data - // {type: "Item", id: "pC37smMSCqu3aSRM"} - let data; try { - data = JSON.parse(event.dataTransfer.getData("text/plain")); - if (data.type !== "Item") { - return; - } // Get item - const item = game.items.get(data.id); + const item = await game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event); if ( + item.entity !== "Item" || !item || (type !== "item" && item.data.type !== type) || (type === "item" && !["item", "weapon", "armor"].includes(item.data.type)) diff --git a/system/scripts/helpers.js b/system/scripts/helpers.js index d931b14..6aed6ce 100644 --- a/system/scripts/helpers.js +++ b/system/scripts/helpers.js @@ -117,7 +117,7 @@ export class HelpersL5r5e { } const item = await Item.create(data, { temporary: true }); - // reinject compendium id + // reinject compendium id (required for properties) item.data._id = data._id; return item;