From 8dab47b3fd3c79de601cc8f53fa123604ef6ca70 Mon Sep 17 00:00:00 2001 From: Dev Date: Fri, 2 Feb 2024 15:35:02 +0000 Subject: [PATCH] Fix selling to trader an amount above stack size causing currency to not stack --- project/src/helpers/ItemHelper.ts | 35 +++++++++++++++++++++++++- project/src/services/PaymentService.ts | 25 +++++++++--------- 2 files changed, 47 insertions(+), 13 deletions(-) diff --git a/project/src/helpers/ItemHelper.ts b/project/src/helpers/ItemHelper.ts index ef335d43..569a4cb3 100644 --- a/project/src/helpers/ItemHelper.ts +++ b/project/src/helpers/ItemHelper.ts @@ -514,7 +514,7 @@ export class ItemHelper } /** - * Split item stack if it exceeds its items StackMaxSize property + * Split item stack if it exceeds its items StackMaxSize property into child items of passed in parent * @param itemToSplit Item to split into smaller stacks * @returns Array of root item + children */ @@ -552,6 +552,39 @@ export class ItemHelper return rootAndChildren; } + /** + * Turn items like money into separate stacks that adhere to max stack size + * @param itemToSplit Item to split into smaller stacks + * @returns + */ + public splitStackIntoSeparateItems(itemToSplit: Item): Item[][] + { + const itemTemplate = this.getItem(itemToSplit._tpl)[1]; + const itemMaxStackSize = itemTemplate._props.StackMaxSize ?? 1; + + // item already within bounds of stack size, return it + if (itemToSplit.upd?.StackObjectsCount <= itemMaxStackSize) + { + return [[itemToSplit]]; + } + + // Split items stack into chunks + const result: Item[][] = []; + let remainingCount = itemToSplit.upd.StackObjectsCount; + while (remainingCount) + { + const amount = Math.min(remainingCount, itemMaxStackSize); + const newItem = this.jsonUtil.clone(itemToSplit); + + newItem._id = this.hashUtil.generate(); + newItem.upd.StackObjectsCount = amount; + remainingCount -= amount; + result.push([newItem]); + } + + return result; + } + /** * Find Barter items from array of items * @param {string} by tpl or id diff --git a/project/src/services/PaymentService.ts b/project/src/services/PaymentService.ts index 89a0d738..a2626528 100644 --- a/project/src/services/PaymentService.ts +++ b/project/src/services/PaymentService.ts @@ -7,7 +7,7 @@ import { PaymentHelper } from "@spt-aki/helpers/PaymentHelper"; import { TraderHelper } from "@spt-aki/helpers/TraderHelper"; import { IPmcData } from "@spt-aki/models/eft/common/IPmcData"; import { Item } from "@spt-aki/models/eft/common/tables/IItem"; -import { IAddItemDirectRequest } from "@spt-aki/models/eft/inventory/IAddItemDirectRequest"; +import { IAddItemsDirectRequest } from "@spt-aki/models/eft/inventory/IAddItemsDirectRequest"; import { IItemEventRouterResponse } from "@spt-aki/models/eft/itemEvent/IItemEventRouterResponse"; import { IProcessBuyTradeRequestData } from "@spt-aki/models/eft/trade/IProcessBuyTradeRequestData"; import { IProcessSellTradeRequestData } from "@spt-aki/models/eft/trade/IProcessSellTradeRequestData"; @@ -219,23 +219,24 @@ export class PaymentService } } + const rootCurrencyReward = { + _id: this.hashUtil.generate(), + _tpl: currency, + upd: { + StackObjectsCount: calcAmount + } + }; + const rewards = this.itemHelper.splitStackIntoSeparateItems(rootCurrencyReward); + if (!skipSendingMoneyToStash) { - const addItemToStashRequest: IAddItemDirectRequest = { - itemWithModsToAdd: [ - { - _id: this.hashUtil.generate(), - _tpl: currency, - upd: { - StackObjectsCount: calcAmount - } - } - ], + const addItemToStashRequest: IAddItemsDirectRequest = { + itemsWithModsToAdd: rewards, foundInRaid: false, callback: null, useSortingTable: true }; - this.inventoryHelper.addItemToStash(sessionID, addItemToStashRequest, pmcData, output); + this.inventoryHelper.addItemsToStash(sessionID, addItemToStashRequest, pmcData, output); } // set current sale sum