From 069c95e2cfcc079e01827652aa53345f1bf18a6f Mon Sep 17 00:00:00 2001 From: Deathmax Date: Sat, 4 Jun 2011 14:21:41 +0800 Subject: [PATCH] Spawn protection. 5 block radius. /protectspawn - toggle spawn protection. --- TShockAPI/Commands.cs | 7 +++++ TShockAPI/ConfigFile.cs | 1 + TShockAPI/ConfigurationManager.cs | 3 ++ TShockAPI/TShock.cs | 51 ++++++++++++++++++++++++++++--- 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index f9829ee1..599a27d8 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -55,6 +55,7 @@ namespace TShockAPI TShock.admincommandList.Add("help", new CommandDelegate(Help)); TShock.admincommandList.Add("slap", new CommandDelegate(Slap)); TShock.admincommandList.Add("off-nosave", new CommandDelegate(OffNoSave)); + TShock.admincommandList.Add("protectspawn", new CommandDelegate(ProtectSpawn)); TShock.commandList.Add("help", new CommandDelegate(Help)); TShock.commandList.Add("kill", new CommandDelegate(Kill)); } @@ -559,6 +560,12 @@ namespace TShockAPI else Tools.SendMessage(args.PlayerID, "Invalid syntax! Proper syntax: /slap [dmg]", new float[] { 255f, 0f, 0f }); } + + public static void ProtectSpawn(CommandArgs args) + { + ConfigurationManager.spawnProtect = (ConfigurationManager.spawnProtect == false); + Tools.SendMessage(args.PlayerID, "Spawn is now " + (ConfigurationManager.spawnProtect ? "protected" : "open") + "."); + } #endregion } } diff --git a/TShockAPI/ConfigFile.cs b/TShockAPI/ConfigFile.cs index 1ce76424..72ed278c 100644 --- a/TShockAPI/ConfigFile.cs +++ b/TShockAPI/ConfigFile.cs @@ -23,5 +23,6 @@ namespace TShockAPI public bool KickKillTileAbusers = false; public bool BanExplosives = true; public bool KickExplosives = true; + public bool SpawnProtection = true; } } diff --git a/TShockAPI/ConfigurationManager.cs b/TShockAPI/ConfigurationManager.cs index aacce18c..a53db9de 100644 --- a/TShockAPI/ConfigurationManager.cs +++ b/TShockAPI/ConfigurationManager.cs @@ -29,6 +29,7 @@ namespace TShockAPI public static bool kickTnt = false; public static bool banBoom = true; public static bool kickBoom = true; + public static bool spawnProtect = true; public enum NPCList : int { @@ -58,6 +59,7 @@ namespace TShockAPI kickTnt = cfg.KickKillTileAbusers; banBoom = cfg.BanExplosives; kickBoom = cfg.KickExplosives; + spawnProtect = cfg.SpawnProtection; } public static void WriteJsonConfiguration() @@ -87,6 +89,7 @@ namespace TShockAPI cfg.KickKillTileAbusers = true; cfg.BanExplosives = true; cfg.KickExplosives = true; + cfg.SpawnProtection = true; string json = JsonConvert.SerializeObject(cfg, Formatting.Indented); TextWriter tr = new StreamWriter(FileTools.SaveDir + "config.json"); diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs index 9ed1bcfe..d5292119 100644 --- a/TShockAPI/TShock.cs +++ b/TShockAPI/TShock.cs @@ -137,8 +137,6 @@ namespace TShockAPI NetHooks.OnPreGetData -= GetData; NetHooks.OnGreetPlayer -= new NetHooks.GreetPlayerD(OnGreetPlayer); NpcHooks.OnStrikeNpc -= new NpcHooks.StrikeNpcD(NpcHooks_OnStrikeNpc); - ConfigurationManager.WriteJsonConfiguration(); - Log.Info("Shutting down..."); } /* @@ -178,10 +176,20 @@ namespace TShockAPI int y = br.ReadInt32(); byte typetile = br.ReadByte(); + if (type == 0 || type == 1) + if (ConfigurationManager.spawnProtect) + if (!players[e.Msg.whoAmI].IsAdmin()) + { + var flag = CheckSpawn(x, y); + if (flag) + { + Tools.SendMessage(e.Msg.whoAmI, "The spawn is protected!", new float[] { 255f, 0f, 0f }); + e.Handled = true; + } + } + if (type == 0 && BlacklistTiles[Main.tile[x, y].type] && Main.player[e.Msg.whoAmI].active) - { players[e.Msg.whoAmI].tileThreshold++; - } } return; } @@ -332,6 +340,30 @@ namespace TShockAPI e.Handled = true; } } + else if (e.MsgID == 0x30) + { + int x; + int y; + byte liquid; + bool lava; + using (var br = new BinaryReader(new MemoryStream(e.Msg.readBuffer, e.Index, e.Length))) + { + x = br.ReadInt32(); + y = br.ReadInt32(); + liquid = br.ReadByte(); + lava = br.ReadBoolean(); + } + if (ConfigurationManager.spawnProtect) + if (!players[e.Msg.whoAmI].IsAdmin()) + { + var flag = CheckSpawn(x, y); + if (flag) + { + Tools.SendMessage(e.Msg.whoAmI, "The spawn is protected!", new float[] { 255f, 0f, 0f }); + e.Handled = true; + } + } + } } catch (Exception ex) { @@ -683,5 +715,16 @@ namespace TShockAPI } return false; } + + public static bool CheckSpawn(int x, int y) + { + Vector2 tile = new Vector2((float)x, (float)y); + Vector2 spawn = new Vector2((float)Main.spawnTileX, (float)Main.spawnTileY); + var distance = Vector2.Distance(spawn, tile); + if (distance > 5) + return false; + else + return true; + } } } \ No newline at end of file