From 077c897d53371d12c812be5145d917c59583d7cb Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 11 May 2022 19:29:57 -0400 Subject: [PATCH] replace event.HasListeners() with property --- src/SMAPI/Framework/Events/IManagedEvent.cs | 3 +++ src/SMAPI/Framework/Events/ManagedEvent.cs | 11 +++++------ src/SMAPI/Framework/SCore.cs | 10 +++++----- src/SMAPI/Framework/SGame.cs | 4 ++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/SMAPI/Framework/Events/IManagedEvent.cs b/src/SMAPI/Framework/Events/IManagedEvent.cs index 0ae8c55a..55994c04 100644 --- a/src/SMAPI/Framework/Events/IManagedEvent.cs +++ b/src/SMAPI/Framework/Events/IManagedEvent.cs @@ -8,5 +8,8 @@ namespace StardewModdingAPI.Framework.Events *********/ /// A human-readable name for the event. string EventName { get; } + + /// Whether any handlers are listening to the event. + bool HasListeners { get; } } } diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index a34106ce..8a3ca839 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -39,6 +39,9 @@ namespace StardewModdingAPI.Framework.Events /// public string EventName { get; } + /// + public bool HasListeners { get; private set; } + /********* ** Public methods @@ -52,12 +55,6 @@ namespace StardewModdingAPI.Framework.Events this.ModRegistry = modRegistry; } - /// Get whether anything is listening to the event. - public bool HasListeners() - { - return this.Handlers.Count > 0; - } - /// Add an event handler. /// The event handler. /// The mod which added the event handler. @@ -70,6 +67,7 @@ namespace StardewModdingAPI.Framework.Events this.Handlers.Add(managedHandler); this.CachedHandlers = null; + this.HasListeners = true; this.HasPriorities |= priority != EventPriority.Normal; } } @@ -88,6 +86,7 @@ namespace StardewModdingAPI.Framework.Events this.Handlers.RemoveAt(i); this.CachedHandlers = null; + this.HasListeners = this.Handlers.Count != 0; this.HasRemovedHandlers = true; break; } diff --git a/src/SMAPI/Framework/SCore.cs b/src/SMAPI/Framework/SCore.cs index 667fcf0f..c1e03634 100644 --- a/src/SMAPI/Framework/SCore.cs +++ b/src/SMAPI/Framework/SCore.cs @@ -886,7 +886,7 @@ namespace StardewModdingAPI.Framework bool raiseWorldEvents = !state.SaveID.IsChanged; // don't report changes from unloaded => loaded // 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 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)); // chest items changed - if (events.ChestInventoryChanged.HasListeners()) + if (events.ChestInventoryChanged.HasListeners) { foreach (var pair in locState.ChestItems) { @@ -1077,7 +1077,7 @@ namespace StardewModdingAPI.Framework } // raise event - if (this.EventManager.LocaleChanged.HasListeners()) + if (this.EventManager.LocaleChanged.HasListeners) { this.EventManager.LocaleChanged.Raise( new LocaleChangedEventArgs( @@ -1140,7 +1140,7 @@ namespace StardewModdingAPI.Framework /// The asset name that was loaded. 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())); } @@ -1148,7 +1148,7 @@ namespace StardewModdingAPI.Framework /// The invalidated asset names. private void OnAssetsInvalidated(IList assetNames) { - if (this.EventManager.AssetsInvalidated.HasListeners()) + if (this.EventManager.AssetsInvalidated.HasListeners) this.EventManager.AssetsInvalidated.Raise(new AssetsInvalidatedEventArgs(assetNames, assetNames.Select(p => p.GetBaseAssetName()))); } diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 0a8a068f..38043e1c 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -353,7 +353,7 @@ namespace StardewModdingAPI.Framework } if (Game1.currentMinigame != null) { - if (events.Rendering.HasListeners()) + if (events.Rendering.HasListeners) { Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); events.Rendering.RaiseEmpty(); @@ -372,7 +372,7 @@ namespace StardewModdingAPI.Framework Game1.PushUIMode(); this.drawOverlays(Game1.spriteBatch); Game1.PopUIMode(); - if (events.Rendered.HasListeners()) + if (events.Rendered.HasListeners) { Game1.spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null); events.Rendered.RaiseEmpty();