From 11920cde48e830cf0f3dcc9a450b5528a7371c80 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Tue, 26 Dec 2017 20:53:00 -0700 Subject: [PATCH 01/26] Move Utils.FindPlayer -> TSPlayer.FindByNameOrID. This makes TShock more OOP like (get the matching players from the player as opposed to getting the players from a random class that shouldn't exist). --- CHANGELOG.md | 1 + TShockAPI/Commands.cs | 48 +++++++++++++++++------------------ TShockAPI/Rest/RestManager.cs | 2 +- TShockAPI/TSPlayer.cs | 37 +++++++++++++++++++++++++++ TShockAPI/Utils.cs | 35 ------------------------- 5 files changed, 63 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54e5d06a..c1fdfeca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Replace `TShock.CheckTilePermission` with `TSPlayer.HasBuildPermission`, `TSPlayer.HasPaintPermission`, and `TSPlayer.HasModifiedIceSuccessfully` respectively. (@hakusaro) * Fix stack hack detection being inconsistent between two different check points. Moved `TShock.HackedInventory` to `TSPlayer.HasHackedItemStacks`. Added `GetDataHandlers.GetDataHandledEventArgs` which is where most hooks will inherit from in the future. (@hakusaro) * All `GetDataHandlers` hooks now inherit from `GetDataHandledEventArgs` which includes a `TSPlayer` and a `MemoryStream` of raw data. (@hakusaro) +* Replaced `Utils.FindPlayer` with `TSPlayer.FindByNameOrID` to more appropriately be object orientated. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 542d3367..68a3619a 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -1195,7 +1195,7 @@ namespace TShockAPI return; } - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count < 1) args.Player.SendErrorMessage("Invalid player."); else if (players.Count > 1) @@ -1265,7 +1265,7 @@ namespace TShockAPI } string plStr = args.Parameters[0]; - var players = TShock.Utils.FindPlayer(plStr); + var players = TSPlayer.FindByNameOrID(plStr); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -1308,7 +1308,7 @@ namespace TShockAPI string targetGeneralizedName = ""; // Effective ban target assignment - List players = TShock.Utils.FindPlayer(args.Parameters[1]); + List players = TSPlayer.FindByNameOrID(args.Parameters[1]); UserAccount offlineUserAccount = TShock.UserAccounts.GetUserAccountByName(args.Parameters[1]); // Storage variable to determine if the command executor is the server console @@ -1658,7 +1658,7 @@ namespace TShockAPI } string playerNameToMatch = string.Join(" ", args.Parameters); - var matchedPlayers = TShock.Utils.FindPlayer(playerNameToMatch); + var matchedPlayers = TSPlayer.FindByNameOrID(playerNameToMatch); if (matchedPlayers.Count < 1) { args.Player.SendErrorMessage("No players matched \"{0}\".", playerNameToMatch); @@ -1696,7 +1696,7 @@ namespace TShockAPI TSPlayer targetPlayer = args.Player; if (args.Parameters.Count == 1 && args.Player.HasPermission(Permissions.uploadothersdata)) { - List players = TShock.Utils.FindPlayer(args.Parameters[0]); + List players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count > 1) { TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); @@ -1777,7 +1777,7 @@ namespace TShockAPI return; } - List ply = TShock.Utils.FindPlayer(args.Parameters[0]); + List ply = TSPlayer.FindByNameOrID(args.Parameters[0]); if (ply.Count < 1) { args.Player.SendErrorMessage("Could not find player {0}.", args.Parameters[0]); @@ -2396,7 +2396,7 @@ namespace TShockAPI if (args.Parameters.Count == 1) { - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) @@ -2425,8 +2425,8 @@ namespace TShockAPI return; } - var players1 = TShock.Utils.FindPlayer(args.Parameters[0]); - var players2 = TShock.Utils.FindPlayer(args.Parameters[1]); + var players1 = TSPlayer.FindByNameOrID(args.Parameters[0]); + var players2 = TSPlayer.FindByNameOrID(args.Parameters[1]); if (players2.Count == 0) args.Player.SendErrorMessage("Invalid player!"); @@ -2520,7 +2520,7 @@ namespace TShockAPI } string playerName = String.Join(" ", args.Parameters); - var players = TShock.Utils.FindPlayer(playerName); + var players = TSPlayer.FindByNameOrID(playerName); if (players.Count == 0) { if (playerName == "*") @@ -2605,7 +2605,7 @@ namespace TShockAPI player = String.Join(" ", args.Parameters); } - var players = TShock.Utils.FindPlayer(player); + var players = TSPlayer.FindByNameOrID(player); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -2766,7 +2766,7 @@ namespace TShockAPI return; } - var foundplr = TShock.Utils.FindPlayer(args.Parameters[1]); + var foundplr = TSPlayer.FindByNameOrID(args.Parameters[1]); if (foundplr.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -4075,7 +4075,7 @@ namespace TShockAPI } string plStr = args.Parameters[0]; - var players = TShock.Utils.FindPlayer(plStr); + var players = TSPlayer.FindByNameOrID(plStr); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -4929,7 +4929,7 @@ namespace TShockAPI return; } - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -4977,7 +4977,7 @@ namespace TShockAPI return; } - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -5029,7 +5029,7 @@ namespace TShockAPI int annoy = 5; int.TryParse(args.Parameters[1], out annoy); - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) @@ -5049,7 +5049,7 @@ namespace TShockAPI args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}confuse ", Specifier); return; } - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) @@ -5069,7 +5069,7 @@ namespace TShockAPI args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}rocket ", Specifier); return; } - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) @@ -5098,7 +5098,7 @@ namespace TShockAPI args.Player.SendErrorMessage("Invalid syntax! Proper syntax: {0}firework [red|green|blue|yellow]", Specifier); return; } - var players = TShock.Utils.FindPlayer(args.Parameters[0]); + var players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) @@ -5266,7 +5266,7 @@ namespace TShockAPI } string plStr = String.Join(" ", args.Parameters); - var players = TShock.Utils.FindPlayer(plStr); + var players = TSPlayer.FindByNameOrID(plStr); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -5531,7 +5531,7 @@ namespace TShockAPI if (item.type >= 1 && item.type < Main.maxItemTypes) { - var players = TShock.Utils.FindPlayer(plStr); + var players = TSPlayer.FindByNameOrID(plStr); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -5577,7 +5577,7 @@ namespace TShockAPI if (args.Parameters.Count > 0) { string plStr = String.Join(" ", args.Parameters); - var players = TShock.Utils.FindPlayer(plStr); + var players = TSPlayer.FindByNameOrID(plStr); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -5662,7 +5662,7 @@ namespace TShockAPI } int id = 0; int time = 60; - var foundplr = TShock.Utils.FindPlayer(args.Parameters[0]); + var foundplr = TSPlayer.FindByNameOrID(args.Parameters[0]); if (foundplr.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); @@ -5794,7 +5794,7 @@ namespace TShockAPI return; } string plStr = String.Join(" ", args.Parameters); - var players = TShock.Utils.FindPlayer(plStr); + var players = TSPlayer.FindByNameOrID(plStr); if (players.Count == 0) { args.Player.SendErrorMessage("Invalid player!"); diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index 636e0407..1cf5fe96 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -1244,7 +1244,7 @@ namespace TShockAPI if (string.IsNullOrWhiteSpace(name)) return RestMissingParam("player"); - var found = TShock.Utils.FindPlayer(name); + var found = TSPlayer.FindByNameOrID(name); switch(found.Count) { case 1: diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 3c747b9d..26dac4e9 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -73,6 +73,43 @@ namespace TShockAPI /// public static readonly TSPlayer All = new TSPlayer("All"); + /// + /// Finds a TSPlayer based on name or ID + /// + /// Player name or ID + /// A list of matching players + public static List FindByNameOrID(string plr) + { + var found = new List(); + // Avoid errors caused by null search + if (plr == null) + return found; + + byte plrID; + if (byte.TryParse(plr, out plrID) && plrID < Main.maxPlayers) + { + TSPlayer player = TShock.Players[plrID]; + if (player != null && player.Active) + { + return new List { player }; + } + } + + string plrLower = plr.ToLower(); + foreach (TSPlayer player in TShock.Players) + { + if (player != null) + { + // Must be an EXACT match + if (player.Name == plr) + return new List { player }; + if (player.Name.ToLower().StartsWith(plrLower)) + found.Add(player); + } + } + return found; + } + /// /// The amount of tiles that the player has killed in the last second. /// diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 99480b58..0b2ced71 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -210,42 +210,7 @@ namespace TShockAPI return Main.player.Where(p => null != p && p.active).Count(); } - /// - /// Finds a TSPlayer based on name or ID - /// - /// Player name or ID - /// A list of matching players - public List FindPlayer(string plr) - { - var found = new List(); - // Avoid errors caused by null search - if (plr == null) - return found; - byte plrID; - if (byte.TryParse(plr, out plrID) && plrID < Main.maxPlayers) - { - TSPlayer player = TShock.Players[plrID]; - if (player != null && player.Active) - { - return new List { player }; - } - } - - string plrLower = plr.ToLower(); - foreach (TSPlayer player in TShock.Players) - { - if (player != null) - { - // Must be an EXACT match - if (player.Name == plr) - return new List { player }; - if (player.Name.ToLower().StartsWith(plrLower)) - found.Add(player); - } - } - return found; - } //Random should not be generated in a method Random r = new Random(); From 9648e9ac0c5960283e1e6f2a673282d15ec26633 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Tue, 26 Dec 2017 20:55:33 -0700 Subject: [PATCH 02/26] Remove excess space --- TShockAPI/Utils.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 0b2ced71..2a827950 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -210,8 +210,6 @@ namespace TShockAPI return Main.player.Where(p => null != p && p.active).Count(); } - - //Random should not be generated in a method Random r = new Random(); From b613fdcda64d93e08c1175c1183cbf27bc18f730 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Tue, 26 Dec 2017 21:14:16 -0700 Subject: [PATCH 03/26] Remove Utils.ActivePlayers Duplicated what an array already stored and told us, but added code and was obscure. It predated TSPlayers existing. --- CHANGELOG.md | 1 + TShockAPI/Commands.cs | 2 +- TShockAPI/GetDataHandlers.cs | 2 +- TShockAPI/StatTracker.cs | 2 +- TShockAPI/TShock.cs | 8 ++++---- TShockAPI/Utils.cs | 15 +++------------ 6 files changed, 11 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1fdfeca..85daa04d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -66,6 +66,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Fix stack hack detection being inconsistent between two different check points. Moved `TShock.HackedInventory` to `TSPlayer.HasHackedItemStacks`. Added `GetDataHandlers.GetDataHandledEventArgs` which is where most hooks will inherit from in the future. (@hakusaro) * All `GetDataHandlers` hooks now inherit from `GetDataHandledEventArgs` which includes a `TSPlayer` and a `MemoryStream` of raw data. (@hakusaro) * Replaced `Utils.FindPlayer` with `TSPlayer.FindByNameOrID` to more appropriately be object orientated. (@hakusaro) +* Removed `Utils.ActivePlayers()` -- use `TShock.Players.Length` instead. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 68a3619a..32e06ad5 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4818,7 +4818,7 @@ namespace TShockAPI return; } - args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Utils.ActivePlayers(), TShock.Config.MaxSlots); + args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Players.Length, TShock.Config.MaxSlots); PaginationTools.SendPage( args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(TShock.Utils.GetPlayers(displayIdsRequested)), new PaginationTools.Settings diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index d5fafec5..93b6e652 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1946,7 +1946,7 @@ namespace TShockAPI if (OnGetSection(args.Player, args.Data, args.Data.ReadInt32(), args.Data.ReadInt32())) return true; - if (TShock.Utils.ActivePlayers() + 1 > TShock.Config.MaxSlots && + if (TShock.Players.Length + 1 > TShock.Config.MaxSlots && !args.Player.HasPermission(Permissions.reservedslot)) { TShock.Utils.ForceKick(args.Player, TShock.Config.ServerFullReason, true); diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs index 1151d170..0d1d2b6c 100644 --- a/TShockAPI/StatTracker.cs +++ b/TShockAPI/StatTracker.cs @@ -144,7 +144,7 @@ namespace TShockAPI return new JsonData() { port = Terraria.Netplay.ListenPort, - currentPlayers = TShock.Utils.ActivePlayers(), + currentPlayers = TShock.Players.Length, maxPlayers = TShock.Config.MaxSlots, systemRam = GetTotalSystemRam(ServerApi.RunningMono), version = TShock.VersionNum.ToString(), diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index d534d67e..6329dab9 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1243,7 +1243,7 @@ namespace TShockAPI var player = new TSPlayer(args.Who); - if (Utils.ActivePlayers() + 1 > Config.MaxSlots + Config.ReservedSlots) + if (TShock.Players.Length + 1 > Config.MaxSlots + Config.ReservedSlots) { Utils.ForceKick(player, Config.ServerFullNoReservedReason, true, false); args.Handled = true; @@ -1402,7 +1402,7 @@ namespace TShockAPI } // The last player will leave after this hook is executed. - if (Utils.ActivePlayers() == 1) + if (TShock.Players.Length == 1) { if (Config.SaveWorldOnLastPlayerExit) SaveManager.Instance.SaveWorld(); @@ -1623,7 +1623,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.Utils.ActivePlayers(), + player.Group.Name, player.Country, TShock.Players.Length, TShock.Config.MaxSlots); if (!player.SilentJoinInProgress) Utils.Broadcast(string.Format("{0} ({1}) has joined.", player.Name, player.Country), Color.Yellow); @@ -1631,7 +1631,7 @@ namespace TShockAPI else { Log.Info("{0} ({1}) from '{2}' group joined. ({3}/{4})", player.Name, player.IP, - player.Group.Name, TShock.Utils.ActivePlayers(), TShock.Config.MaxSlots); + player.Group.Name, TShock.Players.Length, 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 2a827950..8f92e8ad 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -201,15 +201,6 @@ 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(); @@ -1433,7 +1424,7 @@ namespace TShockAPI } else { - invasionSize = 100 + (TShock.Config.InvasionMultiplier * ActivePlayers()); + invasionSize = 100 + (TShock.Config.InvasionMultiplier * TShock.Players.Length); } // Order matters @@ -1467,12 +1458,12 @@ namespace TShockAPI } /// Updates the console title with some pertinent information. - /// If the server is empty; determines if we should use Utils.ActivePlayers() for player count or 0. + /// If the server is empty; determines if we should use TShock.Players.Length 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 : ActivePlayers(), + empty ? 0 : TShock.Players.Length, TShock.Config.MaxSlots, Main.worldName, Netplay.ServerIP.ToString(), Netplay.ListenPort, TShock.VersionNum); } From f79c6a2962e1afe25427e74e9b5563477557898e Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 13:00:13 -0700 Subject: [PATCH 04/26] Move Utils.Kick to TSPlayer.Kick --- CHANGELOG.md | 1 + TShockAPI/Bouncer.cs | 6 +++--- TShockAPI/Commands.cs | 4 ++-- TShockAPI/TSPlayer.cs | 32 ++++++++++++++++++++++++++++++++ TShockAPI/TShock.cs | 2 +- TShockAPI/Utils.cs | 36 +----------------------------------- 6 files changed, 40 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85daa04d..41263289 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,6 +67,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * All `GetDataHandlers` hooks now inherit from `GetDataHandledEventArgs` which includes a `TSPlayer` and a `MemoryStream` of raw data. (@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) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index dd898b62..a3268bf0 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -256,7 +256,7 @@ namespace TShockAPI { if (TShock.Config.KickOnDamageThresholdBroken) { - TShock.Utils.Kick(args.Player, string.Format("NPC damage exceeded {0}.", TShock.Config.MaxDamage)); + args.Player.Kick(string.Format("NPC damage exceeded {0}.", TShock.Config.MaxDamage)); args.Handled = true; return; } @@ -313,7 +313,7 @@ namespace TShockAPI { if (TShock.Config.KickOnDamageThresholdBroken) { - TShock.Utils.Kick(args.Player, string.Format("Player damage exceeded {0}.", TShock.Config.MaxDamage)); + args.Player.Kick(string.Format("Player damage exceeded {0}.", TShock.Config.MaxDamage)); args.Handled = true; return; } @@ -958,7 +958,7 @@ namespace TShockAPI { if (playerDeathReason.GetDeathText(TShock.Players[id].Name).ToString().Length > 500) { - TShock.Utils.Kick(TShock.Players[id], "Death reason outside of normal bounds.", true); + TShock.Players[id].Kick("Death reason outside of normal bounds.", true); args.Handled = true; return; } diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 32e06ad5..5f0fc008 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -783,7 +783,7 @@ namespace TShockAPI { TShock.Log.Warn(String.Format("{0} ({1}) had {2} or more invalid login attempts and was kicked automatically.", args.Player.IP, args.Player.Name, TShock.Config.MaximumLoginAttempts)); - TShock.Utils.Kick(args.Player, "Too many invalid login attempts."); + args.Player.Kick("Too many invalid login attempts."); return; } @@ -1279,7 +1279,7 @@ namespace TShockAPI string reason = args.Parameters.Count > 1 ? String.Join(" ", args.Parameters.GetRange(1, args.Parameters.Count - 1)) : "Misbehaviour."; - if (!TShock.Utils.Kick(players[0], reason, !args.Player.RealPlayer, false, args.Player.Name)) + if (!players[0].Kick(reason, !args.Player.RealPlayer, false, args.Player.Name)) { args.Player.SendErrorMessage("You can't kick another admin!"); } diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 26dac4e9..bb94688a 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -1579,6 +1579,38 @@ namespace TShockAPI LogStackFrame(); } + /// + /// Disconnects this player from the server with a reason. + /// + /// The reason to display to the user and to the server on kick. + /// If the kick should happen regardless of immunity to kick permissions. + /// If no message should be broadcasted to the server. + /// The originator of the kick, for display purposes. + /// If the player's server side character should be saved on kick. + public bool Kick(string reason, bool force = false, bool silent = false, string adminUserName = null, bool saveSSI = false) + { + if (!ConnectionAlive) + return true; + if (force || !HasPermission(Permissions.immunetokick)) + { + SilentKickInProgress = silent; + if (IsLoggedIn && saveSSI) + SaveServerCharacter(); + Disconnect(string.Format("Kicked: {0}", reason)); + TShock.Log.ConsoleInfo(string.Format("Kicked {0} for : '{1}'", Name, reason)); + string verb = force ? "force " : ""; + if (!silent) + { + if (string.IsNullOrWhiteSpace(adminUserName)) + TShock.Utils.Broadcast(string.Format("{0} was {1}kicked for '{2}'", Name, verb, reason.ToLower()), Color.Green); + else + TShock.Utils.Broadcast(string.Format("{0} {1}kicked {2} for '{3}'", adminUserName, verb, Name, reason.ToLower()), Color.Green); + } + return true; + } + return false; + } + [Conditional("DEBUG")] private void LogStackFrame() { diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 6329dab9..61714a8c 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1426,7 +1426,7 @@ namespace TShockAPI if (args.Text.Length > 500) { - Utils.Kick(tsplr, "Crash attempt via long chat packet.", true); + tsplr.Kick("Crash attempt via long chat packet.", true); args.Handled = true; return; } diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 8f92e8ad..54411a55 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -558,41 +558,7 @@ namespace TShockAPI /// bool saveSSI (default: false) public void ForceKick(TSPlayer player, string reason, bool silent = false, bool saveSSI = false) { - Kick(player, reason, true, silent, null, saveSSI); - } - - /// - /// Kicks a player from the server.. - /// - /// TSPlayer player - /// string reason - /// bool force (default: false) - /// bool silent (default: false) - /// string adminUserName (default: null) - /// bool saveSSI (default: false) - public bool Kick(TSPlayer player, string reason, bool force = false, bool silent = false, string adminUserName = null, bool saveSSI = false) - { - if (!player.ConnectionAlive) - return true; - if (force || !player.HasPermission(Permissions.immunetokick)) - { - string playerName = player.Name; - player.SilentKickInProgress = silent; - if (player.IsLoggedIn && saveSSI) - player.SaveServerCharacter(); - player.Disconnect(string.Format("Kicked: {0}", reason)); - TShock.Log.ConsoleInfo(string.Format("Kicked {0} for : '{1}'", playerName, reason)); - string verb = force ? "force " : ""; - if (!silent) - { - if (string.IsNullOrWhiteSpace(adminUserName)) - Broadcast(string.Format("{0} was {1}kicked for '{2}'", playerName, verb, reason.ToLower()), Color.Green); - else - Broadcast(string.Format("{0} {1}kicked {2} for '{3}'", adminUserName, verb, playerName, reason.ToLower()), Color.Green); - } - return true; - } - return false; + player.Kick(reason, true, silent, null, saveSSI); } /// From 9e91db9ea446d78533d61ff49dff5ba6a6276455 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 17:43:21 -0700 Subject: [PATCH 05/26] Removed force kick for attempting to use setup On many servers, players are tricked into running the /auth or /setup command to get them kicked. This is stupid. Since the system is disabled anyway, we don't need to kick them. --- TShockAPI/Commands.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 5f0fc008..a8c44869 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4838,7 +4838,6 @@ namespace TShockAPI else { args.Player.SendWarningMessage("The initial setup system is disabled. This incident has been logged."); - TShock.Utils.ForceKick(args.Player, "The initial setup system is disabled.", true, true); TShock.Log.Warn("{0} attempted to use the initial setup system even though it's disabled.", args.Player.IP); return; } From 1ea04ffd4f568e0ba708599ea9ea3007622a441e Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 18:01:53 -0700 Subject: [PATCH 06/26] Remove Utils.ForceKick; use TSPlayer.Kick. Some anti-cheat calls were changed to "You have been Bounced." I don't think we need to tell clients why they were disconnected so they can tune their hacks better. --- CHANGELOG.md | 1 + TShockAPI/Bouncer.cs | 4 ++-- TShockAPI/GetDataHandlers.cs | 27 ++++++++++++++------------- TShockAPI/Rest/RestManager.cs | 4 ++-- TShockAPI/TShock.cs | 16 ++++++++-------- TShockAPI/Utils.cs | 14 +------------- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41263289..8b81bb18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * 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) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index a3268bf0..d1d004b2 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -77,7 +77,7 @@ namespace TShockAPI if (String.IsNullOrEmpty(args.Player.Name)) { - TShock.Utils.ForceKick(args.Player, "Blank name.", true); + args.Player.Kick("Your client sent a blank character name.", true, true); args.Handled = true; return; } @@ -941,7 +941,7 @@ namespace TShockAPI if (damage > 20000) //Abnormal values have the potential to cause infinite loops in the server. { - TShock.Utils.ForceKick(args.Player, "Crash Exploit Attempt", true); + args.Player.Kick("Failed to shade polygon normals.", true, true); TShock.Log.ConsoleError("Death Exploit Attempt: Damage {0}", damage); args.Handled = true; return; diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 93b6e652..89056593 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -349,7 +349,7 @@ namespace TShockAPI } /// /// PlayerInfo - called at a PlayerInfo event - /// If this is cancelled, the server will ForceKick the player. If this should be changed in the future, let someone know. + /// If this is cancelled, the server will kick the player. If this should be changed in the future, let someone know. /// public static HandlerList PlayerInfo = new HandlerList(); @@ -1715,13 +1715,13 @@ namespace TShockAPI if (OnPlayerInfo(args.Player, args.Data, playerid, hair, skinVariant, difficulty, name)) { - TShock.Utils.ForceKick(args.Player, "A plugin cancelled the event.", true); + args.Player.Kick("A plugin on this server stopped your login.", true, true); return true; } if (name.Trim().Length == 0) { - TShock.Utils.ForceKick(args.Player, "Empty Name.", true); + args.Player.Kick("You have been Bounced.", true, true); return true; } if (args.Player.ReceivedInfo) @@ -1749,12 +1749,12 @@ namespace TShockAPI } if (TShock.Config.MediumcoreOnly && difficulty < 1) { - TShock.Utils.ForceKick(args.Player, "Server is set to mediumcore and above characters only!", true); + args.Player.Kick("You need to join with a mediumcore player or higher.", true, true); return true; } if (TShock.Config.HardcoreOnly && difficulty < 2) { - TShock.Utils.ForceKick(args.Player, "Server is set to hardcore characters only!", true); + args.Player.Kick("You need to join with a hardcore player.", true, true); return true; } args.Player.Difficulty = difficulty; @@ -1884,9 +1884,10 @@ namespace TShockAPI Hooks.PlayerHooks.OnPlayerPostLogin(args.Player); return true; } - TShock.Utils.ForceKick(args.Player, "Invalid user account password.", true); + args.Player.Kick("Your password did not match this character's password.", true, true); return true; } + if (!string.IsNullOrEmpty(TShock.Config.ServerPassword)) { if (TShock.Config.ServerPassword == password) @@ -1897,11 +1898,11 @@ namespace TShockAPI NetMessage.SendData((int)PacketTypes.WorldInfo, args.Player.Index); return true; } - TShock.Utils.ForceKick(args.Player, "Incorrect server password", true); + args.Player.Kick("Invalid server password.", true, true); return true; } - TShock.Utils.ForceKick(args.Player, "Bad password attempt", true); + args.Player.Kick("You have been Bounced.", true, true); return true; } @@ -1949,7 +1950,7 @@ namespace TShockAPI if (TShock.Players.Length + 1 > TShock.Config.MaxSlots && !args.Player.HasPermission(Permissions.reservedslot)) { - TShock.Utils.ForceKick(args.Player, TShock.Config.ServerFullReason, true); + args.Player.Kick(TShock.Config.ServerFullReason, true, true); return true; } @@ -2439,11 +2440,11 @@ namespace TShockAPI if (TShock.Config.BanOnHardcoreDeath) { if (!TShock.Utils.Ban(args.Player, TShock.Config.HardcoreBanReason, false, "hardcore-death")) - TShock.Utils.ForceKick(args.Player, "Death results in a ban, but you are immune to bans.", true); + args.Player.Kick("You died! Normally, you'd be banned.", true, true); } else { - TShock.Utils.ForceKick(args.Player, TShock.Config.HardcoreKickReason, true, false); + args.Player.Kick(TShock.Config.HardcoreKickReason, true, true, null, false); } } @@ -2503,11 +2504,11 @@ namespace TShockAPI if (TShock.Config.BanOnMediumcoreDeath) { if (!TShock.Utils.Ban(args.Player, TShock.Config.MediumcoreBanReason, false, "mediumcore-death")) - TShock.Utils.ForceKick(args.Player, "Death results in a ban, but you are immune to bans.", true); + args.Player.Kick("You died! Normally, you'd be banned.", true, true); } else { - TShock.Utils.ForceKick(args.Player, TShock.Config.MediumcoreKickReason, true, false); + args.Player.Kick(TShock.Config.MediumcoreKickReason, true, true, null, false); } return true; } diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index 1cf5fe96..cad79834 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -982,7 +982,7 @@ namespace TShockAPI return ret; TSPlayer player = (TSPlayer)ret; - TShock.Utils.ForceKick(player, null == args.Parameters["reason"] ? "Kicked via web" : args.Parameters["reason"], false, true); + player.Kick(null == args.Parameters["reason"] ? "Kicked via web" : args.Parameters["reason"], false, true, null, true); return RestResponse("Player " + player.Name + " was kicked"); } @@ -1002,7 +1002,7 @@ namespace TShockAPI TSPlayer player = (TSPlayer)ret; var reason = null == args.Parameters["reason"] ? "Banned via web" : args.Parameters["reason"]; TShock.Bans.AddBan2(player.IP, player.Name, "", "", reason); - TShock.Utils.ForceKick(player, reason, false, true); + player.Kick(reason, true, false, null, true); return RestResponse("Player " + player.Name + " was banned"); } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 61714a8c..18b618e7 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -481,13 +481,13 @@ namespace TShockAPI // And then get rid of them. if (potentialBan.Expiration == "") { - Utils.ForceKick(args.Player, String.Format("Permanently banned by {0} for {1}", potentialBan.BanningUser - ,potentialBan.Reason), false, false); + args.Player.Kick(String.Format("Permanently banned by {0} for {1}", potentialBan.BanningUser + ,potentialBan.Reason), true, true); } else { - Utils.ForceKick(args.Player, String.Format("Still banned by {0} for {1}", potentialBan.BanningUser, - potentialBan.Reason), false, false); + args.Player.Kick(String.Format("Still banned by {0} for {1}", potentialBan.BanningUser, + potentialBan.Reason), true, true); } } } @@ -1245,14 +1245,14 @@ namespace TShockAPI if (TShock.Players.Length + 1 > Config.MaxSlots + Config.ReservedSlots) { - Utils.ForceKick(player, Config.ServerFullNoReservedReason, true, false); + player.Kick(Config.ServerFullNoReservedReason, true, true, null, false); args.Handled = true; return; } if (!FileTools.OnWhitelist(player.IP)) { - Utils.ForceKick(player, Config.WhitelistKickReason, true, false); + player.Kick(Config.WhitelistKickReason, true, true, null, false); args.Handled = true; return; } @@ -1265,7 +1265,7 @@ namespace TShockAPI { if (Config.KickProxyUsers) { - Utils.ForceKick(player, "Proxies are not allowed.", true, false); + player.Kick("Connecting via a proxy is not allowed.", true, true, null, false); args.Handled = true; return; } @@ -1287,7 +1287,7 @@ namespace TShockAPI if (Config.KickEmptyUUID && String.IsNullOrWhiteSpace(player.UUID)) { - Utils.ForceKick(player, "Your client did not send a UUID, this server is not configured to accept such a client.", true); + player.Kick("Your client sent a blank UUID. Configure it to send one or use a different client.", true, true, null, false); args.Handled = true; return; } diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 54411a55..0f4d724a 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -506,7 +506,7 @@ namespace TShockAPI { if (player != null && player.Active) { - ForceKick(player, reason, false, true); + player.Kick(reason, true, true, null, true); } } } @@ -549,18 +549,6 @@ namespace TShockAPI Hooks.GeneralHooks.OnReloadEvent(player); } - /// - /// Kicks a player from the server without checking for immunetokick permission. - /// - /// TSPlayer player - /// string reason - /// bool silent (default: false) - /// bool saveSSI (default: false) - public void ForceKick(TSPlayer player, string reason, bool silent = false, bool saveSSI = false) - { - player.Kick(reason, true, silent, null, saveSSI); - } - /// /// Bans and kicks a player from the server. /// From 8301d6a6f31878b493be59b447befb824fecfb14 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 18:06:01 -0700 Subject: [PATCH 07/26] Remove Utils.ForceKickAll; add TSPlayer.KickAll. --- CHANGELOG.md | 1 + TShockAPI/TSPlayer.cs | 16 ++++++++++++++++ TShockAPI/Utils.cs | 19 +------------------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b81bb18..a4a2a203 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * 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) +* Replaced `Utils.ForceKickAll()` with `TSPlayer.KickAll`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index bb94688a..d40d362e 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -110,6 +110,22 @@ namespace TShockAPI return found; } + /// + /// Disconnects all players from the server without checking for immunetokick permission. + /// + /// If the kick should bypass permission checks. + /// The reason for the kick. + public static void KickAll(bool force, string reason) + { + foreach (TSPlayer player in TShock.Players) + { + if (player != null && player.Active) + { + player.Kick(reason, force, true, null, true); + } + } + } + /// /// The amount of tiles that the player has killed in the last second. /// diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 0f4d724a..3daebc79 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -496,21 +496,6 @@ namespace TShockAPI return GetPrefixByName(idOrName); } - /// - /// Kicks all player from the server without checking for immunetokick permission. - /// - /// string reason - public void ForceKickAll(string reason) - { - foreach (TSPlayer player in TShock.Players) - { - if (player != null && player.Active) - { - player.Kick(reason, true, true, null, true); - } - } - } - /// /// Stops the server after kicking all players with a reason message, and optionally saving the world /// @@ -520,12 +505,10 @@ namespace TShockAPI { TShock.ShuttingDown = true; - ForceKickAll(reason); if (save) SaveManager.Instance.SaveWorld(); - // Save takes a while so kick again - ForceKickAll(reason); + TSPlayer.KickAll(true, reason); // Broadcast so console can see we are shutting down as well TShock.Utils.Broadcast(reason, Color.Red); From fc689dbbc53c537dd60b526c539b5eda4fc622f8 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 18:20:47 -0700 Subject: [PATCH 08/26] Remove TSPlayer.KickAll MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "☕⚡ Ivan - Today at 6:20 PM @☕⚡ ハクサロ wouldnt TSPlayer.All.Kick() do the trick?" --- CHANGELOG.md | 1 - TShockAPI/TSPlayer.cs | 16 ---------------- 2 files changed, 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4a2a203..8b81bb18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,7 +69,6 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * 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) -* Replaced `Utils.ForceKickAll()` with `TSPlayer.KickAll`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index d40d362e..bb94688a 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -110,22 +110,6 @@ namespace TShockAPI return found; } - /// - /// Disconnects all players from the server without checking for immunetokick permission. - /// - /// If the kick should bypass permission checks. - /// The reason for the kick. - public static void KickAll(bool force, string reason) - { - foreach (TSPlayer player in TShock.Players) - { - if (player != null && player.Active) - { - player.Kick(reason, force, true, null, true); - } - } - } - /// /// The amount of tiles that the player has killed in the last second. /// From 70f22c1e70fa6cb4c7ad6884ad8667e726d03e10 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 18:23:11 -0700 Subject: [PATCH 09/26] Fix Travis --- TShockAPI/Utils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 3daebc79..58b83786 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -508,7 +508,7 @@ namespace TShockAPI if (save) SaveManager.Instance.SaveWorld(); - TSPlayer.KickAll(true, reason); + TSPlayer.All.Kick(reason, true, true, null, true); // Broadcast so console can see we are shutting down as well TShock.Utils.Broadcast(reason, Color.Red); From 70c8ef548fcf63fac0d9f1c51ad753ab3ba605c0 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 19:09:08 -0700 Subject: [PATCH 10/26] Fix merge conflict part 2 --- TShockAPI/Rest/RestManager.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index de34395e..d47130b6 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -1003,7 +1003,6 @@ namespace TShockAPI var reason = null == args.Parameters["reason"] ? "Banned via web" : args.Parameters["reason"]; TShock.Bans.AddBan(player.IP, player.Name, "", "", reason); player.Kick(reason, true, false, null, true); - TShock.Utils.ForceKick(player, reason, false, true); return RestResponse("Player " + player.Name + " was banned"); } From a5a3aae59953883299e03ba7a215402d48c69f53 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Wed, 27 Dec 2017 19:29:01 -0700 Subject: [PATCH 11/26] Remove Utils.GetPlayerIP() --- CHANGELOG.md | 1 + TShockAPI/Utils.cs | 19 ------------------- 2 files changed, 1 insertion(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0e08b08..99fb71bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * 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) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index f6af228d..642c109c 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -96,25 +96,6 @@ namespace TShockAPI return players; } - /// - /// Finds a player and gets IP as string - /// - /// string playername - public string GetPlayerIP(string playername) - { - foreach (TSPlayer player in TShock.Players) - { - if (player != null && player.Active) - { - if (playername.ToLower() == player.Name.ToLower()) - { - return player.IP; - } - } - } - return null; - } - /// /// It's a clamp function /// From 152c67f27c05b6c3382934057745fc98bb9e762b Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 07:58:32 -0700 Subject: [PATCH 12/26] Move Utils.Ban -> TSPlayer.Ban. Arguably, this is one of the more controversial methods that's being kept. Because it kicks and bans a target player, it's more useful than removing it and requiring people to interface with the TShock Ban Manager directly (not a good move for the future). Whether or not this method sucks is up for debate, but right now I think it's totally fine to keep it around in a different location. --- CHANGELOG.md | 3 ++- TShockAPI/GetDataHandlers.cs | 4 ++-- TShockAPI/TSPlayer.cs | 26 ++++++++++++++++++++++++++ TShockAPI/Utils.cs | 28 ---------------------------- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99fb71bb..432f6b2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,7 +71,8 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * 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) +* Removed `Utils.GetPlayerIP()`. (@hakusaro) +* Moved `Utils.Ban()` to `TSPlayer.Ban()`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 320affd9..9de92643 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -2384,7 +2384,7 @@ namespace TShockAPI { if (TShock.Config.BanOnHardcoreDeath) { - if (!TShock.Utils.Ban(args.Player, TShock.Config.HardcoreBanReason, false, "hardcore-death")) + if (!args.Player.Ban(TShock.Config.HardcoreBanReason, false, "hardcore-death")) args.Player.Kick("You died! Normally, you'd be banned.", true, true); } else @@ -2448,7 +2448,7 @@ namespace TShockAPI { if (TShock.Config.BanOnMediumcoreDeath) { - if (!TShock.Utils.Ban(args.Player, TShock.Config.MediumcoreBanReason, false, "mediumcore-death")) + if (!args.Player.Ban(TShock.Config.MediumcoreBanReason, false, "mediumcore-death")) args.Player.Kick("You died! Normally, you'd be banned.", true, true); } else diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index bf7d776c..d38066ce 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -1574,6 +1574,32 @@ namespace TShockAPI return false; } + /// + /// Bans and disconnects the player from the server. + /// + /// The reason to be displayed to the server. + /// If the ban should bypass immunity to ban checks. + /// The player who initiated the ban. + public bool Ban(string reason, bool force = false, string adminUserName = null) + { + if (!ConnectionAlive) + return true; + if (force || !HasPermission(Permissions.immunetoban)) + { + string ip = IP; + string uuid = UUID; + TShock.Bans.AddBan(ip, Name, uuid, "", reason, false, adminUserName); + Disconnect(string.Format("Banned: {0}", reason)); + string verb = force ? "force " : ""; + if (string.IsNullOrWhiteSpace(adminUserName)) + TSPlayer.All.SendInfoMessage("{0} was {1}banned for '{2}'.", Name, verb, reason); + else + TSPlayer.All.SendInfoMessage("{0} {1}banned {2} for '{3}'.", adminUserName, verb, Name, reason); + return true; + } + return false; + } + [Conditional("DEBUG")] private void LogStackFrame() { diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 642c109c..5e5e7580 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -507,34 +507,6 @@ namespace TShockAPI Hooks.GeneralHooks.OnReloadEvent(player); } - /// - /// Bans and kicks a player from the server. - /// - /// TSPlayer player - /// string reason - /// bool force (default: false) - /// string adminUserName (default: null) - public bool Ban(TSPlayer player, string reason, bool force = false, string adminUserName = null) - { - if (!player.ConnectionAlive) - return true; - if (force || !player.HasPermission(Permissions.immunetoban)) - { - string ip = player.IP; - string uuid = player.UUID; - string playerName = player.Name; - TShock.Bans.AddBan(ip, playerName, uuid, "", reason, false, adminUserName); - player.Disconnect(string.Format("Banned: {0}", reason)); - string verb = force ? "force " : ""; - if (string.IsNullOrWhiteSpace(adminUserName)) - TSPlayer.All.SendInfoMessage("{0} was {1}banned for '{2}'.", playerName, verb, reason); - else - TSPlayer.All.SendInfoMessage("{0} {1}banned {2} for '{3}'.", adminUserName, verb, playerName, reason); - return true; - } - return false; - } - /// HasBanExpired - Returns whether or not a ban has expired or not. /// ban - The ban object to check. /// byName - Defines whether or not the ban should be checked by name. From afdedee1c474b18e15bca65dc21aced7276bdba1 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:04:20 -0700 Subject: [PATCH 13/26] Change Utils.Reload to not take a player. There are two occasions in the codebase when Utils.Reload is called and we're competent enough that we can just call the event when the reload happens. Unrelated note, but shouldn't this event be called prior to reloading? I've kept it the same to preserve existing behavior, but I think it should probably happen before the reload event takes place. Either way, I think this is fine. --- TShockAPI/Commands.cs | 3 ++- TShockAPI/Rest/RestManager.cs | 3 ++- TShockAPI/Utils.cs | 3 +-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 2c593d8f..25860577 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -3798,7 +3798,8 @@ namespace TShockAPI private static void Reload(CommandArgs args) { - TShock.Utils.Reload(args.Player); + TShock.Utils.Reload(); + Hooks.GeneralHooks.OnReloadEvent(args.Player); args.Player.SendSuccessMessage( "Configuration, permissions, and regions reload complete. Some changes may require a server restart."); diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index d47130b6..5174b560 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -339,7 +339,8 @@ namespace TShockAPI [Token] private object ServerReload(RestRequestArgs args) { - TShock.Utils.Reload(new TSRestPlayer(args.TokenData.Username, TShock.Groups.GetGroupByName(args.TokenData.UserGroupName))); + TShock.Utils.Reload(); + Hooks.GeneralHooks.OnReloadEvent(new TSRestPlayer(args.TokenData.Username, TShock.Groups.GetGroupByName(args.TokenData.UserGroupName))); return RestResponse("Configuration, permissions, and regions reload complete. Some changes may require a server restart."); } diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 5e5e7580..2c95af15 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -495,7 +495,7 @@ namespace TShockAPI /// /// Reloads all configuration settings, groups, regions and raises the reload event. /// - public void Reload(TSPlayer player) + public void Reload() { FileTools.SetupConfig(); TShock.HandleCommandLinePostConfigLoad(Environment.GetCommandLineArgs()); @@ -504,7 +504,6 @@ namespace TShockAPI TShock.Itembans.UpdateItemBans(); TShock.ProjectileBans.UpdateBans(); TShock.TileBans.UpdateBans(); - Hooks.GeneralHooks.OnReloadEvent(player); } /// HasBanExpired - Returns whether or not a ban has expired or not. From 48393d60c7ece5993f61728775ab557a9f0bad68 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:15:04 -0700 Subject: [PATCH 14/26] Move SendMultipleMatchError to TSPlayer. This is a great method for a player to have and not for the Utils class to have. --- CHANGELOG.md | 1 + TShockAPI/Commands.cs | 74 +++++++++++++++++++++---------------------- TShockAPI/TSPlayer.cs | 16 ++++++++++ TShockAPI/Utils.cs | 17 ---------- 4 files changed, 54 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 432f6b2d..3320a56c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Removed `Utils.ForceKick()`. (@hakusaro) * Removed `Utils.GetPlayerIP()`. (@hakusaro) * Moved `Utils.Ban()` to `TSPlayer.Ban()`. (@hakusaro) +* Moved `Utils.SendMultipleMatchError()` to `TSPlayer.SendMultipleMatchError`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 25860577..edbdcd83 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -1199,7 +1199,7 @@ namespace TShockAPI if (players.Count < 1) args.Player.SendErrorMessage("Invalid player."); else if (players.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); else { var message = new StringBuilder(); @@ -1272,7 +1272,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); } else { @@ -1355,7 +1355,7 @@ namespace TShockAPI // Bad case: Players contains more than 1 person so we can't ban them if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); return; } @@ -1666,7 +1666,7 @@ namespace TShockAPI } else if (matchedPlayers.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, matchedPlayers.Select(p => p.Name)); + args.Player.SendMultipleMatchError(matchedPlayers.Select(p => p.Name)); return; } @@ -1699,7 +1699,7 @@ namespace TShockAPI List players = TSPlayer.FindByNameOrID(args.Parameters[0]); if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); return; } else if (players.Count == 0) @@ -1786,7 +1786,7 @@ namespace TShockAPI if (ply.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, ply.Select(p => p.Account.Name)); + args.Player.SendMultipleMatchError(ply.Select(p => p.Account.Name)); } if (!TShock.Groups.GroupExists(args.Parameters[1])) @@ -2326,7 +2326,7 @@ namespace TShockAPI } else if (npcs.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, npcs.Select(n => $"{n.FullName}({n.type})")); + args.Player.SendMultipleMatchError(npcs.Select(n => $"{n.FullName}({n.type})")); } else { @@ -2400,7 +2400,7 @@ namespace TShockAPI if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); else { var target = players[0]; @@ -2431,7 +2431,7 @@ namespace TShockAPI if (players2.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players2.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players2.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players2.Select(p => p.Name)); else if (players1.Count == 0) { if (args.Parameters[0] == "*") @@ -2471,7 +2471,7 @@ namespace TShockAPI args.Player.SendErrorMessage("Invalid player!"); } else if (players1.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players1.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players1.Select(p => p.Name)); else { var source = players1[0]; @@ -2544,7 +2544,7 @@ namespace TShockAPI args.Player.SendErrorMessage("Invalid player!"); } else if (players.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); else { var plr = players[0]; @@ -2583,7 +2583,7 @@ namespace TShockAPI if (matches.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, matches.Select(n => $"{n.FullName}({n.whoAmI})")); + args.Player.SendMultipleMatchError(matches.Select(n => $"{n.FullName}({n.whoAmI})")); return; } if (matches.Count == 0) @@ -2612,7 +2612,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); } else { @@ -2774,7 +2774,7 @@ namespace TShockAPI } else if (foundplr.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, foundplr.Select(p => p.Name)); + args.Player.SendMultipleMatchError(foundplr.Select(p => p.Name)); return; } @@ -3265,7 +3265,7 @@ namespace TShockAPI } else if (items.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, items.Select(i => $"{i.Name}({i.netID})")); + args.Player.SendMultipleMatchError(items.Select(i => $"{i.Name}({i.netID})")); } else { @@ -3291,7 +3291,7 @@ namespace TShockAPI } else if (items.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, items.Select(i => $"{i.Name}({i.netID})")); + args.Player.SendMultipleMatchError(items.Select(i => $"{i.Name}({i.netID})")); } else { @@ -3336,7 +3336,7 @@ namespace TShockAPI } else if (items.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, items.Select(i => $"{i.Name}({i.netID})")); + args.Player.SendMultipleMatchError(items.Select(i => $"{i.Name}({i.netID})")); } else { @@ -3362,7 +3362,7 @@ namespace TShockAPI } else if (items.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, items.Select(i => $"{i.Name}({i.netID})")); + args.Player.SendMultipleMatchError(items.Select(i => $"{i.Name}({i.netID})")); } else { @@ -4083,7 +4083,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); } else { @@ -4936,7 +4936,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); } else if (players[0].HasPermission(Permissions.mute)) { @@ -4984,7 +4984,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); } else if (args.Player.mute) { @@ -5033,7 +5033,7 @@ namespace TShockAPI if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); else { var ply = players[0]; @@ -5053,7 +5053,7 @@ namespace TShockAPI if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); else { var ply = players[0]; @@ -5073,7 +5073,7 @@ namespace TShockAPI if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); else { var ply = players[0]; @@ -5102,7 +5102,7 @@ namespace TShockAPI if (players.Count == 0) args.Player.SendErrorMessage("Invalid player!"); else if (players.Count > 1) - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); else { int type = 167; @@ -5273,7 +5273,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); } else { @@ -5304,7 +5304,7 @@ namespace TShockAPI } else if (npcs.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, npcs.Select(n => $"{n.FullName}({n.type})")); + args.Player.SendMultipleMatchError(npcs.Select(n => $"{n.FullName}({n.type})")); return; } else @@ -5359,7 +5359,7 @@ namespace TShockAPI } else if (matchedItems.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, matchedItems.Select(i => $"{i.Name}({i.netID})")); + args.Player.SendMultipleMatchError(matchedItems.Select(i => $"{i.Name}({i.netID})")); return; } else @@ -5389,7 +5389,7 @@ namespace TShockAPI if (prefixIds.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, prefixIds.Select(p => p.ToString())); + args.Player.SendMultipleMatchError(prefixIds.Select(p => p.ToString())); return; } else if (prefixIds.Count == 0) @@ -5442,7 +5442,7 @@ namespace TShockAPI } else if (npcs.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, npcs.Select(n => $"{n.FullName}({n.type})")); + args.Player.SendMultipleMatchError(npcs.Select(n => $"{n.FullName}({n.type})")); return; } else if (args.Parameters[1].Length > 200) @@ -5507,7 +5507,7 @@ namespace TShockAPI } else if (items.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, items.Select(i => $"{i.Name}({i.netID})")); + args.Player.SendMultipleMatchError(items.Select(i => $"{i.Name}({i.netID})")); } else { @@ -5538,7 +5538,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); } else { @@ -5585,7 +5585,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); return; } else @@ -5634,7 +5634,7 @@ namespace TShockAPI } else if (found.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, found.Select(f => Lang.GetBuffName(f))); + args.Player.SendMultipleMatchError(found.Select(f => Lang.GetBuffName(f))); return; } id = found[0]; @@ -5670,7 +5670,7 @@ namespace TShockAPI } else if (foundplr.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, foundplr.Select(p => p.Name)); + args.Player.SendMultipleMatchError(foundplr.Select(p => p.Name)); return; } else @@ -5685,7 +5685,7 @@ namespace TShockAPI } else if (found.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, found.Select(b => Lang.GetBuffName(b))); + args.Player.SendMultipleMatchError(found.Select(b => Lang.GetBuffName(b))); return; } id = found[0]; @@ -5802,7 +5802,7 @@ namespace TShockAPI } else if (players.Count > 1) { - TShock.Utils.SendMultipleMatchError(args.Player, players.Select(p => p.Name)); + args.Player.SendMultipleMatchError(players.Select(p => p.Name)); return; } else diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index d38066ce..24578de6 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -34,6 +34,7 @@ using TShockAPI.DB; using TShockAPI.Hooks; using TShockAPI.Net; using Timer = System.Timers.Timer; +using System.Linq; namespace TShockAPI { @@ -1600,6 +1601,21 @@ namespace TShockAPI return false; } + /// + /// Sends the player an error message stating that more than one match was found + /// appending a csv list of the matches. + /// + /// An enumerable list with the matches + public void SendMultipleMatchError(IEnumerable matches) + { + SendErrorMessage("More than one match found: "); + + var lines = PaginationTools.BuildLinesFromTerms(matches.ToArray()); + lines.ForEach(SendInfoMessage); + + SendErrorMessage("Use \"my query\" for items with spaces."); + } + [Conditional("DEBUG")] private void LogStackFrame() { diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 2c95af15..b8f084bf 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -593,23 +593,6 @@ namespace TShockAPI return ""; } - /// - /// Sends the player an error message stating that more than one match was found - /// appending a csv list of the matches. - /// - /// Player to send the message to - /// An enumerable list with the matches - public void SendMultipleMatchError(TSPlayer ply, IEnumerable matches) - { - ply.SendErrorMessage("More than one match found: "); - - var lines = PaginationTools.BuildLinesFromTerms(matches.ToArray()); - - lines.ForEach(ply.SendInfoMessage); - - ply.SendErrorMessage("Use \"my query\" for items with spaces."); - } - /// /// Checks if world has hit the max number of chests /// From f06d1fd23874744d068d9b50e0c8a7e77669f505 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:26:51 -0700 Subject: [PATCH 15/26] Remove Utils.GetPlayers(). This is a public method that only has two uses in TShock and both of them are listing players to a player. A foreach isn't rocket science and this method was originally created just because there was no good object to iterate on (e.g., a TSPlayer array). --- CHANGELOG.md | 1 + TShockAPI/Commands.cs | 20 +++++++++++++++++++- TShockAPI/Utils.cs | 39 +++++++++++---------------------------- 3 files changed, 31 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3320a56c..04b5b844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Removed `Utils.GetPlayerIP()`. (@hakusaro) * Moved `Utils.Ban()` to `TSPlayer.Ban()`. (@hakusaro) * Moved `Utils.SendMultipleMatchError()` to `TSPlayer.SendMultipleMatchError`. (@hakusaro) +* Removed `Utils.GetPlayers()`. Iterate over the TSPlayers on the server and make your own list. ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index edbdcd83..d7cdcf96 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4820,8 +4820,26 @@ namespace TShockAPI } args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Players.Length, TShock.Config.MaxSlots); + + var players = new List(); + + foreach (TSPlayer ply in TShock.Players) + { + if (ply != null && ply.Active) + { + if (displayIdsRequested) + { + players.Add(String.Format("{0} (ID: {1}{2})", ply.Name, ply.Index, ply.Account != null ? ", ID: " + ply.Account.ID : "")); + } + else + { + players.Add(ply.Name); + } + } + } + PaginationTools.SendPage( - args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(TShock.Utils.GetPlayers(displayIdsRequested)), + args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(players), new PaginationTools.Settings { IncludeHeader = false, diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index b8f084bf..c6cfc7c4 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -69,33 +69,6 @@ namespace TShockAPI return mess.Split(':')[0]; } - /// - /// Returns a list of current players on the server - /// - /// bool includeIDs - whether or not the string of each player name should include ID data - /// List of strings with names - public List GetPlayers(bool includeIDs) - { - var players = new List(); - - foreach (TSPlayer ply in TShock.Players) - { - if (ply != null && ply.Active) - { - if (includeIDs) - { - players.Add(String.Format("{0} (IX: {1}{2})", ply.Name, ply.Index, ply.Account != null ? ", ID: " + ply.Account.ID : "")); - } - else - { - players.Add(ply.Name); - } - } - } - - return players; - } - /// /// It's a clamp function /// @@ -549,8 +522,18 @@ namespace TShockAPI continue; } + var players = new List(); + + foreach (TSPlayer ply in TShock.Players) + { + if (ply != null && ply.Active) + { + players.Add(ply.Name); + } + } + foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)); - foo = foo.Replace("%players%", String.Join(",", GetPlayers(false))); + foo = foo.Replace("%players%", String.Join(",", players)); player.SendMessage(foo, lineColor); } From 17d151b8f82f0ec7fceefb5084796f482b97d4a3 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:32:45 -0700 Subject: [PATCH 16/26] Move HasBanExpired to Bans.RemoveBanIfExpired(). The ban system needs a full rewrite anyway, but this move removes something from Utils, puts it closer to its operating point, simplifies the method, and clarifies what it actually does. --- CHANGELOG.md | 1 + TShockAPI/DB/BanManager.cs | 14 ++++++++++++++ TShockAPI/TShock.cs | 2 +- TShockAPI/Utils.cs | 23 ----------------------- 4 files changed, 16 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04b5b844..b51cfcdd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Moved `Utils.Ban()` to `TSPlayer.Ban()`. (@hakusaro) * Moved `Utils.SendMultipleMatchError()` to `TSPlayer.SendMultipleMatchError`. (@hakusaro) * Removed `Utils.GetPlayers()`. Iterate over the TSPlayers on the server and make your own list. +* Removed `Utils.HasBanExpired()` and replaced with `Bans.RemoveBanIfExpired()`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/DB/BanManager.cs b/TShockAPI/DB/BanManager.cs index 53a258d2..c83ff7e0 100644 --- a/TShockAPI/DB/BanManager.cs +++ b/TShockAPI/DB/BanManager.cs @@ -294,6 +294,20 @@ namespace TShockAPI.DB } return false; } + + /// Removes a ban if it has expired. + /// The candidate ban to check. + /// If the ban has been removed. + public bool RemoveBanIfExpired(Ban ban) + { + if (!string.IsNullOrWhiteSpace(ban.Expiration) && (ban.ExpirationDateTime != null) && (DateTime.UtcNow >= ban.ExpirationDateTime)) + { + RemoveBan(ban.IP, false, false, false); + return true; + } + + return false; + } } /// diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index a4ac0c5e..9549b6fb 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1310,7 +1310,7 @@ namespace TShockAPI if (ban != null) { - if (!Utils.HasBanExpired(ban)) + if (!Bans.RemoveBanIfExpired(ban)) { DateTime exp; if (!DateTime.TryParse(ban.Expiration, out exp)) diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index c6cfc7c4..778d211c 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -479,29 +479,6 @@ namespace TShockAPI TShock.TileBans.UpdateBans(); } - /// HasBanExpired - Returns whether or not a ban has expired or not. - /// ban - The ban object to check. - /// byName - Defines whether or not the ban should be checked by name. - /// bool - True if the ban has expired. - public bool HasBanExpired(Ban ban, bool byName = false) - { - if (!string.IsNullOrWhiteSpace(ban.Expiration) && (ban.ExpirationDateTime != null) && (DateTime.UtcNow >= ban.ExpirationDateTime)) - { - if (byName) - { - TShock.Bans.RemoveBan(ban.Name, true, true, false); - } - else - { - TShock.Bans.RemoveBan(ban.IP, false, false, false); - } - - return true; - } - - return false; - } - /// /// Shows a file to the user. /// From 3ac52091ea2cef67b8a410d817232093c584473f Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:40:44 -0700 Subject: [PATCH 17/26] Move SendFileToUser to TSP.SendFileTextAsMessage. This is not a great method, but it's actually the only method in TShock that interpolates the %map% and %players% variables and it used in at least three places in the codebase. Since it's already so specialized, it's not worth changing it to take an actual File object, in my humble opinion. This also clarifies what the method does and what makes it special, as opposed to being fairly generic. --- CHANGELOG.md | 1 + TShockAPI/Commands.cs | 4 ++-- TShockAPI/TSPlayer.cs | 37 +++++++++++++++++++++++++++++++++++++ TShockAPI/TShock.cs | 2 +- TShockAPI/Utils.cs | 38 -------------------------------------- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b51cfcdd..5fdfb994 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -76,6 +76,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Moved `Utils.SendMultipleMatchError()` to `TSPlayer.SendMultipleMatchError`. (@hakusaro) * Removed `Utils.GetPlayers()`. Iterate over the TSPlayers on the server and make your own list. * Removed `Utils.HasBanExpired()` and replaced with `Bans.RemoveBanIfExpired()`. (@hakusaro) +* Removed `Utils.SendFileToUser()` and replaced with `TSPlayer.SendFileTextAsMessage()`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index d7cdcf96..e6b7feca 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4979,12 +4979,12 @@ namespace TShockAPI private static void Motd(CommandArgs args) { - TShock.Utils.ShowFileToUser(args.Player, FileTools.MotdPath); + args.Player.SendFileTextAsMessage(FileTools.MotdPath); } private static void Rules(CommandArgs args) { - TShock.Utils.ShowFileToUser(args.Player, FileTools.RulesPath); + args.Player.SendFileTextAsMessage(FileTools.RulesPath); } private static void Whisper(CommandArgs args) diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 24578de6..c71141e7 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -1458,6 +1458,43 @@ namespace TShockAPI SendDataFromPlayer(PacketTypes.SmartTextMessage, ply, msg, red, green, blue, -1); } + /// + /// Sends the text of a given file to the player. Replacement of %map% and %players% if in the file. + /// + /// Filename relative to + public void SendFileTextAsMessage(string file) + { + string foo = ""; + bool containsOldFormat = false; + using (var tr = new StreamReader(file)) + { + Color lineColor; + while ((foo = tr.ReadLine()) != null) + { + lineColor = Color.White; + if (string.IsNullOrWhiteSpace(foo)) + { + continue; + } + + var players = new List(); + + foreach (TSPlayer ply in TShock.Players) + { + if (ply != null && ply.Active) + { + players.Add(ply.Name); + } + } + + foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)); + foo = foo.Replace("%players%", String.Join(",", players)); + + SendMessage(foo, lineColor); + } + } + } + /// /// Wounds the player with the given damage. /// diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 9549b6fb..26594077 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1637,7 +1637,7 @@ namespace TShockAPI if (Config.DisplayIPToAdmins) Utils.SendLogs(string.Format("{0} has joined. IP: {1}", player.Name, player.IP), Color.Blue); - Utils.ShowFileToUser(player, FileTools.MotdPath); + player.SendFileTextAsMessage(FileTools.MotdPath); string pvpMode = Config.PvPMode.ToLowerInvariant(); if (pvpMode == "always") diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 778d211c..2907abb8 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -479,44 +479,6 @@ namespace TShockAPI TShock.TileBans.UpdateBans(); } - /// - /// Shows a file to the user. - /// - /// Player the file contents will be sent to - /// Filename relative to - public void ShowFileToUser(TSPlayer player, string file) - { - string foo = ""; - bool containsOldFormat = false; - using (var tr = new StreamReader(file)) - { - Color lineColor; - while ((foo = tr.ReadLine()) != null) - { - lineColor = Color.White; - if (string.IsNullOrWhiteSpace(foo)) - { - continue; - } - - var players = new List(); - - foreach (TSPlayer ply in TShock.Players) - { - if (ply != null && ply.Active) - { - players.Add(ply.Name); - } - } - - foo = foo.Replace("%map%", (TShock.Config.UseServerName ? TShock.Config.ServerName : Main.worldName)); - foo = foo.Replace("%players%", String.Join(",", players)); - - player.SendMessage(foo, lineColor); - } - } - } - /// /// Returns a Group from the name of the group /// From 332e6b2518fcab6fd1502434942170181325c4be Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:45:29 -0700 Subject: [PATCH 18/26] Remove Utils.GetGroup(). This method duplicates Groups.GetGroupByName(). --- CHANGELOG.md | 1 + TShockAPI/Commands.cs | 6 +++--- TShockAPI/DB/GroupManager.cs | 6 +++--- TShockAPI/GetDataHandlers.cs | 4 ++-- TShockAPI/Rest/RestManager.cs | 2 +- TShockAPI/Rest/SecureRest.cs | 2 +- TShockAPI/Utils.cs | 17 ----------------- 7 files changed, 11 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fdfb994..f428630c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Removed `Utils.GetPlayers()`. Iterate over the TSPlayers on the server and make your own list. * Removed `Utils.HasBanExpired()` and replaced with `Bans.RemoveBanIfExpired()`. (@hakusaro) * Removed `Utils.SendFileToUser()` and replaced with `TSPlayer.SendFileTextAsMessage()`. (@hakusaro) +* Removed `Utils.GetGroup()` also have you seen `Groups.GetGroupByName()`? (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index e6b7feca..2faa71bc 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -842,7 +842,7 @@ namespace TShockAPI { args.Player.PlayerData = TShock.CharacterDB.GetPlayerData(args.Player, account.ID); - var group = TShock.Utils.GetGroup(account.Group); + var group = TShock.Groups.GetGroupByName(account.Group); args.Player.Group = group; args.Player.tempGroup = null; @@ -1810,7 +1810,7 @@ namespace TShockAPI ply[0].tempGroupTimer.Start(); } - Group g = TShock.Utils.GetGroup(args.Parameters[1]); + Group g = TShock.Groups.GetGroupByName(args.Parameters[1]); ply[0].tempGroup = g; @@ -3225,7 +3225,7 @@ namespace TShockAPI args.Player.SendErrorMessage("Invalid group."); return; } - Group grp = TShock.Utils.GetGroup(args.Parameters[1]); + Group grp = TShock.Groups.GetGroupByName(args.Parameters[1]); List permissions = grp.TotalPermissions; PaginationTools.SendPage(args.Player, pageNumber, PaginationTools.BuildLinesFromTerms(permissions), diff --git a/TShockAPI/DB/GroupManager.cs b/TShockAPI/DB/GroupManager.cs index 11f1cc06..9b399dd1 100644 --- a/TShockAPI/DB/GroupManager.cs +++ b/TShockAPI/DB/GroupManager.cs @@ -434,7 +434,7 @@ namespace TShockAPI.DB if (database.Query("DELETE FROM GroupList WHERE GroupName=@0", name) == 1) { - groups.Remove(TShock.Utils.GetGroup(name)); + groups.Remove(TShock.Groups.GetGroupByName(name)); return "Group " + name + " has been deleted successfully."; } @@ -454,7 +454,7 @@ namespace TShockAPI.DB if (!GroupExists(name)) return "Error: Group doesn't exist."; - var group = TShock.Utils.GetGroup(name); + var group = TShock.Groups.GetGroupByName(name); var oldperms = group.Permissions; // Store old permissions in case of error permissions.ForEach(p => group.AddPermission(p)); @@ -477,7 +477,7 @@ namespace TShockAPI.DB if (!GroupExists(name)) return "Error: Group doesn't exist."; - var group = TShock.Utils.GetGroup(name); + var group = TShock.Groups.GetGroupByName(name); var oldperms = group.Permissions; // Store old permissions in case of error permissions.ForEach(p => group.RemovePermission(p)); diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 9de92643..5150e598 100644 --- a/TShockAPI/GetDataHandlers.cs +++ b/TShockAPI/GetDataHandlers.cs @@ -1725,7 +1725,7 @@ namespace TShockAPI args.Player.PlayerData = TShock.CharacterDB.GetPlayerData(args.Player, account.ID); - var group = TShock.Utils.GetGroup(account.Group); + var group = TShock.Groups.GetGroupByName(account.Group); args.Player.Group = group; args.Player.tempGroup = null; @@ -1797,7 +1797,7 @@ namespace TShockAPI args.Player.State = 2; NetMessage.SendData((int)PacketTypes.WorldInfo, args.Player.Index); - var group = TShock.Utils.GetGroup(account.Group); + var group = TShock.Groups.GetGroupByName(account.Group); args.Player.Group = group; args.Player.tempGroup = null; diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index 5174b560..073d7fb8 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -411,7 +411,7 @@ namespace TShockAPI var players = new ArrayList(); foreach (TSPlayer tsPlayer in TShock.Players.Where(p => null != p)) { - var p = PlayerFilter(tsPlayer, args.Parameters, ((args.TokenData.UserGroupName) != "" && TShock.Utils.GetGroup(args.TokenData.UserGroupName).HasPermission(RestPermissions.viewips))); + var p = PlayerFilter(tsPlayer, args.Parameters, ((args.TokenData.UserGroupName) != "" && TShock.Groups.GetGroupByName(args.TokenData.UserGroupName).HasPermission(RestPermissions.viewips))); if (null != p) players.Add(p); } diff --git a/TShockAPI/Rest/SecureRest.cs b/TShockAPI/Rest/SecureRest.cs index 4000efab..fb6a395d 100644 --- a/TShockAPI/Rest/SecureRest.cs +++ b/TShockAPI/Rest/SecureRest.cs @@ -144,7 +144,7 @@ namespace Rests return new RestObject("403") { Error = "Username or password may be incorrect or this account may not have sufficient privileges." }; } - Group userGroup = TShock.Utils.GetGroup(userAccount.Group); + Group userGroup = TShock.Groups.GetGroupByName(userAccount.Group); if (!userGroup.HasPermission(RestPermissions.restapi) && userAccount.Group != "superadmin") { AddTokenToBucket(context.RemoteEndPoint.Address.ToString()); diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 2907abb8..6c2f427d 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -479,23 +479,6 @@ namespace TShockAPI TShock.TileBans.UpdateBans(); } - /// - /// Returns a Group from the name of the group - /// - /// string groupName - public Group GetGroup(string groupName) - { - //first attempt on cached groups - for (int i = 0; i < TShock.Groups.groups.Count; i++) - { - if (TShock.Groups.groups[i].Name.Equals(groupName)) - { - return TShock.Groups.groups[i]; - } - } - return Group.DefaultGroup; - } - /// /// Returns an IPv4 address from a DNS query /// From 4eebaf6c44b4824ce9d2e7eca680f4a846a4aeff Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Fri, 29 Dec 2017 08:50:13 -0700 Subject: [PATCH 19/26] Rename methods that were ambiguous in meaning. This renames some methods in the Utils class to better reflect what they do or what they provide back. It should be clearer what these methods are for now than before. --- CHANGELOG.md | 2 ++ TShockAPI/Bouncer.cs | 4 ++-- TShockAPI/FileTools.cs | 2 +- TShockAPI/Utils.cs | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f428630c..e9a0ab61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -78,6 +78,8 @@ This is the rolling changelog for TShock for Terraria. Use past tense when addin * Removed `Utils.HasBanExpired()` and replaced with `Bans.RemoveBanIfExpired()`. (@hakusaro) * Removed `Utils.SendFileToUser()` and replaced with `TSPlayer.SendFileTextAsMessage()`. (@hakusaro) * Removed `Utils.GetGroup()` also have you seen `Groups.GetGroupByName()`? (@hakusaro) +* `Utils.MaxChests()` is now `Utils.HasWorldReachedMaxChests()`. (@hakusaro) +* `Utils.GetIPv4Address()` is now `Utils.GetIPv4AddressFromHostname()`. (@hakusaro) ## TShock 4.3.25 * Fixed a critical exploit in the Terraria protocol that could cause massive unpreventable world corruption as well as a number of other problems. Thanks to @bartico6 for reporting. Fixed by the efforts of @QuiCM, @hakusaro, and tips in the right directioon from @bartico6. diff --git a/TShockAPI/Bouncer.cs b/TShockAPI/Bouncer.cs index d334143b..6605f1e5 100644 --- a/TShockAPI/Bouncer.cs +++ b/TShockAPI/Bouncer.cs @@ -627,7 +627,7 @@ namespace TShockAPI && Main.tile[tileX, tileY].type != TileID.Containers && Main.tile[tileX, tileY].type != TileID.Dressers && Main.tile[tileX, tileY].type != TileID.Containers2 - && (!TShock.Utils.MaxChests() && Main.tile[tileX, tileY].type != TileID.Dirt)) //Chest + && (!TShock.Utils.HasWorldReachedMaxChests() && Main.tile[tileX, tileY].type != TileID.Dirt)) //Chest { args.Player.SendTileSquare(tileX, tileY, 3); args.Handled = true; @@ -1409,7 +1409,7 @@ namespace TShockAPI } if (action == EditAction.PlaceTile && (editData == TileID.Containers || editData == TileID.Containers2)) { - if (TShock.Utils.MaxChests()) + if (TShock.Utils.HasWorldReachedMaxChests()) { args.Player.SendErrorMessage("The world's chest limit has been reached - unable to place more."); args.Player.SendTileSquare(tileX, tileY, 3); diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index 6938149c..2690ef14 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -154,7 +154,7 @@ namespace TShockAPI { if (string.IsNullOrWhiteSpace(line)) continue; - contains = TShock.Utils.GetIPv4Address(line).Equals(ip); + contains = TShock.Utils.GetIPv4AddressFromHostname(line).Equals(ip); if (contains) return true; } diff --git a/TShockAPI/Utils.cs b/TShockAPI/Utils.cs index 6c2f427d..fc550600 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -483,7 +483,7 @@ namespace TShockAPI /// Returns an IPv4 address from a DNS query /// /// string ip - public string GetIPv4Address(string hostname) + public string GetIPv4AddressFromHostname(string hostname) { try { @@ -502,7 +502,7 @@ namespace TShockAPI /// Checks if world has hit the max number of chests /// /// True if the entire chest array is used - public bool MaxChests() + public bool HasWorldReachedMaxChests() { for (int i = 0; i < Main.chest.Length; i++) { From 17cfdc3a16b45607804abb62dbc5946d2fe9f560 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 31 Dec 2017 01:13:03 -0700 Subject: [PATCH 20/26] Add TSPlayer.ActivePlayers --- CHANGELOG.md | 2 +- TShockAPI/TSPlayer.cs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e9a0ab61..a9dd9d55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,7 +68,7 @@ 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) +* Removed `Utils.ActivePlayers()` -- use `TSPlayer.ActivePlayers.Count` 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/TSPlayer.cs b/TShockAPI/TSPlayer.cs index c71141e7..2b51ddd0 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -74,6 +74,18 @@ namespace TShockAPI /// public static readonly TSPlayer All = new TSPlayer("All"); + /// + /// Gets the number of active players on the server. + /// + /// The number of active players on the server. + public static ICollection ActivePlayers + { + get + { + return TShock.Players.Where(p => null != p && p.TPlayer.active) as ICollection; + } + } + /// /// Finds a TSPlayer based on name or ID /// From 521283c36b6d00b40fe5e02d133f0e45f5ff20b3 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 31 Dec 2017 01:13:33 -0700 Subject: [PATCH 21/26] Migrate calls from Players.Length to ActivePlayers As pointed out by @QuiCM, TShock.Players is actually an array and not a smarter collection, so length will return the total collection size and not the active players. An earlier commit was added that gives TSPlayer an ICollection that contains only active players. This is now the basis of determining the number of active players on the server. --- TShockAPI/Commands.cs | 2 +- TShockAPI/StatTracker.cs | 2 +- TShockAPI/TShock.cs | 4 ++-- TShockAPI/Utils.cs | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 2faa71bc..92264b3b 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})", TSPlayer.ActivePlayers.Count, TShock.Config.MaxSlots); var players = new List(); diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs index 0d1d2b6c..1063d70d 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 = TSPlayer.ActivePlayers.Count, maxPlayers = TShock.Config.MaxSlots, systemRam = GetTotalSystemRam(ServerApi.RunningMono), version = TShock.VersionNum.ToString(), diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 26594077..cb617166 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -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, TSPlayer.ActivePlayers.Count, 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, TSPlayer.ActivePlayers.Count, 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..c8012c74 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -1108,7 +1108,7 @@ namespace TShockAPI } else { - invasionSize = 100 + (TShock.Config.InvasionMultiplier * TShock.Players.Length); + invasionSize = 100 + (TShock.Config.InvasionMultiplier * TSPlayer.ActivePlayers.Count); } // Order matters @@ -1147,7 +1147,7 @@ namespace TShockAPI { 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 : TSPlayer.ActivePlayers.Count, TShock.Config.MaxSlots, Main.worldName, Netplay.ServerIP.ToString(), Netplay.ListenPort, TShock.VersionNum); } From 43ddb73112b28489505c508be4a92ba80fdc3cab Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 31 Dec 2017 01:24:54 -0700 Subject: [PATCH 22/26] Revert "Migrate calls from Players.Length to ActivePlayers" This reverts commit 521283c36b6d00b40fe5e02d133f0e45f5ff20b3. --- TShockAPI/Commands.cs | 2 +- TShockAPI/StatTracker.cs | 2 +- TShockAPI/TShock.cs | 4 ++-- TShockAPI/Utils.cs | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 92264b3b..2faa71bc 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4819,7 +4819,7 @@ namespace TShockAPI return; } - args.Player.SendSuccessMessage("Online Players ({0}/{1})", TSPlayer.ActivePlayers.Count, TShock.Config.MaxSlots); + args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Players.Length, TShock.Config.MaxSlots); var players = new List(); diff --git a/TShockAPI/StatTracker.cs b/TShockAPI/StatTracker.cs index 1063d70d..0d1d2b6c 100644 --- a/TShockAPI/StatTracker.cs +++ b/TShockAPI/StatTracker.cs @@ -144,7 +144,7 @@ namespace TShockAPI return new JsonData() { port = Terraria.Netplay.ListenPort, - currentPlayers = TSPlayer.ActivePlayers.Count, + currentPlayers = TShock.Players.Length, maxPlayers = TShock.Config.MaxSlots, systemRam = GetTotalSystemRam(ServerApi.RunningMono), version = TShock.VersionNum.ToString(), diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index cb617166..26594077 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -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, TSPlayer.ActivePlayers.Count, + player.Group.Name, player.Country, TShock.Players.Length, 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, TSPlayer.ActivePlayers.Count, TShock.Config.MaxSlots); + player.Group.Name, TShock.Players.Length, 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 c8012c74..fc550600 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -1108,7 +1108,7 @@ namespace TShockAPI } else { - invasionSize = 100 + (TShock.Config.InvasionMultiplier * TSPlayer.ActivePlayers.Count); + invasionSize = 100 + (TShock.Config.InvasionMultiplier * TShock.Players.Length); } // Order matters @@ -1147,7 +1147,7 @@ namespace TShockAPI { 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 : TSPlayer.ActivePlayers.Count, + empty ? 0 : TShock.Players.Length, TShock.Config.MaxSlots, Main.worldName, Netplay.ServerIP.ToString(), Netplay.ListenPort, TShock.VersionNum); } From cb2ed840387eed3d3afdbcd8fa98fb08a132b3eb Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 31 Dec 2017 01:25:05 -0700 Subject: [PATCH 23/26] Revert "Add TSPlayer.ActivePlayers" This reverts commit 17cfdc3a16b45607804abb62dbc5946d2fe9f560. --- CHANGELOG.md | 2 +- TShockAPI/TSPlayer.cs | 12 ------------ 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9dd9d55..e9a0ab61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,7 +68,7 @@ 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 `TSPlayer.ActivePlayers.Count` instead. (@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/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 2b51ddd0..c71141e7 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -74,18 +74,6 @@ namespace TShockAPI /// public static readonly TSPlayer All = new TSPlayer("All"); - /// - /// Gets the number of active players on the server. - /// - /// The number of active players on the server. - public static ICollection ActivePlayers - { - get - { - return TShock.Players.Where(p => null != p && p.TPlayer.active) as ICollection; - } - } - /// /// Finds a TSPlayer based on name or ID /// From a5cad284a8a9327b144828dcf1c81c7540897008 Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 31 Dec 2017 01:28:21 -0700 Subject: [PATCH 24/26] Revert "Remove Utils.ActivePlayers" This reverts commit b613fdcda64d93e08c1175c1183cbf27bc18f730. --- CHANGELOG.md | 1 - TShockAPI/Commands.cs | 2 +- TShockAPI/GetDataHandlers.cs | 2 +- TShockAPI/StatTracker.cs | 2 +- TShockAPI/TShock.cs | 8 ++++---- TShockAPI/Utils.cs | 15 ++++++++++++--- 6 files changed, 19 insertions(+), 11 deletions(-) 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); } From df4da817b64c76139b16318b0fd647a84e66e20c Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 31 Dec 2017 01:30:13 -0700 Subject: [PATCH 25/26] Rename Utils.ActivePlayers to something better. Issues related to ICollections being null led to reverting migrating this to TSPlayer until a better solution is uncovered. --- TShockAPI/Commands.cs | 2 +- TShockAPI/GetDataHandlers.cs | 2 +- TShockAPI/StatTracker.cs | 2 +- TShockAPI/TShock.cs | 8 ++++---- TShockAPI/Utils.cs | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 4f9bf7d9..36dabc6b 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -4819,7 +4819,7 @@ namespace TShockAPI return; } - args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Utils.ActivePlayers(), TShock.Config.MaxSlots); + args.Player.SendSuccessMessage("Online Players ({0}/{1})", TShock.Utils.GetActivePlayerCount(), TShock.Config.MaxSlots); var players = new List(); diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs index 3524f733..f4cd0a9e 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.Utils.ActivePlayers() + 1 > TShock.Config.MaxSlots && + if (TShock.Utils.GetActivePlayerCount() + 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 1151d170..807c530c 100644 --- a/TShockAPI/StatTracker.cs +++ b/TShockAPI/StatTracker.cs @@ -144,7 +144,7 @@ namespace TShockAPI return new JsonData() { port = Terraria.Netplay.ListenPort, - currentPlayers = TShock.Utils.ActivePlayers(), + currentPlayers = TShock.Utils.GetActivePlayerCount(), maxPlayers = TShock.Config.MaxSlots, systemRam = GetTotalSystemRam(ServerApi.RunningMono), version = TShock.VersionNum.ToString(), diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 56bc45a8..5a795972 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -1241,7 +1241,7 @@ namespace TShockAPI var player = new TSPlayer(args.Who); - if (Utils.ActivePlayers() + 1 > Config.MaxSlots + Config.ReservedSlots) + if (Utils.GetActivePlayerCount() + 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 (Utils.ActivePlayers() == 1) + if (Utils.GetActivePlayerCount() == 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.Utils.ActivePlayers(), + player.Group.Name, player.Country, TShock.Utils.GetActivePlayerCount(), 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.Utils.ActivePlayers(), TShock.Config.MaxSlots); + player.Group.Name, TShock.Utils.GetActivePlayerCount(), 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 f3c35591..09e7d542 100644 --- a/TShockAPI/Utils.cs +++ b/TShockAPI/Utils.cs @@ -153,7 +153,7 @@ namespace TShockAPI /// Gets the number of active players on the server. /// /// The number of active players on the server. - public int ActivePlayers() + public int GetActivePlayerCount() { return Main.player.Where(p => null != p && p.active).Count(); } @@ -1117,7 +1117,7 @@ namespace TShockAPI } else { - invasionSize = 100 + (TShock.Config.InvasionMultiplier * ActivePlayers()); + invasionSize = 100 + (TShock.Config.InvasionMultiplier * GetActivePlayerCount()); } // Order matters @@ -1151,12 +1151,12 @@ namespace TShockAPI } /// Updates the console title with some pertinent information. - /// If the server is empty; determines if we should use Utils.ActivePlayers() for player count or 0. + /// If the server is empty; determines if we should use Utils.GetActivePlayerCount() 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 : ActivePlayers(), + empty ? 0 : GetActivePlayerCount(), TShock.Config.MaxSlots, Main.worldName, Netplay.ServerIP.ToString(), Netplay.ListenPort, TShock.VersionNum); } From 2846ad915f40f28628e1b84cd08f78237b069bea Mon Sep 17 00:00:00 2001 From: Lucas Nicodemus Date: Sun, 31 Dec 2017 01:41:16 -0700 Subject: [PATCH 26/26] Change default motd format to use new colors Since we just removed the old color format, we probably shouldn't ship the old motd that still uses legacy colors to people. --- TShockAPI/FileTools.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index 2690ef14..3059a3af 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -26,7 +26,7 @@ namespace TShockAPI public class FileTools { private const string MotdFormat = - "This server is running TShock for Terraria.\n Type /help for a list of commands.\n%255,000,000%Current map: %map%\nCurrent players: %players%"; + "This is [c/FF0000:%map%] on [c/00FFFF:TShock for Terraria].\n[c/00FF00:Current players:] [c/FFFF00:%players%]\nType [c/FF0000:/help] for a list of commands.\n"; /// /// Path to the file containing the rules. ///