diff options
Diffstat (limited to 'src/SMAPI/Framework/StateTracking')
4 files changed, 35 insertions, 40 deletions
diff --git a/src/SMAPI/Framework/StateTracking/ChestTracker.cs b/src/SMAPI/Framework/StateTracking/ChestTracker.cs index 3ce70c17..65f58ee7 100644 --- a/src/SMAPI/Framework/StateTracking/ChestTracker.cs +++ b/src/SMAPI/Framework/StateTracking/ChestTracker.cs @@ -1,12 +1,10 @@ using System; using System.Collections.Generic; using System.Linq; -using StardewModdingAPI.Events; using StardewModdingAPI.Framework.StateTracking.Comparers; using StardewModdingAPI.Framework.StateTracking.FieldWatchers; using StardewValley; using StardewValley.Objects; -using ChangeType = StardewModdingAPI.Events.ChangeType; namespace StardewModdingAPI.Framework.StateTracking { @@ -83,26 +81,12 @@ namespace StardewModdingAPI.Framework.StateTracking this.Removed.Clear(); } - /// <summary>Get the inventory changes since the last update.</summary> - public IEnumerable<ItemStackChange> GetInventoryChanges() + /// <summary>Get the inventory changes since the last update, if anything changed.</summary> + /// <param name="changes">The inventory changes, or <c>null</c> if nothing changed.</param> + /// <returns>Returns whether anything changed.</returns> + public bool TryGetInventoryChanges(out SnapshotItemListDiff changes) { - // removed - foreach (Item item in this.Removed) - yield return new ItemStackChange { Item = item, StackChange = -item.Stack, ChangeType = ChangeType.Removed }; - - // added - foreach (Item item in this.Added) - yield return new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added }; - - // stack size changed - foreach (var entry in this.StackSizes) - { - Item item = entry.Key; - int prevStack = entry.Value; - - if (item.Stack != prevStack) - yield return new ItemStackChange { Item = item, StackChange = item.Stack - prevStack, ChangeType = ChangeType.StackChange }; - } + return SnapshotItemListDiff.TryGetChanges(added: this.Added, removed: this.Removed, stackSizes: this.StackSizes, out changes); } /// <summary>Release watchers and resources.</summary> diff --git a/src/SMAPI/Framework/StateTracking/PlayerTracker.cs b/src/SMAPI/Framework/StateTracking/PlayerTracker.cs index d25f3345..cf49a7c1 100644 --- a/src/SMAPI/Framework/StateTracking/PlayerTracker.cs +++ b/src/SMAPI/Framework/StateTracking/PlayerTracker.cs @@ -2,10 +2,9 @@ using System; using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Enums; -using StardewModdingAPI.Events; +using StardewModdingAPI.Framework.StateTracking.Comparers; using StardewModdingAPI.Framework.StateTracking.FieldWatchers; using StardewValley; -using ChangeType = StardewModdingAPI.Events.ChangeType; namespace StardewModdingAPI.Framework.StateTracking { @@ -99,20 +98,24 @@ namespace StardewModdingAPI.Framework.StateTracking return this.Player.currentLocation ?? this.LastValidLocation; } - /// <summary>Get the inventory changes since the last update.</summary> - public IEnumerable<ItemStackChange> GetInventoryChanges() + /// <summary>Get the inventory changes since the last update, if anything changed.</summary> + /// <param name="changes">The inventory changes, or <c>null</c> if nothing changed.</param> + /// <returns>Returns whether anything changed.</returns> + public bool TryGetInventoryChanges(out SnapshotItemListDiff changes) { - IDictionary<Item, int> previous = this.PreviousInventory; IDictionary<Item, int> current = this.GetInventory(); - foreach (Item item in previous.Keys.Union(current.Keys)) + + ISet<Item> added = new HashSet<Item>(new ObjectReferenceComparer<Item>()); + ISet<Item> removed = new HashSet<Item>(new ObjectReferenceComparer<Item>()); + foreach (Item item in this.PreviousInventory.Keys.Union(current.Keys)) { - if (!previous.TryGetValue(item, out int prevStack)) - yield return new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added }; - else if (!current.TryGetValue(item, out int newStack)) - yield return new ItemStackChange { Item = item, StackChange = -item.Stack, ChangeType = ChangeType.Removed }; - else if (prevStack != newStack) - yield return new ItemStackChange { Item = item, StackChange = newStack - prevStack, ChangeType = ChangeType.StackChange }; + if (!this.PreviousInventory.ContainsKey(item)) + added.Add(item); + else if (!current.ContainsKey(item)) + removed.Add(item); } + + return SnapshotItemListDiff.TryGetChanges(added: added, removed: removed, stackSizes: this.PreviousInventory, out changes); } /// <summary>Release watchers and resources.</summary> diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs index edfba736..6ae52fd0 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; -using System.Linq; using Microsoft.Xna.Framework; -using StardewModdingAPI.Events; using StardewValley; using StardewValley.Buildings; using StardewValley.Objects; @@ -37,7 +35,7 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots public SnapshotListDiff<KeyValuePair<Vector2, TerrainFeature>> TerrainFeatures { get; } = new SnapshotListDiff<KeyValuePair<Vector2, TerrainFeature>>(); /// <summary>Tracks changed chest inventories.</summary> - public IDictionary<Chest, ItemStackChange[]> ChestItems { get; } = new Dictionary<Chest, ItemStackChange[]>(); + public IDictionary<Chest, SnapshotItemListDiff> ChestItems { get; } = new Dictionary<Chest, SnapshotItemListDiff>(); /********* @@ -66,8 +64,7 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots this.ChestItems.Clear(); foreach (ChestTracker tracker in watcher.ChestWatchers.Values) { - ItemStackChange[] changes = tracker.GetInventoryChanges().ToArray(); - if (changes.Length > 0) + if (tracker.TryGetInventoryChanges(out SnapshotItemListDiff changes)) this.ChestItems[tracker.Chest] = changes; } } diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/PlayerSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/PlayerSnapshot.cs index 7bcd9f82..f0fb9485 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/PlayerSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/PlayerSnapshot.cs @@ -11,6 +11,13 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots internal class PlayerSnapshot { /********* + ** Fields + *********/ + /// <summary>An empty item list diff.</summary> + private readonly SnapshotItemListDiff EmptyItemListDiff = new SnapshotItemListDiff(new Item[0], new Item[0], new ItemStackSizeChange[0]); + + + /********* ** Accessors *********/ /// <summary>The player being tracked.</summary> @@ -27,7 +34,7 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots .ToDictionary(skill => skill, skill => new SnapshotDiff<int>()); /// <summary>Get a list of inventory changes.</summary> - public IEnumerable<ItemStackChange> InventoryChanges { get; private set; } + public SnapshotItemListDiff Inventory { get; private set; } /********* @@ -47,7 +54,11 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots this.Location.Update(watcher.LocationWatcher); foreach (var pair in this.Skills) pair.Value.Update(watcher.SkillWatchers[pair.Key]); - this.InventoryChanges = watcher.GetInventoryChanges().ToArray(); + + this.Inventory = watcher.TryGetInventoryChanges(out SnapshotItemListDiff itemChanges) + ? itemChanges + : this.EmptyItemListDiff; + } } } |