replace event.HasListeners() with property

This commit is contained in:
Jesse Plamondon-Willard 2022-05-11 19:29:57 -04:00
parent 05b39b7cd9
commit 077c897d53
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
4 changed files with 15 additions and 13 deletions

View File

@ -8,5 +8,8 @@ namespace StardewModdingAPI.Framework.Events
*********/ *********/
/// <summary>A human-readable name for the event.</summary> /// <summary>A human-readable name for the event.</summary>
string EventName { get; } string EventName { get; }
/// <summary>Whether any handlers are listening to the event.</summary>
bool HasListeners { get; }
} }
} }

View File

@ -39,6 +39,9 @@ namespace StardewModdingAPI.Framework.Events
/// <inheritdoc /> /// <inheritdoc />
public string EventName { get; } public string EventName { get; }
/// <inheritdoc />
public bool HasListeners { get; private set; }
/********* /*********
** Public methods ** Public methods
@ -52,12 +55,6 @@ namespace StardewModdingAPI.Framework.Events
this.ModRegistry = modRegistry; this.ModRegistry = modRegistry;
} }
/// <summary>Get whether anything is listening to the event.</summary>
public bool HasListeners()
{
return this.Handlers.Count > 0;
}
/// <summary>Add an event handler.</summary> /// <summary>Add an event handler.</summary>
/// <param name="handler">The event handler.</param> /// <param name="handler">The event handler.</param>
/// <param name="mod">The mod which added the event handler.</param> /// <param name="mod">The mod which added the event handler.</param>
@ -70,6 +67,7 @@ namespace StardewModdingAPI.Framework.Events
this.Handlers.Add(managedHandler); this.Handlers.Add(managedHandler);
this.CachedHandlers = null; this.CachedHandlers = null;
this.HasListeners = true;
this.HasPriorities |= priority != EventPriority.Normal; this.HasPriorities |= priority != EventPriority.Normal;
} }
} }
@ -88,6 +86,7 @@ namespace StardewModdingAPI.Framework.Events
this.Handlers.RemoveAt(i); this.Handlers.RemoveAt(i);
this.CachedHandlers = null; this.CachedHandlers = null;
this.HasListeners = this.Handlers.Count != 0;
this.HasRemovedHandlers = true; this.HasRemovedHandlers = true;
break; break;
} }

View File

@ -886,7 +886,7 @@ namespace StardewModdingAPI.Framework
bool raiseWorldEvents = !state.SaveID.IsChanged; // don't report changes from unloaded => loaded bool raiseWorldEvents = !state.SaveID.IsChanged; // don't report changes from unloaded => loaded
// location list changes // location list changes
if (state.Locations.LocationList.IsChanged && (events.LocationListChanged.HasListeners() || verbose)) if (state.Locations.LocationList.IsChanged && (events.LocationListChanged.HasListeners || verbose))
{ {
var added = state.Locations.LocationList.Added.ToArray(); var added = state.Locations.LocationList.Added.ToArray();
var removed = state.Locations.LocationList.Removed.ToArray(); var removed = state.Locations.LocationList.Removed.ToArray();
@ -929,7 +929,7 @@ namespace StardewModdingAPI.Framework
events.ObjectListChanged.Raise(new ObjectListChangedEventArgs(location, locState.Objects.Added, locState.Objects.Removed)); events.ObjectListChanged.Raise(new ObjectListChangedEventArgs(location, locState.Objects.Added, locState.Objects.Removed));
// chest items changed // chest items changed
if (events.ChestInventoryChanged.HasListeners()) if (events.ChestInventoryChanged.HasListeners)
{ {
foreach (var pair in locState.ChestItems) foreach (var pair in locState.ChestItems)
{ {
@ -1077,7 +1077,7 @@ namespace StardewModdingAPI.Framework
} }
// raise event // raise event
if (this.EventManager.LocaleChanged.HasListeners()) if (this.EventManager.LocaleChanged.HasListeners)
{ {
this.EventManager.LocaleChanged.Raise( this.EventManager.LocaleChanged.Raise(
new LocaleChangedEventArgs( new LocaleChangedEventArgs(
@ -1140,7 +1140,7 @@ namespace StardewModdingAPI.Framework
/// <param name="assetName">The asset name that was loaded.</param> /// <param name="assetName">The asset name that was loaded.</param>
private void OnAssetLoaded(IContentManager contentManager, IAssetName assetName) private void OnAssetLoaded(IContentManager contentManager, IAssetName assetName)
{ {
if (this.EventManager.AssetReady.HasListeners()) if (this.EventManager.AssetReady.HasListeners)
this.EventManager.AssetReady.Raise(new AssetReadyEventArgs(assetName, assetName.GetBaseAssetName())); this.EventManager.AssetReady.Raise(new AssetReadyEventArgs(assetName, assetName.GetBaseAssetName()));
} }
@ -1148,7 +1148,7 @@ namespace StardewModdingAPI.Framework
/// <param name="assetNames">The invalidated asset names.</param> /// <param name="assetNames">The invalidated asset names.</param>
private void OnAssetsInvalidated(IList<IAssetName> assetNames) private void OnAssetsInvalidated(IList<IAssetName> assetNames)
{ {
if (this.EventManager.AssetsInvalidated.HasListeners()) if (this.EventManager.AssetsInvalidated.HasListeners)
this.EventManager.AssetsInvalidated.Raise(new AssetsInvalidatedEventArgs(assetNames, assetNames.Select(p => p.GetBaseAssetName()))); this.EventManager.AssetsInvalidated.Raise(new AssetsInvalidatedEventArgs(assetNames, assetNames.Select(p => p.GetBaseAssetName())));
} }

View File

@ -353,7 +353,7 @@ namespace StardewModdingAPI.Framework
} }
if (Game1.currentMinigame != null) if (Game1.currentMinigame != null)
{ {
if (events.Rendering.HasListeners()) if (events.Rendering.HasListeners)
{ {
Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);
events.Rendering.RaiseEmpty(); events.Rendering.RaiseEmpty();
@ -372,7 +372,7 @@ namespace StardewModdingAPI.Framework
Game1.PushUIMode(); Game1.PushUIMode();
this.drawOverlays(Game1.spriteBatch); this.drawOverlays(Game1.spriteBatch);
Game1.PopUIMode(); Game1.PopUIMode();
if (events.Rendered.HasListeners()) if (events.Rendered.HasListeners)
{ {
Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null);
events.Rendered.RaiseEmpty(); events.Rendered.RaiseEmpty();