From 0411dcf3db277ed0d3c9f0201b7554a7d61ed1e8 Mon Sep 17 00:00:00 2001 From: wartech0 Date: Tue, 31 Dec 2019 04:20:36 -0600 Subject: Finished chest events --- src/SMAPI/Framework/SGame.cs | 4 +--- src/SMAPI/Framework/StateTracking/ChestTracker.cs | 6 +++--- src/SMAPI/Framework/StateTracking/LocationTracker.cs | 15 +++++++++------ .../StateTracking/Snapshots/LocationSnapshot.cs | 20 ++++++++++---------- .../Snapshots/WorldLocationsSnapshot.cs | 2 +- 5 files changed, 24 insertions(+), 23 deletions(-) (limited to 'src/SMAPI/Framework') diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 64da1cf1..c62bcf84 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -707,9 +707,7 @@ namespace StardewModdingAPI.Framework { foreach (var pair in locState.ChestItems) { - var diff = pair.Value; - if (diff.IsChanged) - events.ChestInventoryChanged.Raise(new ChestInventoryChangedEventArgs(location, pair.Key, diff.Added, diff.Removed)); + events.ChestInventoryChanged.Raise(new ChestInventoryChangedEventArgs(location, pair.Key, pair.Value)); } } diff --git a/src/SMAPI/Framework/StateTracking/ChestTracker.cs b/src/SMAPI/Framework/StateTracking/ChestTracker.cs index 4440bf4b..74039753 100644 --- a/src/SMAPI/Framework/StateTracking/ChestTracker.cs +++ b/src/SMAPI/Framework/StateTracking/ChestTracker.cs @@ -44,15 +44,15 @@ namespace StardewModdingAPI.Framework.StateTracking public void Reset() { - this.PreviousInventory = this.CurrentInventory; + if(this.CurrentInventory!=null) + this.PreviousInventory = this.CurrentInventory; } public IEnumerable GetInventoryChanges() { IDictionary previous = this.PreviousInventory; - Console.WriteLine(previous.Count); IDictionary current = this.GetInventory(); - Console.WriteLine(current.Count); + foreach (Item item in previous.Keys.Union(current.Keys)) { if (!previous.TryGetValue(item, out int prevStack)) diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs index 170fd537..659efc57 100644 --- a/src/SMAPI/Framework/StateTracking/LocationTracker.cs +++ b/src/SMAPI/Framework/StateTracking/LocationTracker.cs @@ -49,7 +49,7 @@ namespace StardewModdingAPI.Framework.StateTracking public IDictionaryWatcher TerrainFeaturesWatcher { get; } /// Tracks items added or removed to chests. - public Dictionary> ChestWatchers = new Dictionary>(); + public Dictionary ChestWatchers = new Dictionary(); /********* @@ -89,6 +89,9 @@ namespace StardewModdingAPI.Framework.StateTracking watcher.Update(); this.UpdateChestWatcherList(added: this.ObjectsWatcher.Added, removed: this.ObjectsWatcher.Removed); + + foreach (var watcher in this.ChestWatchers) + watcher.Value.Update(); } /// Set the current value as the baseline. @@ -96,6 +99,9 @@ namespace StardewModdingAPI.Framework.StateTracking { foreach (IWatcher watcher in this.Watchers) watcher.Reset(); + + foreach (var watcher in this.ChestWatchers) + watcher.Value.Reset(); } /// Stop watching the player fields and release all references. @@ -117,9 +123,8 @@ namespace StardewModdingAPI.Framework.StateTracking // remove unused watchers foreach (KeyValuePair pair in removed) { - if (pair.Value is Chest && this.ChestWatchers.TryGetValue(pair.Key, out ICollectionWatcher watcher)) + if (pair.Value is Chest && this.ChestWatchers.TryGetValue(pair.Key, out ChestTracker watcher)) { - this.Watchers.Remove(watcher); this.ChestWatchers.Remove(pair.Key); } } @@ -129,9 +134,7 @@ namespace StardewModdingAPI.Framework.StateTracking { if (pair.Value is Chest chest && !this.ChestWatchers.ContainsKey(pair.Key)) { - ICollectionWatcher watcher = new NetListWatcher(chest.items); - this.Watchers.Add(watcher); - this.ChestWatchers.Add(pair.Key, watcher); + this.ChestWatchers.Add(pair.Key, new ChestTracker(chest)); } } } diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs index 4074336b..62a56c84 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; +using StardewModdingAPI.Events; using StardewValley; using StardewValley.Buildings; using StardewValley.TerrainFeatures; @@ -35,7 +36,7 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots public SnapshotListDiff> TerrainFeatures { get; } = new SnapshotListDiff>(); /// Tracks changed chest inventories. - public IDictionary> ChestItems { get; } = new Dictionary>(); + public IDictionary ChestItems { get; } = new Dictionary(); /********* @@ -61,17 +62,16 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots this.TerrainFeatures.Update(watcher.TerrainFeaturesWatcher); // chest inventories - foreach (Vector2 key in this.ChestItems.Keys.ToArray()) - { - if (!watcher.ChestWatchers.ContainsKey(key)) - this.ChestItems.Remove(key); - } foreach (var pair in watcher.ChestWatchers) { - if (!this.ChestItems.TryGetValue(pair.Key, out var diff)) - this.ChestItems[pair.Key] = diff = new SnapshotListDiff(); - - diff.Update(pair.Value); + IEnumerable temp = pair.Value.GetInventoryChanges(); + if (temp.Any()) + if (this.ChestItems.ContainsKey(pair.Value.Chest)) + this.ChestItems[pair.Value.Chest] = pair.Value.GetInventoryChanges().ToArray(); + else + this.ChestItems.Add(pair.Value.Chest, pair.Value.GetInventoryChanges().ToArray()); + else + this.ChestItems.Remove(pair.Value.Chest); } } } diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs index ed8001d6..73ed2d8f 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs @@ -43,7 +43,7 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots foreach (LocationTracker locationWatcher in watcher.Locations) { if (!this.LocationsDict.TryGetValue(locationWatcher.Location, out LocationSnapshot snapshot)) - this.LocationsDict[locationWatcher.Location] = snapshot = new LocationSnapshot(locationWatcher); + this.LocationsDict[locationWatcher.Location] = snapshot = new LocationSnapshot(locationWatcher.Location); snapshot.Update(locationWatcher); } -- cgit