diff options
-rw-r--r-- | src/SMAPI/Events/IWorldEvents.cs | 3 | ||||
-rw-r--r-- | src/SMAPI/Events/WorldNpcListChangedEventArgs.cs | 38 | ||||
-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 | 35 | ||||
-rw-r--r-- | src/SMAPI/Framework/StateTracking/LocationTracker.cs | 7 | ||||
-rw-r--r-- | src/SMAPI/StardewModdingAPI.csproj | 1 |
7 files changed, 82 insertions, 13 deletions
diff --git a/src/SMAPI/Events/IWorldEvents.cs b/src/SMAPI/Events/IWorldEvents.cs index 7ec26bae..ce288ae1 100644 --- a/src/SMAPI/Events/IWorldEvents.cs +++ b/src/SMAPI/Events/IWorldEvents.cs @@ -14,6 +14,9 @@ namespace StardewModdingAPI.Events /// <summary>Raised after buildings are added or removed in a location.</summary> event EventHandler<WorldBuildingListChangedEventArgs> BuildingListChanged; + /// <summary>Raised after NPCs are added or removed in a location.</summary> + event EventHandler<WorldNpcListChangedEventArgs> NpcListChanged; + /// <summary>Raised after objects are added or removed in a location.</summary> event EventHandler<WorldObjectListChangedEventArgs> ObjectListChanged; diff --git a/src/SMAPI/Events/WorldNpcListChangedEventArgs.cs b/src/SMAPI/Events/WorldNpcListChangedEventArgs.cs new file mode 100644 index 00000000..e251f894 --- /dev/null +++ b/src/SMAPI/Events/WorldNpcListChangedEventArgs.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StardewValley; + +namespace StardewModdingAPI.Events +{ + /// <summary>Event arguments for a <see cref="IWorldEvents.NpcListChanged"/> event.</summary> + public class WorldNpcListChangedEventArgs : EventArgs + { + /********* + ** Accessors + *********/ + /// <summary>The location which changed.</summary> + public GameLocation Location { get; } + + /// <summary>The NPCs added to the location.</summary> + public IEnumerable<NPC> Added { get; } + + /// <summary>The NPCs removed from the location.</summary> + public IEnumerable<NPC> Removed { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="location">The location which changed.</param> + /// <param name="added">The NPCs added to the location.</param> + /// <param name="removed">The NPCs removed from the location.</param> + public WorldNpcListChangedEventArgs(GameLocation location, IEnumerable<NPC> added, IEnumerable<NPC> 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 c909c1bd..cb331e7a 100644 --- a/src/SMAPI/Framework/Events/EventManager.cs +++ b/src/SMAPI/Framework/Events/EventManager.cs @@ -20,6 +20,9 @@ namespace StardewModdingAPI.Framework.Events /// <summary>Raised after buildings are added or removed in a location.</summary> public readonly ManagedEvent<WorldBuildingListChangedEventArgs> World_BuildingListChanged; + /// <summary>Raised after NPCs are added or removed in a location.</summary> + public readonly ManagedEvent<WorldNpcListChangedEventArgs> World_NpcListChanged; + /// <summary>Raised after objects are added or removed in a location.</summary> public readonly ManagedEvent<WorldObjectListChangedEventArgs> World_ObjectListChanged; @@ -230,6 +233,7 @@ namespace StardewModdingAPI.Framework.Events // init events (new) this.World_BuildingListChanged = ManageEventOf<WorldBuildingListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.LocationListChanged)); this.World_LocationListChanged = ManageEventOf<WorldLocationListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.BuildingListChanged)); + this.World_NpcListChanged = ManageEventOf<WorldNpcListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.NpcListChanged)); this.World_ObjectListChanged = ManageEventOf<WorldObjectListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.ObjectListChanged)); this.World_TerrainFeatureListChanged = ManageEventOf<WorldTerrainFeatureListChangedEventArgs>(nameof(IModEvents.World), nameof(IWorldEvents.TerrainFeatureListChanged)); diff --git a/src/SMAPI/Framework/Events/ModWorldEvents.cs b/src/SMAPI/Framework/Events/ModWorldEvents.cs index 14646c5d..ca851550 100644 --- a/src/SMAPI/Framework/Events/ModWorldEvents.cs +++ b/src/SMAPI/Framework/Events/ModWorldEvents.cs @@ -33,6 +33,13 @@ namespace StardewModdingAPI.Framework.Events remove => this.EventManager.World_BuildingListChanged.Remove(value); } + /// <summary>Raised after NPCs are added or removed in a location.</summary> + public event EventHandler<WorldNpcListChangedEventArgs> NpcListChanged + { + add => this.EventManager.World_NpcListChanged.Add(value); + remove => this.EventManager.World_NpcListChanged.Remove(value); + } + /// <summary>Raised after objects are added or removed in a location.</summary> public event EventHandler<WorldObjectListChangedEventArgs> ObjectListChanged { diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 02d6dd2c..38f96566 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -555,18 +555,6 @@ namespace StardewModdingAPI.Framework { foreach (LocationTracker watcher in this.LocationsWatcher.Locations) { - // objects changed - if (watcher.ObjectsWatcher.IsChanged) - { - GameLocation location = watcher.Location; - 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)); - this.Events.Location_ObjectsChanged.Raise(new EventArgsLocationObjectsChanged(location, added, removed)); - } - // buildings changed if (watcher.BuildingsWatcher.IsChanged) { @@ -579,6 +567,29 @@ namespace StardewModdingAPI.Framework this.Events.Location_BuildingsChanged.Raise(new EventArgsLocationBuildingsChanged(location, added, removed)); } + // NPCs changed + if (watcher.NpcsWatcher.IsChanged) + { + GameLocation location = watcher.Location; + NPC[] added = watcher.NpcsWatcher.Added.ToArray(); + NPC[] removed = watcher.NpcsWatcher.Removed.ToArray(); + watcher.NpcsWatcher.Reset(); + + this.Events.World_NpcListChanged.Raise(new WorldNpcListChangedEventArgs(location, added, removed)); + } + + // objects changed + if (watcher.ObjectsWatcher.IsChanged) + { + GameLocation location = watcher.Location; + 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)); + this.Events.Location_ObjectsChanged.Raise(new EventArgsLocationObjectsChanged(location, added, removed)); + } + // terrain features changed if (watcher.TerrainFeaturesWatcher.IsChanged) { diff --git a/src/SMAPI/Framework/StateTracking/LocationTracker.cs b/src/SMAPI/Framework/StateTracking/LocationTracker.cs index d31b1128..4ac455ac 100644 --- a/src/SMAPI/Framework/StateTracking/LocationTracker.cs +++ b/src/SMAPI/Framework/StateTracking/LocationTracker.cs @@ -33,6 +33,9 @@ namespace StardewModdingAPI.Framework.StateTracking /// <summary>Tracks added or removed buildings.</summary> public ICollectionWatcher<Building> BuildingsWatcher { get; } + /// <summary>Tracks added or removed NPCs.</summary> + public ICollectionWatcher<NPC> NpcsWatcher { get; } + /// <summary>Tracks added or removed objects.</summary> public IDictionaryWatcher<Vector2, Object> ObjectsWatcher { get; } @@ -50,15 +53,17 @@ namespace StardewModdingAPI.Framework.StateTracking this.Location = location; // init watchers - this.ObjectsWatcher = WatcherFactory.ForNetDictionary(location.netObjects); this.BuildingsWatcher = location is BuildableGameLocation buildableLocation ? WatcherFactory.ForNetCollection(buildableLocation.buildings) : (ICollectionWatcher<Building>)WatcherFactory.ForObservableCollection(new ObservableCollection<Building>()); + this.NpcsWatcher = WatcherFactory.ForNetCollection(location.characters); + this.ObjectsWatcher = WatcherFactory.ForNetDictionary(location.netObjects); this.TerrainFeaturesWatcher = WatcherFactory.ForNetDictionary(location.terrainFeatures); this.Watchers.AddRange(new IWatcher[] { this.BuildingsWatcher, + this.NpcsWatcher, this.ObjectsWatcher, this.TerrainFeaturesWatcher }); diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 50d8c533..37b624fb 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\WorldNpcListChangedEventArgs.cs" /> <Compile Include="Events\WorldTerrainFeatureListChangedEventArgs.cs" /> <Compile Include="Events\WorldBuildingListChangedEventArgs.cs" /> <Compile Include="Events\WorldLocationListChangedEventArgs.cs" /> |