diff --git a/DeluxeGrabber/ModEntry.cs b/DeluxeGrabber/ModEntry.cs index c0a0839..55c2219 100644 --- a/DeluxeGrabber/ModEntry.cs +++ b/DeluxeGrabber/ModEntry.cs @@ -83,7 +83,7 @@ private void OnObjectListChanged(object sender, ObjectListChangedEventArgs e) { continue; } - if ((grabber.heldObject.Value as Chest).items.Count >= Chest.capacity) { + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity) { return; } @@ -119,7 +119,7 @@ private void OnObjectListChanged(object sender, ObjectListChangedEventArgs e) { } } - if ((grabber.heldObject.Value as Chest).items.Count > 0) { + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() > 0) { grabber.showNextIndex.Value = true; } } @@ -174,7 +174,7 @@ private void AutograbBuildings() { bool full = false; foreach (Vector2 tile in grabbables) { - if ((grabber.heldObject.Value as Chest).items.Count >= Chest.capacity) { + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity) { Monitor.Log($" Grabber is full", LogLevel.Trace); full = true; break; @@ -218,7 +218,7 @@ private void AutograbBuildings() { } // update sprite if grabber has items in it - if (grabber != null && (grabber.heldObject.Value as Chest).items.Count > 0) { + if (grabber != null && (grabber.heldObject.Value as Chest).items.CountIgnoreNull() > 0) { grabber.showNextIndex.Value = true; } } @@ -235,10 +235,10 @@ private void AutograbCrops() { foreach (KeyValuePair pair in location.Objects.Pairs) { if (pair.Value.Name.Contains("Grabber")) { SObject grabber = pair.Value; - if ((grabber.heldObject.Value == null) || (grabber.heldObject.Value as Chest).items.Count >= Chest.capacity) { + if ((grabber.heldObject.Value == null) || (grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity) { continue; } - bool full = (grabber.heldObject.Value as Chest).items.Count >= Chest.capacity; + bool full = (grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity; for (int x = (int)pair.Key.X - range; x < pair.Key.X + range + 1; x ++) { for (int y = (int)pair.Key.Y - range; y < pair.Key.Y + range + 1 && !full; y++) { Vector2 tile = new Vector2(x, y); @@ -278,10 +278,10 @@ private void AutograbCrops() { } } } - full = (grabber.heldObject.Value as Chest).items.Count >= Chest.capacity; + full = (grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity; } } - if (grabber != null && (grabber.heldObject.Value as Chest).items.Count > 0) { + if (grabber != null && (grabber.heldObject.Value as Chest).items.CountIgnoreNull() > 0) { grabber.showNextIndex.Value = true; } } @@ -440,7 +440,7 @@ private void AutograbWorld() { if (location.Name.Equals("Forest")) { foreach (TerrainFeature feature in location.terrainFeatures.Values) { - if ((grabber.heldObject.Value as Chest).items.Count >= Chest.capacity) { + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity) { Monitor.Log("Global grabber full", LogLevel.Info); return; } @@ -498,7 +498,7 @@ private void AutograbWorld() { break; } - if ((grabber.heldObject.Value as Chest).items.Count >= Chest.capacity) { + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity) { Monitor.Log("Global grabber full", LogLevel.Info); return; } @@ -528,7 +528,7 @@ private void AutograbWorld() { // add items to grabber and remove from floor foreach (Vector2 tile in grabbables) { - if ((grabber.heldObject.Value as Chest).items.Count >= Chest.capacity) { + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity) { Monitor.Log("Global grabber full", LogLevel.Info); return; } @@ -571,7 +571,7 @@ private void AutograbWorld() { foreach (SObject obj in location.Objects.Values) { - if ((grabber.heldObject.Value as Chest).items.Count >= Chest.capacity) + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() >= Chest.capacity) { Monitor.Log("Global grabber full", LogLevel.Info); return; @@ -604,7 +604,7 @@ private void AutograbWorld() { Monitor.Log($" {location} - found {pair.Value} {pair.Key}{plural}", LogLevel.Trace); } - if ((grabber.heldObject.Value as Chest).items.Count > 0) { + if ((grabber.heldObject.Value as Chest).items.CountIgnoreNull() > 0) { grabber.showNextIndex.Value = true; } } diff --git a/DeluxeGrabber/NetListExtend.cs b/DeluxeGrabber/NetListExtend.cs new file mode 100644 index 0000000..0bbe710 --- /dev/null +++ b/DeluxeGrabber/NetListExtend.cs @@ -0,0 +1,13 @@ +using Netcode; +using System.Linq; + +namespace DeluxeGrabber +{ + public static class NetListExtend + { + public static int CountIgnoreNull(this NetObjectList list) where T : class, INetObject + { + return list.Count(item => item != null); + } + } +}