diff --git a/CHANGELOG.md b/CHANGELOG.md index e9a0ab61..cd2292ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,7 +68,6 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Removed _all obsolete methods in TShock marked obsolete prior to this version (all of them)_ (@hakusaro). * Removed broken noclip detection and attempted prevention. TShock wasn't doing a good job at stopping noclip. It's always worse to claim that you do something that you can't/don't do, so removing this is better than keeping broken detection in. (@hakusaro) * Replaced `Utils.FindPlayer` with `TSPlayer.FindByNameOrID` to more appropriately be object orientated. (@hakusaro) -* Removed `Utils.ActivePlayers()` -- use `TShock.Players.Length` instead. (@hakusaro) * Moved `Utils.Kick()` to `TSPlayer` since its first argument was a `TSPlayer` object. (@hakusaro) * Removed `Utils.ForceKick()`. (@hakusaro) * Removed `Utils.GetPlayerIP()`. (@hakusaro) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 2faa71bc..4f9bf7d9 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4819,7 +4819,7 @@ namespace TShockAPI return; } - args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Players.Length, TShock.Config.MaxSlots); + args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Utils.ActivePlayers(), TShock.Config.MaxSlots); var players = new List(); diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 5150e598..3524f733 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1892,7 +1892,7 @@ namespace TShockAPI if (OnGetSection(args.Player, args.Data, args.Data.ReadInt32(), args.Data.ReadInt32())) return true; - if (TShock.Players.Length + 1 > TShock.Config.MaxSlots && + if (TShock.Utils.ActivePlayers() + 1 > TShock.Config.MaxSlots && !args.Player.HasPermission(Permissions.reservedslot)) { args.Player.Kick(TShock.Config.ServerFullReason, true, true); diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs index 0d1d2b6c..1151d170 100644 --- a/TShockAPI/StatTracker.cs +++ b/TShockAPI/StatTracker.cs @@ -144,7 +144,7 @@ namespace TShockAPI return new JsonData() { port = Terraria.Netplay.ListenPort, - currentPlayers = TShock.Players.Length, + currentPlayers = TShock.Utils.ActivePlayers(), maxPlayers = TShock.Config.MaxSlots, systemRam = GetTotalSystemRam(ServerApi.RunningMono), version = TShock.VersionNum.ToString(), diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 26594077..56bc45a8 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1241,7 +1241,7 @@ namespace TShockAPI var player = new TSPlayer(args.Who); - if (TShock.Players.Length + 1 > Config.MaxSlots + Config.ReservedSlots) + if (Utils.ActivePlayers() + 1 > Config.MaxSlots + Config.ReservedSlots) { player.Kick(Config.ServerFullNoReservedReason, true, true, null, false); args.Handled = true; @@ -1400,7 +1400,7 @@ namespace TShockAPI } // The last player will leave after this hook is executed. - if (TShock.Players.Length == 1) + if (Utils.ActivePlayers() == 1) { if (Config.SaveWorldOnLastPlayerExit) SaveManager.Instance.SaveWorld(); @@ -1621,7 +1621,7 @@ namespace TShockAPI if (Config.EnableGeoIP && TShock.Geo != null) { Log.Info("{0} ({1}) from '{2}' group from '{3}' joined. ({4}/{5})", player.Name, player.IP, - player.Group.Name, player.Country, TShock.Players.Length, + player.Group.Name, player.Country, TShock.Utils.ActivePlayers(), TShock.Config.MaxSlots); if (!player.SilentJoinInProgress) Utils.Broadcast(string.Format("{0} ({1}) has joined.", player.Name, player.Country), Color.Yellow); @@ -1629,7 +1629,7 @@ namespace TShockAPI else { Log.Info("{0} ({1}) from '{2}' group joined. ({3}/{4})", player.Name, player.IP, - player.Group.Name, TShock.Players.Length, TShock.Config.MaxSlots); + player.Group.Name, TShock.Utils.ActivePlayers(), TShock.Config.MaxSlots); if (!player.SilentJoinInProgress) Utils.Broadcast(player.Name + " has joined.", Color.Yellow); } diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index fc550600..f3c35591 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -149,6 +149,15 @@ namespace TShockAPI } } + /// + /// Gets the number of active players on the server. + /// + /// The number of active players on the server. + public int ActivePlayers() + { + return Main.player.Where(p => null != p && p.active).Count(); + } + //Random should not be generated in a method Random r = new Random(); @@ -1108,7 +1117,7 @@ namespace TShockAPI } else { - invasionSize = 100 + (TShock.Config.InvasionMultiplier * TShock.Players.Length); + invasionSize = 100 + (TShock.Config.InvasionMultiplier * ActivePlayers()); } // Order matters @@ -1142,12 +1151,12 @@ namespace TShockAPI } /// Updates the console title with some pertinent information. - /// If the server is empty; determines if we should use TShock.Players.Length for player count or 0. + /// If the server is empty; determines if we should use Utils.ActivePlayers() for player count or 0. internal void SetConsoleTitle(bool empty) { Console.Title = string.Format("{0}{1}/{2} on {3} @ {4}:{5} (TShock for Terraria v{6})", !string.IsNullOrWhiteSpace(TShock.Config.ServerName) ? TShock.Config.ServerName + " - " : "", - empty ? 0 : TShock.Players.Length, + empty ? 0 : ActivePlayers(), TShock.Config.MaxSlots, Main.worldName, Netplay.ServerIP.ToString(), Netplay.ListenPort, TShock.VersionNum); }