add terrain feature list changed event (#310)

This commit is contained in:
Jesse Plamondon-Willard 2018-06-01 01:58:31 -04:00
parent cca7bf1970
commit b3f116a8f1
7 changed files with 82 additions and 7 deletions

View File

@ -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;
}
}

View File

@ -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();
}
}
}

View File

@ -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));

View File

@ -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

View File

@ -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

View File

@ -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
});
}

View File

@ -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" />