From 9a3b4260f7b8c59c41335b6d945df99ad4637bc3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 4 Nov 2016 15:42:21 -0400 Subject: refactor to avoid an out parameter --- src/StardewModdingAPI/Inheritance/SGame.cs | 41 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 21 deletions(-) (limited to 'src/StardewModdingAPI/Inheritance/SGame.cs') 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 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 } } - /// Get whether the player inventory has changed. - /// The player's current inventory. - /// An out parameter populated with the detected changes. - private bool HasInventoryChanged(List items, out List changedItems) + /// Get the player inventory changes between two states. + /// The player's current inventory. + /// The player's previous inventory. + private IEnumerable GetInventoryChanges(IEnumerable current, IDictionary previous) { - changedItems = new List(); - 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 -- cgit