From aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc Mon Sep 17 00:00:00 2001 From: wartech0 Date: Sun, 29 Dec 2019 08:06:02 -0600 Subject: Added the new ChestItemChanged event. --- src/SMAPI/Framework/Events/EventManager.cs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/SMAPI/Framework/Events/EventManager.cs') diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index 18b00f69..dad45f95 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -148,6 +148,9 @@ namespace StardewModdingAPI.Framework.Events /// Raised after objects are added or removed in a location. public readonly ManagedEvent ObjectListChanged; + /// Raised after a new item is added or removed from a chest in a location. + public readonly ManagedEvent ChestItemsChanged; + /// Raised after terrain features (like floors and trees) are added or removed in a location. public readonly ManagedEvent TerrainFeatureListChanged; @@ -221,6 +224,7 @@ namespace StardewModdingAPI.Framework.Events this.LocationListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.BuildingListChanged)); this.NpcListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.NpcListChanged)); this.ObjectListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.ObjectListChanged)); + this.ChestItemsChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.ChestItemChanged)); this.TerrainFeatureListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.TerrainFeatureListChanged)); this.LoadStageChanged = ManageEventOf(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.LoadStageChanged)); -- cgit From 1286a90ec2fb0dcf26bd59feec714544844e4398 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 29 Dec 2019 13:29:25 -0500 Subject: minor refactoring This commit... - removes key fields added to non-keyed types like NetListWatcher and SnapshotListDiff; - fixes existing chests not being watched; - fixes diffs not correctly updated for added/removed chests; - performs minor cleanup, adds missing docs, etc. --- src/SMAPI.sln | 4 +- src/SMAPI/Events/ChestInventoryChangedEventArgs.cs | 47 +++++++++++ src/SMAPI/Events/ChestItemChangedEventArgs.cs | 47 ----------- src/SMAPI/Events/IWorldEvents.cs | 4 +- src/SMAPI/Framework/Events/EventManager.cs | 6 +- src/SMAPI/Framework/Events/ModWorldEvents.cs | 7 +- src/SMAPI/Framework/SGame.cs | 11 ++- src/SMAPI/Framework/SnapshotListDiff.cs | 8 -- .../StateTracking/FieldWatchers/NetListWatcher.cs | 90 ++++++++++++---------- .../Framework/StateTracking/LocationTracker.cs | 74 +++++++++++------- .../StateTracking/Snapshots/LocationSnapshot.cs | 20 ++++- 11 files changed, 179 insertions(+), 139 deletions(-) create mode 100644 src/SMAPI/Events/ChestInventoryChangedEventArgs.cs delete mode 100644 src/SMAPI/Events/ChestItemChangedEventArgs.cs (limited to 'src/SMAPI/Framework/Events/EventManager.cs') diff --git a/src/SMAPI.sln b/src/SMAPI.sln index 65ea330c..62eaa777 100644 --- a/src/SMAPI.sln +++ b/src/SMAPI.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.902 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.28729.10 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".root", ".root", "{86C452BE-D2D8-45B4-B63F-E329EB06CEDA}" ProjectSection(SolutionItems) = preProject diff --git a/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs b/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs new file mode 100644 index 00000000..0b54e909 --- /dev/null +++ b/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using StardewValley; + +namespace StardewModdingAPI.Events +{ + /// Event arguments for a event. + public class ChestInventoryChangedEventArgs : EventArgs + { + /********* + ** Accessors + *********/ + /// The location containing the chest. + public GameLocation Location { get; } + + /// The tile position of the chest. + public Vector2 Tile { get; } + + /// The objects added to the location. + public IEnumerable Added { get; } + + /// The objects removed from the location. + public IEnumerable Removed { get; } + + /// Whether this is the location containing the local player. + public bool IsCurrentLocation => object.ReferenceEquals(this.Location, Game1.player?.currentLocation); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The location containing the chest. + /// The tile position of the chest. + /// The objects added to the location. + /// The objects removed from the location. + internal ChestInventoryChangedEventArgs(GameLocation location, Vector2 tile, IEnumerable added, IEnumerable removed) + { + this.Location = location; + this.Tile = tile; + this.Added = added.ToArray(); + this.Removed = removed.ToArray(); + } + } +} diff --git a/src/SMAPI/Events/ChestItemChangedEventArgs.cs b/src/SMAPI/Events/ChestItemChangedEventArgs.cs deleted file mode 100644 index 6b06487c..00000000 --- a/src/SMAPI/Events/ChestItemChangedEventArgs.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.Xna.Framework; -using StardewValley; -using Item = StardewValley.Item; - -namespace StardewModdingAPI.Events -{ - /// Event arguments for a event. - public class ChestItemChangedEventArgs : EventArgs - { - /********* - ** Accessors - *********/ - /// The location which changed. - public GameLocation Location { get; } - - /// The objects added to the location. - public IEnumerable Added { get; } - - /// The objects removed from the location. - public IEnumerable Removed { get; } - - /// The location of the chest from where the item was added or removed - public Vector2 LocationOfChest { get; } - - /// Whether this is the location containing the local player. - public bool IsCurrentLocation => object.ReferenceEquals(this.Location, Game1.player?.currentLocation); - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The location which changed. - /// The objects added to the location. - /// The objects removed from the location. - internal ChestItemChangedEventArgs(GameLocation location, IEnumerable added, IEnumerable removed, Vector2 locationOfChest) - { - this.Location = location; - this.Added = added.ToArray(); - this.Removed = removed.ToArray(); - this.LocationOfChest = locationOfChest; - } - } -} diff --git a/src/SMAPI/Events/IWorldEvents.cs b/src/SMAPI/Events/IWorldEvents.cs index 6f9b71a7..9569a57b 100644 --- a/src/SMAPI/Events/IWorldEvents.cs +++ b/src/SMAPI/Events/IWorldEvents.cs @@ -23,8 +23,8 @@ namespace StardewModdingAPI.Events /// Raised after objects are added or removed in a location. event EventHandler ObjectListChanged; - /// Raised after items are added or removed from a chest in a location. - event EventHandler ChestItemChanged; + /// Raised after items are added or removed from a chest. + event EventHandler ChestInventoryChanged; /// Raised after terrain features (like floors and trees) are added or removed in a location. event EventHandler TerrainFeatureListChanged; diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index dad45f95..892cbc7b 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -148,8 +148,8 @@ namespace StardewModdingAPI.Framework.Events /// Raised after objects are added or removed in a location. public readonly ManagedEvent ObjectListChanged; - /// Raised after a new item is added or removed from a chest in a location. - public readonly ManagedEvent ChestItemsChanged; + /// Raised after items are added or removed from a chest. + public readonly ManagedEvent ChestInventoryChanged; /// Raised after terrain features (like floors and trees) are added or removed in a location. public readonly ManagedEvent TerrainFeatureListChanged; @@ -224,7 +224,7 @@ namespace StardewModdingAPI.Framework.Events this.LocationListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.BuildingListChanged)); this.NpcListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.NpcListChanged)); this.ObjectListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.ObjectListChanged)); - this.ChestItemsChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.ChestItemChanged)); + this.ChestInventoryChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.ChestInventoryChanged)); this.TerrainFeatureListChanged = ManageEventOf(nameof(IModEvents.World), nameof(IWorldEvents.TerrainFeatureListChanged)); this.LoadStageChanged = ManageEventOf(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.LoadStageChanged)); diff --git a/src/SMAPI/Framework/Events/ModWorldEvents.cs b/src/SMAPI/Framework/Events/ModWorldEvents.cs index 8d6701ec..2ae69669 100644 --- a/src/SMAPI/Framework/Events/ModWorldEvents.cs +++ b/src/SMAPI/Framework/Events/ModWorldEvents.cs @@ -51,10 +51,11 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.ObjectListChanged.Remove(value); } - public event EventHandler ChestItemChanged + /// Raised after items are added or removed from a chest. + public event EventHandler ChestInventoryChanged { - add => this.EventManager.ChestItemsChanged.Add(value); - remove => this.EventManager.ChestItemsChanged.Remove(value); + add => this.EventManager.ChestInventoryChanged.Add(value); + remove => this.EventManager.ChestInventoryChanged.Remove(value); } /// Raised after terrain features (like floors and trees) are added or removed in a location. diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 564fec1d..64da1cf1 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -703,8 +703,15 @@ namespace StardewModdingAPI.Framework events.ObjectListChanged.Raise(new ObjectListChangedEventArgs(location, locState.Objects.Added, locState.Objects.Removed)); // chest items changed - if (locState.ChestItems.IsChanged) - events.ChestItemsChanged.Raise(new ChestItemChangedEventArgs(location, locState.ChestItems.Added, locState.ChestItems.Removed, locState.ChestItems.Key)); + if (events.ChestInventoryChanged.HasListeners()) + { + 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)); + } + } // terrain features changed if (locState.TerrainFeatures.IsChanged) diff --git a/src/SMAPI/Framework/SnapshotListDiff.cs b/src/SMAPI/Framework/SnapshotListDiff.cs index b14cacdb..d4d5df50 100644 --- a/src/SMAPI/Framework/SnapshotListDiff.cs +++ b/src/SMAPI/Framework/SnapshotListDiff.cs @@ -29,18 +29,10 @@ namespace StardewModdingAPI.Framework /// The added values. public IEnumerable Added => this.AddedImpl; - public Microsoft.Xna.Framework.Vector2 Key; /********* ** Public methods *********/ - - public void Update(ICollectionWatcher watcher, Microsoft.Xna.Framework.Vector2 key) - { - this.Key = key; - this.Update(watcher.IsChanged, watcher.Removed, watcher.Added); - } - /// Update the snapshot. /// Whether the value changed since the last update. /// The removed values. diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs index cac66356..8aa0eab5 100644 --- a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs @@ -1,21 +1,18 @@ -using System; using System.Collections.Generic; using Netcode; namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers { - internal class NetListWatcher : BaseDisposableWatcher, ICollectionWatcher + /// A watcher which detects changes to a net list field. + /// The list value type. + internal class NetListWatcher : BaseDisposableWatcher, ICollectionWatcher where TValue : class, INetObject { - - /********* ** Fields *********/ /// The field being watched. - private readonly NetList> Field; - - public TKey Key { get; } + private readonly NetList> Field; /// The pairs added since the last reset. private readonly IList AddedImpl = new List(); @@ -23,26 +20,47 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers /// The pairs removed since the last reset. private readonly IList RemovedImpl = new List(); + + /********* + ** Accessors + *********/ + /// Whether the collection changed since the last reset. + public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; + + /// The values added since the last reset. + public IEnumerable Added => this.AddedImpl; + + /// The values removed since the last reset. + public IEnumerable Removed => this.RemovedImpl; + + /********* ** Public methods *********/ /// Construct an instance. /// The field to watch. - public NetListWatcher(NetList> field, TKey key) + public NetListWatcher(NetList> field) { this.Field = field; - this.Key = key; field.OnElementChanged += this.OnElementChanged; field.OnArrayReplaced += this.OnArrayReplaced; } - public bool IsChanged => this.AddedImpl.Count > 0 || this.RemovedImpl.Count > 0; - - public IEnumerable Added => this.AddedImpl; + /// Set the current value as the baseline. + public void Reset() + { + this.AddedImpl.Clear(); + this.RemovedImpl.Clear(); + } - public IEnumerable Removed => this.RemovedImpl; + /// Update the current value if needed. + public void Update() + { + this.AssertNotDisposed(); + } - public void Dispose() + /// Stop watching the field and release all references. + public override void Dispose() { if (!this.IsDisposed) { @@ -53,47 +71,37 @@ namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers base.Dispose(); } - public void Reset() - { - this.AddedImpl.Clear(); - this.RemovedImpl.Clear(); - } - - public void Update() - { - this.AssertNotDisposed(); - } /********* ** Private methods *********/ - private void OnArrayReplaced(NetList> list, IList before, IList after) + /// A callback invoked when the value list is replaced. + /// The net field whose values changed. + /// The previous list of values. + /// The new list of values. + private void OnArrayReplaced(NetList> list, IList oldValues, IList newValues) { this.AddedImpl.Clear(); this.RemovedImpl.Clear(); - foreach(var obj in after) - this.AddedImpl.Add(obj); + foreach (TValue value in newValues) + this.AddedImpl.Add(value); - foreach(var obj in before) - this.RemovedImpl.Add(obj); + foreach (TValue value in oldValues) + this.RemovedImpl.Add(value); } - private void OnElementChanged(NetList> list, int index, TValue oldValue, TValue newValue) + /// A callback invoked when an entry is replaced. + /// The net field whose values changed. + /// The list index which changed. + /// The previous value. + /// The new value. + private void OnElementChanged(NetList> list, int index, TValue oldValue, TValue newValue) { - - /* checks for stack addition / subtraction changing stacks does not fire off an element changed event - if ((oldValue != null && newValue != null) && oldValue.CompareTo(newValue) < 0) - this.AddedImpl.Add(newValue); - //Stack Removed from - if ((oldValue != null && newValue != null) && oldValue.CompareTo(newValue) > 0) - this.RemovedImpl.Add(newValue); - */ - - if(newValue!=null) + if (newValue != null) this.AddedImpl.Add(newValue); - if(oldValue!=null) + if (oldValue != null) this.RemovedImpl.Add(oldValue); } } diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs index ef4a8b64..170fd537 100644 --- a/src/SMAPI/Framework/StateTracking/LocationTracker.cs +++ b/src/SMAPI/Framework/StateTracking/LocationTracker.cs @@ -5,9 +5,9 @@ using StardewModdingAPI.Framework.StateTracking.FieldWatchers; using StardewValley; using StardewValley.Buildings; using StardewValley.Locations; +using StardewValley.Objects; using StardewValley.TerrainFeatures; -using Object = StardewValley.Object; -using Chest = StardewValley.Objects.Chest; +using SObject = StardewValley.Object; namespace StardewModdingAPI.Framework.StateTracking { @@ -43,12 +43,14 @@ namespace StardewModdingAPI.Framework.StateTracking public ICollectionWatcher NpcsWatcher { get; } /// Tracks added or removed objects. - public IDictionaryWatcher ObjectsWatcher { get; } + public IDictionaryWatcher ObjectsWatcher { get; } /// Tracks added or removed terrain features. public IDictionaryWatcher TerrainFeaturesWatcher { get; } - public Dictionary> activeChestWatchers = new Dictionary>(); + /// Tracks items added or removed to chests. + public Dictionary> ChestWatchers = new Dictionary>(); + /********* ** Public methods @@ -76,13 +78,8 @@ namespace StardewModdingAPI.Framework.StateTracking this.ObjectsWatcher, this.TerrainFeaturesWatcher }); - } - /// Stop watching the player fields and release all references. - public void Dispose() - { - foreach (IWatcher watcher in this.Watchers) - watcher.Dispose(); + this.UpdateChestWatcherList(added: location.Objects.Pairs, removed: new KeyValuePair[0]); } /// Update the current value if needed. @@ -91,24 +88,7 @@ namespace StardewModdingAPI.Framework.StateTracking foreach (IWatcher watcher in this.Watchers) watcher.Update(); - foreach (KeyValuePair obj in this.ObjectsWatcher.Added.Where(p => p.Value is Chest)) - { - if (!this.activeChestWatchers.ContainsKey(obj.Key)) - { - //create a new watcher for chests items - Chest temp = obj.Value as Chest; - NetListWatcher tempItemWatcher = new NetListWatcher(temp.items, obj.Key); - this.Watchers.Add(tempItemWatcher); - this.activeChestWatchers.Add(obj.Key, tempItemWatcher); - } - } - - foreach (KeyValuePair obj in this.ObjectsWatcher.Removed) - { - this.activeChestWatchers.TryGetValue(obj.Key, out NetListWatcher tempItemWatcher); - this.Watchers.Remove(tempItemWatcher); - this.activeChestWatchers.Remove(obj.Key); - } + this.UpdateChestWatcherList(added: this.ObjectsWatcher.Added, removed: this.ObjectsWatcher.Removed); } /// Set the current value as the baseline. @@ -117,5 +97,43 @@ namespace StardewModdingAPI.Framework.StateTracking foreach (IWatcher watcher in this.Watchers) watcher.Reset(); } + + /// Stop watching the player fields and release all references. + public void Dispose() + { + foreach (IWatcher watcher in this.Watchers) + watcher.Dispose(); + } + + + /********* + ** Private methods + *********/ + /// Update the watcher list for added or removed chests. + /// The objects added to the location. + /// The objects removed from the location. + private void UpdateChestWatcherList(IEnumerable> added, IEnumerable> removed) + { + // remove unused watchers + foreach (KeyValuePair pair in removed) + { + if (pair.Value is Chest && this.ChestWatchers.TryGetValue(pair.Key, out ICollectionWatcher watcher)) + { + this.Watchers.Remove(watcher); + this.ChestWatchers.Remove(pair.Key); + } + } + + // add new watchers + foreach (KeyValuePair pair in added) + { + 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); + } + } + } } } diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs index 31cf29c3..4074336b 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Microsoft.Xna.Framework; using StardewValley; using StardewValley.Buildings; @@ -33,7 +34,8 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots /// Tracks added or removed terrain features. public SnapshotListDiff> TerrainFeatures { get; } = new SnapshotListDiff>(); - public SnapshotListDiff ChestItems { get; } = new SnapshotListDiff(); + /// Tracks changed chest inventories. + public IDictionary> ChestItems { get; } = new Dictionary>(); /********* @@ -50,6 +52,7 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots /// The watcher to snapshot. public void Update(LocationTracker watcher) { + // main lists this.Buildings.Update(watcher.BuildingsWatcher); this.Debris.Update(watcher.DebrisWatcher); this.LargeTerrainFeatures.Update(watcher.LargeTerrainFeaturesWatcher); @@ -57,8 +60,19 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots this.Objects.Update(watcher.ObjectsWatcher); this.TerrainFeatures.Update(watcher.TerrainFeaturesWatcher); - foreach (var obj in watcher.activeChestWatchers) - this.ChestItems.Update(obj.Value, obj.Key); + // 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); + } } } } -- cgit