diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-12-04 23:16:13 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2018-12-04 23:49:09 -0500 |
commit | dad67e213e68eb85c534d7c1c4035dfde90ff822 (patch) | |
tree | c041f2e3ae3e3d648709c0c57e2e91fc3967b708 /src/SMAPI/Framework/StateTracking/WorldLocationsTracker.cs | |
parent | e8ae2d627d59f9cd28d797d2178b7acdead5ace1 (diff) | |
download | SMAPI-dad67e213e68eb85c534d7c1c4035dfde90ff822.tar.gz SMAPI-dad67e213e68eb85c534d7c1c4035dfde90ff822.tar.bz2 SMAPI-dad67e213e68eb85c534d7c1c4035dfde90ff822.zip |
fix world events in the mines (#603)
Diffstat (limited to 'src/SMAPI/Framework/StateTracking/WorldLocationsTracker.cs')
-rw-r--r-- | src/SMAPI/Framework/StateTracking/WorldLocationsTracker.cs | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/src/SMAPI/Framework/StateTracking/WorldLocationsTracker.cs b/src/SMAPI/Framework/StateTracking/WorldLocationsTracker.cs index 5a259663..d9d598f8 100644 --- a/src/SMAPI/Framework/StateTracking/WorldLocationsTracker.cs +++ b/src/SMAPI/Framework/StateTracking/WorldLocationsTracker.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; @@ -19,6 +18,9 @@ namespace StardewModdingAPI.Framework.StateTracking /// <summary>Tracks changes to the location list.</summary> private readonly ICollectionWatcher<GameLocation> LocationListWatcher; + /// <summary>Tracks changes to the list of active mine locations.</summary> + private readonly ICollectionWatcher<MineShaft> MineLocationListWatcher; + /// <summary>A lookup of the tracked locations.</summary> private IDictionary<GameLocation, LocationTracker> LocationDict { get; } = new Dictionary<GameLocation, LocationTracker>(new ObjectReferenceComparer<GameLocation>()); @@ -50,24 +52,34 @@ namespace StardewModdingAPI.Framework.StateTracking *********/ /// <summary>Construct an instance.</summary> /// <param name="locations">The game's list of locations.</param> - public WorldLocationsTracker(ObservableCollection<GameLocation> locations) + /// <param name="activeMineLocations">The game's list of active mine locations.</param> + public WorldLocationsTracker(ObservableCollection<GameLocation> locations, IList<MineShaft> activeMineLocations) { this.LocationListWatcher = WatcherFactory.ForObservableCollection(locations); + this.MineLocationListWatcher = WatcherFactory.ForReferenceList(activeMineLocations); } /// <summary>Update the current value if needed.</summary> public void Update() { - // detect location changes + // detect added/removed locations + this.LocationListWatcher.Update(); + this.MineLocationListWatcher.Update(); if (this.LocationListWatcher.IsChanged) { this.Remove(this.LocationListWatcher.Removed); this.Add(this.LocationListWatcher.Added); } + if (this.MineLocationListWatcher.IsChanged) + { + this.Remove(this.MineLocationListWatcher.Removed); + this.Add(this.MineLocationListWatcher.Added); + } - // detect building changes + // detect building changed foreach (LocationTracker watcher in this.Locations.ToArray()) { + watcher.Update(); if (watcher.BuildingsWatcher.IsChanged) { this.Remove(watcher.BuildingsWatcher.Removed); @@ -75,7 +87,7 @@ namespace StardewModdingAPI.Framework.StateTracking } } - // detect building interior changed (e.g. construction completed) + // detect building interiors changed (e.g. construction completed) foreach (KeyValuePair<Building, GameLocation> pair in this.BuildingIndoors.Where(p => !object.Equals(p.Key.indoors.Value, p.Value))) { GameLocation oldIndoors = pair.Value; @@ -86,10 +98,6 @@ namespace StardewModdingAPI.Framework.StateTracking if (newIndoors != null) this.Removed.Add(newIndoors); } - - // update watchers - foreach (IWatcher watcher in this.Locations) - watcher.Update(); } /// <summary>Set the current location list as the baseline.</summary> @@ -98,21 +106,21 @@ namespace StardewModdingAPI.Framework.StateTracking this.Removed.Clear(); this.Added.Clear(); this.LocationListWatcher.Reset(); + this.MineLocationListWatcher.Reset(); } /// <summary>Set the current value as the baseline.</summary> public void Reset() { this.ResetLocationList(); - foreach (IWatcher watcher in this.Locations) + foreach (IWatcher watcher in this.GetWatchers()) watcher.Reset(); } /// <summary>Stop watching the player fields and release all references.</summary> public void Dispose() { - this.LocationListWatcher.Dispose(); - foreach (IWatcher watcher in this.Locations) + foreach (IWatcher watcher in this.GetWatchers()) watcher.Dispose(); } @@ -180,11 +188,11 @@ namespace StardewModdingAPI.Framework.StateTracking // remove old location if needed this.Remove(location); - // track change + // add location this.Added.Add(location); - - // add this.LocationDict[location] = new LocationTracker(location); + + // add buildings if (location is BuildableGameLocation buildableLocation) this.Add(buildableLocation.buildings); } @@ -219,5 +227,17 @@ namespace StardewModdingAPI.Framework.StateTracking this.Remove(buildableLocation.buildings); } } + + /**** + ** Helpers + ****/ + /// <summary>The underlying watchers.</summary> + private IEnumerable<IWatcher> GetWatchers() + { + yield return this.LocationListWatcher; + yield return this.MineLocationListWatcher; + foreach (LocationTracker watcher in this.Locations) + yield return watcher; + } } } |