diff options
author | wartech0 <wartech0@hotmail.com> | 2019-12-31 01:36:18 -0600 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-12-31 16:25:51 -0500 |
commit | 2894b4322304022b1924e4554c762b560b66b614 (patch) | |
tree | 21234b572c620a36b83c7d9966602487b612f7a3 /src/SMAPI/Framework/StateTracking | |
parent | 1286a90ec2fb0dcf26bd59feec714544844e4398 (diff) | |
download | SMAPI-2894b4322304022b1924e4554c762b560b66b614.tar.gz SMAPI-2894b4322304022b1924e4554c762b560b66b614.tar.bz2 SMAPI-2894b4322304022b1924e4554c762b560b66b614.zip |
reworking chest event handler
Diffstat (limited to 'src/SMAPI/Framework/StateTracking')
-rw-r--r-- | src/SMAPI/Framework/StateTracking/ChestTracker.cs | 79 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs | 2 |
2 files changed, 80 insertions, 1 deletions
diff --git a/src/SMAPI/Framework/StateTracking/ChestTracker.cs b/src/SMAPI/Framework/StateTracking/ChestTracker.cs new file mode 100644 index 00000000..4440bf4b --- /dev/null +++ b/src/SMAPI/Framework/StateTracking/ChestTracker.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StardewModdingAPI.Enums; +using StardewModdingAPI.Events; +using StardewModdingAPI.Framework.StateTracking.FieldWatchers; +using StardewValley; +using ChangeType = StardewModdingAPI.Events.ChangeType; +using Chest = StardewValley.Objects.Chest; + +namespace StardewModdingAPI.Framework.StateTracking +{ + internal class ChestTracker + { + /********* + ** Fields + *********/ + /// <summary>The chest's inventory as of the last reset.</summary> + private IDictionary<Item, int> PreviousInventory; + + /// <summary>The chest's inventory change as of the last update.</summary> + private IDictionary<Item, int> CurrentInventory; + + /********* + ** Accessors + *********/ + /// <summary>The chest being tracked</summary> + public Chest Chest { get; } + + /********* + ** Public methods + *********/ + public ChestTracker(Chest chest) + { + this.Chest = chest; + this.PreviousInventory = this.GetInventory(); + } + + public void Update() + { + this.CurrentInventory = this.GetInventory(); + } + + + public void Reset() + { + this.PreviousInventory = this.CurrentInventory; + } + + public IEnumerable<ItemStackChange> GetInventoryChanges() + { + IDictionary<Item, int> previous = this.PreviousInventory; + Console.WriteLine(previous.Count); + IDictionary<Item, int> current = this.GetInventory(); + Console.WriteLine(current.Count); + foreach (Item item in previous.Keys.Union(current.Keys)) + { + if (!previous.TryGetValue(item, out int prevStack)) + yield return new ItemStackChange { Item = item, StackChange = item.Stack, ChangeType = ChangeType.Added }; + else if (!current.TryGetValue(item, out int newStack)) + yield return new ItemStackChange { Item = item, StackChange = -item.Stack, ChangeType = ChangeType.Removed }; + else if (prevStack != newStack) + yield return new ItemStackChange { Item = item, StackChange = newStack - prevStack, ChangeType = ChangeType.StackChange }; + } + } + + /********* + ** Private methods + *********/ + + private IDictionary<Item, int> GetInventory() + { + return this.Chest.items + .Where(n => n != null) + .Distinct() + .ToDictionary(n => n, n => n.Stack); + } + } +} diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs index 73ed2d8f..ed8001d6 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.Location); + this.LocationsDict[locationWatcher.Location] = snapshot = new LocationSnapshot(locationWatcher); snapshot.Update(locationWatcher); } |