diff options
Diffstat (limited to 'src/SMAPI')
-rw-r--r-- | src/SMAPI/Events/ChestInventoryChangedEventArgs.cs | 20 | ||||
-rw-r--r-- | src/SMAPI/Events/ItemStackChange.cs | 5 | ||||
-rw-r--r-- | src/SMAPI/Framework/SGame.cs | 4 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/ChestTracker.cs | 21 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/LocationTracker.cs | 6 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/PlayerTracker.cs | 2 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs | 12 |
7 files changed, 30 insertions, 40 deletions
diff --git a/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs b/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs index a0c63fdc..7771cd7c 100644 --- a/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs +++ b/src/SMAPI/Events/ChestInventoryChangedEventArgs.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; -using System.Linq; -using Microsoft.Xna.Framework; using StardewValley; +using StardewValley.Objects; namespace StardewModdingAPI.Events { @@ -12,24 +10,24 @@ namespace StardewModdingAPI.Events /********* ** Accessors *********/ + /// <summary>The chest whose inventory changed.</summary> + public Chest Chest { get; } + /// <summary>The location containing the chest.</summary> public GameLocation Location { get; } - /// <summary>The tile position of the chest.</summary> - public StardewValley.Objects.Chest Chest { get; } - - /// <summary>The inventory changes added to the chest.</summary> + /// <summary>The inventory changes in the chest.</summary> public ItemStackChange[] Changes { get; } + /********* ** Public methods *********/ /// <summary>Construct an instance.</summary> + /// <param name="chest">The chest whose inventory changed.</param> /// <param name="location">The location containing the chest.</param> - /// <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, StardewValley.Objects.Chest chest, ItemStackChange[] changes) + /// <param name="changes">The inventory changes in the chest.</param> + internal ChestInventoryChangedEventArgs(Chest chest, GameLocation location, ItemStackChange[] changes) { this.Location = location; this.Chest = chest; diff --git a/src/SMAPI/Events/ItemStackChange.cs b/src/SMAPI/Events/ItemStackChange.cs index dbb529d6..cb5d2b88 100644 --- a/src/SMAPI/Events/ItemStackChange.cs +++ b/src/SMAPI/Events/ItemStackChange.cs @@ -16,10 +16,5 @@ 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(); - } } } diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index c62bcf84..eccb6387 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -706,9 +706,7 @@ namespace StardewModdingAPI.Framework if (events.ChestInventoryChanged.HasListeners()) { foreach (var pair in locState.ChestItems) - { - events.ChestInventoryChanged.Raise(new ChestInventoryChangedEventArgs(location, pair.Key, pair.Value)); - } + events.ChestInventoryChanged.Raise(new ChestInventoryChangedEventArgs(pair.Key, location, pair.Value)); } // terrain features changed diff --git a/src/SMAPI/Framework/StateTracking/ChestTracker.cs b/src/SMAPI/Framework/StateTracking/ChestTracker.cs index 74039753..f907b9a5 100644 --- a/src/SMAPI/Framework/StateTracking/ChestTracker.cs +++ b/src/SMAPI/Framework/StateTracking/ChestTracker.cs @@ -1,15 +1,13 @@ -using System; using System.Collections.Generic; using System.Linq; -using StardewModdingAPI.Enums; using StardewModdingAPI.Events; -using StardewModdingAPI.Framework.StateTracking.FieldWatchers; using StardewValley; +using StardewValley.Objects; using ChangeType = StardewModdingAPI.Events.ChangeType; -using Chest = StardewValley.Objects.Chest; namespace StardewModdingAPI.Framework.StateTracking { + /// <summary>Tracks changes to a chest's items.</summary> internal class ChestTracker { /********* @@ -21,33 +19,39 @@ namespace StardewModdingAPI.Framework.StateTracking /// <summary>The chest's inventory change as of the last update.</summary> private IDictionary<Item, int> CurrentInventory; + /********* ** Accessors *********/ - /// <summary>The chest being tracked</summary> + /// <summary>The chest being tracked.</summary> public Chest Chest { get; } + /********* ** Public methods *********/ + /// <summary>Construct an instance.</summary> + /// <param name="chest">The chest being tracked.</param> public ChestTracker(Chest chest) { this.Chest = chest; this.PreviousInventory = this.GetInventory(); } + /// <summary>Update the current values if needed.</summary> public void Update() { this.CurrentInventory = this.GetInventory(); } - + /// <summary>Reset all trackers so their current values are the baseline.</summary> public void Reset() { - if(this.CurrentInventory!=null) + if (this.CurrentInventory != null) this.PreviousInventory = this.CurrentInventory; } + /// <summary>Get the inventory changes since the last update.</summary> public IEnumerable<ItemStackChange> GetInventoryChanges() { IDictionary<Item, int> previous = this.PreviousInventory; @@ -64,10 +68,11 @@ namespace StardewModdingAPI.Framework.StateTracking } } + /********* ** Private methods *********/ - + /// <summary>Get the player's current inventory.</summary> private IDictionary<Item, int> GetInventory() { return this.Chest.items diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs index 659efc57..7445add9 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, ChestTracker> ChestWatchers = new Dictionary<Vector2, ChestTracker>(); + public IDictionary<Vector2, ChestTracker> ChestWatchers { get; } = new Dictionary<Vector2, ChestTracker>(); /********* @@ -124,18 +124,14 @@ namespace StardewModdingAPI.Framework.StateTracking foreach (KeyValuePair<Vector2, SObject> pair in removed) { if (pair.Value is Chest && this.ChestWatchers.TryGetValue(pair.Key, out ChestTracker watcher)) - { this.ChestWatchers.Remove(pair.Key); - } } // add new watchers foreach (KeyValuePair<Vector2, SObject> pair in added) { if (pair.Value is Chest chest && !this.ChestWatchers.ContainsKey(pair.Key)) - { this.ChestWatchers.Add(pair.Key, new ChestTracker(chest)); - } } } } diff --git a/src/SMAPI/Framework/StateTracking/PlayerTracker.cs b/src/SMAPI/Framework/StateTracking/PlayerTracker.cs index 6302a889..cd7d75ec 100644 --- a/src/SMAPI/Framework/StateTracking/PlayerTracker.cs +++ b/src/SMAPI/Framework/StateTracking/PlayerTracker.cs @@ -99,7 +99,7 @@ namespace StardewModdingAPI.Framework.StateTracking return this.Player.currentLocation ?? this.LastValidLocation; } - /// <summary>Get the player inventory changes between two states.</summary> + /// <summary>Get the inventory changes since the last update.</summary> public IEnumerable<ItemStackChange> GetInventoryChanges() { IDictionary<Item, int> previous = this.PreviousInventory; diff --git a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs index 62a56c84..4e08a1ac 100644 --- a/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs +++ b/src/SMAPI/Framework/StateTracking/Snapshots/LocationSnapshot.cs @@ -4,6 +4,7 @@ using Microsoft.Xna.Framework; using StardewModdingAPI.Events; using StardewValley; using StardewValley.Buildings; +using StardewValley.Objects; using StardewValley.TerrainFeatures; namespace StardewModdingAPI.Framework.StateTracking.Snapshots @@ -36,7 +37,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<StardewValley.Objects.Chest, ItemStackChange[]> ChestItems { get; } = new Dictionary<StardewValley.Objects.Chest, ItemStackChange[]>(); + public IDictionary<Chest, ItemStackChange[]> ChestItems { get; } = new Dictionary<Chest, ItemStackChange[]>(); /********* @@ -64,12 +65,9 @@ namespace StardewModdingAPI.Framework.StateTracking.Snapshots // chest inventories foreach (var pair in watcher.ChestWatchers) { - 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()); + ItemStackChange[] changes = pair.Value.GetInventoryChanges().ToArray(); + if (changes.Length > 0) + this.ChestItems[pair.Value.Chest] = changes; else this.ChestItems.Remove(pair.Value.Chest); } |