Fix selling to trader an amount above stack size causing currency to not stack

This commit is contained in:
Dev 2024-02-02 15:35:02 +00:00
parent a1e84992a2
commit 8dab47b3fd
2 changed files with 47 additions and 13 deletions

View File

@ -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

View File

@ -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