From 34d4de1d540630f92b021dc1574a503a1b30882d Mon Sep 17 00:00:00 2001 From: Chomp Date: Sun, 22 Jun 2025 08:35:58 +0100 Subject: [PATCH] Fixed nullref in loot generation when Location has no item limits --- .../Generators/LocationLootGenerator.cs | 21 +++++++++++-------- .../Helpers/CounterTrackerHelper.cs | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs b/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs index 45b1e89c..289118d5 100644 --- a/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs +++ b/Libraries/SPTarkov.Server.Core/Generators/LocationLootGenerator.cs @@ -63,7 +63,10 @@ public class LocationLootGenerator( ); // Store items with spawn count limits inside so they can be accessed later inside static/dynamic loot spawn methods - counterTrackerHelper.AddDataToTrack(itemsWithSpawnCountLimitsClone); + if (itemsWithSpawnCountLimitsClone is not null) + { + counterTrackerHelper.AddDataToTrack(itemsWithSpawnCountLimitsClone); + } // Create containers with loot result.AddRange(GenerateStaticContainers(locationId.ToLower(), staticAmmoDist)); @@ -191,7 +194,7 @@ public class LocationLootGenerator( _logger.Debug($"Added {guaranteedContainers.Count} guaranteed containers"); } - // Randomisation is turned off globally or just turned off for this map + // Randomisation is turned off for location / globally if ( !_locationConfig.ContainerRandomisationSettings.Enabled || !_locationConfig.ContainerRandomisationSettings.Maps.ContainsKey(locationId) @@ -405,7 +408,7 @@ public class LocationLootGenerator( containerDistribution.Add(new ProbabilityObject(x, value, value)); } - chosenContainerIds.AddRange(containerDistribution.Draw((int)containerData.ChosenCount)); + chosenContainerIds.AddRange(containerDistribution.Draw((int) containerData.ChosenCount)); return chosenContainerIds; } @@ -752,13 +755,13 @@ public class LocationLootGenerator( /// /// /// Location to generate loot for - /// + /// Dictionary of itemTpls and their max spawn count /// Array of spawn points with loot in them public List GenerateDynamicLoot( LooseLoot dynamicLootDist, Dictionary> staticAmmoDist, string locationName, - Dictionary spawnLimitedLoot + Dictionary? spawnLimitedLoot ) { List loot = []; @@ -859,7 +862,7 @@ public class LocationLootGenerator( if (randomSpawnPointCount > 0 && spawnPointArray.Count > 0) // Add randomly chosen spawn points { - foreach (var si in spawnPointArray.Draw((int)randomSpawnPointCount, false)) + foreach (var si in spawnPointArray.Draw((int) randomSpawnPointCount, false)) { chosenSpawnPoints.Add(spawnPointArray.Data(si)); } @@ -1010,13 +1013,13 @@ public class LocationLootGenerator( /// Forced loot locations that must be added /// Name of map currently having force loot created for /// - /// + /// Dictionary of itemTpls and their max spawn count protected void AddForcedLoot( List lootLocationTemplates, List forcedSpawnPoints, string locationName, Dictionary> staticAmmoDist, - Dictionary spawnLimitedLoot + Dictionary? spawnLimitedLoot ) { if (spawnLimitedLoot is not null) @@ -1067,7 +1070,7 @@ public class LocationLootGenerator( if (lootItem is null) { _logger.Warning( - $"Item with spawn point id {spawnPointLocationId} could not be found, skipping" + $"Item with spawn point id: {spawnPointLocationId} could not be found, skipping" ); continue; } diff --git a/Libraries/SPTarkov.Server.Core/Helpers/CounterTrackerHelper.cs b/Libraries/SPTarkov.Server.Core/Helpers/CounterTrackerHelper.cs index c8366702..955cd7d3 100644 --- a/Libraries/SPTarkov.Server.Core/Helpers/CounterTrackerHelper.cs +++ b/Libraries/SPTarkov.Server.Core/Helpers/CounterTrackerHelper.cs @@ -26,7 +26,7 @@ namespace SPTarkov.Server.Core.Helpers public bool IncrementCount(string key, int countToIncrementBy = 1) { // Not tracked, skip - if (!_maxCounts.ContainsKey(key)) + if (!_maxCounts.Any() || !_maxCounts.ContainsKey(key)) { return false; }