diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-04 15:42:21 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-04 15:42:21 -0400 |
commit | 9a3b4260f7b8c59c41335b6d945df99ad4637bc3 (patch) | |
tree | b540d170146a9fcd0e5aff92728d898db5aca9cb /src/StardewModdingAPI/Inheritance/SGame.cs | |
parent | bec7025aa49193f0191c3ce850339032d244b7b1 (diff) | |
download | SMAPI-9a3b4260f7b8c59c41335b6d945df99ad4637bc3.tar.gz SMAPI-9a3b4260f7b8c59c41335b6d945df99ad4637bc3.tar.bz2 SMAPI-9a3b4260f7b8c59c41335b6d945df99ad4637bc3.zip |
refactor to avoid an out parameter
Diffstat (limited to 'src/StardewModdingAPI/Inheritance/SGame.cs')
-rw-r--r-- | src/StardewModdingAPI/Inheritance/SGame.cs | 41 |
1 files changed, 20 insertions, 21 deletions
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 |