summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking
diff options
context:
space:
mode:
authorwartech0 <wartech0@hotmail.com>2019-12-29 08:06:02 -0600
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-12-31 16:19:43 -0500
commitaef1b8ac2898e147e6200fe257e8fdd82ee7fdbc (patch)
treede3026eeac70d90e729802b8213cbb4004f2dee9 /src/SMAPI/Framework/StateTracking
parentdca60f42b2048d6b0b27517b9e7686665e61e9c2 (diff)
downloadSMAPI-aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc.tar.gz
SMAPI-aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc.tar.bz2
SMAPI-aef1b8ac2898e147e6200fe257e8fdd82ee7fdbc.zip
Added the new ChestItemChanged event.
Diffstat (limited to 'src/SMAPI/Framework/StateTracking')
-rw-r--r--src/SMAPI/Framework/StateTracking/FieldWatchers/NetListWatcher.cs100
-rw-r--r--src/SMAPI/Framework/StateTracking/LocationTracker.cs21
-rw-r--r--src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs5
3 files changed, 126 insertions, 0 deletions
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);
}
}
}