From bc18550a3f646d7f7d2a02c142102c6c11b99486 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 10:12:12 +1000 Subject: [PATCH 01/10] Fixed ForceKickAll using the wrong Player.Active Fixed logs being sent to non active player Notify the server console as well when tshock updates are available Made all command methods private --- TShockAPI/Commands.cs | 104 ++++++++++++++++++------------------- TShockAPI/Tools.cs | 4 +- TShockAPI/UpdateManager.cs | 58 ++++++++++++--------- 3 files changed, 86 insertions(+), 80 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 629945b6..aa159c72 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -233,19 +233,24 @@ namespace TShockAPI } } + public static void AddUpdateCommand() + { + Commands.ChatCommands.Add(new Command("updatenow", "maintenance", Commands.UpdateNow)); + } + #region Command Methods - public static void Rules(CommandArgs args) + private static void Rules(CommandArgs args) { Tools.ShowFileToUser(args.Player, "rules.txt"); } - public static void CheckUpdates(CommandArgs args) + private static void CheckUpdates(CommandArgs args) { ThreadPool.QueueUserWorkItem(UpdateManager.CheckUpdate); } - public static void PartyChat(CommandArgs args) + private static void PartyChat(CommandArgs args) { int playerTeam = args.Player.Team; if (playerTeam != 0) @@ -263,18 +268,18 @@ namespace TShockAPI } } - public static void ThirdPerson(CommandArgs args) + private static void ThirdPerson(CommandArgs args) { string msg = args.Message.Remove(0, 3); Tools.Broadcast(string.Format("*{0} {1}", args.Player.Name, msg), 205, 133, 63); } - public static void Playing(CommandArgs args) + private static void Playing(CommandArgs args) { args.Player.SendMessage(string.Format("Current players: {0}.", Tools.GetPlayers()), 255, 240, 20); } - public static void Kick(CommandArgs args) + private static void Kick(CommandArgs args) { if (args.Parameters.Count < 1) { @@ -307,10 +312,8 @@ namespace TShockAPI } } - public static void Ban(CommandArgs args) + private static void Ban(CommandArgs args) { - int adminplr = args.PlayerID; - if (args.Parameters.Count < 1) { args.Player.SendMessage("Invalid syntax! Proper syntax: /ban [reason]", Color.Red); @@ -342,7 +345,7 @@ namespace TShockAPI } } - public static void BanIP(CommandArgs args) + private static void BanIP(CommandArgs args) { if (args.Parameters.Count < 1) { @@ -360,7 +363,7 @@ namespace TShockAPI TShock.Bans.AddBan(ip, "", reason); } - public static void UnBan(CommandArgs args) + private static void UnBan(CommandArgs args) { if (args.Parameters.Count < 1) { @@ -386,7 +389,7 @@ namespace TShockAPI } } - public static void UnBanIP(CommandArgs args) + private static void UnBanIP(CommandArgs args) { if (args.Parameters.Count < 1) { @@ -412,20 +415,20 @@ namespace TShockAPI } } - public static void Off(CommandArgs args) + private static void Off(CommandArgs args) { Tools.ForceKickAll("Server shutting down!"); WorldGen.saveWorld(); Netplay.disconnect = true; } - public static void OffNoSave(CommandArgs args) + private static void OffNoSave(CommandArgs args) { Tools.ForceKickAll("Server shutting down!"); Netplay.disconnect = true; } - public static void DebugConfiguration(CommandArgs args) + private static void DebugConfiguration(CommandArgs args) { args.Player.SendMessage("TShock Config:"); string lineOne = string.Format("BanCheater : {0}, KickCheater : {1}, BanGriefer : {2}, KickGriefer : {3}", @@ -447,19 +450,19 @@ namespace TShockAPI args.Player.SendMessage(lineFour, Color.Yellow); } - public static void Reload(CommandArgs args) + private static void Reload(CommandArgs args) { FileTools.SetupConfig(); args.Player.SendMessage("Configuration reload complete. Some changes may require server restart."); } - public static void DropMeteor(CommandArgs args) + private static void DropMeteor(CommandArgs args) { WorldGen.spawnMeteor = false; WorldGen.dropMeteor(); } - public static void Star(CommandArgs args) + private static void Star(CommandArgs args) { int penis56 = 12; int penis57 = Main.rand.Next(Main.maxTilesX - 50) + 100; @@ -475,7 +478,7 @@ namespace TShockAPI Projectile.NewProjectile(vector.X, vector.Y, speedX, speedY, 12, 0x3e8, 10f, Main.myPlayer); } - public static void Bloodmoon(CommandArgs args) + private static void Bloodmoon(CommandArgs args) { Tools.Broadcast(string.Format("{0} turned on blood moon.", args.Player.Name)); Main.bloodMoon = true; @@ -485,25 +488,25 @@ namespace TShockAPI NetMessage.syncPlayers(); } - public static void Eater(CommandArgs args) + private static void Eater(CommandArgs args) { Tools.NewNPC(NPCList.WORLD_EATER, args.Player); Tools.Broadcast(string.Format("{0} has spawned an eater of worlds!", args.Player.Name)); } - public static void Eye(CommandArgs args) + private static void Eye(CommandArgs args) { Tools.NewNPC(NPCList.EYE, args.Player); Tools.Broadcast(string.Format("{0} has spawned an eye!", args.Player.Name)); } - public static void Skeletron(CommandArgs args) + private static void Skeletron(CommandArgs args) { Tools.NewNPC(NPCList.SKELETRON, args.Player); Tools.Broadcast(string.Format("{0} has spawned skeletron!", args.Player.Name)); } - public static void Hardcore(CommandArgs args) + private static void Hardcore(CommandArgs args) { foreach (NPCList type in Enum.GetValues(typeof(NPCList))) { @@ -512,7 +515,7 @@ namespace TShockAPI Tools.Broadcast(string.Format("{0} has spawned all bosses!", args.Player.Name)); } - public static void Invade(CommandArgs args) + private static void Invade(CommandArgs args) { if (Main.invasionSize <= 0) { @@ -526,20 +529,20 @@ namespace TShockAPI } } - public static void Password(CommandArgs args) + private static void Password(CommandArgs args) { string passwd = args.Message.Remove(0, 9).Trim(); Netplay.password = passwd; args.Player.SendMessage(string.Format("Server password changed to: {0}", passwd)); } - public static void Save(CommandArgs args) + private static void Save(CommandArgs args) { WorldGen.saveWorld(); args.Player.SendMessage("World saved."); } - public static void Home(CommandArgs args) + private static void Home(CommandArgs args) { int ply = args.PlayerID; TShock.Teleport(ply, Main.player[args.PlayerID].SpawnX * 16 + 8 - Main.player[ply].width / 2, @@ -547,7 +550,7 @@ namespace TShockAPI args.Player.SendMessage("Teleported to your spawnpoint."); } - public static void Spawn(CommandArgs args) + private static void Spawn(CommandArgs args) { int ply = args.PlayerID; TShock.Teleport(ply, Main.spawnTileX * 16 + 8 - Main.player[ply].width / 2, @@ -555,7 +558,7 @@ namespace TShockAPI args.Player.SendMessage("Teleported to the map's spawnpoint."); } - public static void AuthToken(CommandArgs args) + private static void AuthToken(CommandArgs args) { if (ConfigurationManager.AuthToken == 0) { @@ -565,17 +568,14 @@ namespace TShockAPI if (givenCode == ConfigurationManager.AuthToken) { TextWriter tw = new StreamWriter(FileTools.UsersPath, true); - tw.Write("\n" + - Tools.GetRealIP( - Convert.ToString(Netplay.serverSock[args.PlayerID].tcpClient.Client.RemoteEndPoint)) + - " superadmin"); + tw.Write("\n" + args.Player.IP + " superadmin"); args.Player.SendMessage("SuperAdmin authenticated. Please re-connect using the same IP."); ConfigurationManager.AuthToken = 0; tw.Close(); } } - public static void TP(CommandArgs args) + private static void TP(CommandArgs args) { int adminplr = args.PlayerID; @@ -599,7 +599,7 @@ namespace TShockAPI } } - public static void TPHere(CommandArgs args) + private static void TPHere(CommandArgs args) { if (args.Parameters.Count < 1) { @@ -626,7 +626,7 @@ namespace TShockAPI } } - public static void SpawnMob(CommandArgs args) + private static void SpawnMob(CommandArgs args) { if (args.Parameters.Count < 1 || args.Parameters.Count > 2) { @@ -661,10 +661,8 @@ namespace TShockAPI args.Player.SendMessage("Invalid mob type!", Color.Red); } - public static void Item(CommandArgs args) + private static void Item(CommandArgs args) { - int adminplr = args.PlayerID; - if (args.Parameters.Count < 1) { args.Player.SendMessage("Invalid syntax! Proper syntax: /item ", Color.Red); @@ -689,7 +687,7 @@ namespace TShockAPI bool flag = false; for (int i = 0; i < 40; i++) { - if (!Main.player[adminplr].inventory[i].active) + if (!args.TPlayer.inventory[i].active) { int id = Terraria.Item.NewItem(0, 0, 0, 0, type, 1, true); Main.item[id].position.X = args.Player.X; @@ -705,7 +703,7 @@ namespace TShockAPI args.Player.SendMessage("You don't have free slots!", Color.Red); } - public static void Give(CommandArgs args) + private static void Give(CommandArgs args) { if (args.Parameters.Count != 2) { @@ -774,7 +772,7 @@ namespace TShockAPI } } - public static void Heal(CommandArgs args) + private static void Heal(CommandArgs args) { if (args.Parameters.Count > 0) { @@ -814,7 +812,7 @@ namespace TShockAPI } } - public static void Butcher(CommandArgs args) + private static void Butcher(CommandArgs args) { int killcount = 0; for (int i = 0; i < Main.npc.Length; i++) @@ -829,7 +827,7 @@ namespace TShockAPI Tools.Broadcast(string.Format("Killed {0} NPCs.", killcount)); } - public static void MaxSpawns(CommandArgs args) + private static void MaxSpawns(CommandArgs args) { if (args.Parameters.Count != 1) @@ -845,7 +843,7 @@ namespace TShockAPI Tools.Broadcast(string.Format("{0} changed the maximum spawns to: {1}", args.Player.Name, amount)); } - public static void SpawnRate(CommandArgs args) + private static void SpawnRate(CommandArgs args) { if (args.Parameters.Count != 1) { @@ -860,7 +858,7 @@ namespace TShockAPI Tools.Broadcast(string.Format("{0} changed the spawn rate to: {1}", args.Player.Name, amount)); } - public static void Help(CommandArgs args) + private static void Help(CommandArgs args) { args.Player.SendMessage("TShock Commands:"); int page = 1; @@ -900,7 +898,7 @@ namespace TShockAPI } } - public static void Time(CommandArgs args) + private static void Time(CommandArgs args) { if (args.Parameters.Count != 1) { @@ -951,10 +949,8 @@ namespace TShockAPI } } - public static void Kill(CommandArgs args) + private static void Kill(CommandArgs args) { - int adminplr = args.PlayerID; - if (args.Parameters.Count < 1) { args.Player.SendMessage("Invalid syntax! Proper syntax: /kill ", Color.Red); @@ -980,7 +976,7 @@ namespace TShockAPI } } - public static void Slap(CommandArgs args) + private static void Slap(CommandArgs args) { if (args.Parameters.Count < 1 || args.Parameters.Count > 2) { @@ -1017,19 +1013,19 @@ namespace TShockAPI } } - public static void ToggleAntiBuild(CommandArgs args) + private static void ToggleAntiBuild(CommandArgs args) { ConfigurationManager.DisableBuild = (ConfigurationManager.DisableBuild == false); Tools.Broadcast(string.Format("Anti-build is now {0}.", (ConfigurationManager.DisableBuild ? "on" : "off"))); } - public static void ProtectSpawn(CommandArgs args) + private static void ProtectSpawn(CommandArgs args) { ConfigurationManager.SpawnProtect = (ConfigurationManager.SpawnProtect == false); Tools.Broadcast(string.Format("Spawn is now {0}.", (ConfigurationManager.SpawnProtect ? "protected" : "open"))); } - public static void UpdateNow(CommandArgs args) + private static void UpdateNow(CommandArgs args) { Process TServer = Process.GetCurrentProcess(); diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index d6bba608..f72e5c0d 100755 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -117,7 +117,7 @@ namespace TShockAPI TSPlayer.Server.SendMessage(log, color); foreach (TSPlayer player in TShock.Players) { - if (player != null && player.Group.HasPermission("logs")) + if (player != null && player.Active && player.Group.HasPermission("logs")) player.SendMessage(log, color); } } @@ -198,7 +198,7 @@ namespace TShockAPI { foreach(TSPlayer player in TShock.Players) { - if (player != null && player.TPlayer.active) + if (player != null && player.Active) { Tools.ForceKick(player, reason); } diff --git a/TShockAPI/UpdateManager.cs b/TShockAPI/UpdateManager.cs index 64bc3d13..4150f6d3 100644 --- a/TShockAPI/UpdateManager.cs +++ b/TShockAPI/UpdateManager.cs @@ -37,11 +37,30 @@ namespace TShockAPI /// Check once every X minutes. /// static readonly int CheckXMinutes = 30; + + public static void UpdateProcedureCheck() + { + if ((DateTime.Now - lastcheck).TotalMinutes >= CheckXMinutes) + { + ThreadPool.QueueUserWorkItem(CheckUpdate); + lastcheck = DateTime.Now; + } + } + + public static void CheckUpdate(object o) + { + if (ServerIsOutOfDate()) + { + EnableUpdateCommand(); + NotifyAdministrators(globalChanges); + } + } + /// /// Checks to see if the server is out of date. /// /// - public static bool ServerIsOutOfDate() + private static bool ServerIsOutOfDate() { WebClient client = new WebClient(); client.Headers.Add("user-agent", @@ -65,42 +84,33 @@ namespace TShockAPI return false; } - public static void EnableUpdateCommand() + private static void EnableUpdateCommand() { - Commands.ChatCommands.Add(new Command("updatenow", "maintenance", Commands.UpdateNow)); - updateCmd = true; + if (!updateCmd) + { + Commands.AddUpdateCommand(); + updateCmd = true; + } } - - public static void NotifyAdministrators(string[] changes) + + private static void NotifyAdministrators(string[] changes) { + NotifyAdministrator(TSPlayer.Server, changes); foreach (TSPlayer player in TShock.Players) { if (player != null && player.Active && player.Group.HasPermission("maintenance")) { - player.SendMessage("The server is out of date. To update, type /updatenow."); - for (int j = 4; j < changes.Length; j++) - { - player.SendMessage(changes[j], Color.Red); - } + NotifyAdministrator(player, changes); } } } - public static void UpdateProcedureCheck() + private static void NotifyAdministrator(TSPlayer player, string[] changes) { - if ((DateTime.Now - lastcheck).TotalMinutes >= CheckXMinutes) + player.SendMessage("The server is out of date. To update, type /updatenow."); + for (int j = 4; j < changes.Length; j++) { - ThreadPool.QueueUserWorkItem(CheckUpdate); - lastcheck = DateTime.Now; - } - } - - public static void CheckUpdate(object o) - { - if (ServerIsOutOfDate()) - { - EnableUpdateCommand(); - NotifyAdministrators(globalChanges); + player.SendMessage(changes[j], Color.Red); } } } From 07d607a9b616092aa185c391d5c1ca1e62cd61a4 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 10:18:22 +1000 Subject: [PATCH 02/10] Fixed json config file changes made while server is running lost if reload is not performed before restart Fixed unban not saving to file until server shutdown --- TShockAPI/BanManager.cs | 1 + TShockAPI/FileTools.cs | 2 ++ TShockAPI/TShock.cs | 11 ++--------- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/TShockAPI/BanManager.cs b/TShockAPI/BanManager.cs index f7905914..7d67bdf1 100644 --- a/TShockAPI/BanManager.cs +++ b/TShockAPI/BanManager.cs @@ -74,6 +74,7 @@ namespace TShockAPI public void RemoveBan(Ban ban) { Bans.Remove(ban); + SaveBans(); } /// diff --git a/TShockAPI/FileTools.cs b/TShockAPI/FileTools.cs index 773691b1..a87f94af 100644 --- a/TShockAPI/FileTools.cs +++ b/TShockAPI/FileTools.cs @@ -66,6 +66,8 @@ namespace TShockAPI if (File.Exists(ConfigPath)) { ConfigurationManager.ReadJsonConfiguration(); + // Add all the missing config properties in the json file + ConfigurationManager.WriteJsonConfiguration(); } else { diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 078de510..ae11f7e4 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -139,14 +139,8 @@ namespace TShockAPI public override void Initialize() { - try - { - FileTools.SetupConfig(); - } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - } + FileTools.SetupConfig(); + string version = string.Format("TShock Version {0} ({1}) now running.", Version, VersionCodename); Console.WriteLine(version); @@ -179,7 +173,6 @@ namespace TShockAPI public override void DeInitialize() { Bans.SaveBans(); - ConfigurationManager.WriteJsonConfiguration(); GameHooks.PostInitialize -= OnPostInit; GameHooks.Update -= OnUpdate; ServerHooks.Chat -= OnChat; From efc5ed5fcfaab80dc2332b4c56b544c280599913 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 10:33:50 +1000 Subject: [PATCH 03/10] Dunno why VS makes that change --- TShockAPI/Group.cs | 32 ++++++++++++++++---------------- TShockAPI/TShock.cs | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/TShockAPI/Group.cs b/TShockAPI/Group.cs index 11cfcdf9..de53b247 100644 --- a/TShockAPI/Group.cs +++ b/TShockAPI/Group.cs @@ -1,19 +1,19 @@ -/* -TShock, a server mod for Terraria -Copyright (C) 2011 The TShock Team - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . +/* +TShock, a server mod for Terraria +Copyright (C) 2011 The TShock Team + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . */ using System; using System.Collections.Generic; diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index ae11f7e4..878b9128 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -250,7 +250,7 @@ namespace TShockAPI Tools.ForceKickAll("Server shutting down!"); } else if (text.StartsWith("playing")) - { + { int count = 0; foreach (TSPlayer player in Players) { From 15924c543eebf67f8be90aca6eab104d6c12f46e Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 12:09:50 +1000 Subject: [PATCH 04/10] Notify user if command failed Fixed /heal not working from server console (cant heal self from console now) /playing from console now outputs the same as the overridden playing Using args.Player or args.TPlayer in commands instead of Index --- TShockAPI/Commands.cs | 54 ++++++++++++++++++++----------------------- TShockAPI/TShock.cs | 53 ++++++++++++++++++++++++------------------ TShockAPI/Tools.cs | 1 + 3 files changed, 57 insertions(+), 51 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index aa159c72..67b64c66 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -44,11 +44,6 @@ namespace TShockAPI get { return Player.TPlayer; } } - public int PlayerID - { - get { return Player.Index; } - } - public CommandArgs(string message, TSPlayer ply, List args) { Message = message; @@ -80,6 +75,7 @@ namespace TShockAPI } catch (Exception e) { + ply.SendMessage("Command failed, check logs for more details."); Log.Error(e.ToString()); } @@ -544,17 +540,15 @@ namespace TShockAPI private static void Home(CommandArgs args) { - int ply = args.PlayerID; - TShock.Teleport(ply, Main.player[args.PlayerID].SpawnX * 16 + 8 - Main.player[ply].width / 2, - Main.player[args.PlayerID].SpawnY * 16 - Main.player[ply].height); + TShock.Teleport(args.Player.Index, args.TPlayer.SpawnX * 16 + 8 - args.TPlayer.width / 2, + args.TPlayer.SpawnY * 16 - args.TPlayer.height); args.Player.SendMessage("Teleported to your spawnpoint."); } private static void Spawn(CommandArgs args) { - int ply = args.PlayerID; - TShock.Teleport(ply, Main.spawnTileX * 16 + 8 - Main.player[ply].width / 2, - Main.spawnTileY * 16 - Main.player[ply].height); + TShock.Teleport(args.Player.Index, Main.spawnTileX * 16 + 8 - args.TPlayer.width / 2, + Main.spawnTileY * 16 - args.TPlayer.height); args.Player.SendMessage("Teleported to the map's spawnpoint."); } @@ -577,8 +571,6 @@ namespace TShockAPI private static void TP(CommandArgs args) { - int adminplr = args.PlayerID; - if (args.Parameters.Count < 1) { args.Player.SendMessage("Invalid syntax! Proper syntax: /tp ", Color.Red); @@ -594,7 +586,7 @@ namespace TShockAPI else { var plr = player[0]; - TShock.Teleport(adminplr, plr.TPlayer.position.X, plr.TPlayer.position.Y); + TShock.Teleport(args.Player.Index, plr.X, plr.Y); args.Player.SendMessage(string.Format("Teleported to {0}", plr.Name)); } } @@ -620,7 +612,7 @@ namespace TShockAPI else { var plr = player[0]; - TShock.Teleport(plr.Index, args.TPlayer.position.X, args.TPlayer.position.Y); + TShock.Teleport(plr.Index, args.Player.X, args.Player.Y); plr.SendMessage(string.Format("You were teleported to {0}.", plr.Name)); args.Player.SendMessage(string.Format("You brought {0} here.", plr.Name)); } @@ -749,20 +741,13 @@ namespace TShockAPI { if (!plr.TPlayer.inventory[i].active) { - //Main.player[player].inventory[i].SetDefaults(type); - //Main.player[player].inventory[i].stack = Main.player[player].inventory[i].maxStack; int id = Terraria.Item.NewItem(0, 0, 0, 0, type, 1, true); - Main.item[id].position.X = plr.TPlayer.position.X; - Main.item[id].position.Y = plr.TPlayer.position.Y; + Main.item[id].position.X = plr.X; + Main.item[id].position.Y = plr.Y; Main.item[id].stack = Main.item[id].maxStack; - //TShock.SendDataAll(21, -1, "", id); NetMessage.SendData(21, -1, -1, "", id, 0f, 0f, 0f); - args.Player.SendMessage( - string.Format("Gave {0} some {1}.", plr.Name, Main.item[id].name)); - plr.SendMessage( - string.Format("{0} gave you some {1}.", args.Player.Name, - Main.item[id].name)); - //TShock.UpdateInventories(); + args.Player.SendMessage(string.Format("Gave {0} some {1}.", plr.Name, Main.item[id].name)); + plr.SendMessage(string.Format("{0} gave you some {1}.", args.Player.Name, Main.item[id].name)); flag = true; break; } @@ -789,11 +774,22 @@ namespace TShockAPI else { var plr = player[0]; - DropHearts(plr.TPlayer.position.X, plr.TPlayer.position.Y, 20); - args.Player.SendMessage(string.Format("You just healed {0}", plr.Name)); - plr.SendMessage(string.Format("{0} just healed you!", plr.Name)); + DropHearts(plr.X, plr.Y, 20); + if (plr == args.Player) + { + args.Player.SendMessage("You just got healed!"); + } + else + { + args.Player.SendMessage(string.Format("You just healed {0}", plr.Name)); + plr.SendMessage(string.Format("{0} just healed you!", args.Player.Name)); + } } } + else if (args.Player.Index < 0) + { + args.Player.SendMessage("You cant heal yourself!"); + } else { DropHearts(args.Player.X, args.Player.Y, 20); diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 878b9128..6d4b09b6 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -215,7 +215,35 @@ namespace TShockAPI /// private void ServerHooks_OnCommand(string text, HandledEventArgs e) { - if (text.StartsWith("/")) + // Damn you ThreadStatic and Redigit + if (Main.rand == null) + { + Main.rand = new Random(); + } + + if (text.StartsWith("exit")) + { + Tools.ForceKickAll("Server shutting down!"); + } + else if (text.StartsWith("playing") || text.StartsWith("/playing")) + { + int count = 0; + foreach (TSPlayer player in Players) + { + if (player != null && player.Active) + { + count++; + TSPlayer.Server.SendMessage(string.Format("{0} ({1}) [{2}]", player.Name, player.IP, player.Group.Name)); + } + } + TSPlayer.Server.SendMessage(string.Format("{0} players connected.", count)); + e.Handled = true; + } + else if (text.StartsWith("say ")) + { + Log.Info(string.Format("Server said: {0}", text.Remove(0, 4))); + } + else if (text.StartsWith("/")) { string cmdStr = text.Remove(0, 1); var args = Commands.ParseParameters(cmdStr); @@ -236,7 +264,7 @@ namespace TShockAPI if (cmd == null) { - TSPlayer.Server.SendMessage("That command does not exist, try /help", Color.Red); + TSPlayer.Server.SendMessage("That command does not exist, try /help"); } else { @@ -245,26 +273,7 @@ namespace TShockAPI } e.Handled = true; } - else if (text.StartsWith("exit")) - { - Tools.ForceKickAll("Server shutting down!"); - } - else if (text.StartsWith("playing")) - { - int count = 0; - foreach (TSPlayer player in Players) - { - if (player != null && player.Active) - { - count++; - Console.WriteLine(string.Format("{0} ({1}) [{2}]", player.Name, player.IP, player.Group.Name)); - } - } - Console.WriteLine(string.Format("{0} players connected.", count)); - e.Handled = true; - } - else if (text.StartsWith("say ")) - Log.Info(string.Format("Server said: {0}", text.Remove(0, 4))); + } private void NpcHooks_OnStrikeNpc(NpcStrikeEventArgs e) diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index f72e5c0d..31bcbc78 100755 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -101,6 +101,7 @@ namespace TShockAPI Log.Info(string.Format("Broadcast: {0}", msg)); } + public static void Broadcast(string msg, Color color) { Broadcast(msg, color.R, color.G, color.B); From 9ed47768adee6e833c0952ffffe5f68a4c3bd327 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 12:14:34 +1000 Subject: [PATCH 05/10] Grrrr another ThreadStatic --- TShockAPI/TShock.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 6d4b09b6..5e4a5556 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -220,6 +220,10 @@ namespace TShockAPI { Main.rand = new Random(); } + if (WorldGen.genRand == null) + { + WorldGen.genRand = new Random(); + } if (text.StartsWith("exit")) { From f84949affaed36c3180feecf10380ccea3acdee0 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 13:18:48 +1000 Subject: [PATCH 06/10] All commands now working from console (some print message that they can't be performed from console) PlayerDamage accept TSPlayer Added property to check if TSPlayer is real and if connection for that player is alive --- TShockAPI/Commands.cs | 750 ++++++++++++++++++++++-------------------- TShockAPI/TSPlayer.cs | 27 +- TShockAPI/TShock.cs | 6 +- TShockAPI/Tools.cs | 6 +- 4 files changed, 419 insertions(+), 370 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 67b64c66..a78a9c7a 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -234,46 +234,7 @@ namespace TShockAPI Commands.ChatCommands.Add(new Command("updatenow", "maintenance", Commands.UpdateNow)); } - #region Command Methods - - private static void Rules(CommandArgs args) - { - Tools.ShowFileToUser(args.Player, "rules.txt"); - } - - private static void CheckUpdates(CommandArgs args) - { - ThreadPool.QueueUserWorkItem(UpdateManager.CheckUpdate); - } - - private static void PartyChat(CommandArgs args) - { - int playerTeam = args.Player.Team; - if (playerTeam != 0) - { - string msg = string.Format("<{0}> {1}", args.Player.Name, args.Message.Remove(0, 2)); - foreach (TSPlayer player in TShock.Players) - { - if (player != null && player.Active && player.Team == playerTeam) - player.SendMessage(msg, Main.teamColor[playerTeam].R, Main.teamColor[playerTeam].G, Main.teamColor[playerTeam].B); - } - } - else - { - args.Player.SendMessage("You are not in a party!", 255, 240, 20); - } - } - - private static void ThirdPerson(CommandArgs args) - { - string msg = args.Message.Remove(0, 3); - Tools.Broadcast(string.Format("*{0} {1}", args.Player.Name, msg), 205, 133, 63); - } - - private static void Playing(CommandArgs args) - { - args.Player.SendMessage(string.Format("Current players: {0}.", Tools.GetPlayers()), 255, 240, 20); - } + #region Player Management Commands private static void Kick(CommandArgs args) { @@ -411,6 +372,10 @@ namespace TShockAPI } } + #endregion Player Management Commands + + #region Server Maintenence Commands + private static void Off(CommandArgs args) { Tools.ForceKickAll("Server shutting down!"); @@ -424,34 +389,42 @@ namespace TShockAPI Netplay.disconnect = true; } - private static void DebugConfiguration(CommandArgs args) + private static void CheckUpdates(CommandArgs args) { - args.Player.SendMessage("TShock Config:"); - string lineOne = string.Format("BanCheater : {0}, KickCheater : {1}, BanGriefer : {2}, KickGriefer : {3}", - ConfigurationManager.BanCheater, ConfigurationManager.KickCheater, - ConfigurationManager.BanGriefer, ConfigurationManager.KickGriefer); - args.Player.SendMessage(lineOne, Color.Yellow); - string lineTwo = string.Format("BanTnt : {0}, KickTnt : {1}, BanBoom : {2}, KickBoom : {3}", - ConfigurationManager.BanTnt, ConfigurationManager.KickTnt, - ConfigurationManager.BanBoom, ConfigurationManager.KickBoom); - args.Player.SendMessage(lineTwo, Color.Yellow); - string lineThree = string.Format("RangeChecks : {0}, DisableBuild : {1}, ProtectSpawn : {2}, ProtectRadius : {3}", - ConfigurationManager.RangeChecks, ConfigurationManager.DisableBuild, - ConfigurationManager.SpawnProtect, ConfigurationManager.SpawnProtectRadius); - args.Player.SendMessage(lineThree, Color.Yellow); - string lineFour = string.Format("MaxSlots : {0}, SpamChecks : {1}, InvMultiplier : {2}, DMS : {3}, SpawnRate {4}", - ConfigurationManager.MaxSlots, ConfigurationManager.SpamChecks, - ConfigurationManager.InvasionMultiplier, ConfigurationManager.DefaultMaxSpawns, - ConfigurationManager.DefaultSpawnRate); - args.Player.SendMessage(lineFour, Color.Yellow); + ThreadPool.QueueUserWorkItem(UpdateManager.CheckUpdate); } - private static void Reload(CommandArgs args) + private static void UpdateNow(CommandArgs args) { - FileTools.SetupConfig(); - args.Player.SendMessage("Configuration reload complete. Some changes may require server restart."); + Process TServer = Process.GetCurrentProcess(); + + StreamWriter sw = new StreamWriter("pid"); + sw.Write(TServer.Id); + sw.Close(); + + sw = new StreamWriter("pn"); + sw.Write(TServer.ProcessName + " " + Environment.CommandLine); + sw.Close(); + + WebClient client = new WebClient(); + client.Headers.Add("user-agent", "TShock"); + byte[] updatefile = client.DownloadData("http://tsupdate.shankshock.com/UpdateTShock.exe"); + + BinaryWriter bw = new BinaryWriter(new FileStream("UpdateTShock.exe", FileMode.Create)); + bw.Write(updatefile); + bw.Close(); + + Process.Start(new ProcessStartInfo("UpdateTShock.exe")); + + Tools.ForceKickAll("Server shutting down for update!"); + WorldGen.saveWorld(); + Netplay.disconnect = true; } + #endregion Server Maintenence Commands + + #region Cause Events and Spawn Monsters Commands + private static void DropMeteor(CommandArgs args) { WorldGen.spawnMeteor = false; @@ -484,6 +457,20 @@ namespace TShockAPI NetMessage.syncPlayers(); } + private static void Invade(CommandArgs args) + { + if (Main.invasionSize <= 0) + { + Tools.Broadcast(string.Format("{0} has started an invasion.", args.Player.Name)); + TShock.StartInvasion(); + } + else + { + Tools.Broadcast(string.Format("{0} has ended an invasion.", args.Player.Name)); + Main.invasionSize = 0; + } + } + private static void Eater(CommandArgs args) { Tools.NewNPC(NPCList.WORLD_EATER, args.Player); @@ -511,32 +498,44 @@ namespace TShockAPI Tools.Broadcast(string.Format("{0} has spawned all bosses!", args.Player.Name)); } - private static void Invade(CommandArgs args) + private static void SpawnMob(CommandArgs args) { - if (Main.invasionSize <= 0) + if (args.Parameters.Count < 1 || args.Parameters.Count > 2) { - Tools.Broadcast(string.Format("{0} has started an invasion.", args.Player.Name)); - TShock.StartInvasion(); + args.Player.SendMessage("Invalid syntax! Proper syntax: /spawnmob [amount]", Color.Red); + return; + } + if (args.Parameters[0].Length == 0) + { + args.Player.SendMessage("Missing mob name/id", Color.Red); + return; + } + + int type = -1; + int amount = 1; + + if (!int.TryParse(args.Parameters[0], out type)) + type = TShock.GetNPCID(args.Parameters[0]); + if (args.Parameters.Count == 2 && !int.TryParse(args.Parameters[1], out amount)) + { + args.Player.SendMessage("Invalid syntax! Proper syntax: /spawnmob [amount]", Color.Red); + return; + } + + if (type >= 1 && type < Main.maxNPCTypes) + { + int npcid = -1; + for (int i = 0; i < amount; i++) + npcid = NPC.NewNPC((int)args.Player.X, (int)args.Player.Y, type, 0); + Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", Main.npc[npcid].name, amount)); } else - { - Tools.Broadcast(string.Format("{0} has ended an invasion.", args.Player.Name)); - Main.invasionSize = 0; - } + args.Player.SendMessage("Invalid mob type!", Color.Red); } - private static void Password(CommandArgs args) - { - string passwd = args.Message.Remove(0, 9).Trim(); - Netplay.password = passwd; - args.Player.SendMessage(string.Format("Server password changed to: {0}", passwd)); - } + #endregion Cause Events and Spawn Monsters Commands - private static void Save(CommandArgs args) - { - WorldGen.saveWorld(); - args.Player.SendMessage("World saved."); - } + #region Teleport Commands private static void Home(CommandArgs args) { @@ -552,23 +551,6 @@ namespace TShockAPI args.Player.SendMessage("Teleported to the map's spawnpoint."); } - private static void AuthToken(CommandArgs args) - { - if (ConfigurationManager.AuthToken == 0) - { - return; - } - int givenCode = Convert.ToInt32(args.Parameters[0]); - if (givenCode == ConfigurationManager.AuthToken) - { - TextWriter tw = new StreamWriter(FileTools.UsersPath, true); - tw.Write("\n" + args.Player.IP + " superadmin"); - args.Player.SendMessage("SuperAdmin authenticated. Please re-connect using the same IP."); - ConfigurationManager.AuthToken = 0; - tw.Close(); - } - } - private static void TP(CommandArgs args) { if (args.Parameters.Count < 1) @@ -618,39 +600,325 @@ namespace TShockAPI } } - private static void SpawnMob(CommandArgs args) + #endregion Teleport Commands + + #region Server Config Commands + + private static void DebugConfiguration(CommandArgs args) + { + args.Player.SendMessage("TShock Config:"); + string lineOne = string.Format("BanCheater : {0}, KickCheater : {1}, BanGriefer : {2}, KickGriefer : {3}", + ConfigurationManager.BanCheater, ConfigurationManager.KickCheater, + ConfigurationManager.BanGriefer, ConfigurationManager.KickGriefer); + args.Player.SendMessage(lineOne, Color.Yellow); + string lineTwo = string.Format("BanTnt : {0}, KickTnt : {1}, BanBoom : {2}, KickBoom : {3}", + ConfigurationManager.BanTnt, ConfigurationManager.KickTnt, + ConfigurationManager.BanBoom, ConfigurationManager.KickBoom); + args.Player.SendMessage(lineTwo, Color.Yellow); + string lineThree = string.Format("RangeChecks : {0}, DisableBuild : {1}, ProtectSpawn : {2}, ProtectRadius : {3}", + ConfigurationManager.RangeChecks, ConfigurationManager.DisableBuild, + ConfigurationManager.SpawnProtect, ConfigurationManager.SpawnProtectRadius); + args.Player.SendMessage(lineThree, Color.Yellow); + string lineFour = string.Format("MaxSlots : {0}, SpamChecks : {1}, InvMultiplier : {2}, DMS : {3}, SpawnRate {4}", + ConfigurationManager.MaxSlots, ConfigurationManager.SpamChecks, + ConfigurationManager.InvasionMultiplier, ConfigurationManager.DefaultMaxSpawns, + ConfigurationManager.DefaultSpawnRate); + args.Player.SendMessage(lineFour, Color.Yellow); + } + + private static void Reload(CommandArgs args) + { + FileTools.SetupConfig(); + args.Player.SendMessage("Configuration reload complete. Some changes may require server restart."); + } + + private static void Password(CommandArgs args) + { + string passwd = args.Message.Remove(0, 9).Trim(); + Netplay.password = passwd; + args.Player.SendMessage(string.Format("Server password changed to: {0}", passwd)); + } + + private static void Save(CommandArgs args) + { + WorldGen.saveWorld(); + args.Player.SendMessage("World saved."); + } + + private static void MaxSpawns(CommandArgs args) + { + + if (args.Parameters.Count != 1) + { + args.Player.SendMessage("Invalid syntax! Proper syntax: /maxspawns ", Color.Red); + return; + } + + int amount = Convert.ToInt32(args.Parameters[0]); + int.TryParse(args.Parameters[0], out amount); + NPC.defaultMaxSpawns = amount; + ConfigurationManager.DefaultMaxSpawns = amount; + Tools.Broadcast(string.Format("{0} changed the maximum spawns to: {1}", args.Player.Name, amount)); + } + + private static void SpawnRate(CommandArgs args) + { + if (args.Parameters.Count != 1) + { + args.Player.SendMessage("Invalid syntax! Proper syntax: /spawnrate ", Color.Red); + return; + } + + int amount = Convert.ToInt32(args.Parameters[0]); + int.TryParse(args.Parameters[0], out amount); + NPC.defaultSpawnRate = amount; + ConfigurationManager.DefaultSpawnRate = amount; + Tools.Broadcast(string.Format("{0} changed the spawn rate to: {1}", args.Player.Name, amount)); + } + + private static void Time(CommandArgs args) + { + if (args.Parameters.Count != 1) + { + args.Player.SendMessage("Invalid syntax! Proper syntax: /time ", Color.Red); + return; + } + + switch (args.Parameters[0]) + { + case "day": + Main.time = 0; + Main.dayTime = true; + NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); + NetMessage.syncPlayers(); + Tools.Broadcast(string.Format("{0} set time to day.", args.Player.Name)); + break; + case "night": + Main.time = 0; + Main.dayTime = false; + NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); + NetMessage.syncPlayers(); + Tools.Broadcast(string.Format("{0} set time to night.", args.Player.Name)); + break; + case "dusk": + Main.dayTime = false; + Main.time = 0.0; + NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); + NetMessage.syncPlayers(); + Tools.Broadcast(string.Format("{0} set time to dusk.", args.Player.Name)); + break; + case "noon": + Main.dayTime = true; + Main.time = 27000.0; + NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); + NetMessage.syncPlayers(); + Tools.Broadcast(string.Format("{0} set time to noon.", args.Player.Name)); + break; + case "midnight": + Main.dayTime = false; + Main.time = 16200.0; + NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); + NetMessage.syncPlayers(); + Tools.Broadcast(string.Format("{0} set time to midnight.", args.Player.Name)); + break; + default: + args.Player.SendMessage("Invalid syntax! Proper syntax: /time ", Color.Red); + break; + } + } + + #endregion Server Config Commands + + #region World Protection Commands + + private static void ToggleAntiBuild(CommandArgs args) + { + ConfigurationManager.DisableBuild = (ConfigurationManager.DisableBuild == false); + Tools.Broadcast(string.Format("Anti-build is now {0}.", (ConfigurationManager.DisableBuild ? "on" : "off"))); + } + + private static void ProtectSpawn(CommandArgs args) + { + ConfigurationManager.SpawnProtect = (ConfigurationManager.SpawnProtect == false); + Tools.Broadcast(string.Format("Spawn is now {0}.", (ConfigurationManager.SpawnProtect ? "protected" : "open"))); + } + + #endregion World Protection Commands + + #region General Commands + + private static void Help(CommandArgs args) + { + args.Player.SendMessage("TShock Commands:"); + int page = 1; + if (args.Parameters.Count > 0) + int.TryParse(args.Parameters[0], out page); + var cmdlist = new List(); + for (int j = 0; j < ChatCommands.Count; j++) + { + if (ChatCommands[j].CanRun(args.Player)) + { + cmdlist.Add(ChatCommands[j]); + } + } + var sb = new StringBuilder(); + if (cmdlist.Count > (15 * (page - 1))) + { + for (int j = (15 * (page - 1)); j < (15 * page); j++) + { + if (sb.Length != 0) + sb.Append(", "); + sb.Append("/").Append(cmdlist[j].Name); + if (j == cmdlist.Count - 1) + { + args.Player.SendMessage(sb.ToString(), Color.Yellow); + break; + } + if ((j + 1) % 5 == 0) + { + args.Player.SendMessage(sb.ToString(), Color.Yellow); + sb.Clear(); + } + } + } + if (cmdlist.Count > (15 * page)) + { + args.Player.SendMessage(string.Format("Type /help {0} for more commands.", (page + 1)), Color.Yellow); + } + } + + private static void Playing(CommandArgs args) + { + args.Player.SendMessage(string.Format("Current players: {0}.", Tools.GetPlayers()), 255, 240, 20); + } + + private static void AuthToken(CommandArgs args) + { + if (ConfigurationManager.AuthToken == 0) + { + return; + } + int givenCode = Convert.ToInt32(args.Parameters[0]); + if (givenCode == ConfigurationManager.AuthToken) + { + TextWriter tw = new StreamWriter(FileTools.UsersPath, true); + tw.Write("\n" + args.Player.IP + " superadmin"); + args.Player.SendMessage("SuperAdmin authenticated. Please re-connect using the same IP."); + ConfigurationManager.AuthToken = 0; + tw.Close(); + } + } + + private static void ThirdPerson(CommandArgs args) + { + string msg = args.Message.Remove(0, 3); + Tools.Broadcast(string.Format("*{0} {1}", args.Player.Name, msg), 205, 133, 63); + } + + private static void PartyChat(CommandArgs args) + { + int playerTeam = args.Player.Team; + if (playerTeam != 0) + { + string msg = string.Format("<{0}> {1}", args.Player.Name, args.Message.Remove(0, 2)); + foreach (TSPlayer player in TShock.Players) + { + if (player != null && player.Active && player.Team == playerTeam) + player.SendMessage(msg, Main.teamColor[playerTeam].R, Main.teamColor[playerTeam].G, Main.teamColor[playerTeam].B); + } + } + else + { + args.Player.SendMessage("You are not in a party!", 255, 240, 20); + } + } + + private static void Rules(CommandArgs args) + { + Tools.ShowFileToUser(args.Player, "rules.txt"); + } + + #endregion General Commands + + #region Cheat Commands + + private static void Slap(CommandArgs args) { if (args.Parameters.Count < 1 || args.Parameters.Count > 2) { - args.Player.SendMessage("Invalid syntax! Proper syntax: /spawnmob [amount]", Color.Red); + args.Player.SendMessage("Invalid syntax! Proper syntax: /slap [dmg]", Color.Red); return; } if (args.Parameters[0].Length == 0) { - args.Player.SendMessage("Missing mob name/id", Color.Red); + args.Player.SendMessage("Missing player name", Color.Red); return; } - int type = -1; - int amount = 1; - - if (!int.TryParse(args.Parameters[0], out type)) - type = TShock.GetNPCID(args.Parameters[0]); - if (args.Parameters.Count == 2 && !int.TryParse(args.Parameters[1], out amount)) + string plStr = args.Parameters[0]; + var player = Tools.FindPlayer(plStr); + if (player.Count == 0) { - args.Player.SendMessage("Invalid syntax! Proper syntax: /spawnmob [amount]", Color.Red); - return; + args.Player.SendMessage("Invalid player!", Color.Red); } - - if (type >= 1 && type < Main.maxNPCTypes) + else if (player.Count > 1) { - int npcid = -1; - for (int i = 0; i < amount; i++) - npcid = NPC.NewNPC((int)args.Player.X, (int)args.Player.Y, type, 0); - Tools.Broadcast(string.Format("{0} was spawned {1} time(s).", Main.npc[npcid].name, amount)); + args.Player.SendMessage("More than one player matched!", Color.Red); } else - args.Player.SendMessage("Invalid mob type!", Color.Red); + { + var plr = player[0]; + int damage = 5; + if (args.Parameters.Count == 2) + { + int.TryParse(args.Parameters[1], out damage); + } + TShock.PlayerDamage(plr, damage); + Tools.Broadcast(string.Format("{0} slapped {1} for {2} damage.", + args.Player.Name, plr.Name, damage)); + } + } + + private static void Kill(CommandArgs args) + { + if (args.Parameters.Count < 1) + { + args.Player.SendMessage("Invalid syntax! Proper syntax: /kill ", Color.Red); + return; + } + + string plStr = String.Join(" ", args.Parameters); + var player = Tools.FindPlayer(plStr); + if (player.Count == 0) + { + args.Player.SendMessage("Invalid player!", Color.Red); + } + else if (player.Count > 1) + { + args.Player.SendMessage("More than one player matched!", Color.Red); + } + else + { + var plr = player[0]; + args.Player.SendMessage(string.Format("You just killed {0}!", plr.Name)); + plr.SendMessage(string.Format("{0} just killed you!", args.Player.Name)); + TShock.PlayerDamage(plr, 999999); + } + } + + private static void Butcher(CommandArgs args) + { + int killcount = 0; + for (int i = 0; i < Main.npc.Length; i++) + { + if (!Main.npc[i].townNPC && Main.npc[i].active) + { + Main.npc[i].StrikeNPC(99999, 90f, 1); + NetMessage.SendData(28, -1, -1, "", i, 99999, 90f, 1); + killcount++; + } + } + Tools.Broadcast(string.Format("Killed {0} NPCs.", killcount)); } private static void Item(CommandArgs args) @@ -676,6 +944,12 @@ namespace TShockAPI return; } + if (!args.Player.RealPlayer) + { + args.Player.SendMessage("You cant get items!"); + return; + } + bool flag = false; for (int i = 0; i < 40; i++) { @@ -786,7 +1060,7 @@ namespace TShockAPI } } } - else if (args.Player.Index < 0) + else if (!args.Player.RealPlayer) { args.Player.SendMessage("You cant heal yourself!"); } @@ -808,246 +1082,6 @@ namespace TShockAPI } } - private static void Butcher(CommandArgs args) - { - int killcount = 0; - for (int i = 0; i < Main.npc.Length; i++) - { - if (!Main.npc[i].townNPC && Main.npc[i].active) - { - Main.npc[i].StrikeNPC(99999, 90f, 1); - NetMessage.SendData(28, -1, -1, "", i, 99999, 90f, 1); - killcount++; - } - } - Tools.Broadcast(string.Format("Killed {0} NPCs.", killcount)); - } - - private static void MaxSpawns(CommandArgs args) - { - - if (args.Parameters.Count != 1) - { - args.Player.SendMessage("Invalid syntax! Proper syntax: /maxspawns ", Color.Red); - return; - } - - int amount = Convert.ToInt32(args.Parameters[0]); - int.TryParse(args.Parameters[0], out amount); - NPC.defaultMaxSpawns = amount; - ConfigurationManager.DefaultMaxSpawns = amount; - Tools.Broadcast(string.Format("{0} changed the maximum spawns to: {1}", args.Player.Name, amount)); - } - - private static void SpawnRate(CommandArgs args) - { - if (args.Parameters.Count != 1) - { - args.Player.SendMessage("Invalid syntax! Proper syntax: /spawnrate ", Color.Red); - return; - } - - int amount = Convert.ToInt32(args.Parameters[0]); - int.TryParse(args.Parameters[0], out amount); - NPC.defaultSpawnRate = amount; - ConfigurationManager.DefaultSpawnRate = amount; - Tools.Broadcast(string.Format("{0} changed the spawn rate to: {1}", args.Player.Name, amount)); - } - - private static void Help(CommandArgs args) - { - args.Player.SendMessage("TShock Commands:"); - int page = 1; - if (args.Parameters.Count > 0) - int.TryParse(args.Parameters[0], out page); - var cmdlist = new List(); - for (int j = 0; j < ChatCommands.Count; j++) - { - if (ChatCommands[j].CanRun(args.Player)) - { - cmdlist.Add(ChatCommands[j]); - } - } - var sb = new StringBuilder(); - if (cmdlist.Count > (15 * (page - 1))) - { - for (int j = (15 * (page - 1)); j < (15 * page); j++) - { - if (sb.Length != 0) - sb.Append(", "); - sb.Append("/").Append(cmdlist[j].Name); - if (j == cmdlist.Count - 1) - { - args.Player.SendMessage(sb.ToString(), Color.Yellow); - break; - } - if ((j + 1) % 5 == 0) - { - args.Player.SendMessage(sb.ToString(), Color.Yellow); - sb.Clear(); - } - } - } - if (cmdlist.Count > (15 * page)) - { - args.Player.SendMessage(string.Format("Type /help {0} for more commands.", (page + 1)), Color.Yellow); - } - } - - private static void Time(CommandArgs args) - { - if (args.Parameters.Count != 1) - { - args.Player.SendMessage("Invalid syntax! Proper syntax: /time ", Color.Red); - return; - } - - switch (args.Parameters[0]) - { - case "day": - Main.time = 0; - Main.dayTime = true; - NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); - NetMessage.syncPlayers(); - Tools.Broadcast(string.Format("{0} set time to day.", args.Player.Name)); - break; - case "night": - Main.time = 0; - Main.dayTime = false; - NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); - NetMessage.syncPlayers(); - Tools.Broadcast(string.Format("{0} set time to night.", args.Player.Name)); - break; - case "dusk": - Main.dayTime = false; - Main.time = 0.0; - NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); - NetMessage.syncPlayers(); - Tools.Broadcast(string.Format("{0} set time to dusk.", args.Player.Name)); - break; - case "noon": - Main.dayTime = true; - Main.time = 27000.0; - NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); - NetMessage.syncPlayers(); - Tools.Broadcast(string.Format("{0} set time to noon.", args.Player.Name)); - break; - case "midnight": - Main.dayTime = false; - Main.time = 16200.0; - NetMessage.SendData(18, -1, -1, "", 0, 0, Main.sunModY, Main.moonModY); - NetMessage.syncPlayers(); - Tools.Broadcast(string.Format("{0} set time to midnight.", args.Player.Name)); - break; - default: - args.Player.SendMessage("Invalid syntax! Proper syntax: /time ", Color.Red); - break; - } - } - - private static void Kill(CommandArgs args) - { - if (args.Parameters.Count < 1) - { - args.Player.SendMessage("Invalid syntax! Proper syntax: /kill ", Color.Red); - return; - } - - string plStr = String.Join(" ", args.Parameters); - var player = Tools.FindPlayer(plStr); - if (player.Count == 0) - { - args.Player.SendMessage("Invalid player!", Color.Red); - } - else if (player.Count > 1) - { - args.Player.SendMessage("More than one player matched!", Color.Red); - } - else - { - var plr = player[0]; - args.Player.SendMessage(string.Format("You just killed {0}!", plr.Name)); - plr.SendMessage(string.Format("{0} just killed you!", args.Player.Name)); - TShock.PlayerDamage(plr.Index, 999999); - } - } - - private static void Slap(CommandArgs args) - { - if (args.Parameters.Count < 1 || args.Parameters.Count > 2) - { - args.Player.SendMessage( "Invalid syntax! Proper syntax: /slap [dmg]", Color.Red); - return; - } - if (args.Parameters[0].Length == 0) - { - args.Player.SendMessage( "Missing player name", Color.Red); - return; - } - - string plStr = args.Parameters[0]; - var player = Tools.FindPlayer(plStr); - if (player.Count == 0) - { - args.Player.SendMessage( "Invalid player!", Color.Red); - } - else if (player.Count > 1) - { - args.Player.SendMessage( "More than one player matched!", Color.Red); - } - else - { - var plr = player[0]; - int damage = 5; - if (args.Parameters.Count == 2) - { - int.TryParse(args.Parameters[1], out damage); - } - TShock.PlayerDamage(plr.Index, damage); - Tools.Broadcast(string.Format("{0} slapped {1} for {2} damage.", - args.Player.Name, plr.Name, damage)); - } - } - - private static void ToggleAntiBuild(CommandArgs args) - { - ConfigurationManager.DisableBuild = (ConfigurationManager.DisableBuild == false); - Tools.Broadcast(string.Format("Anti-build is now {0}.", (ConfigurationManager.DisableBuild ? "on" : "off"))); - } - - private static void ProtectSpawn(CommandArgs args) - { - ConfigurationManager.SpawnProtect = (ConfigurationManager.SpawnProtect == false); - Tools.Broadcast(string.Format("Spawn is now {0}.", (ConfigurationManager.SpawnProtect ? "protected" : "open"))); - } - - private static void UpdateNow(CommandArgs args) - { - Process TServer = Process.GetCurrentProcess(); - - StreamWriter sw = new StreamWriter("pid"); - sw.Write(TServer.Id); - sw.Close(); - - sw = new StreamWriter("pn"); - sw.Write(TServer.ProcessName + " " + Environment.CommandLine); - sw.Close(); - - WebClient client = new WebClient(); - client.Headers.Add("user-agent", "TShock"); - byte[] updatefile = client.DownloadData("http://tsupdate.shankshock.com/UpdateTShock.exe"); - - BinaryWriter bw = new BinaryWriter(new FileStream("UpdateTShock.exe", FileMode.Create)); - bw.Write(updatefile); - bw.Close(); - - Process.Start(new ProcessStartInfo("UpdateTShock.exe")); - - Tools.ForceKickAll("Server shutting down for update!"); - WorldGen.saveWorld(); - Netplay.disconnect = true; - } - - #endregion Command Methods + #endregion Cheat Comamnds } } \ No newline at end of file diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index 3bfc2066..c24c9b0b 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -33,19 +33,28 @@ namespace TShockAPI public Group Group { get; set; } public bool ReceivedInfo { get; set; } public int Index { get; protected set; } - + public bool RealPlayer + { + get { return Index >= 0 && Index < Main.maxNetPlayers; } + } + public bool ConnectionAlive + { + get { return RealPlayer ? Netplay.serverSock[Index].active && !Netplay.serverSock[Index].kill : false; } + } /// /// Terraria Player /// public Player TPlayer { get; protected set; } - public string Name { get { return TPlayer.name; } } public string IP { - get { return Tools.GetRealIP(Netplay.serverSock[Index].tcpClient.Client.RemoteEndPoint.ToString()); } + get + { + return RealPlayer ? Tools.GetRealIP(Netplay.serverSock[Index].tcpClient.Client.RemoteEndPoint.ToString()) : ""; + } } public bool Active { @@ -55,14 +64,20 @@ namespace TShockAPI { get { return TPlayer.team; } } - public float X { - get { return TPlayer.position.X; } + get + { + + return RealPlayer ? TPlayer.position.X : Main.spawnTileX * 16; + } } public float Y { - get { return TPlayer.position.Y; } + get + { + return RealPlayer ? TPlayer.position.Y : Main.spawnTileY * 16; + } } public int TileX { diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 5e4a5556..a1bbcae4 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -294,7 +294,7 @@ namespace TShockAPI private void GetData(GetDataEventArgs e) { - if (!Netplay.serverSock[e.Msg.whoAmI].active || Netplay.serverSock[e.Msg.whoAmI].kill) + if (!Players[e.Msg.whoAmI].ConnectionAlive) return; if (Main.verboseNetplay) @@ -935,9 +935,9 @@ namespace TShockAPI } } - public static void PlayerDamage(int plr, int damage) + public static void PlayerDamage(TSPlayer player, int damage) { - NetMessage.SendData(26, -1, -1, "", plr, ((new Random()).Next(-1, 1)), damage, (float)0); + NetMessage.SendData(26, -1, -1, "", player.Index, ((new Random()).Next(-1, 1)), damage, (float)0); } //TODO : Notify the player if there is more than one match. (or do we want a First() kinda thing?) diff --git a/TShockAPI/Tools.cs b/TShockAPI/Tools.cs index 31bcbc78..f5984c27 100755 --- a/TShockAPI/Tools.cs +++ b/TShockAPI/Tools.cs @@ -213,7 +213,7 @@ namespace TShockAPI /// string reason public static void ForceKick(TSPlayer player, string reason) { - if (!Netplay.serverSock[player.Index].active || Netplay.serverSock[player.Index].kill) + if (!player.ConnectionAlive) 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)); @@ -226,7 +226,7 @@ namespace TShockAPI /// string reason public static bool Kick(TSPlayer player, string reason, string adminUserName = "") { - if (!Netplay.serverSock[player.Index].active || Netplay.serverSock[player.Index].kill) + if (!player.ConnectionAlive) return true; if (!player.Group.HasPermission("immunetokick")) { @@ -249,7 +249,7 @@ namespace TShockAPI /// string reason public static bool Ban(TSPlayer player, string reason, string adminUserName = "") { - if (!Netplay.serverSock[player.Index].active || Netplay.serverSock[player.Index].kill) + if (!player.ConnectionAlive) return true; if (!player.Group.HasPermission("immunetoban")) { From ad456ac7b624b89b9fba2e8d979c5f46526da557 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 13:20:27 +1000 Subject: [PATCH 07/10] Added sm alias for spawnmob --- TShockAPI/Commands.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index a78a9c7a..9ca2b200 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -196,6 +196,7 @@ namespace TShockAPI ChatCommands.Add(new Command("skeletron", "spawnboss", Skeletron)); ChatCommands.Add(new Command("hardcore", "spawnboss", Hardcore)); ChatCommands.Add(new Command("spawnmob", "spawnmob", SpawnMob)); + ChatCommands.Add(new Command("sm", "spawnmob", SpawnMob)); ChatCommands.Add(new Command("home", "tp", Home)); ChatCommands.Add(new Command("spawn", "tp", Spawn)); ChatCommands.Add(new Command("tp", "tp", TP)); From 6ed2b5044d99a2a40a993599c2d5e00c6971ee99 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 14:03:06 +1000 Subject: [PATCH 08/10] Pulled the duplicated code for handling commands into helper method --- TShockAPI/Commands.cs | 206 +++++++++++++++++++++++++----------------- TShockAPI/TShock.cs | 67 +------------- 2 files changed, 127 insertions(+), 146 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 9ca2b200..e9853157 100755 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -93,89 +93,7 @@ namespace TShockAPI } public static class Commands { - public static List ChatCommands = new List(); - - /// - /// Parses a string of parameters into a list. Handles quotes. - /// - /// - /// - public static List ParseParameters(string str) - { - var ret = new List(); - var sb = new StringBuilder(); - bool instr = false; - for (int i = 0; i < str.Length; i++) - { - char c = str[i]; - - if (instr) - { - if (c == '\\') - { - if (i + 1 >= str.Length) - break; - c = GetEscape(str[++i]); - } - else if (c == '"') - { - ret.Add(sb.ToString()); - sb.Clear(); - instr = false; - continue; - } - sb.Append(c); - } - else - { - if (IsWhiteSpace(c)) - { - if (sb.Length > 0) - { - ret.Add(sb.ToString()); - sb.Clear(); - } - } - else if (c == '"') - { - if (sb.Length > 0) - { - ret.Add(sb.ToString()); - sb.Clear(); - } - instr = true; - } - else - { - sb.Append(c); - } - } - } - if (sb.Length > 0) - ret.Add(sb.ToString()); - - return ret; - } - - static char GetEscape(char c) - { - switch (c) - { - case '\\': - return '\\'; - case '"': - return '"'; - case 't': - return '\t'; - default: - return c; - } - } - - static bool IsWhiteSpace(char c) - { - return c == ' ' || c == '\t' || c == '\n'; - } + private static List ChatCommands = new List(); public static void InitCommands() { @@ -235,6 +153,128 @@ namespace TShockAPI Commands.ChatCommands.Add(new Command("updatenow", "maintenance", Commands.UpdateNow)); } + public static bool HandleCommand(TSPlayer player, string text) + { + string cmdText = text.Remove(0, 1); + + var args = Commands.ParseParameters(cmdText); + if (args.Count < 1) + return false; + + string cmdName = args[0]; + args.RemoveAt(0); + + Command cmd = null; + foreach (Command command in Commands.ChatCommands) + { + if (command.Name.Equals(cmdName)) + { + cmd = command; + } + } + + if (cmd == null) + { + player.SendMessage("That command does not exist, try /help", Color.Red); + } + else + { + if (!cmd.CanRun(player)) + { + Tools.SendLogs(string.Format("{0} tried to execute {1}", player.Name, cmd.Name), Color.Red); + player.SendMessage("You do not have access to that command.", Color.Red); + } + else + { + Tools.SendLogs(string.Format("{0} executed: /{1}", player.Name, cmdText), Color.Red); + cmd.Run(cmdText, player, args); + } + } + return true; + } + + /// + /// Parses a string of parameters into a list. Handles quotes. + /// + /// + /// + private static List ParseParameters(string str) + { + var ret = new List(); + var sb = new StringBuilder(); + bool instr = false; + for (int i = 0; i < str.Length; i++) + { + char c = str[i]; + + if (instr) + { + if (c == '\\') + { + if (i + 1 >= str.Length) + break; + c = GetEscape(str[++i]); + } + else if (c == '"') + { + ret.Add(sb.ToString()); + sb.Clear(); + instr = false; + continue; + } + sb.Append(c); + } + else + { + if (IsWhiteSpace(c)) + { + if (sb.Length > 0) + { + ret.Add(sb.ToString()); + sb.Clear(); + } + } + else if (c == '"') + { + if (sb.Length > 0) + { + ret.Add(sb.ToString()); + sb.Clear(); + } + instr = true; + } + else + { + sb.Append(c); + } + } + } + if (sb.Length > 0) + ret.Add(sb.ToString()); + + return ret; + } + + private static char GetEscape(char c) + { + switch (c) + { + case '\\': + return '\\'; + case '"': + return '"'; + case 't': + return '\t'; + default: + return c; + } + } + + private static bool IsWhiteSpace(char c) + { + return c == ' ' || c == '\t' || c == '\n'; + } + #region Player Management Commands private static void Kick(CommandArgs args) diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index a1bbcae4..1e726df4 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -249,33 +249,8 @@ namespace TShockAPI } else if (text.StartsWith("/")) { - string cmdStr = text.Remove(0, 1); - var args = Commands.ParseParameters(cmdStr); - if (args.Count < 1) - return; - - string scmd = args[0]; - args.RemoveAt(0); - - Command cmd = null; - for (int i = 0; i < Commands.ChatCommands.Count; i++) - { - if (Commands.ChatCommands[i].Name.Equals(scmd)) - { - cmd = Commands.ChatCommands[i]; - } - } - - if (cmd == null) - { - TSPlayer.Server.SendMessage("That command does not exist, try /help"); - } - else - { - Tools.SendLogs(string.Format("{0} executed: /{1}", TSPlayer.Server.Name, cmdStr), Color.Red); - cmd.Run(cmdStr, TSPlayer.Server, args); - } - e.Handled = true; + if (Commands.HandleCommand(TSPlayer.Server, text)) + e.Handled = true; } } @@ -685,42 +660,8 @@ namespace TShockAPI if (text.StartsWith("/")) { - text = text.Remove(0, 1); - - var args = Commands.ParseParameters(text); - if (args.Count < 1) - return; - - string scmd = args[0]; - args.RemoveAt(0); - - Command cmd = null; - for (int i = 0; i < Commands.ChatCommands.Count; i++) - { - if (Commands.ChatCommands[i].Name.Equals(scmd)) - { - cmd = Commands.ChatCommands[i]; - } - } - - if (cmd == null) - { - tsplr.SendMessage("That command does not exist, try /help", Color.Red); - } - else - { - if (!cmd.CanRun(tsplr)) - { - Tools.SendLogs(string.Format("{0} tried to execute {1}", tsplr.Name, cmd.Name), Color.Red); - tsplr.SendMessage("You do not have access to that command.", Color.Red); - } - else - { - Tools.SendLogs(string.Format("{0} executed: /{1}", tsplr.Name, text), Color.Red); - cmd.Run(text, tsplr, args); - } - } - e.Handled = true; + if (Commands.HandleCommand(tsplr, text)) + e.Handled = true; } else { From 8d1377c7cf293ab6e79f0c5ec7cb93a67927396f Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 16:17:00 +1000 Subject: [PATCH 09/10] =?UTF-8?q?=E0=B2=A0=5F=20=E0=B2=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TShockAPI/TSPlayer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index c24c9b0b..92de581e 100644 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -85,7 +85,7 @@ namespace TShockAPI } public int TileY { - get { return (int)(X / 16); } + get { return (int)(Y / 16); } } public TSPlayer(int index) From f3be17a9117246c782280361188878d2f54e1a16 Mon Sep 17 00:00:00 2001 From: ricky Date: Thu, 16 Jun 2011 21:00:54 +1000 Subject: [PATCH 10/10] Moved all GetData checks to GetDataHandlers Fixed GetBanByName not working --- TShockAPI/GetDataHandlers.cs | 426 +++++++++++++++++++++ TShockAPI/TShock.cs | 723 ++++++++--------------------------- TShockAPI/TShockAPI.csproj | 1 + 3 files changed, 596 insertions(+), 554 deletions(-) create mode 100755 TShockAPI/GetDataHandlers.cs diff --git a/TShockAPI/GetDataHandlers.cs b/TShockAPI/GetDataHandlers.cs new file mode 100755 index 00000000..9f43c8d1 --- /dev/null +++ b/TShockAPI/GetDataHandlers.cs @@ -0,0 +1,426 @@ +/* +TShock, a server mod for Terraria +Copyright (C) 2011 The TShock Team + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Text; +using Microsoft.Xna.Framework; +using StreamBinary; +using Terraria; +using TerrariaAPI; + +namespace TShockAPI +{ + public delegate bool GetDataHandlerDelegate(GetDataHandlerArgs args); + public class GetDataHandlerArgs : EventArgs + { + public TSPlayer Player { get; private set; } + public MemoryStream Data { get; private set; } + + public Player TPlayer + { + get { return Player.TPlayer; } + } + + public GetDataHandlerArgs(TSPlayer player, MemoryStream data) + { + Player = player; + Data = data; + } + } + public static class GetDataHandlers + { + private static Dictionary GetDataHandlerDelegates; + private static bool[] BlacklistTiles; + + public static void InitGetDataHandler() + { + #region Blacklisted tiles + + BlacklistTiles = new bool[Main.maxTileSets]; + BlacklistTiles[0] = true; + BlacklistTiles[1] = true; + BlacklistTiles[2] = true; + BlacklistTiles[6] = true; + BlacklistTiles[7] = true; + BlacklistTiles[8] = true; + BlacklistTiles[9] = true; + BlacklistTiles[22] = true; + BlacklistTiles[23] = true; + BlacklistTiles[25] = true; + BlacklistTiles[30] = true; + BlacklistTiles[37] = true; + BlacklistTiles[38] = true; + BlacklistTiles[39] = true; + BlacklistTiles[40] = true; + BlacklistTiles[41] = true; + BlacklistTiles[43] = true; + BlacklistTiles[44] = true; + BlacklistTiles[45] = true; + BlacklistTiles[46] = true; + BlacklistTiles[47] = true; + BlacklistTiles[53] = true; + BlacklistTiles[54] = true; + BlacklistTiles[56] = true; + BlacklistTiles[57] = true; + BlacklistTiles[58] = true; + BlacklistTiles[59] = true; + BlacklistTiles[60] = true; + BlacklistTiles[63] = true; + BlacklistTiles[64] = true; + BlacklistTiles[65] = true; + BlacklistTiles[66] = true; + BlacklistTiles[67] = true; + BlacklistTiles[68] = true; + BlacklistTiles[70] = true; + BlacklistTiles[75] = true; + BlacklistTiles[76] = true; + + #endregion Blacklisted tiles + + GetDataHandlerDelegates = new Dictionary + { + {PacketTypes.PlayerInfo, HandlePlayerInfo}, + {PacketTypes.TileSendSection, HandleSendSection}, + {PacketTypes.PlayerUpdate, HandlePlayerUpdate}, + {PacketTypes.Tile, HandleTile}, + {PacketTypes.TileSendSquare, HandleSendTileSquare}, + {PacketTypes.NPCUpdate, HandleNpcUpdate}, + {PacketTypes.PlayerDamage, HandlePlayerDamage}, + {PacketTypes.ProjectileNew, HandleProjectileNew}, + {PacketTypes.TogglePVP, HandleTogglePvp}, + {PacketTypes.TileKill, HandleTileKill}, + {PacketTypes.PlayerKillMe, HandlePlayerKillMe}, + {PacketTypes.LiquidSet, HandleLiquidSet}, + }; + } + + public static bool HandlerGetData(PacketTypes type, TSPlayer player, MemoryStream data) + { + GetDataHandlerDelegate handler; + if (GetDataHandlerDelegates.TryGetValue(type, out handler)) + { + try + { + return handler(new GetDataHandlerArgs(player, data)); + } + catch (Exception ex) + { + Log.Error(ex.ToString()); + } + } + return false; + } + + private static bool HandlePlayerInfo(GetDataHandlerArgs args) + { + byte playerid = args.Data.ReadInt8(); + byte hair = args.Data.ReadInt8(); + Color hairColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8()); + Color skinColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8()); + Color eyeColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8()); + Color shirtColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8()); + Color underShirtColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8()); + Color pantsColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8()); + Color shoeColor = new Color(args.Data.ReadInt8(), args.Data.ReadInt8(), args.Data.ReadInt8()); + string name = Encoding.ASCII.GetString(args.Data.ReadBytes((int) (args.Data.Length - args.Data.Position - 1))); + + if (hair >= Main.maxHair) + { + Tools.ForceKick(args.Player, "Hair crash exploit."); + return true; + } + + if (name.Length > 32) + { + Tools.ForceKick(args.Player, "Name exceeded 32 characters."); + return true; + } + if (name.Trim().Length == 0) + { + Tools.ForceKick(args.Player, "Empty Name."); + return true; + } + var ban = TShock.Bans.GetBanByName(name); + if (ban != null) + { + Tools.ForceKick(args.Player, string.Format("You are banned: {0}", ban.Reason)); + return true; + } + if (args.Player.ReceivedInfo) + { + return Tools.HandleGriefer(args.Player, "Sent client info more than once"); + } + + args.Player.ReceivedInfo = true; + return false; + } + + private static bool HandleSendTileSquare(GetDataHandlerArgs args) + { + short size = args.Data.ReadInt16(); + int x = args.Data.ReadInt32(); + int y = args.Data.ReadInt32(); + int plyX = Math.Abs(args.Player.TileX); + int plyY = Math.Abs(args.Player.TileY); + int tileX = Math.Abs(x); + int tileY = Math.Abs(y); + if (size > 5 || (ConfigurationManager.RangeChecks && (Math.Abs(plyX - tileX) > 32 || Math.Abs(plyY - tileY) > 32))) + { + Log.Debug(string.Format("SendTileSquare(PlyXY:{0}_{1}, TileXY:{2}_{3}, Result:{4}_{5}, Size:{6})", + plyX, plyY, tileX, tileY, Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), size)); + return Tools.HandleGriefer(args.Player, "Send Tile Square Abuse"); + } + return false; + } + + private static bool HandleTile(GetDataHandlerArgs args) + { + byte type = args.Data.ReadInt8(); + int x = args.Data.ReadInt32(); + int y = args.Data.ReadInt32(); + byte tiletype = args.Data.ReadInt8(); + if (type == 1 || type == 3) + { + int plyX = Math.Abs(args.Player.TileX); + int plyY = Math.Abs(args.Player.TileY); + int tileX = Math.Abs(x); + int tileY = Math.Abs(y); + + if (ConfigurationManager.RangeChecks && ((Math.Abs(plyX - tileX) > 32) || (Math.Abs(plyY - tileY) > 32))) + { + Log.Debug(string.Format("TilePlaced(PlyXY:{0}_{1}, TileXY:{2}_{3}, Result:{4}_{5}, Type:{6})", + plyX, plyY, tileX, tileY, Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), tiletype)); + return Tools.HandleGriefer(args.Player, "Placing impossible to place blocks."); + } + } + if (ConfigurationManager.DisableBuild) + { + if (!args.Player.Group.HasPermission("editspawn")) + { + args.Player.SendMessage("World protected from changes.", Color.Red); + TShock.SendTileSquare(args.Player, x, y); + return true; + } + } + if (ConfigurationManager.SpawnProtect) + { + if (!args.Player.Group.HasPermission("editspawn")) + { + var flag = TShock.CheckSpawn(x, y); + if (flag) + { + args.Player.SendMessage("Spawn protected from changes.", Color.Red); + TShock.SendTileSquare(args.Player, x, y); + return true; + } + } + } + if (type == 0 && BlacklistTiles[Main.tile[x, y].type] && args.Player.Active) + { + args.Player.TileThreshold++; + var coords = new Vector2(x, y); + if (!args.Player.TilesDestroyed.ContainsKey(coords)) + args.Player.TilesDestroyed.Add(coords, Main.tile[x, y]); + } + + return false; + } + + private static bool HandleTogglePvp(GetDataHandlerArgs args) + { + int id = args.Data.ReadByte(); + bool pvp = args.Data.ReadBoolean(); + + args.TPlayer.hostile = pvp; + if (id != args.Player.Index) + args.TPlayer.hostile = true; + if (ConfigurationManager.PermaPvp) + args.TPlayer.hostile = true; + NetMessage.SendData(30, -1, -1, "", args.Player.Index); + return true; + } + + private static bool HandleSendSection(GetDataHandlerArgs args) + { + return Tools.HandleGriefer(args.Player, "SendSection abuse."); + } + + private static bool HandleNpcUpdate(GetDataHandlerArgs args) + { + return Tools.HandleGriefer(args.Player, "Spawn NPC abuse"); + } + + private static bool HandlePlayerUpdate(GetDataHandlerArgs args) + { + byte plr = args.Data.ReadInt8(); + byte control = args.Data.ReadInt8(); + byte item = args.Data.ReadInt8(); + float posx = args.Data.ReadSingle(); + float posy = args.Data.ReadSingle(); + float velx = args.Data.ReadSingle(); + float vely = args.Data.ReadSingle(); + + if (Main.verboseNetplay) + Debug.WriteLine("Update: {{{0},{1}}} {{{2}, {3}}}", (int)posx, (int)posy, (int)velx, (int)vely); + + if (plr != args.Player.Index) + { + return Tools.HandleGriefer(args.Player, "Update Player abuse"); + } + return false; + } + + private static bool HandleProjectileNew(GetDataHandlerArgs args) + { + short ident = args.Data.ReadInt16(); + float posx = args.Data.ReadSingle(); + float posy = args.Data.ReadSingle(); + float velx = args.Data.ReadSingle(); + float vely = args.Data.ReadSingle(); + float knockback = args.Data.ReadSingle(); + short dmg = args.Data.ReadInt16(); + byte owner = args.Data.ReadInt8(); + byte type = args.Data.ReadInt8(); + + if (type == 29 || type == 28 || type == 37) + { + Log.Debug(string.Format("Explosive(PlyXY:{0}_{1}, Type:{2})", args.Player.TileX, args.Player.TileY, type)); + return Tools.HandleExplosivesUser(args.Player, "Throwing an explosive device."); + } + return false; + } + + private static bool HandlePlayerKillMe(GetDataHandlerArgs args) + { + byte id = args.Data.ReadInt8(); + byte hitdirection = args.Data.ReadInt8(); + short dmg = args.Data.ReadInt16(); + bool pvp = args.Data.ReadBoolean(); + + if (id != args.Player.Index) + { + return Tools.HandleGriefer(args.Player, "Trying to execute KillMe on someone else."); + } + return false; + } + + private static bool HandlePlayerDamage(GetDataHandlerArgs args) + { + byte playerid = args.Data.ReadInt8(); + byte direction = args.Data.ReadInt8(); + Int16 damage = args.Data.ReadInt16(); + byte pvp = args.Data.ReadInt8(); + + return !TShock.Players[playerid].TPlayer.hostile; + } + + private static bool HandleLiquidSet(GetDataHandlerArgs args) + { + int x = args.Data.ReadInt32(); + int y = args.Data.ReadInt32(); + byte liquid = args.Data.ReadInt8(); + bool lava = args.Data.ReadBoolean(); + + //The liquid was picked up. + if (liquid == 0) + return false; + + int plyX = Math.Abs(args.Player.TileX); + int plyY = Math.Abs(args.Player.TileY); + int tileX = Math.Abs(x); + int tileY = Math.Abs(y); + + bool bucket = false; + for (int i = 0; i < 44; i++) + { + if (args.TPlayer.inventory[i].type >= 205 && args.TPlayer.inventory[i].type <= 207) + { + bucket = true; + break; + } + } + + if (lava && !args.Player.Group.HasPermission("canlava")) + { + args.Player.SendMessage("You do not have permission to use lava", Color.Red); + Tools.SendLogs(string.Format("{0} tried using lava", args.Player.Name), Color.Red); + // TODO need to revert player changes so they are insync with server + return true; + } + if (!lava && !args.Player.Group.HasPermission("canwater")) + { + args.Player.SendMessage("You do not have permission to use water", Color.Red); + Tools.SendLogs(string.Format("{0} tried using water", args.Player.Name), Color.Red); + // TODO need to revert player changes so they are insync with server + return true; + } + + if (!bucket) + { + Log.Debug(string.Format("{0}(PlyXY:{1}_{2}, TileXY:{3}_{4}, Result:{5}_{6}, Amount:{7})", + lava ? "Lava" : "Water", plyX, plyY, tileX, tileY, + Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), liquid)); + return Tools.HandleGriefer(args.Player, "Manipulating liquid without bucket."); ; + } + if (ConfigurationManager.RangeChecks && ((Math.Abs(plyX - tileX) > 32) || (Math.Abs(plyY - tileY) > 32))) + { + Log.Debug(string.Format("Liquid(PlyXY:{0}_{1}, TileXY:{2}_{3}, Result:{4}_{5}, Amount:{6})", + plyX, plyY, + tileX, tileY, + Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), + liquid + )); + return Tools.HandleGriefer(args.Player, "Placing impossible to place liquid."); ; + } + + if (ConfigurationManager.SpawnProtect) + { + if (!args.Player.Group.HasPermission("editspawn")) + { + var flag = TShock.CheckSpawn(x, y); + if (flag) + { + args.Player.SendMessage("The spawn is protected!", Color.Red); + // TODO need to revert player changes so they are insync with server + return true; + } + } + } + return false; + } + + private static bool HandleTileKill(GetDataHandlerArgs args) + { + int tilex = args.Data.ReadInt32(); + int tiley = args.Data.ReadInt32(); + if (tilex < 0 || tilex >= Main.maxTilesX || tiley < 0 || tiley >= Main.maxTilesY) + return false; + + if (Main.tile[tilex, tiley].type != 0x15) //Chest + { + Log.Debug(string.Format("TileKill(TileXY:{0}_{1}, Type:{2})", + tilex, tiley, Main.tile[tilex, tiley].type)); + Tools.ForceKick(args.Player, string.Format("Tile Kill abuse ({0})", Main.tile[tilex, tiley].type)); + return true; + } + return false; + } + } +} diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 1e726df4..fd18f449 100755 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -20,10 +20,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Net; -using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; -using StreamBinary; using Terraria; using TerrariaAPI; using TerrariaAPI.Hooks; @@ -41,13 +39,8 @@ namespace TShockAPI public static readonly string VersionCodename = "Forgot to increase the version."; - private static bool[] BlacklistTiles; - public static BanManager Bans = new BanManager(Path.Combine(SavePath, "bans.txt")); - delegate bool HandleGetDataD(MemoryStream data, GetDataEventArgs e); - Dictionary GetDataFuncs; - public override Version Version { get { return VersionNum; } @@ -68,73 +61,9 @@ namespace TShockAPI get { return "The administration modification of the future."; } } - static TShock() - { - //Tools.LoadGroups(); - - #region Blacklisted tiles - - BlacklistTiles = new bool[0x80]; - BlacklistTiles[0] = true; - BlacklistTiles[1] = true; - BlacklistTiles[2] = true; - BlacklistTiles[6] = true; - BlacklistTiles[7] = true; - BlacklistTiles[8] = true; - BlacklistTiles[9] = true; - BlacklistTiles[22] = true; - BlacklistTiles[23] = true; - BlacklistTiles[25] = true; - BlacklistTiles[30] = true; - BlacklistTiles[37] = true; - BlacklistTiles[38] = true; - BlacklistTiles[39] = true; - BlacklistTiles[40] = true; - BlacklistTiles[41] = true; - BlacklistTiles[43] = true; - BlacklistTiles[44] = true; - BlacklistTiles[45] = true; - BlacklistTiles[46] = true; - BlacklistTiles[47] = true; - BlacklistTiles[53] = true; - BlacklistTiles[54] = true; - BlacklistTiles[56] = true; - BlacklistTiles[57] = true; - BlacklistTiles[58] = true; - BlacklistTiles[59] = true; - BlacklistTiles[60] = true; - BlacklistTiles[63] = true; - BlacklistTiles[64] = true; - BlacklistTiles[65] = true; - BlacklistTiles[66] = true; - BlacklistTiles[67] = true; - BlacklistTiles[68] = true; - BlacklistTiles[70] = true; - BlacklistTiles[75] = true; - BlacklistTiles[76] = true; - - #endregion Blacklisted tiles - } - public TShock(Main game) : base(game) { - - GetDataFuncs = new Dictionary - { - {(byte)PacketTypes.PlayerInfo, HandlePlayerInfo}, - {(byte)PacketTypes.TileSendSection, HandleSendSection}, - {(byte)PacketTypes.PlayerUpdate, HandlePlayerUpdate}, - {(byte)PacketTypes.Tile, HandleTile}, - {(byte)PacketTypes.TileSendSquare, HandleSendTileSquare}, - {(byte)PacketTypes.NPCUpdate, HandleNpcUpdate}, - {(byte)PacketTypes.PlayerDamage, HandlePlayerDamage}, - {(byte)PacketTypes.ProjectileNew, HandleProjectileNew}, - {(byte)PacketTypes.TogglePVP, HandleTogglePvp}, - {(byte)PacketTypes.TileKill, HandleTileKill}, - {(byte)PacketTypes.PlayerKillMe, HandlePlayerKillMe}, - {(byte)PacketTypes.LiquidSet, HandleLiquidSet}, - }; } public override void Initialize() @@ -150,13 +79,13 @@ namespace TShockAPI GameHooks.PostInitialize += OnPostInit; GameHooks.Update += OnUpdate; - ServerHooks.Chat += OnChat; ServerHooks.Join += OnJoin; ServerHooks.Leave += OnLeave; + ServerHooks.Chat += OnChat; + ServerHooks.Command += ServerHooks_OnCommand; NetHooks.GetData += GetData; NetHooks.GreetPlayer += OnGreetPlayer; NpcHooks.StrikeNpc += NpcHooks_OnStrikeNpc; - ServerHooks.Command += ServerHooks_OnCommand; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Log.Info("Hooks initialized"); @@ -164,6 +93,9 @@ namespace TShockAPI Bans.LoadBans(); Log.Info("Bans initialized"); + GetDataHandlers.InitGetDataHandler(); + Log.Info("Get data handlers initialized"); + Commands.InitCommands(); Log.Info("Commands initialized"); @@ -175,9 +107,9 @@ namespace TShockAPI Bans.SaveBans(); GameHooks.PostInitialize -= OnPostInit; GameHooks.Update -= OnUpdate; - ServerHooks.Chat -= OnChat; ServerHooks.Join -= OnJoin; ServerHooks.Leave -= OnLeave; + ServerHooks.Chat -= OnChat; ServerHooks.Command -= ServerHooks_OnCommand; NetHooks.GetData -= GetData; NetHooks.GreetPlayer -= OnGreetPlayer; @@ -203,11 +135,151 @@ namespace TShockAPI Log.Error(e.ExceptionObject.ToString()); } + private void HandleCommandLine(string[] parms) + { + for (int i = 0; i < parms.Length; i++) + { + if (parms[i].ToLower() == "-ip") + { + IPAddress ip; + if (IPAddress.TryParse(parms[++i], out ip)) + { + Netplay.serverListenIP = ip; + Console.Write("Using IP: {0}", ip); + } + else + { + Console.WriteLine("Bad IP: {0}", parms[i]); + } + } + } + } + /* * Hooks: * */ + private void OnPostInit() + { + if (!File.Exists(Path.Combine(SavePath, "auth.lck"))) + { + var r = new Random((int)DateTime.Now.ToBinary()); + ConfigurationManager.AuthToken = r.Next(100000, 10000000); + Console.WriteLine("TShock Notice: To become SuperAdmin, join the game and type /auth " + + ConfigurationManager.AuthToken); + Console.WriteLine("This token will only display ONCE. This only works ONCE. If you don't use it and the server goes down, delete auth.lck."); + FileTools.CreateFile(Path.Combine(SavePath, "auth.lck")); + } + } + + private void OnUpdate(GameTime time) + { + UpdateManager.UpdateProcedureCheck(); + foreach (TSPlayer player in TShock.Players) + { + if (player != null && player.Active) + { + if (player.TileThreshold >= 20) + { + if (Tools.HandleTntUser(player, "Kill tile abuse detected.")) + { + RevertKillTile(player); + player.TileThreshold = 0; + player.TilesDestroyed.Clear(); + } + else if (player.TileThreshold > 0) + { + player.TileThreshold = 0; + player.TilesDestroyed.Clear(); + } + + } + else if (player.TileThreshold > 0) + { + player.TileThreshold = 0; + } + } + } + } + + private void OnJoin(int ply, HandledEventArgs handler) + { + if (Main.netMode != 2 || handler.Handled) + return; + + var player = new TSPlayer(ply); + player.Group = Tools.GetGroupForIP(player.IP); + + if (Tools.ActivePlayers() + 1 > ConfigurationManager.MaxSlots && + !player.Group.HasPermission("reservedslot")) + { + Tools.ForceKick(player, "Server is full"); + handler.Handled = true; + } + else + { + var ban = Bans.GetBanByIp(player.IP); + if (ban != null) + { + Tools.ForceKick(player, string.Format("You are banned: {0}", ban.Reason)); + handler.Handled = true; + } + else if (!FileTools.OnWhitelist(player.IP)) + { + Tools.ForceKick(player, "Not on whitelist."); + handler.Handled = true; + } + } + + Players[ply] = player; + Netplay.serverSock[ply].spamCheck = ConfigurationManager.SpamChecks; + } + + private void OnLeave(int ply) + { + if (Main.netMode != 2) + return; + + var tsplr = Players[ply]; + if (tsplr != null && tsplr.ReceivedInfo) + Log.Info(string.Format("{0} left.", tsplr.Name)); + + Players[ply] = null; + } + + private void OnChat(messageBuffer msg, int ply, string text, HandledEventArgs e) + { + if (Main.netMode != 2) + return; + + if (msg.whoAmI != ply) + { + e.Handled = Tools.HandleGriefer(Players[ply], "Faking Chat"); + return; + } + + var tsplr = Players[msg.whoAmI]; + + if (tsplr.Group.HasPermission("adminchat") && !text.StartsWith("/")) + { + Tools.Broadcast(ConfigurationManager.AdminChatPrefix + "<" + tsplr.Name + "> " + text, + (byte)ConfigurationManager.AdminChatRGB[0], (byte)ConfigurationManager.AdminChatRGB[1], (byte)ConfigurationManager.AdminChatRGB[2]); + e.Handled = true; + return; + } + + if (text.StartsWith("/")) + { + if (Commands.HandleCommand(tsplr, text)) + e.Handled = true; + } + else + { + Log.Info(string.Format("{0} said: {1}", tsplr.Name, text)); + } + } + /// /// When a server command is run. /// @@ -255,363 +327,31 @@ namespace TShockAPI } - private void NpcHooks_OnStrikeNpc(NpcStrikeEventArgs e) - { - if (ConfigurationManager.InfiniteInvasion) - { - IncrementKills(); - if (Main.invasionSize < 10) - { - Main.invasionSize = 20000000; - } - } - } - private void GetData(GetDataEventArgs e) { - if (!Players[e.Msg.whoAmI].ConnectionAlive) + PacketTypes type = (PacketTypes) e.MsgID; + TSPlayer player = Players[e.Msg.whoAmI]; + + if (!player.ConnectionAlive) return; if (Main.verboseNetplay) - Debug.WriteLine("{0:X} ({2}): {3} ({1:XX})", e.Msg.whoAmI, e.MsgID, Main.player[e.Msg.whoAmI].dead ? "dead " : "alive", MsgNames[e.MsgID]); + Debug.WriteLine("{0:X} ({2}): {3} ({1:XX})", player.Index, (byte) type, player.TPlayer.dead ? "dead " : "alive", type.ToString()); - HandleGetDataD func; - if (GetDataFuncs.TryGetValue(e.MsgID, out func)) + using (var data = new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)) { - using (var data = new MemoryStream(e.Msg.readBuffer, e.Index, e.Length)) + try { - try - { - e.Handled = func(data, e); - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } + if (GetDataHandlers.HandlerGetData(type, player, data)) + e.Handled = true; + } + catch (Exception ex) + { + Log.Error(ex.ToString()); } } } - bool HandlePlayerInfo(MemoryStream data, GetDataEventArgs e) - { - 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(player, string.Format("You are banned: {0}", ban.Reason)); - return true; - } - byte hair = e.Msg.readBuffer[e.Index + 1]; - if (hair > 0x10) - { - 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(player, "Name exceeded 32 characters."); - return true; - } - if (name.Trim().Length == 0) - { - Tools.ForceKick(player, "Empty Name."); - return true; - } - if (player.ReceivedInfo) - { - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Sent client info more than once"); - } - - player.ReceivedInfo = true; - return false; - } - - bool HandleSendTileSquare(MemoryStream data, GetDataEventArgs e) - { - short size = data.ReadInt16(); - int x = data.ReadInt32(); - int y = data.ReadInt32(); - int plyX = Math.Abs((int)Main.player[e.Msg.whoAmI].position.X / 16); - int plyY = Math.Abs((int)Main.player[e.Msg.whoAmI].position.Y / 16); - int tileX = Math.Abs(x); - int tileY = Math.Abs(y); - if (size > 5 || (ConfigurationManager.RangeChecks && (Math.Abs(plyX - tileX) > 32 || Math.Abs(plyY - tileY) > 32))) - { - Log.Debug(string.Format("SendTileSquare(PlyXY:{0}_{1}, TileXY:{2}_{3}, Result:{4}_{5}, Size:{6})", - plyX, plyY, - tileX, tileY, - Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), - size - )); - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Send Tile Square Abuse"); - } - return false; - } - - bool HandleTile(MemoryStream data, GetDataEventArgs e) - { - byte type = data.ReadInt8(); - int x = data.ReadInt32(); - int y = data.ReadInt32(); - byte tiletype = data.ReadInt8(); - if (type == 1 || type == 3) - { - int plyX = Math.Abs((int)Main.player[e.Msg.whoAmI].position.X / 16); - int plyY = Math.Abs((int)Main.player[e.Msg.whoAmI].position.Y / 16); - int tileX = Math.Abs(x); - int tileY = Math.Abs(y); - - if (Players[e.Msg.whoAmI].TPlayer.selectedItem == 0x72) //Dirt Rod - { - return Tools.Kick(Players[e.Msg.whoAmI], "Using dirt rod"); - } - - if (ConfigurationManager.RangeChecks && ((Math.Abs(plyX - tileX) > 32) || (Math.Abs(plyY - tileY) > 32))) - { - Log.Debug(string.Format("TilePlaced(PlyXY:{0}_{1}, TileXY:{2}_{3}, Result:{4}_{5}, Type:{6})", - plyX, plyY, - tileX, tileY, - Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), - tiletype - )); - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Placing impossible to place blocks."); - } - } - if (ConfigurationManager.DisableBuild) - { - if (!Players[e.Msg.whoAmI].Group.HasPermission("editspawn")) - { - Players[e.Msg.whoAmI].SendMessage("World protected from changes.", Color.Red); - RevertPlayerChanges(e.Msg.whoAmI, x, y); - return true; - } - } - if (ConfigurationManager.SpawnProtect) - { - if (!Players[e.Msg.whoAmI].Group.HasPermission("editspawn")) - { - var flag = CheckSpawn(x, y); - if (flag) - { - Players[e.Msg.whoAmI].SendMessage("Spawn protected from changes.", Color.Red); - RevertPlayerChanges(e.Msg.whoAmI, x, y); - return true; - } - } - } - - if (type == 0 && BlacklistTiles[Main.tile[x, y].type] && Main.player[e.Msg.whoAmI].active) - { - Players[e.Msg.whoAmI].TileThreshold++; - var coords = new Vector2(x, y); - if (!Players[e.Msg.whoAmI].TilesDestroyed.ContainsKey(coords)) - Players[e.Msg.whoAmI].TilesDestroyed.Add(coords, Main.tile[x, y]); - } - - return false; - } - - private static void RevertPlayerChanges(int player, int x, int y) - { - NetMessage.SendData(20, player, -1, "", 10, (float)(x - 5), (float)(y - 5), 0f); - } - - bool HandleTogglePvp(MemoryStream data, GetDataEventArgs e) - { - int id = data.ReadByte(); - bool pvp = data.ReadBoolean(); - - Main.player[e.Msg.whoAmI].hostile = pvp; - if (id != e.Msg.whoAmI) - Main.player[e.Msg.whoAmI].hostile = true; - if (ConfigurationManager.PermaPvp) - Main.player[e.Msg.whoAmI].hostile = true; - NetMessage.SendData(30, -1, -1, "", e.Msg.whoAmI); - return true; - } - - bool HandleSendSection(MemoryStream data, GetDataEventArgs e) - { - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "SendSection abuse."); - } - - bool HandleNpcUpdate(MemoryStream data, GetDataEventArgs e) - { - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Spawn NPC abuse"); - } - - bool HandlePlayerUpdate(MemoryStream data, GetDataEventArgs e) - { - byte plr = data.ReadInt8(); - byte control = data.ReadInt8(); - byte item = data.ReadInt8(); - float posx = data.ReadSingle(); - float posy = data.ReadSingle(); - float velx = data.ReadSingle(); - float vely = data.ReadSingle(); - - if (Main.verboseNetplay) - Debug.WriteLine("Update: {{{0},{1}}} {{{2}, {3}}}", (int)posx, (int)posy, (int)velx, (int)vely); - - if (plr != e.Msg.whoAmI) - { - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Update Player abuse"); - } - return false; - } - - bool HandleProjectileNew(MemoryStream data, GetDataEventArgs e) - { - short ident = data.ReadInt16(); - float posx = data.ReadSingle(); - float posy = data.ReadSingle(); - float velx = data.ReadSingle(); - float vely = data.ReadSingle(); - float knockback = data.ReadSingle(); - short dmg = data.ReadInt16(); - byte owner = data.ReadInt8(); - byte type = data.ReadInt8(); - - if (type == 29 || type == 28 || type == 37) - { - var plr = Main.player[e.Msg.whoAmI]; - Log.Debug(string.Format("Explosive(PlyXY:{0}_{1}, Type:{2})", - (int)(plr.position.X / 16), - (int)(plr.position.Y / 16), - type - )); - return Tools.HandleExplosivesUser(Players[e.Msg.whoAmI], "Throwing an explosive device."); - } - return false; - } - - bool HandlePlayerKillMe(MemoryStream data, GetDataEventArgs e) - { - byte id = data.ReadInt8(); - byte hitdirection = data.ReadInt8(); - short dmg = data.ReadInt16(); - bool pvp = data.ReadBoolean(); - - if (id != e.Msg.whoAmI) - { - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Trying to execute KillMe on someone else."); - } - return false; - } - - bool HandlePlayerDamage(MemoryStream data, GetDataEventArgs e) - { - byte playerid = data.ReadInt8(); - byte direction = data.ReadInt8(); - Int16 damage = data.ReadInt16(); - byte pvp = data.ReadInt8(); - - return !Main.player[playerid].hostile; - } - - bool HandleLiquidSet(MemoryStream data, GetDataEventArgs e) - { - int x = data.ReadInt32(); - int y = data.ReadInt32(); - byte liquid = data.ReadInt8(); - bool lava = data.ReadBoolean(); - - //The liquid was picked up. - if (liquid == 0) - return false; - - int plyX = Math.Abs((int)Main.player[e.Msg.whoAmI].position.X / 16); - int plyY = Math.Abs((int)Main.player[e.Msg.whoAmI].position.Y / 16); - int tileX = Math.Abs(x); - int tileY = Math.Abs(y); - - bool bucket = false; - for (int i = 0; i < 44; i++) - { - if (Main.player[e.Msg.whoAmI].inventory[i].type >= 205 && Main.player[e.Msg.whoAmI].inventory[i].type <= 207) - { - bucket = true; - break; - } - } - - if (lava && !Players[e.Msg.whoAmI].Group.HasPermission("canlava")) - { - Players[e.Msg.whoAmI].SendMessage("You do not have permission to use lava", Color.Red); - Tools.SendLogs(string.Format("{0} tried using lava", Main.player[e.Msg.whoAmI].name), Color.Red); - - return true; - } - if (!lava && !Players[e.Msg.whoAmI].Group.HasPermission("canwater")) - { - Players[e.Msg.whoAmI].SendMessage("You do not have permission to use water", Color.Red); - Tools.SendLogs(string.Format("{0} tried using water", Main.player[e.Msg.whoAmI].name), Color.Red); - return true; - } - - if (!bucket) - { - Log.Debug(string.Format("{0}(PlyXY:{1}_{2}, TileXY:{3}_{4}, Result:{5}_{6}, Amount:{7})", - lava ? "Lava" : "Water", - plyX, plyY, - tileX, tileY, - Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), - liquid - )); - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Manipulating liquid without bucket."); ; - } - if (ConfigurationManager.RangeChecks && ((Math.Abs(plyX - tileX) > 32) || (Math.Abs(plyY - tileY) > 32))) - { - Log.Debug(string.Format("Liquid(PlyXY:{0}_{1}, TileXY:{2}_{3}, Result:{4}_{5}, Amount:{6})", - plyX, plyY, - tileX, tileY, - Math.Abs(plyX - tileX), Math.Abs(plyY - tileY), - liquid - )); - return Tools.HandleGriefer(Players[e.Msg.whoAmI], "Placing impossible to place liquid."); ; - } - - if (ConfigurationManager.SpawnProtect) - { - if (!Players[e.Msg.whoAmI].Group.HasPermission("editspawn")) - { - var flag = CheckSpawn(x, y); - if (flag) - { - Players[e.Msg.whoAmI].SendMessage("The spawn is protected!", Color.Red); - return true; - } - } - } - return false; - } - - bool HandleTileKill(MemoryStream data, GetDataEventArgs e) - { - int tilex = data.ReadInt32(); - int tiley = data.ReadInt32(); - if (tilex < 0 || tilex >= Main.maxTilesX || tiley < 0 || tiley >= Main.maxTilesY) - return false; - - if (Main.tile[tilex, tiley].type != 0x15) //Chest - { - Log.Debug(string.Format("TileKill(TileXY:{0}_{1}, Type:{2})", - tilex, tiley, - 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; - } - private void OnGreetPlayer(int who, HandledEventArgs e) { if (Main.netMode != 2) @@ -637,144 +377,14 @@ namespace TShockAPI e.Handled = true; } - private void OnChat(messageBuffer msg, int ply, string text, HandledEventArgs e) + private void NpcHooks_OnStrikeNpc(NpcStrikeEventArgs e) { - if (Main.netMode != 2) - return; - - if (msg.whoAmI != ply) + if (ConfigurationManager.InfiniteInvasion) { - e.Handled = Tools.HandleGriefer(Players[ply], "Faking Chat"); - return; - } - - var tsplr = Players[msg.whoAmI]; - - if (tsplr.Group.HasPermission("adminchat") && !text.StartsWith("/")) - { - Tools.Broadcast(ConfigurationManager.AdminChatPrefix + "<" + tsplr.Name + "> " + text, - (byte)ConfigurationManager.AdminChatRGB[0], (byte)ConfigurationManager.AdminChatRGB[1], (byte)ConfigurationManager.AdminChatRGB[2]); - e.Handled = true; - return; - } - - if (text.StartsWith("/")) - { - if (Commands.HandleCommand(tsplr, text)) - e.Handled = true; - } - else - { - Log.Info(string.Format("{0} said: {1}", tsplr.Name, text)); - } - } - - private void OnJoin(int ply, HandledEventArgs handler) - { - if (Main.netMode != 2) - { - return; - } - - var player = new TSPlayer(ply); - player.Group = Tools.GetGroupForIP(player.IP); - - if (Tools.ActivePlayers() + 1 > ConfigurationManager.MaxSlots && - !player.Group.HasPermission("reservedslot")) - { - Tools.ForceKick(player, "Server is full"); - handler.Handled = true; - } - else - { - var ban = Bans.GetBanByIp(player.IP); - if (ban != null) + IncrementKills(); + if (Main.invasionSize < 10) { - Tools.ForceKick(player, string.Format("You are banned: {0}", ban.Reason)); - handler.Handled = true; - } - else if (!FileTools.OnWhitelist(player.IP)) - { - Tools.ForceKick(player, "Not on whitelist."); - handler.Handled = true; - } - } - - Players[ply] = player; - Netplay.serverSock[ply].spamCheck = ConfigurationManager.SpamChecks; - } - - private void OnLeave(int ply) - { - if (Main.netMode != 2) - return; - - var tsplr = Players[ply]; - if (tsplr != null && tsplr.ReceivedInfo) - Log.Info(string.Format("{0} left.", tsplr.Name)); - - Players[ply] = null; - } - - private void OnPostInit() - { - if (!File.Exists(Path.Combine(SavePath, "auth.lck"))) - { - var r = new Random((int)DateTime.Now.ToBinary()); - ConfigurationManager.AuthToken = r.Next(100000, 10000000); - Console.WriteLine("TShock Notice: To become SuperAdmin, join the game and type /auth " + - ConfigurationManager.AuthToken); - Console.WriteLine("This token will only display ONCE. This only works ONCE. If you don't use it and the server goes down, delete auth.lck."); - FileTools.CreateFile(Path.Combine(SavePath, "auth.lck")); - } - } - - void HandleCommandLine(string[] parms) - { - for (int i = 0; i < parms.Length; i++) - { - if (parms[i].ToLower() == "-ip") - { - IPAddress ip; - if (IPAddress.TryParse(parms[++i], out ip)) - { - Netplay.serverListenIP = ip; - Console.Write("Using IP: {0}", ip); - } - else - { - Console.WriteLine("Bad IP: {0}", parms[i]); - } - } - } - } - - private void OnUpdate(GameTime time) - { - UpdateManager.UpdateProcedureCheck(); - foreach (TSPlayer player in TShock.Players) - { - if (player != null && player.Active) - { - if (player.TileThreshold >= 20) - { - if (Tools.HandleTntUser(player, "Kill tile abuse detected.")) - { - RevertKillTile(player); - player.TileThreshold = 0; - player.TilesDestroyed.Clear(); - } - else if (player.TileThreshold > 0) - { - player.TileThreshold = 0; - player.TilesDestroyed.Clear(); - } - - } - else if (player.TileThreshold > 0) - { - player.TileThreshold = 0; - } + Main.invasionSize = 20000000; } } } @@ -881,6 +491,11 @@ namespace TShockAPI NetMessage.SendData(26, -1, -1, "", player.Index, ((new Random()).Next(-1, 1)), damage, (float)0); } + public static void SendTileSquare(TSPlayer player, int x, int y, int size = 10) + { + NetMessage.SendData(20, player.Index, -1, "", size, (float)(x - (size / 2)), (float)(y - (size / 2)), 0f); + } + //TODO : Notify the player if there is more than one match. (or do we want a First() kinda thing?) public static int GetNPCID(string name, bool exact = false) { diff --git a/TShockAPI/TShockAPI.csproj b/TShockAPI/TShockAPI.csproj index dc9bb6d0..4f5919fb 100644 --- a/TShockAPI/TShockAPI.csproj +++ b/TShockAPI/TShockAPI.csproj @@ -61,6 +61,7 @@ +