summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-04-21 20:37:17 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-04-21 20:37:17 -0400
commiteead352af26d0fcc5cac147d0eb5ec384854d931 (patch)
tree78895ff201953f065667976c0e861168dddcebc1 /src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs
parentb346d28d3858b79c6c4cde55faac34ecdedeaff1 (diff)
downloadSMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.tar.gz
SMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.tar.bz2
SMAPI-eead352af26d0fcc5cac147d0eb5ec384854d931.zip
rewrite world/player state tracking (#453)
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs')
-rw-r--r--src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs
new file mode 100644
index 00000000..188ed9f3
--- /dev/null
+++ b/src/SMAPI/Framework/StateTracking/FieldWatchers/NetValueWatcher.cs
@@ -0,0 +1,83 @@
+using Netcode;
+
+namespace StardewModdingAPI.Framework.StateTracking.FieldWatchers
+{
+ /// <summary>A watcher which detects changes to a net value field.</summary>
+ internal class NetValueWatcher<T, TSelf> : BaseDisposableWatcher, IValueWatcher<T> where TSelf : NetFieldBase<T, TSelf>
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The field being watched.</summary>
+ private readonly NetFieldBase<T, TSelf> Field;
+
+
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>Whether the value changed since the last reset.</summary>
+ public bool IsChanged { get; private set; }
+
+ /// <summary>The field value at the last reset.</summary>
+ public T PreviousValue { get; private set; }
+
+ /// <summary>The latest value.</summary>
+ public T CurrentValue { get; private set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="field">The field to watch.</param>
+ public NetValueWatcher(NetFieldBase<T, TSelf> field)
+ {
+ this.Field = field;
+ this.PreviousValue = field.Value;
+ this.CurrentValue = field.Value;
+
+ field.fieldChangeVisibleEvent += this.OnValueChanged;
+ field.fieldChangeEvent += this.OnValueChanged;
+ }
+
+ /// <summary>Update the current value if needed.</summary>
+ public void Update()
+ {
+ this.AssertNotDisposed();
+ }
+
+ /// <summary>Set the current value as the baseline.</summary>
+ public void Reset()
+ {
+ this.AssertNotDisposed();
+
+ this.PreviousValue = this.CurrentValue;
+ this.IsChanged = false;
+ }
+
+ /// <summary>Stop watching the field and release all references.</summary>
+ public override void Dispose()
+ {
+ if (!this.IsDisposed)
+ {
+ this.Field.fieldChangeEvent -= this.OnValueChanged;
+ this.Field.fieldChangeVisibleEvent -= this.OnValueChanged;
+ }
+ base.Dispose();
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>A callback invoked when the field's value changes.</summary>
+ /// <param name="field">The field being watched.</param>
+ /// <param name="oldValue">The old field value.</param>
+ /// <param name="newValue">The new field value.</param>
+ private void OnValueChanged(TSelf field, T oldValue, T newValue)
+ {
+ this.CurrentValue = newValue;
+ this.IsChanged = true;
+ }
+ }
+}