Fixed exception when pawn uses a bucket

This commit is contained in:
amevarashi 2023-07-09 09:49:51 +05:00
parent 811162875d
commit 2c225a2d41
2 changed files with 17 additions and 7 deletions

View File

@ -145,14 +145,15 @@ namespace RJWSexperience.Cum
if (!sexFillsCumbuckets)
return;
IEnumerable<Building_CumBucket> buckets = props.pawn.GetAdjacentBuildings<Building_CumBucket>();
// Enumerable throws System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
List<Building_CumBucket> buckets = props.pawn.GetAdjacentBuildings<Building_CumBucket>().ToList();
if (buckets?.EnumerableCount() > 0)
if (buckets?.Count > 0)
{
var initialCum = CumUtility.GetCumVolume(props.pawn);
var initialCum = GetCumVolume(props.pawn);
foreach (Building_CumBucket bucket in buckets)
{
bucket.AddCum(initialCum / buckets.EnumerableCount());
bucket.AddCum(initialCum / buckets.Count);
}
}
}

View File

@ -3,6 +3,7 @@ using rjw;
using rjw.Modules.Interactions.Enums;
using rjw.Modules.Interactions.Helpers;
using rjw.Modules.Interactions.Objects;
using RJWSexperience.Logs;
using System.Collections.Generic;
using Verse;
using Verse.AI;
@ -11,6 +12,7 @@ namespace RJWSexperience
{
public static class RJWUtility
{
private static readonly rjw.Modules.Shared.Logs.ILog s_log = LogManager.GetLogger<DebugLogProvider>("RJWUtility");
/// <summary>
/// For ideo patch
/// </summary>
@ -155,21 +157,28 @@ namespace RJWSexperience
public static Building_CumBucket FindClosestBucket(this Pawn pawn)
{
List<Building> buckets = pawn.Map.listerBuildings.allBuildingsColonist.FindAll(x => x is Building_CumBucket bucket && bucket.StoredStackCount < RsDefOf.Thing.GatheredCum.stackLimit);
if (buckets.NullOrEmpty())
if (buckets.Count == 0)
{
s_log.Message("FindClosestBucket: No buckets on the map or buckets are full");
return null;
}
Dictionary<Building, float> targets = new Dictionary<Building, float>();
for (int i = 0; i < buckets.Count; i++)
{
if (pawn.CanReach(buckets[i], PathEndMode.ClosestTouch, Danger.None))
if (pawn.CanReach(buckets[i], PathEndMode.ClosestTouch, Danger.Some))
{
targets.Add(buckets[i], pawn.Position.DistanceTo(buckets[i].Position));
}
}
if (!targets.NullOrEmpty())
if (targets.Count > 0)
{
return (Building_CumBucket)targets.MinBy(x => x.Value).Key;
}
else
{
s_log.Message("FindClosestBucket: No reachable buckets");
}
return null;
}
}