From a976c511019d91ae2a3e0e428ade18e73ddeaab2 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 01:04:28 +1000 Subject: [PATCH] ForceKick now takes in TSPlayer instead of int Removed Tools.GetPlayerIP, use TSPlayer.IP instead --- TShockAPI/TSPlayer.cs | 29 +++++++++++++++---------- TShockAPI/TShock.cs | 49 +++++++++++++++++++++++-------------------- TShockAPI/Tools.cs | 26 ++++++++--------------- 3 files changed, 53 insertions(+), 51 deletions(-) diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 5dd09524..3bfc2066 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -25,15 +25,14 @@ namespace TShockAPI public class TSPlayer { public static readonly TSPlayer Server = new ServerPlayer(); - public static readonly TSPlayer All = new TSPlayer(new Player { name = "All", whoAmi = -1 }); + public static readonly TSPlayer All = new TSPlayer("All"); public uint TileThreshold { get; set; } public Dictionary TilesDestroyed { get; set; } public bool SyncHP { get; set; } public bool SyncMP { get; set; } public Group Group { get; set; } public bool ReceivedInfo { get; set; } - - public int Index { get { return TPlayer.whoAmi; } } + public int Index { get; protected set; } /// /// Terraria Player @@ -44,14 +43,14 @@ namespace TShockAPI { get { return TPlayer.name; } } + public string IP + { + get { return Tools.GetRealIP(Netplay.serverSock[Index].tcpClient.Client.RemoteEndPoint.ToString()); } + } public bool Active { get { return TPlayer.active; } } - public string IP - { - get { return Tools.GetRealIP(Netplay.serverSock[Index].tcpClient.Client.RemoteEndPoint.ToString()); } - } public int Team { get { return TPlayer.team; } @@ -74,10 +73,19 @@ namespace TShockAPI get { return (int)(X / 16); } } - public TSPlayer(Player ply) + public TSPlayer(int index) { TilesDestroyed = new Dictionary(); - TPlayer = ply; + Index = index; + TPlayer = Main.player[index]; + Group = new Group("null"); + } + + protected TSPlayer(String playerName) + { + TilesDestroyed = new Dictionary(); + Index = -1; + TPlayer = new Player { name = playerName, whoAmi = -1 }; Group = new Group("null"); } @@ -101,8 +109,7 @@ namespace TShockAPI public class ServerPlayer : TSPlayer { - public ServerPlayer() - : base(new Player { name = "Server" }) + public ServerPlayer() : base("Server") { Group = new SuperAdminGroup(); } diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 203ed518..078de510 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -313,41 +313,42 @@ namespace TShockAPI bool HandlePlayerInfo(MemoryStream data, GetDataEventArgs e) { - var ban = Bans.GetBanByName(Main.player[e.Msg.whoAmI].name); + var player = Players[e.Msg.whoAmI]; + if (player == null) + { + Tools.ForceKick(new TSPlayer(e.Msg.whoAmI), "Player doesn't exist"); + return true; + } + var ban = Bans.GetBanByName(player.Name); if (ban != null) { - Tools.ForceKick(e.Msg.whoAmI, string.Format("You are banned: {0}", ban.Reason)); + Tools.ForceKick(player, string.Format("You are banned: {0}", ban.Reason)); return true; } byte hair = e.Msg.readBuffer[e.Index + 1]; if (hair > 0x10) { - Tools.ForceKick(e.Msg.whoAmI, "Hair crash exploit."); + Tools.ForceKick(player, "Hair crash exploit."); return true; } string name = Encoding.ASCII.GetString(e.Msg.readBuffer, e.Index + 23, (e.Length - (e.Index + 23)) + e.Index - 1); if (name.Length > 32) { - Tools.ForceKick(e.Msg.whoAmI, "Name exceeded 32 characters."); + Tools.ForceKick(player, "Name exceeded 32 characters."); return true; } if (name.Trim().Length == 0) { - Tools.ForceKick(e.Msg.whoAmI, "Empty Name."); + Tools.ForceKick(player, "Empty Name."); return true; } - if (Players[e.Msg.whoAmI] == null) - { - Tools.ForceKick(e.Msg.whoAmI, "Player doesn't exist"); - return true; - } - if (Players[e.Msg.whoAmI].ReceivedInfo) + if (player.ReceivedInfo) { return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Sent client info more than once"); } - Players[e.Msg.whoAmI].ReceivedInfo = true; + player.ReceivedInfo = true; return false; } @@ -624,7 +625,7 @@ namespace TShockAPI tilex, tiley, Main.tile[tilex, tiley].type )); - Tools.ForceKick(e.Msg.whoAmI, string.Format("Tile Kill abuse ({0})", Main.tile[tilex, tiley].type)); + Tools.ForceKick(Players[e.Msg.whoAmI], string.Format("Tile Kill abuse ({0})", Main.tile[tilex, tiley].type)); return true; } return false; @@ -636,7 +637,7 @@ namespace TShockAPI return; TSPlayer player = Players[who]; - Log.Info(string.Format("{0} ({1}) from '{2}' group joined.", player.Name, Tools.GetPlayerIP(who), player.Group.Name)); + Log.Info(string.Format("{0} ({1}) from '{2}' group joined.", player.Name, player.IP, player.Group.Name)); Tools.ShowFileToUser(player, "motd.txt"); if (HackedHealth(who)) @@ -728,31 +729,31 @@ namespace TShockAPI return; } - string ip = Tools.GetPlayerIP(ply); - Players[ply] = new TSPlayer(Main.player[ply]); - Players[ply].Group = Tools.GetGroupForIP(ip); + var player = new TSPlayer(ply); + player.Group = Tools.GetGroupForIP(player.IP); if (Tools.ActivePlayers() + 1 > ConfigurationManager.MaxSlots && - !Players[ply].Group.HasPermission("reservedslot")) + !player.Group.HasPermission("reservedslot")) { - Tools.ForceKick(ply, "Server is full"); + Tools.ForceKick(player, "Server is full"); handler.Handled = true; } else { - var ban = Bans.GetBanByIp(ip); + var ban = Bans.GetBanByIp(player.IP); if (ban != null) { - Tools.ForceKick(ply, string.Format("You are banned: {0}", ban.Reason)); + Tools.ForceKick(player, string.Format("You are banned: {0}", ban.Reason)); handler.Handled = true; } - else if (!FileTools.OnWhitelist(ip)) + else if (!FileTools.OnWhitelist(player.IP)) { - Tools.ForceKick(ply, "Not on whitelist."); + Tools.ForceKick(player, "Not on whitelist."); handler.Handled = true; } } + Players[ply] = player; Netplay.serverSock[ply].spamCheck = ConfigurationManager.SpamChecks; } @@ -764,6 +765,8 @@ namespace TShockAPI var tsplr = Players[ply]; if (tsplr != null && tsplr.ReceivedInfo) Log.Info(string.Format("{0} left.", tsplr.Name)); + + Players[ply] = null; } private void OnPostInit() diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index a3d8d613..3d01ec6f 100755 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -45,16 +45,6 @@ namespace TShockAPI return mess.Split(':')[0]; } - /// - /// Gets the IP from a player index - /// - /// Player Index - /// IP - public static string GetPlayerIP(int plr) - { - return GetRealIP(Netplay.serverSock[plr].tcpClient.Client.RemoteEndPoint.ToString()); - } - /// /// Used for some places where a list of players might be used. /// @@ -206,9 +196,9 @@ namespace TShockAPI /// string reason public static void ForceKickAll(string reason) { - for (int player = 0; player < Main.maxPlayers; player++) + foreach(TSPlayer player in TShock.Players) { - if (Main.player[player].active) + if (player != null && player.TPlayer.active) { Tools.ForceKick(player, reason); } @@ -220,11 +210,13 @@ namespace TShockAPI /// /// int player /// string reason - public static void ForceKick(int ply, string reason) + public static void ForceKick(TSPlayer player, string reason) { - string ip = GetPlayerIP(ply); - NetMessage.SendData(0x2, ply, -1, reason, 0x0, 0f, 0f, 0f); - Log.Info(string.Format("{0} was force kicked for : {1}", ip, reason)); + Log.Info("ForceKick Pre : " + player.Index + " status " + Main.player[player.Index].active + " " + player.TPlayer.active + " " + Main.player[player.Index].name + " " + player.TPlayer.name); + if (!Netplay.serverSock[player.Index].active || Netplay.serverSock[player.Index].kill) + return; + NetMessage.SendData(0x2, player.Index, -1, reason, 0x0, 0f, 0f, 0f); + Log.Info(string.Format("{0} was force kicked for : {1}", player.IP, reason)); } /// @@ -261,7 +253,7 @@ namespace TShockAPI return true; if (!player.Group.HasPermission("immunetoban")) { - string ip = GetPlayerIP(player.Index); + string ip = player.IP; string playerName = player.Name; TShock.Bans.AddBan(ip, playerName, reason); NetMessage.SendData(0x2, player.Index, -1, string.Format("Banned: {0}", reason), 0x0, 0f, 0f, 0f);