From bc3649d2a1d5ac506049f6d6e709201d352f060c Mon Sep 17 00:00:00 2001 From: Dev Date: Fri, 14 Jun 2024 19:18:55 +0100 Subject: [PATCH] Fixed issue where server would enter infinite loop when selling item to trader and stash contained no money --- project/src/helpers/InventoryHelper.ts | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/project/src/helpers/InventoryHelper.ts b/project/src/helpers/InventoryHelper.ts index 96e5759e..f93bc445 100644 --- a/project/src/helpers/InventoryHelper.ts +++ b/project/src/helpers/InventoryHelper.ts @@ -1305,22 +1305,28 @@ export class InventoryHelper */ public isItemInStash(pmcData: IPmcData, itemToCheck: Item): boolean { - const container = itemToCheck; - - while ("parentId" in container) + // Create recursive helper function + const isParentInStash = (itemId: string): boolean => { - if (container.parentId === pmcData.Inventory.stash && container.slotId === "hideout") + // Item not found / has no parent + const item = pmcData.Inventory.items.find((item) => item._id === itemId); + if (!item || !item.parentId) + { + return false; + } + + // Root level. Items parent is the stash with slotId "hideout" + if (item.parentId === pmcData.Inventory.stash && item.slotId === "hideout") { return true; } - if (!pmcData.Inventory.items.some((item) => item._id === container.parentId)) - { - break; - } - } + // Recursive case: Check the items parent + return isParentInStash(item.parentId); + }; - return false; + // Start recursive check + return isParentInStash(itemToCheck._id); } }