summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2016-11-04 15:42:21 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2016-11-04 15:42:21 -0400
commit9a3b4260f7b8c59c41335b6d945df99ad4637bc3 (patch)
treeb540d170146a9fcd0e5aff92728d898db5aca9cb /src
parentbec7025aa49193f0191c3ce850339032d244b7b1 (diff)
downloadSMAPI-9a3b4260f7b8c59c41335b6d945df99ad4637bc3.tar.gz
SMAPI-9a3b4260f7b8c59c41335b6d945df99ad4637bc3.tar.bz2
SMAPI-9a3b4260f7b8c59c41335b6d945df99ad4637bc3.zip
refactor to avoid an out parameter
Diffstat (limited to 'src')
-rw-r--r--src/StardewModdingAPI/Events/PlayerEvents.cs5
-rw-r--r--src/StardewModdingAPI/Inheritance/SGame.cs41
2 files changed, 23 insertions, 23 deletions
diff --git a/src/StardewModdingAPI/Events/PlayerEvents.cs b/src/StardewModdingAPI/Events/PlayerEvents.cs
index 71b43e71..87e6a509 100644
--- a/src/StardewModdingAPI/Events/PlayerEvents.cs
+++ b/src/StardewModdingAPI/Events/PlayerEvents.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using StardewModdingAPI.Inheritance;
using StardewValley;
@@ -45,9 +46,9 @@ namespace StardewModdingAPI.Events
/// <summary>Raise an <see cref="InventoryChanged"/> event.</summary>
/// <param name="inventory">The player's inventory.</param>
/// <param name="changedItems">The inventory changes.</param>
- internal static void InvokeInventoryChanged(List<Item> inventory, List<ItemStackChange> changedItems)
+ internal static void InvokeInventoryChanged(List<Item> inventory, IEnumerable<ItemStackChange> changedItems)
{
- PlayerEvents.InventoryChanged.Invoke(null, new EventArgsInventoryChanged(inventory, changedItems));
+ PlayerEvents.InventoryChanged.Invoke(null, new EventArgsInventoryChanged(inventory, changedItems.ToList()));
}
/// <summary>Rase a <see cref="LeveledUp"/> event.</summary>
diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs
index f3dbc437..a7c63941 100644
--- a/src/StardewModdingAPI/Inheritance/SGame.cs
+++ b/src/StardewModdingAPI/Inheritance/SGame.cs
@@ -999,8 +999,8 @@ namespace StardewModdingAPI.Inheritance
}
// raise player inventory changed
- List<ItemStackChange> changedItems;
- if (this.HasInventoryChanged(Game1.player.items, out changedItems))
+ ItemStackChange[] changedItems = this.GetInventoryChanges(Game1.player.items, this.PreviousItems).ToArray();
+ if (changedItems.Any())
{
PlayerEvents.InvokeInventoryChanged(Game1.player.items, changedItems);
this.PreviousItems = Game1.player.items.Where(n => n != null).ToDictionary(n => n, n => n.Stack);
@@ -1064,38 +1064,37 @@ namespace StardewModdingAPI.Inheritance
}
}
- /// <summary>Get whether the player inventory has changed.</summary>
- /// <param name="items">The player's current inventory.</param>
- /// <param name="changedItems">An out parameter populated with the detected changes.</param>
- private bool HasInventoryChanged(List<Item> items, out List<ItemStackChange> changedItems)
+ /// <summary>Get the player inventory changes between two states.</summary>
+ /// <param name="current">The player's current inventory.</param>
+ /// <param name="previous">The player's previous inventory.</param>
+ private IEnumerable<ItemStackChange> GetInventoryChanges(IEnumerable<Item> current, IDictionary<Item, int> previous)
{
- changedItems = new List<ItemStackChange>();
- Item[] actualItems = items.Where(n => n != null).ToArray();
- foreach (var item in actualItems)
+ current = current.Where(n => n != null).ToArray();
+ foreach (Item item in current)
{
// stack size changed
- if (this.PreviousItems != null && this.PreviousItems.ContainsKey(item))
+ if (previous != null && previous.ContainsKey(item))
{
- if (this.PreviousItems[item] != item.Stack)
- changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack - this.PreviousItems[item], ChangeType = ChangeType.StackChange });
+ if (previous[item] != item.Stack)
+ yield return new ItemStackChange { Item = item, StackChange = item.Stack - previous[item], ChangeType = ChangeType.StackChange };
}
// new item
else
- changedItems.Add(new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added });
+ yield return new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added };
}
// removed items
- if (this.PreviousItems != null)
+ if (previous != null)
{
- changedItems.AddRange(
- from item in this.PreviousItems
- where actualItems.All(i => i != item.Key)
- select new ItemStackChange { Item = item.Key, StackChange = -item.Key.Stack, ChangeType = ChangeType.Removed }
- );
- }
+ foreach (var entry in previous)
+ {
+ if (current.Any(i => i == entry.Key))
+ continue;
- return changedItems.Any();
+ yield return new ItemStackChange { Item = entry.Key, StackChange = -entry.Key.Stack, ChangeType = ChangeType.Removed };
+ }
+ }
}
}
} \ No newline at end of file