From b665b02121c04f7a6f698c87aef73118ad140e09 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 28 Feb 2016 20:14:54 +1100 Subject: [PATCH] Cleanup /bs command. --- Commands/Moderation/CmdBlockSpeed.cs | 135 ++++++++++----------------- Commands/Moderation/CmdTempBan.cs | 8 +- Commands/building/CmdMaze.cs | 33 ++----- Commands/building/CmdUndo.cs | 4 +- Commands/other/CmdTpZone.cs | 6 +- 5 files changed, 69 insertions(+), 117 deletions(-) diff --git a/Commands/Moderation/CmdBlockSpeed.cs b/Commands/Moderation/CmdBlockSpeed.cs index 4b7b3c75a..a6dd2322f 100644 --- a/Commands/Moderation/CmdBlockSpeed.cs +++ b/Commands/Moderation/CmdBlockSpeed.cs @@ -16,109 +16,76 @@ permissions and limitations under the Licenses. */ using System; -namespace MCGalaxy.Commands -{ - public sealed class CmdBlockSpeed : Command - { + +namespace MCGalaxy.Commands { + + public sealed class CmdBlockSpeed : Command { + public override string name { get { return "blockspeed"; } } public override string shortcut { get { return "bs"; } } - public override string type { get { return CommandTypes.Moderation; } } + public override string type { get { return CommandTypes.Moderation; } } public override bool museumUsable { get { return true; } } public override LevelPermission defaultRank { get { return LevelPermission.Operator; } } - public CmdBlockSpeed() { } - public override void Use(Player p, string text) - { - if (text == "") - { - SendEstimation(p); - return; + public override void Use(Player p, string text) { + if (text == "") { SendEstimation(p); return; } + string[] args = text.Split(' '); + string cmd = args[0].ToLower(); + if (cmd == "clear") { + Server.levels.ForEach(lvl => lvl.blockqueue.Clear()); return; } - if (text == "clear") - { - Server.levels.ForEach((l) => { l.blockqueue.Clear(); }); - return; - } - if (text.StartsWith("bs")) - { - try { BlockQueue.blockupdates = int.Parse(text.Split(' ')[1]); } - catch { Player.SendMessage(p, "Invalid number specified."); return; } + if (args.Length == 1) { Help(p); return; } + int value = 0; + + if (cmd == "bs") { + if (!int.TryParse(args[1], out value)) { + Player.SendMessage(p, "Invalid number specified."); return; + } + BlockQueue.blockupdates = value; Player.SendMessage(p, String.Format("Blocks per interval is now {0}.", BlockQueue.blockupdates)); - return; - } - if (text.StartsWith("ts")) - { - try { BlockQueue.time = int.Parse(text.Split(' ')[1]); } - catch { Player.SendMessage(p, "Invalid number specified."); return; } + } else if (cmd == "ts") { + if (!int.TryParse(args[1], out value)) { + Player.SendMessage(p, "Invalid number specified."); return; + } + BlockQueue.time = value; Player.SendMessage(p, String.Format("Block interval is now {0}.", BlockQueue.time)); - return; - } - if (text.StartsWith("buf")) - { + } else if (cmd == "buf") { if (p.level.bufferblocks) - { - p.level.bufferblocks = false; Player.SendMessage(p, String.Format("Block buffering on {0} disabled.", p.level.name)); - } else - { - p.level.bufferblocks = true; Player.SendMessage(p, String.Format("Block buffering on {0} enabled.", p.level.name)); - } - return; - } - if (text.StartsWith("net")) - { - switch (int.Parse(text.Split(' ')[1])) - { - case 2: - BlockQueue.blockupdates = 25; - BlockQueue.time = 100; - break; - case 4: - BlockQueue.blockupdates = 50; - BlockQueue.time = 100; - break; - case 8: - BlockQueue.blockupdates = 100; - BlockQueue.time = 100; - break; - case 12: - BlockQueue.blockupdates = 200; - BlockQueue.time = 100; - break; - case 16: - BlockQueue.blockupdates = 200; - BlockQueue.time = 100; - break; - case 161: - BlockQueue.blockupdates = 100; - BlockQueue.time = 50; - break; - case 20: - BlockQueue.blockupdates = 125; - BlockQueue.time = 50; - break; - case 24: - BlockQueue.blockupdates = 150; - BlockQueue.time = 50; - break; - default: - BlockQueue.blockupdates = 200; - BlockQueue.time = 100; - break; + p.level.bufferblocks = !p.level.bufferblocks; + } else if (cmd == "net") { + if (!int.TryParse(args[1], out value)) { + Player.SendMessage(p, "Invalid number specified."); return; + } + + switch (value) { + case 2: Set(25, 100); break; + case 4: Set(50, 100); break; + case 8: Set(100, 100); break; + case 12: Set(200, 100); break; + case 16: Set(200, 100); break; + case 161: Set(100, 50); break; + case 20: Set(125, 50); break; + case 24: Set(150, 50); break; + default: Set(200, 100); break; } SendEstimation(p); - return; } } - private static void SendEstimation(Player p) - { + + static void Set(int updates, int time) { + BlockQueue.blockupdates = updates; + BlockQueue.time = time; + } + + static void SendEstimation(Player p) { Player.SendMessage(p, String.Format("{0} blocks every {1} milliseconds = {2} blocks per second.", BlockQueue.blockupdates, BlockQueue.time, BlockQueue.blockupdates * (1000 / BlockQueue.time))); Player.SendMessage(p, String.Format("Using ~{0}KB/s times {1} player(s) = ~{2}KB/s", (BlockQueue.blockupdates * (1000 / BlockQueue.time) * 8) / 1000, PlayerInfo.players.Count, PlayerInfo.players.Count * ((BlockQueue.blockupdates * (1000 / BlockQueue.time) * 8) / 1000))); } - public override void Help(Player p) - { + + public override void Help(Player p) { Player.SendMessage(p, "/bs [option] [option value] - Options for block speeds."); Player.SendMessage(p, "Options are: bs (blocks per interval), ts (interval in milliseconds), buf (toggles buffering), clear, net."); Player.SendMessage(p, "/bs net [2,4,8,12,16,20,24] - Presets, divide by 8 and times by 1000 to get blocks per second."); diff --git a/Commands/Moderation/CmdTempBan.cs b/Commands/Moderation/CmdTempBan.cs index 02ce0897e..56d377613 100644 --- a/Commands/Moderation/CmdTempBan.cs +++ b/Commands/Moderation/CmdTempBan.cs @@ -36,10 +36,10 @@ namespace MCGalaxy.Commands if (who == null) { Player.SendMessage(p, "Could not find player"); return; } if (p != null && who.group.Permission >= p.group.Permission) { Player.SendMessage(p, "Cannot ban someone of the same rank"); return; } int minutes; - try - { - minutes = int.Parse(message.Split(' ')[1]); - } catch { Player.SendMessage(p, "Invalid minutes"); return; } + if (!int.TryParse(message.Split(' ')[1], out minutes)) { + Player.SendMessage(p, "Invalid minutes"); return; + } + if (minutes > 1440) { Player.SendMessage(p, "Cannot ban for more than a day"); return; } if (minutes < 1) { Player.SendMessage(p, "Cannot ban someone for less than a minute"); return; } diff --git a/Commands/building/CmdMaze.cs b/Commands/building/CmdMaze.cs index 386bbd5b7..1724c66a6 100644 --- a/Commands/building/CmdMaze.cs +++ b/Commands/building/CmdMaze.cs @@ -29,32 +29,23 @@ namespace MCGalaxy.Commands public override LevelPermission defaultRank { get { return LevelPermission.AdvBuilder; } } public static int randomizer = 0; public static bool[,] wall; - public override void Use(Player p, string message) - { - String[] split = message.Split(' '); - if (split.Length >= 1&&message.Length>0) - { - try - { - randomizer = int.Parse(split[0]); - } - catch (Exception) - { - this.Help(p); return; - } + + public override void Use(Player p, string message) { + if (message.Length > 0 && !int.TryParse(message, out randomizer)) { + Help(p); return; } - Player.SendMessage(p, "Place two blocks to determine the edges"); - + + Player.SendMessage(p, "Place two blocks to determine the edges"); p.Blockchange += new Player.BlockchangeEventHandler(Blockchange1); } - public void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType) - { + + void Blockchange1(Player p, ushort x, ushort y, ushort z, byte type, byte extType) { RevertAndClearState(p, x, y, z); p.blockchangeObject = new CatchPos(x, y, z); p.Blockchange += new Player.BlockchangeEventHandler(Blockchange2); } - public void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type, byte extType) - { + + void Blockchange2(Player p, ushort x, ushort y, ushort z, byte type, byte extType) { RevertAndClearState(p, x, y, z); CatchPos first = (CatchPos)p.blockchangeObject; @@ -178,11 +169,7 @@ namespace MCGalaxy.Commands rand.GetBytes(r); r[0] /= (255 / 4); break; - case 1: - r[0] = (byte)rand2.Next(4); - break; default: - Random rand3 = new Random(Environment.TickCount); r[0] = (byte)rand2.Next(4); break; } diff --git a/Commands/building/CmdUndo.cs b/Commands/building/CmdUndo.cs index 95b374408..e592f841a 100644 --- a/Commands/building/CmdUndo.cs +++ b/Commands/building/CmdUndo.cs @@ -197,9 +197,9 @@ namespace MCGalaxy.Commands if (b == undo.newType || Block.Convert(b) == Block.water || Block.Convert(b) == Block.lava) { ushort x, y, z; - int undoIndex = p.level.currentUndo; - p.level.currentUndo = i; p.level.IntToPos(undo.location, out x, out y, out z); + int undoIndex = p.level.currentUndo; + p.level.currentUndo = i; p.level.currentUndo = undoIndex; p.level.Blockchange(x, y, z, undo.oldType, true, "", undo.oldExtType, false); } diff --git a/Commands/other/CmdTpZone.cs b/Commands/other/CmdTpZone.cs index 4cea9e899..04d7e078d 100644 --- a/Commands/other/CmdTpZone.cs +++ b/Commands/other/CmdTpZone.cs @@ -75,11 +75,9 @@ namespace MCGalaxy.Commands else { int zoneID; - try - { - zoneID = int.Parse(message); + if (!int.TryParse(message, out zoneID)) { + Help(p); return; } - catch { Help(p); return; } if (zoneID < 0 || zoneID > p.level.ZoneList.Count) {