diff options
author | wartech0 <wartech0@hotmail.com> | 2019-12-31 04:20:36 -0600 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2019-12-31 16:26:14 -0500 |
commit | 0411dcf3db277ed0d3c9f0201b7554a7d61ed1e8 (patch) | |
tree | bc71102b65a3a5add74c990ea92aec33c269b626 /src/SMAPI | |
parent | 2894b4322304022b1924e4554c762b560b66b614 (diff) | |
download | SMAPI-0411dcf3db277ed0d3c9f0201b7554a7d61ed1e8.tar.gz SMAPI-0411dcf3db277ed0d3c9f0201b7554a7d61ed1e8.tar.bz2 SMAPI-0411dcf3db277ed0d3c9f0201b7554a7d61ed1e8.zip |
Finished chest events
Diffstat (limited to 'src/SMAPI')
7 files changed, 36 insertions, 38 deletions
diff --git a/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs b/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs index 0b54e909..a0c63fdc 100644 --- a/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs +++ b/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs @@ -16,17 +16,10 @@ namespace StardewModdingAPI.Events public GameLocation Location { get; } /// <summary>The tile position of the chest.</summary> - public Vector2 Tile { 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>Whether this is the location containing the local player.</summary> - public bool IsCurrentLocation => object.ReferenceEquals(this.Location, Game1.player?.currentLocation); + public StardewValley.Objects.Chest Chest { get; } + /// <summary>The inventory changes added to the chest.</summary> + public ItemStackChange[] Changes { get; } /********* ** Public methods @@ -36,12 +29,11 @@ namespace StardewModdingAPI.Events /// <param name="tile">The tile position of the chest.</param> /// <param name="added">The objects added to the location.</param> /// <param name="removed">The objects removed from the location.</param> - internal ChestInventoryChangedEventArgs(GameLocation location, Vector2 tile, IEnumerable<Item> added, IEnumerable<Item> removed) + internal ChestInventoryChangedEventArgs(GameLocation location, StardewValley.Objects.Chest chest, ItemStackChange[] changes) { this.Location = location; - this.Tile = tile; - this.Added = added.ToArray(); - this.Removed = removed.ToArray(); + this.Chest = chest; + this.Changes = changes; } } } diff --git a/src/SMAPI/Events/ItemStackChange.cs b/src/SMAPI/Events/ItemStackChange.cs index f9ae6df6..dbb529d6 100644 --- a/src/SMAPI/Events/ItemStackChange.cs +++ b/src/SMAPI/Events/ItemStackChange.cs @@ -16,5 +16,10 @@ namespace StardewModdingAPI.Events /// <summary>How the inventory slot changed.</summary> public ChangeType ChangeType { get; set; } + + public override string ToString() + { + return this.StackChange + " " + this.Item.Name + " " + this.ChangeType.ToString(); + } } -}
\ No newline at end of file +} diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 64da1cf1..c62bcf84 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -707,9 +707,7 @@ namespace StardewModdingAPI.Framework { 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)); + events.ChestInventoryChanged.Raise(new ChestInventoryChangedEventArgs(location, pair.Key, pair.Value)); } } diff --git a/src/SMAPI/Framework/StateTracking/ChestTracker.cs b/src/SMAPI/Framework/StateTracking/ChestTracker.cs index 4440bf4b..74039753 100644 --- a/src/SMAPI/Framework/StateTracking/ChestTracker.cs +++ b/src/SMAPI/Framework/StateTracking/ChestTracker.cs @@ -44,15 +44,15 @@ namespace StardewModdingAPI.Framework.StateTracking public void Reset() { - this.PreviousInventory = this.CurrentInventory; + if(this.CurrentInventory!=null) + 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)) diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs index 170fd537..659efc57 100644 --- a/src/SMAPI/Framework/StateTracking/LocationTracker.cs +++ b/src/SMAPI/Framework/StateTracking/LocationTracker.cs @@ -49,7 +49,7 @@ namespace StardewModdingAPI.Framework.StateTracking public IDictionaryWatcher<Vector2, TerrainFeature> TerrainFeaturesWatcher { get; } /// <summary>Tracks items added or removed to chests.</summary> - public Dictionary<Vector2, ICollectionWatcher<Item>> ChestWatchers = new Dictionary<Vector2, ICollectionWatcher<Item>>(); + public Dictionary<Vector2, ChestTracker> ChestWatchers = new Dictionary<Vector2, ChestTracker>(); /********* @@ -89,6 +89,9 @@ namespace StardewModdingAPI.Framework.StateTracking watcher.Update(); this.UpdateChestWatcherList(added: this.ObjectsWatcher.Added, removed: this.ObjectsWatcher.Removed); + + foreach (var watcher in this.ChestWatchers) + watcher.Value.Update(); } /// <summary>Set the current value as the baseline.</summary> @@ -96,6 +99,9 @@ namespace StardewModdingAPI.Framework.StateTracking { foreach (IWatcher watcher in this.Watchers) watcher.Reset(); + + foreach (var watcher in this.ChestWatchers) + watcher.Value.Reset(); } /// <summary>Stop watching the player fields and release all references.</summary> @@ -117,9 +123,8 @@ namespace StardewModdingAPI.Framework.StateTracking // remove unused watchers foreach (KeyValuePair<Vector2, SObject> pair in removed) { - if (pair.Value is Chest && this.ChestWatchers.TryGetValue(pair.Key, out ICollectionWatcher<Item> watcher)) + if (pair.Value is Chest && this.ChestWatchers.TryGetValue(pair.Key, out ChestTracker watcher)) { - this.Watchers.Remove(watcher); this.ChestWatchers.Remove(pair.Key); } } @@ -129,9 +134,7 @@ namespace StardewModdingAPI.Framework.StateTracking { if (pair.Value is Chest chest && !this.ChestWatchers.ContainsKey(pair.Key)) { - ICollectionWatcher<Item> watcher = new NetListWatcher<Item>(chest.items); - this.Watchers.Add(watcher); - this.ChestWatchers.Add(pair.Key, watcher); + this.ChestWatchers.Add(pair.Key, new ChestTracker(chest)); } } } diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs index 4074336b..62a56c84 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; +using StardewModdingAPI.Events; using StardewValley; using StardewValley.Buildings; using StardewValley.TerrainFeatures; @@ -35,7 +36,7 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots public SnapshotListDiff<KeyValuePair<Vector2, TerrainFeature>> TerrainFeatures { get; } = new SnapshotListDiff<KeyValuePair<Vector2, TerrainFeature>>(); /// <summary>Tracks changed chest inventories.</summary> - public IDictionary<Vector2, SnapshotListDiff<Item>> ChestItems { get; } = new Dictionary<Vector2, SnapshotListDiff<Item>>(); + public IDictionary<StardewValley.Objects.Chest, ItemStackChange[]> ChestItems { get; } = new Dictionary<StardewValley.Objects.Chest, ItemStackChange[]>(); /********* @@ -61,17 +62,16 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots this.TerrainFeatures.Update(watcher.TerrainFeaturesWatcher); // 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<Item>(); - - diff.Update(pair.Value); + IEnumerable<ItemStackChange> temp = pair.Value.GetInventoryChanges(); + if (temp.Any()) + if (this.ChestItems.ContainsKey(pair.Value.Chest)) + this.ChestItems[pair.Value.Chest] = pair.Value.GetInventoryChanges().ToArray(); + else + this.ChestItems.Add(pair.Value.Chest, pair.Value.GetInventoryChanges().ToArray()); + else + this.ChestItems.Remove(pair.Value.Chest); } } } diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/WorldLocationsSnapshot.cs index ed8001d6..73ed2d8f 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); + this.LocationsDict[locationWatcher.Location] = snapshot = new LocationSnapshot(locationWatcher.Location); snapshot.Update(locationWatcher); } |