From 88c9f71f8c383c47abf78855bbab0b04cc173f2e Mon Sep 17 00:00:00 2001 From: Chomp Date: Fri, 30 May 2025 12:47:01 +0100 Subject: [PATCH] Fixed `AdjustWeighting` not correctly ignoring items in the edit list not found at the destination Added nullguards Added missing comment Simplified if statement --- .../Services/BotEquipmentFilterService.cs | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs b/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs index 4174a72c..55d91e56 100644 --- a/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs +++ b/Libraries/SPTarkov.Server.Core/Services/BotEquipmentFilterService.cs @@ -70,8 +70,9 @@ public class BotEquipmentFilterService { AdjustWeighting(botWeightingAdjustments.Equipment, baseBotNode.BotInventory.Equipment); AdjustWeighting(botWeightingAdjustments.Ammo, baseBotNode.BotInventory.Ammo); - // Don't warn when edited item not found, we're editing usec/bear clothing and they dont have each others clothing - AdjustWeighting(botWeightingAdjustments?.Clothing, baseBotNode.BotAppearance, false); + + // Don't warn when edited item not found, we're editing usec/bear clothing and they don't have each others clothing + AdjustWeighting(botWeightingAdjustments.Clothing, baseBotNode.BotAppearance, false); } if (botWeightingAdjustmentsByPlayerLevel is not null) @@ -455,8 +456,9 @@ public class BotEquipmentFilterService /// /// Weighting change to apply to bot /// Bot item dictionary to adjust + /// When item being adjusted cannot be found at source, show warning message protected void AdjustWeighting( - AdjustmentDetails weightingAdjustments, + AdjustmentDetails? weightingAdjustments, Appearance botItemPool, bool showEditWarnings = true) { @@ -470,6 +472,11 @@ public class BotEquipmentFilterService foreach (var poolAdjustmentKvP in weightingAdjustments.Add) { var locationToUpdate = botItemPool.GetByJsonProp>(poolAdjustmentKvP.Key); + if (locationToUpdate is null) + { + continue; + } + foreach (var itemToAddKvP in poolAdjustmentKvP.Value) { locationToUpdate[itemToAddKvP.Key] = itemToAddKvP.Value; @@ -482,21 +489,27 @@ public class BotEquipmentFilterService foreach (var poolAdjustmentKvP in weightingAdjustments.Edit) { var locationToUpdate = botItemPool.GetByJsonProp>(poolAdjustmentKvP.Key); + if (locationToUpdate is null) + { + continue; + } + foreach (var itemToEditKvP in poolAdjustmentKvP.Value) // Only make change if item exists as we're editing, not adding { - if (locationToUpdate.GetValueOrDefault(itemToEditKvP.Key) != null || locationToUpdate.GetValueOrDefault(itemToEditKvP.Key) == 0) + if (locationToUpdate.ContainsKey(itemToEditKvP.Key)) { locationToUpdate[itemToEditKvP.Key] = itemToEditKvP.Value; + + continue; } - else + + // We tried to add an item flagged as edit only + if (showEditWarnings) { - if (showEditWarnings) + if (_logger.IsLogEnabled(LogLevel.Debug)) { - if (_logger.IsLogEnabled(LogLevel.Debug)) - { - _logger.Debug($"Tried to edit a non - existent item for slot: {poolAdjustmentKvP} {itemToEditKvP}"); - } + _logger.Debug($"Tried to edit a non - existent item for slot: {poolAdjustmentKvP} {itemToEditKvP}"); } } }