Improve handleRecipe()

Better handling of item rewards that need to be split into root + child (ammo boxes)
Use `addItemsToStash()`
move `recipe.isEncoded` check to earlier in function

make use of `getItem()` inside `splitStack()` instead of direct db access
This commit is contained in:
Dev 2024-02-01 11:23:32 +00:00
parent 92d949b445
commit 44560991b0
2 changed files with 44 additions and 47 deletions

View File

@ -27,6 +27,7 @@ import { IHideoutUpgradeRequestData } from "@spt-aki/models/eft/hideout/IHideout
import { IQteData } from "@spt-aki/models/eft/hideout/IQteData";
import { IRecordShootingRangePoints } from "@spt-aki/models/eft/hideout/IRecordShootingRangePoints";
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 { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
import { HideoutAreas } from "@spt-aki/models/enums/HideoutAreas";
@ -812,31 +813,23 @@ export class HideoutController
const rewardIsStackable = this.itemHelper.isItemTplStackable(recipe.endProduct);
if (rewardIsStackable)
{
// Add raw item to array without checking stack size
itemAndChildrenToSendToPlayer.push([{
const rewardToAdd: Item[] = [];
// Create root item
const rootItemToAdd: Item = {
_id: this.hashUtil.generate(),
_tpl: recipe.endProduct,
upd: {
StackObjectsCount: recipe.count
}
}]);
};
// Split item into separate items with acceptable stack sizes
const splitReward = this.itemHelper.splitStack(itemAndChildrenToSendToPlayer[0][0]);
if (splitReward.length > 1)
{
// Empty out reward array and replace with split items
itemAndChildrenToSendToPlayer = [];
for (const item of splitReward)
{
itemAndChildrenToSendToPlayer.push([item]);
}
}
const splitReward = this.itemHelper.splitStack(rewardToAdd[0]);
itemAndChildrenToSendToPlayer.push(splitReward);
}
else
{
// Not stackable, send multiple single items
// Add the initial item to array
if (!rewardIsPreset)
{
@ -854,10 +847,26 @@ export class HideoutController
null,
this.jsonUtil.clone(itemAndChildrenToSendToPlayer[0]),
);
itemAndChildrenToSendToPlayer.push(...[itemAndMods]);
}
}
// Recipe has an `isEncoded` requirement for reward(s), Add `RecodableComponent` property
if (recipe.isEncoded)
{
for (const reward of itemAndChildrenToSendToPlayer)
{
if (!reward[0].upd)
{
reward[0].upd = {}
}
reward[0].upd.RecodableComponent = { IsEncoded: true };
}
}
// Loops over all current productions on profile
const entries = Object.entries(pmcData.Hideout.Production);
let prodId: string;
@ -908,32 +917,20 @@ export class HideoutController
hoursCrafting -= this.hideoutConfig.hoursForSkillCrafting * multiplierCrafting;
}
// Loop over array of sub array (item+children) and add to stash
for (const itemAndChildrenToSend of itemAndChildrenToSendToPlayer)
{
// Recipe has an `isEncoded` requirement on reward, make root item adjustment
if (recipe.isEncoded)
{
if (!itemAndChildrenToSend[0].upd)
{
itemAndChildrenToSend[0].upd = {}
}
itemAndChildrenToSend[0].upd.RecodableComponent = { IsEncoded: true };
}
const additemRequest: IAddItemDirectRequest = {
itemWithModsToAdd: itemAndChildrenToSend,
// Create request for what we want to add to stash
const addItemsRequest: IAddItemsDirectRequest = {
itemsWithModsToAdd: itemAndChildrenToSendToPlayer,
foundInRaid: true,
callback: null,
useSortingTable: false
useSortingTable: false,
callback: null
};
this.inventoryHelper.addItemToStash(sessionID, additemRequest,pmcData, output)
// Add FiR crafted items(s) to player inventory
this.inventoryHelper.addItemsToStash(sessionID, addItemsRequest, pmcData, output);
if (output.warnings.length > 0)
{
return output;
}
}
// - increment skill point for crafting
// - delete the production in profile Hideout.Production

View File

@ -514,9 +514,9 @@ export class ItemHelper
}
/**
* split item stack if it exceeds its items StackMaxSize property
* Split item stack if it exceeds its items StackMaxSize property
* @param itemToSplit Item to split into smaller stacks
* @returns Array of split items
* @returns Array of root item + children
*/
public splitStack(itemToSplit: Item): Item[]
{
@ -525,17 +525,17 @@ export class ItemHelper
return [itemToSplit];
}
const maxStackSize = this.databaseServer.getTables().templates.items[itemToSplit._tpl]._props.StackMaxSize;
const maxStackSize = this.getItem(itemToSplit._tpl)[1]._props.StackMaxSize;
let remainingCount = itemToSplit.upd.StackObjectsCount;
const stacks: Item[] = [];
const rootAndChildren: Item[] = [];
// If the current count is already equal or less than the max
// then just return the item as is.
// return the item as is.
if (remainingCount <= maxStackSize)
{
stacks.push(this.jsonUtil.clone(itemToSplit));
rootAndChildren.push(this.jsonUtil.clone(itemToSplit));
return stacks;
return rootAndChildren;
}
while (remainingCount)
@ -546,10 +546,10 @@ export class ItemHelper
newStack._id = this.hashUtil.generate();
newStack.upd.StackObjectsCount = amount;
remainingCount -= amount;
stacks.push(newStack);
rootAndChildren.push(newStack);
}
return stacks;
return rootAndChildren;
}
/**