From 95ad954fa4b761dd32eefa072622cb1168c4d028 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 15 Jan 2021 18:48:30 -0500 Subject: allow get/setting PerScreen values by screen ID --- docs/release-notes.md | 1 + src/SMAPI/Utilities/PerScreen.cs | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/release-notes.md b/docs/release-notes.md index 5409d9ff..be34e653 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -9,6 +9,7 @@ ## Upcoming release * For modders: + * Expanded `PerScreen` API: you can now get/set the value for any screen. * Added an option to disable rewriting mods for compatibility (thanks to Bpendragon!). This may prevent older mods from loading, but bypasses a Visual Studio crash when debugging. * For the Error Handler mod: diff --git a/src/SMAPI/Utilities/PerScreen.cs b/src/SMAPI/Utilities/PerScreen.cs index 89d08e87..1498488b 100644 --- a/src/SMAPI/Utilities/PerScreen.cs +++ b/src/SMAPI/Utilities/PerScreen.cs @@ -49,20 +49,20 @@ namespace StardewModdingAPI.Utilities /// Get the value for a given screen ID, creating it if needed. /// The screen ID to check. - internal T GetValueForScreen(int screenId) + public T GetValueForScreen(int screenId) { - this.RemoveDeadPlayers(); + this.RemoveDeadScreens(); return this.States.TryGetValue(screenId, out T state) ? state : this.States[screenId] = this.CreateNewState(); } - /// Set the value for a given screen ID, creating it if needed. + /// Set the value for a given screen ID. /// The screen ID whose value set. /// The value to set. - internal void SetValueForScreen(int screenId, T value) + public void SetValueForScreen(int screenId, T value) { - this.RemoveDeadPlayers(); + this.RemoveDeadScreens(); this.States[screenId] = value; } @@ -70,18 +70,17 @@ namespace StardewModdingAPI.Utilities /********* ** Private methods *********/ - /// Remove players who are no longer have a split-screen index. - /// Returns whether any players were removed. - private void RemoveDeadPlayers() + /// Remove screens which are no longer active. + private void RemoveDeadScreens() { if (this.LastRemovedScreenId == Context.LastRemovedScreenId) return; - this.LastRemovedScreenId = Context.LastRemovedScreenId; - foreach (int id in this.States.Keys.ToArray()) + + foreach (var pair in this.States.ToArray()) { - if (!Context.HasScreenId(id)) - this.States.Remove(id); + if (!Context.HasScreenId(pair.Key)) + this.States.Remove(pair.Key); } } } -- cgit