diff options
author | wartech0 <wartech0@hotmail.com> | 2019-12-29 08:06:02 -0600 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-12-31 16:19:43 -0500 |
commit | aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc (patch) | |
tree | de3026eeac70d90e729802b8213cbb4004f2dee9 | |
parent | dca60f42b2048d6b0b27517b9e7686665e61e9c2 (diff) | |
download | SMAPI-aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc.tar.gz SMAPI-aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc.tar.bz2 SMAPI-aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc.zip |
Added the new ChestItemChanged event.
-rw-r--r-- | src/SMAPI.sln | 4 | ||||
-rw-r--r-- | src/SMAPI/Events/ChestItemChangedEventArgs.cs | 47 | ||||
-rw-r--r-- | src/SMAPI/Events/IWorldEvents.cs | 3 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/EventManager.cs | 4 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/ModWorldEvents.cs | 6 | ||||
-rw-r--r-- | src/SMAPI/Framework/SGame.cs | 4 | ||||
-rw-r--r-- | src/SMAPI/Framework/SnapshotListDiff.cs | 8 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs | 100 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/LocationTracker.cs | 21 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs | 5 |
10 files changed, 200 insertions, 2 deletions
diff --git a/src/SMAPI.sln b/src/SMAPI.sln index 62eaa777..65ea330c 100644 --- a/src/SMAPI.sln +++ b/src/SMAPI.sln @@ -1,7 +1,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.28729.10 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.902 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/ChestItemChangedEventArgs.cs b/src/SMAPI/Events/ChestItemChangedEventArgs.cs new file mode 100644 index 00000000..6b06487c --- /dev/null +++ b/src/SMAPI/Events/ChestItemChangedEventArgs.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using StardewValley; +using Item = StardewValley.Item; + +namespace StardewModdingAPI.Events +{ + /// <summary>Event arguments for a <see cref="IWorldEvents.ChestItemChanged"/> event.</summary> + public class ChestItemChangedEventArgs : EventArgs + { + /********* + ** Accessors + *********/ + /// <summary>The location which changed.</summary> + public GameLocation Location { get; } + + /// <summary>The objects added to the location.</summary> + public IEnumerable<Item> Added { get; } + + /// <summary>The objects removed from the location.</summary> + public IEnumerable<Item> Removed { get; } + + /// <summary>The location of the chest from where the item was added or removed</summary> + public Vector2 LocationOfChest { get; } + + /// <summary>Whether this is the location containing the local player.</summary> + public bool IsCurrentLocation => object.ReferenceEquals(this.Location, Game1.player?.currentLocation); + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="location">The location which changed.</param> + /// <param name="added">The objects added to the location.</param> + /// <param name="removed">The objects removed from the location.</param> + internal ChestItemChangedEventArgs(GameLocation location, IEnumerable<Item> added, IEnumerable<Item> 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 0ceffcc1..6f9b71a7 100644 --- a/src/SMAPI/Events/IWorldEvents.cs +++ b/src/SMAPI/Events/IWorldEvents.cs @@ -23,6 +23,9 @@ namespace StardewModdingAPI.Events /// <summary>Raised after objects are added or removed in a location.</summary> event EventHandler<ObjectListChangedEventArgs> ObjectListChanged; + /// <summary>Raised after items are added or removed from a chest in a location.</summary> + event EventHandler<ChestItemChangedEventArgs> ChestItemChanged; + /// <summary>Raised after terrain features (like floors and trees) are added or removed in a location.</summary> event EventHandler<TerrainFeatureListChangedEventArgs> TerrainFeatureListChanged; } 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 /// <summary>Raised after objects are added or removed in a location.</summary> public readonly ManagedEvent<ObjectListChangedEventArgs> ObjectListChanged; + /// <summary>Raised after a new item is added or removed from a chest in a location.</summary> + public readonly ManagedEvent<ChestItemChangedEventArgs> ChestItemsChanged; + /// <summary>Raised after terrain features (like floors and trees) are added or removed in a location.</summary> public readonly ManagedEvent<TerrainFeatureListChangedEventArgs> TerrainFeatureListChanged; @@ -221,6 +224,7 @@ namespace StardewModdingAPI.Framework.Events this.LocationListChanged = ManageEventOf<LocationListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.BuildingListChanged)); this.NpcListChanged = ManageEventOf<NpcListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.NpcListChanged)); this.ObjectListChanged = ManageEventOf<ObjectListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.ObjectListChanged)); + this.ChestItemsChanged = ManageEventOf<ChestItemChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.ChestItemChanged)); this.TerrainFeatureListChanged = ManageEventOf<TerrainFeatureListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.TerrainFeatureListChanged)); this.LoadStageChanged = ManageEventOf<LoadStageChangedEventArgs>(nameof(IModEvents.Specialized), nameof(ISpecializedEvents.LoadStageChanged)); diff --git a/src/SMAPI/Framework/Events/ModWorldEvents.cs b/src/SMAPI/Framework/Events/ModWorldEvents.cs index b85002a3..8d6701ec 100644 --- a/src/SMAPI/Framework/Events/ModWorldEvents.cs +++ b/src/SMAPI/Framework/Events/ModWorldEvents.cs @@ -51,6 +51,12 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.ObjectListChanged.Remove(value); } + public event EventHandler<ChestItemChangedEventArgs> ChestItemChanged + { + add => this.EventManager.ChestItemsChanged.Add(value); + remove => this.EventManager.ChestItemsChanged.Remove(value); + } + /// <summary>Raised after terrain features (like floors and trees) are added or removed in a location.</summary> public event EventHandler<TerrainFeatureListChangedEventArgs> TerrainFeatureListChanged { diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 4774233e..564fec1d 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -702,6 +702,10 @@ namespace StardewModdingAPI.Framework if (locState.Objects.IsChanged) 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)); + // terrain features changed if (locState.TerrainFeatures.IsChanged) events.TerrainFeatureListChanged.Raise(new TerrainFeatureListChangedEventArgs(location, locState.TerrainFeatures.Added, locState.TerrainFeatures.Removed)); diff --git a/src/SMAPI/Framework/SnapshotListDiff.cs b/src/SMAPI/Framework/SnapshotListDiff.cs index d4d5df50..b14cacdb 100644 --- a/src/SMAPI/Framework/SnapshotListDiff.cs +++ b/src/SMAPI/Framework/SnapshotListDiff.cs @@ -29,10 +29,18 @@ namespace StardewModdingAPI.Framework /// <summary>The added values.</summary> public IEnumerable<T> Added => this.AddedImpl; + public Microsoft.Xna.Framework.Vector2 Key; /********* ** Public methods *********/ + + public void Update(ICollectionWatcher<T> watcher, Microsoft.Xna.Framework.Vector2 key) + { + this.Key = key; + this.Update(watcher.IsChanged, watcher.Removed, watcher.Added); + } + /// <summary>Update the snapshot.</summary> /// <param name="isChanged">Whether the value changed since the last update.</param> /// <param name="removed">The removed values.</param> diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs new file mode 100644 index 00000000..cac66356 --- /dev/null +++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using Netcode; + +namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers +{ + internal class NetListWatcher<TKey, TValue> : BaseDisposableWatcher, ICollectionWatcher<TValue> + where TValue : class, INetObject<INetSerializable> + { + + + /********* + ** Fields + *********/ + /// <summary>The field being watched.</summary> + private readonly NetList<TValue, Netcode.NetRef<TValue>> Field; + + public TKey Key { get; } + + /// <summary>The pairs added since the last reset.</summary> + private readonly IList<TValue> AddedImpl = new List<TValue>(); + + /// <summary>The pairs removed since the last reset.</summary> + private readonly IList<TValue> RemovedImpl = new List<TValue>(); + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="field">The field to watch.</param> + public NetListWatcher(NetList<TValue, Netcode.NetRef<TValue>> field, TKey key) + { + 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<TValue> Added => this.AddedImpl; + + public IEnumerable<TValue> Removed => this.RemovedImpl; + + public void Dispose() + { + if (!this.IsDisposed) + { + this.Field.OnElementChanged -= this.OnElementChanged; + this.Field.OnArrayReplaced -= this.OnArrayReplaced; + } + + base.Dispose(); + } + + public void Reset() + { + this.AddedImpl.Clear(); + this.RemovedImpl.Clear(); + } + + public void Update() + { + this.AssertNotDisposed(); + } + + /********* + ** Private methods + *********/ + private void OnArrayReplaced(NetList<TValue, Netcode.NetRef<TValue>> list, IList<TValue> before, IList<TValue> after) + { + this.AddedImpl.Clear(); + this.RemovedImpl.Clear(); + + foreach(var obj in after) + this.AddedImpl.Add(obj); + + foreach(var obj in before) + this.RemovedImpl.Add(obj); + } + + private void OnElementChanged(NetList<TValue, Netcode.NetRef<TValue>> 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) + this.AddedImpl.Add(newValue); + + if(oldValue!=null) + this.RemovedImpl.Add(oldValue); + } + } +} diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs index 1f479e12..ef4a8b64 100644 --- a/src/SMAPI/Framework/StateTracking/LocationTracker.cs +++ b/src/SMAPI/Framework/StateTracking/LocationTracker.cs @@ -7,6 +7,7 @@ using StardewValley.Buildings; using StardewValley.Locations; using StardewValley.TerrainFeatures; using Object = StardewValley.Object; +using Chest = StardewValley.Objects.Chest; namespace StardewModdingAPI.Framework.StateTracking { @@ -47,6 +48,7 @@ namespace StardewModdingAPI.Framework.StateTracking /// <summary>Tracks added or removed terrain features.</summary> public IDictionaryWatcher<Vector2, TerrainFeature> TerrainFeaturesWatcher { get; } + public Dictionary<Vector2, NetListWatcher<Vector2, Item>> activeChestWatchers = new Dictionary<Vector2, NetListWatcher<Vector2, Item>>(); /********* ** Public methods @@ -88,6 +90,25 @@ namespace StardewModdingAPI.Framework.StateTracking { foreach (IWatcher watcher in this.Watchers) watcher.Update(); + + foreach (KeyValuePair<Vector2, Object> 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<Vector2, Item> tempItemWatcher = new NetListWatcher<Vector2, Item>(temp.items, obj.Key); + this.Watchers.Add(tempItemWatcher); + this.activeChestWatchers.Add(obj.Key, tempItemWatcher); + } + } + + foreach (KeyValuePair<Vector2, Object> obj in this.ObjectsWatcher.Removed) + { + this.activeChestWatchers.TryGetValue(obj.Key, out NetListWatcher<Vector2, Item> tempItemWatcher); + this.Watchers.Remove(tempItemWatcher); + this.activeChestWatchers.Remove(obj.Key); + } } /// <summary>Set the current value as the baseline.</summary> diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs index d3029540..31cf29c3 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs @@ -33,6 +33,8 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots /// <summary>Tracks added or removed terrain features.</summary> public SnapshotListDiff<KeyValuePair<Vector2, TerrainFeature>> TerrainFeatures { get; } = new SnapshotListDiff<KeyValuePair<Vector2, TerrainFeature>>(); + public SnapshotListDiff<Item> ChestItems { get; } = new SnapshotListDiff<Item>(); + /********* ** Public methods @@ -54,6 +56,9 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots this.Npcs.Update(watcher.NpcsWatcher); this.Objects.Update(watcher.ObjectsWatcher); this.TerrainFeatures.Update(watcher.TerrainFeaturesWatcher); + + foreach (var obj in watcher.activeChestWatchers) + this.ChestItems.Update(obj.Value, obj.Key); } } } |