diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-01 01:58:31 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-01 01:58:31 -0400 |
commit | b3f116a8f123387b1f2f9ce1a099d1fd8081708d (patch) | |
tree | 4ad91a8c823474786ea92b2a74f2c9f4347c96ba | |
parent | cca7bf197079975bf310ca90c03b78d0f77fdf02 (diff) | |
download | SMAPI-b3f116a8f123387b1f2f9ce1a099d1fd8081708d.tar.gz SMAPI-b3f116a8f123387b1f2f9ce1a099d1fd8081708d.tar.bz2 SMAPI-b3f116a8f123387b1f2f9ce1a099d1fd8081708d.zip |
add terrain feature list changed event (#310)
-rw-r--r-- | src/SMAPI/Events/IWorldEvents.cs | 3 | ||||
-rw-r--r-- | src/SMAPI/Events/WorldTerrainFeatureListChangedEventArgs.cs | 40 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/EventManager.cs | 4 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/ModWorldEvents.cs | 7 | ||||
-rw-r--r-- | src/SMAPI/Framework/SGame.cs | 22 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/LocationTracker.cs | 12 | ||||
-rw-r--r-- | src/SMAPI/StardewModdingAPI.csproj | 1 |
7 files changed, 82 insertions, 7 deletions
diff --git a/src/SMAPI/Events/IWorldEvents.cs b/src/SMAPI/Events/IWorldEvents.cs index a9a5fe6b..7ec26bae 100644 --- a/src/SMAPI/Events/IWorldEvents.cs +++ b/src/SMAPI/Events/IWorldEvents.cs @@ -16,5 +16,8 @@ namespace StardewModdingAPI.Events /// <summary>Raised after objects are added or removed in a location.</summary> event EventHandler<WorldObjectListChangedEventArgs> ObjectListChanged; + + /// <summary>Raised after terrain features are added or removed in a location.</summary> + event EventHandler<WorldTerrainFeatureListChangedEventArgs> TerrainFeatureListChanged; } } diff --git a/src/SMAPI/Events/WorldTerrainFeatureListChangedEventArgs.cs b/src/SMAPI/Events/WorldTerrainFeatureListChangedEventArgs.cs new file mode 100644 index 00000000..cb089811 --- /dev/null +++ b/src/SMAPI/Events/WorldTerrainFeatureListChangedEventArgs.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using StardewValley; +using StardewValley.TerrainFeatures; + +namespace StardewModdingAPI.Events +{ + /// <summary>Event arguments for a <see cref="IWorldEvents.TerrainFeatureListChanged"/> event.</summary> + public class WorldTerrainFeatureListChangedEventArgs : EventArgs + { + /********* + ** Accessors + *********/ + /// <summary>The location which changed.</summary> + public GameLocation Location { get; } + + /// <summary>The terrain features added to the location.</summary> + public IEnumerable<KeyValuePair<Vector2, TerrainFeature>> Added { get; } + + /// <summary>The terrain features removed from the location.</summary> + public IEnumerable<KeyValuePair<Vector2, TerrainFeature>> Removed { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="location">The location which changed.</param> + /// <param name="added">The terrain features added to the location.</param> + /// <param name="removed">The terrain features removed from the location.</param> + public WorldTerrainFeatureListChangedEventArgs(GameLocation location, IEnumerable<KeyValuePair<Vector2, TerrainFeature>> added, IEnumerable<KeyValuePair<Vector2, TerrainFeature>> removed) + { + this.Location = location; + this.Added = added.ToArray(); + this.Removed = removed.ToArray(); + } + } +} diff --git a/src/SMAPI/Framework/Events/EventManager.cs b/src/SMAPI/Framework/Events/EventManager.cs index 0e1e6241..c909c1bd 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -23,6 +23,9 @@ namespace StardewModdingAPI.Framework.Events /// <summary>Raised after objects are added or removed in a location.</summary> public readonly ManagedEvent<WorldObjectListChangedEventArgs> World_ObjectListChanged; + /// <summary>Raised after terrain features are added or removed in a location.</summary> + public readonly ManagedEvent<WorldTerrainFeatureListChangedEventArgs> World_TerrainFeatureListChanged; + /********* ** Events (old) @@ -228,6 +231,7 @@ namespace StardewModdingAPI.Framework.Events this.World_BuildingListChanged = ManageEventOf<WorldBuildingListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.LocationListChanged)); this.World_LocationListChanged = ManageEventOf<WorldLocationListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.BuildingListChanged)); this.World_ObjectListChanged = ManageEventOf<WorldObjectListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.ObjectListChanged)); + this.World_TerrainFeatureListChanged = ManageEventOf<WorldTerrainFeatureListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.TerrainFeatureListChanged)); // init events (old) this.Content_LocaleChanged = ManageEventOf<EventArgsValueChanged<string>>(nameof(ContentEvents), nameof(ContentEvents.AfterLocaleChanged)); diff --git a/src/SMAPI/Framework/Events/ModWorldEvents.cs b/src/SMAPI/Framework/Events/ModWorldEvents.cs index 7036b765..14646c5d 100644 --- a/src/SMAPI/Framework/Events/ModWorldEvents.cs +++ b/src/SMAPI/Framework/Events/ModWorldEvents.cs @@ -40,6 +40,13 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.World_ObjectListChanged.Remove(value); } + /// <summary>Raised after terrain features are added or removed in a location.</summary> + public event EventHandler<WorldTerrainFeatureListChangedEventArgs> TerrainFeatureListChanged + { + add => this.EventManager.World_TerrainFeatureListChanged.Add(value); + remove => this.EventManager.World_TerrainFeatureListChanged.Remove(value); + } + /********* ** Public methods diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 9442c749..02d6dd2c 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -18,11 +18,14 @@ using StardewModdingAPI.Framework.StateTracking.FieldWatchers; using StardewModdingAPI.Framework.Utilities; using StardewValley; using StardewValley.BellsAndWhistles; +using StardewValley.Buildings; using StardewValley.Locations; using StardewValley.Menus; +using StardewValley.TerrainFeatures; using StardewValley.Tools; using xTile.Dimensions; using xTile.Layers; +using Object = StardewValley.Object; namespace StardewModdingAPI.Framework { @@ -556,8 +559,8 @@ namespace StardewModdingAPI.Framework if (watcher.ObjectsWatcher.IsChanged) { GameLocation location = watcher.Location; - var added = watcher.ObjectsWatcher.Added.ToArray(); - var removed = watcher.ObjectsWatcher.Removed.ToArray(); + KeyValuePair<Vector2, Object>[] added = watcher.ObjectsWatcher.Added.ToArray(); + KeyValuePair<Vector2, Object>[] removed = watcher.ObjectsWatcher.Removed.ToArray(); watcher.ObjectsWatcher.Reset(); this.Events.World_ObjectListChanged.Raise(new WorldObjectListChangedEventArgs(location, added, removed)); @@ -568,13 +571,24 @@ namespace StardewModdingAPI.Framework if (watcher.BuildingsWatcher.IsChanged) { GameLocation location = watcher.Location; - var added = watcher.BuildingsWatcher.Added.ToArray(); - var removed = watcher.BuildingsWatcher.Removed.ToArray(); + Building[] added = watcher.BuildingsWatcher.Added.ToArray(); + Building[] removed = watcher.BuildingsWatcher.Removed.ToArray(); watcher.BuildingsWatcher.Reset(); this.Events.World_BuildingListChanged.Raise(new WorldBuildingListChangedEventArgs(location, added, removed)); this.Events.Location_BuildingsChanged.Raise(new EventArgsLocationBuildingsChanged(location, added, removed)); } + + // terrain features changed + if (watcher.TerrainFeaturesWatcher.IsChanged) + { + GameLocation location = watcher.Location; + KeyValuePair<Vector2, TerrainFeature>[] added = watcher.TerrainFeaturesWatcher.Added.ToArray(); + KeyValuePair<Vector2, TerrainFeature>[] removed = watcher.TerrainFeaturesWatcher.Removed.ToArray(); + watcher.TerrainFeaturesWatcher.Reset(); + + this.Events.World_TerrainFeatureListChanged.Raise(new WorldTerrainFeatureListChangedEventArgs(location, added, removed)); + } } } else diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs index 07570401..d31b1128 100644 --- a/src/SMAPI/Framework/StateTracking/LocationTracker.cs +++ b/src/SMAPI/Framework/StateTracking/LocationTracker.cs @@ -6,6 +6,7 @@ using StardewModdingAPI.Framework.StateTracking.FieldWatchers; using StardewValley; using StardewValley.Buildings; using StardewValley.Locations; +using StardewValley.TerrainFeatures; using Object = StardewValley.Object; namespace StardewModdingAPI.Framework.StateTracking @@ -29,12 +30,15 @@ namespace StardewModdingAPI.Framework.StateTracking /// <summary>The tracked location.</summary> public GameLocation Location { get; } - /// <summary>Tracks changes to the location's buildings.</summary> + /// <summary>Tracks added or removed buildings.</summary> public ICollectionWatcher<Building> BuildingsWatcher { get; } - /// <summary>Tracks changes to the location's objects.</summary> + /// <summary>Tracks added or removed objects.</summary> public IDictionaryWatcher<Vector2, Object> ObjectsWatcher { get; } + /// <summary>Tracks added or removed terrain features.</summary> + public IDictionaryWatcher<Vector2, TerrainFeature> TerrainFeaturesWatcher { get; } + /********* ** Public methods @@ -50,11 +54,13 @@ namespace StardewModdingAPI.Framework.StateTracking this.BuildingsWatcher = location is BuildableGameLocation buildableLocation ? WatcherFactory.ForNetCollection(buildableLocation.buildings) : (ICollectionWatcher<Building>)WatcherFactory.ForObservableCollection(new ObservableCollection<Building>()); + this.TerrainFeaturesWatcher = WatcherFactory.ForNetDictionary(location.terrainFeatures); this.Watchers.AddRange(new IWatcher[] { this.BuildingsWatcher, - this.ObjectsWatcher + this.ObjectsWatcher, + this.TerrainFeaturesWatcher }); } diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 951a9e6b..50d8c533 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -88,6 +88,7 @@ <Compile Include="Events\EventArgsLocationBuildingsChanged.cs" /> <Compile Include="Events\IWorldEvents.cs" /> <Compile Include="Events\MultiplayerEvents.cs" /> + <Compile Include="Events\WorldTerrainFeatureListChangedEventArgs.cs" /> <Compile Include="Events\WorldBuildingListChangedEventArgs.cs" /> <Compile Include="Events\WorldLocationListChangedEventArgs.cs" /> <Compile Include="Events\WorldObjectListChangedEventArgs.cs" /> |