From c8ad685081ab4d590fba904b65c13b18bd2f1e90 Mon Sep 17 00:00:00 2001 From: Chomp Date: Tue, 20 May 2025 12:00:01 +0100 Subject: [PATCH] Improved `HasItemWithTpl` and `GetItemFromPoolByTpl` --- .../Helpers/ItemHelper.cs | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs index 5ac6e56c..c930b596 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/ItemHelper.cs @@ -97,36 +97,40 @@ public class ItemHelper( "right_side_plate" ]; - /** - * Does the provided pool of items contain the desired item - * @param itemPool Item collection to check - * @param item Item to look for - * @param slotId OPTIONAL - slotid of desired item - * @returns True if pool contains item - */ - public bool HasItemWithTpl(List itemPool, string item, string slotId = null) + /// + /// Does the provided pool of items contain the desired item + /// + /// Item collection to check + /// Item to look for + /// OPTIONAL - slotId of desired item + /// True if pool contains item + public bool HasItemWithTpl(IEnumerable itemPool, string item, string slotId = "") { // Filter the pool by slotId if provided - var filteredPool = slotId is not null ? itemPool.Where(item => item.SlotId?.StartsWith(slotId) ?? false) : itemPool; + var filteredPool = string.IsNullOrEmpty(slotId) + ? itemPool + : itemPool.Where(item => item.SlotId?.StartsWith(slotId, StringComparison.OrdinalIgnoreCase) ?? false); // Check if any item in the filtered pool matches the provided item return filteredPool.Any(poolItem => string.Equals(poolItem.Template, item, StringComparison.OrdinalIgnoreCase)); } - /** - * Get the first item from provided pool with the desired tpl - * @param itemPool Item collection to search - * @param item Item to look for - * @param slotId OPTIONAL - slotid of desired item - * @returns Item or null - */ - public Item GetItemFromPoolByTpl(List itemPool, string item, string slotId = null) + /// + /// Get the first item from provided pool with the desired tpl + /// + /// Item collection to search + /// Item tpl to find + /// OPTIONAL - slotId of desired item + /// Item or null if no item found + public Item GetItemFromPoolByTpl(IEnumerable itemPool, string tpl, string slotId = "") { // Filter the pool by slotId if provided - var filteredPool = slotId is not null ? itemPool.Where(item => item.SlotId?.StartsWith(slotId) ?? false) : itemPool; + var filteredPool = string.IsNullOrEmpty(slotId) + ? itemPool + : itemPool.Where(item => item.SlotId?.StartsWith(slotId, StringComparison.OrdinalIgnoreCase) ?? false); // Check if any item in the filtered pool matches the provided item - return filteredPool.FirstOrDefault(poolItem => poolItem.Template.Equals(item, StringComparison.OrdinalIgnoreCase)); + return filteredPool.FirstOrDefault(poolItem => poolItem.Template.Equals(tpl, StringComparison.OrdinalIgnoreCase)); } /**