From 0241d151d2cc360e11ccabf2b717c8372d32e61f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Tue, 28 Feb 2017 16:47:39 +1100 Subject: [PATCH] Add CommandParser.GetEnum --- MCGalaxy/Commands/CommandParser.cs | 250 ++++++++++++---------- MCGalaxy/Commands/Fun/CmdMapSet.cs | 6 +- MCGalaxy/Generator/fCraft/MapGenerator.cs | 7 +- MCGalaxy/sharkbite.thresher/Identd.cs | 1 + MCGalaxy/util/Utils.cs | 11 +- 5 files changed, 137 insertions(+), 138 deletions(-) diff --git a/MCGalaxy/Commands/CommandParser.cs b/MCGalaxy/Commands/CommandParser.cs index 9beb6e46b..cb4a8156b 100644 --- a/MCGalaxy/Commands/CommandParser.cs +++ b/MCGalaxy/Commands/CommandParser.cs @@ -18,124 +18,136 @@ using System; namespace MCGalaxy { - - /// Provides helper methods for parsing arguments for commands. - public static class CommandParser { - - /// Attempts to parse the given argument as a boolean, returning whether that succeeded. - public static bool GetBool(Player p, string input, ref bool result) { - if (input == "1" || input.CaselessEq("true") || input.CaselessEq("yes") || input.CaselessEq("on")) { - result = true; return true; - } - - if (input == "0" || input.CaselessEq("false") || input.CaselessEq("no") || input.CaselessEq("off")) { - result = false; return true; - } - - Player.Message(p, "\"{0}\" is not a valid boolean.", input); - Player.Message(p, "Value must be either 1/yes/on or 0/no/off"); - return false; - } - - /// Attempts to parse the given argument as an integer, returning whether that succeeded. - public static bool GetInt(Player p, string input, string type, ref int result, - int min = int.MinValue, int max = int.MaxValue) { - int value; - if (!int.TryParse(input, out value)) { - Player.Message(p, "\"{0}\" is not a valid integer.", input); return false; - } - - if (value < min || value > max) { - // Try to provide more helpful range messages - if (max == int.MaxValue) { - Player.Message(p, "{0} must be {1} or greater", type, min); - } else if (min == int.MinValue) { - Player.Message(p, "{0} must be {1} or less", type, max); - } else { - Player.Message(p, "{0} must be between {1} and {2}", type, min, max); - } - return false; - } - - result = value; return true; - } - - - /// Attempts to parse the given argument as an byte, returning whether that succeeded. - public static bool GetByte(Player p, string input, string type, ref byte result, - byte min = byte.MinValue, byte max = byte.MaxValue) { - int temp = 0; - if (!GetInt(p, input, type, ref temp, min, max)) return false; - - result = (byte)temp; return true; - } - - /// Attempts to parse the given argument as an byte, returning whether that succeeded. - public static bool GetUShort(Player p, string input, string type, ref ushort result, - ushort min = ushort.MinValue, ushort max = ushort.MaxValue) { - int temp = 0; - if (!GetInt(p, input, type, ref temp, min, max)) return false; - - result = (ushort)temp; return true; - } - - /// Attempts to parse the given argument as a hex color, returning whether that succeeded. - public static bool GetHex(Player p, string input, ref CustomColor col) { - if (input.Length > 0 && input[0] == '#') - input = input.Substring(1); - - if (!Utils.IsValidHex(input)) { - Player.Message(p, "\"#{0}\" is not a valid HEX color.", input); return false; - } - - col = Colors.ParseHex(input); return true; - } - - - /// Attempts to parse the given argument as either a block name or a block ID, - /// returning whether that succeeded. - public static bool GetBlock(Player p, string input, out byte block, - out byte extBlock, bool allowSkip = false) { - block = 0; extBlock = 0; - // Skip/None block for draw operations - if (allowSkip && (input.CaselessEq("skip") || input.CaselessEq("none"))) { - block = Block.Invalid; return true; - } - - byte match = Block.Byte(input); - if (match != Block.Invalid) { block = match; return true; } - - // find custom block - match = BlockDefinition.GetBlock(input, p); - if (match == Block.Invalid) { - Player.Message(p, "&cThere is no block \"{0}\".", input); - return false; - } - - // custom block overriding a core block - if (match < Block.CpeCount) { block = match; return true; } - - // Normal custom block - block = Block.custom_block; extBlock = match; - return true; - } + + /// Provides helper methods for parsing arguments for commands. + public static class CommandParser { + + /// Attempts to parse the given argument as a boolean. + public static bool GetBool(Player p, string input, ref bool result) { + if (input == "1" || input.CaselessEq("true") || input.CaselessEq("yes") || input.CaselessEq("on")) { + result = true; return true; + } + + if (input == "0" || input.CaselessEq("false") || input.CaselessEq("no") || input.CaselessEq("off")) { + result = false; return true; + } + + Player.Message(p, "\"{0}\" is not a valid boolean.", input); + Player.Message(p, "Value must be either 1/yes/on or 0/no/off"); + return false; + } + + /// Attempts to parse the given argument as an enumeration member. + public static bool GetEnum(Player p, string input, string type, + ref TEnum result) where TEnum : struct { + try { + result = (TEnum)Enum.Parse(typeof(TEnum), input, true); + return true; + } catch (Exception) { + string[] names = Enum.GetNames(typeof(TEnum)); + Player.Message(p, type + " must be one of the following: " + names.Join()); + return false; + } + } + + + /// Attempts to parse the given argument as an integer. + public static bool GetInt(Player p, string input, string type, ref int result, + int min = int.MinValue, int max = int.MaxValue) { + int value; + if (!int.TryParse(input, out value)) { + Player.Message(p, "\"{0}\" is not a valid integer.", input); return false; + } + + if (value < min || value > max) { + // Try to provide more helpful range messages + if (max == int.MaxValue) { + Player.Message(p, "{0} must be {1} or greater", type, min); + } else if (min == int.MinValue) { + Player.Message(p, "{0} must be {1} or less", type, max); + } else { + Player.Message(p, "{0} must be between {1} and {2}", type, min, max); + } + return false; + } + + result = value; return true; + } + + + /// Attempts to parse the given argument as an byte. + public static bool GetByte(Player p, string input, string type, ref byte result, + byte min = byte.MinValue, byte max = byte.MaxValue) { + int temp = 0; + if (!GetInt(p, input, type, ref temp, min, max)) return false; + + result = (byte)temp; return true; + } + + /// Attempts to parse the given argument as an byte. + public static bool GetUShort(Player p, string input, string type, ref ushort result, + ushort min = ushort.MinValue, ushort max = ushort.MaxValue) { + int temp = 0; + if (!GetInt(p, input, type, ref temp, min, max)) return false; + + result = (ushort)temp; return true; + } + + /// Attempts to parse the given argument as a hex color. + public static bool GetHex(Player p, string input, ref CustomColor col) { + if (input.Length > 0 && input[0] == '#') + input = input.Substring(1); + + if (!Utils.IsValidHex(input)) { + Player.Message(p, "\"#{0}\" is not a valid HEX color.", input); return false; + } + + col = Colors.ParseHex(input); return true; + } + + + /// Attempts to parse the given argument as either a block name or a block ID. + public static bool GetBlock(Player p, string input, out byte block, + out byte extBlock, bool allowSkip = false) { + block = 0; extBlock = 0; + // Skip/None block for draw operations + if (allowSkip && (input.CaselessEq("skip") || input.CaselessEq("none"))) { + block = Block.Invalid; return true; + } + + byte match = Block.Byte(input); + if (match != Block.Invalid) { block = match; return true; } + + // find custom block + match = BlockDefinition.GetBlock(input, p); + if (match == Block.Invalid) { + Player.Message(p, "&cThere is no block \"{0}\".", input); + return false; + } + + // custom block overriding a core block + if (match < Block.CpeCount) { block = match; return true; } + + // Normal custom block + block = Block.custom_block; extBlock = match; + return true; + } - /// Attempts to parse the given argument as either a block name or a block ID, - /// returning whether that succeeded. - /// Also ensures the player is allowed to place the given block. - public static bool GetBlockIfAllowed(Player p, string input, out byte block, - out byte extBlock, bool allowSkip = false) { - if (!GetBlock(p, input, out block, out extBlock, allowSkip)) return false; - if (allowSkip && block == Block.Invalid) return true; - return IsBlockAllowed(p, "draw with ", block); - } - - /// Returns whether the player is allowed to place/modify/delete the given block. - /// Outputs information of which ranks can modify the block if not. - public static bool IsBlockAllowed(Player p, string action, byte block) { - if (p == null || Block.canPlace(p, block)) return true; - Formatter.MessageBlock(p, action, block); - return false; - } - } + /// Attempts to parse the given argument as either a block name or a block ID. + /// Also ensures the player is allowed to place the given block. + public static bool GetBlockIfAllowed(Player p, string input, out byte block, + out byte extBlock, bool allowSkip = false) { + if (!GetBlock(p, input, out block, out extBlock, allowSkip)) return false; + if (allowSkip && block == Block.Invalid) return true; + return IsBlockAllowed(p, "draw with ", block); + } + + /// Returns whether the player is allowed to place/modify/delete the given block. + /// Outputs information of which ranks can modify the block if not. + public static bool IsBlockAllowed(Player p, string action, byte block) { + if (p == null || Block.canPlace(p, block)) return true; + Formatter.MessageBlock(p, action, block); + return false; + } + } } diff --git a/MCGalaxy/Commands/Fun/CmdMapSet.cs b/MCGalaxy/Commands/Fun/CmdMapSet.cs index 6c03d0a17..abf5af452 100644 --- a/MCGalaxy/Commands/Fun/CmdMapSet.cs +++ b/MCGalaxy/Commands/Fun/CmdMapSet.cs @@ -56,10 +56,8 @@ namespace MCGalaxy.Commands { Player.Message(p, "Set pillaring allowed to: " + value); HUD.UpdateAllSecondary(Server.zombie); } else if (args[0].CaselessEq("build") || args[0].CaselessEq("buildtype")) { - BuildType value; - if (!Utils.TryParseEnum(args[1], out value)) { - Player.Message(p, "Value must be 'normal', 'modifyonly', or 'nomodify'"); return; - } + BuildType value = BuildType.Normal; + if (!CommandParser.GetEnum(p, args[1], "Build type", ref value)) return; p.level.BuildType = value; p.level.UpdateBlockPermissions(); diff --git a/MCGalaxy/Generator/fCraft/MapGenerator.cs b/MCGalaxy/Generator/fCraft/MapGenerator.cs index 6072230d9..9ab6826b0 100644 --- a/MCGalaxy/Generator/fCraft/MapGenerator.cs +++ b/MCGalaxy/Generator/fCraft/MapGenerator.cs @@ -401,11 +401,8 @@ namespace MCGalaxy.Generator { static bool GenerateMap(MapGenArgs genArgs) { MapGenTheme theme = MapGenTheme.Forest; - if (genArgs.Args != "" && !Utils.TryParseEnum(genArgs.Args, out theme)) { - string[] themes = Enum.GetNames(typeof(MapGenTheme)); - Player.Message(genArgs.Player, "Seed must be one of the following themes: " + themes.Join()); - return false; - } + if (genArgs.Args != "" && + !CommandParser.GetEnum(genArgs.Player, genArgs.Args, "Seed", ref theme)) return false; MapGenTemplate templ = (MapGenTemplate)Enum.Parse(typeof(MapGenTemplate), genArgs.Theme.Substring(3), true); fCraftMapGeneratorArgs args = fCraftMapGeneratorArgs.MakeTemplate(templ); diff --git a/MCGalaxy/sharkbite.thresher/Identd.cs b/MCGalaxy/sharkbite.thresher/Identd.cs index 75a9e33d2..94cd7ac99 100644 --- a/MCGalaxy/sharkbite.thresher/Identd.cs +++ b/MCGalaxy/sharkbite.thresher/Identd.cs @@ -22,6 +22,7 @@ using System; using System.IO; +using System.Net; using System.Net.Sockets; using System.Threading; using System.Diagnostics; diff --git a/MCGalaxy/util/Utils.cs b/MCGalaxy/util/Utils.cs index 9d44d35b2..c6755943b 100644 --- a/MCGalaxy/util/Utils.cs +++ b/MCGalaxy/util/Utils.cs @@ -67,16 +67,7 @@ namespace MCGalaxy { *srcByte = value; srcByte++; } } - - public static bool TryParseEnum(string value, out TEnum result) where TEnum : struct { - try { - result = (TEnum)Enum.Parse(typeof(TEnum), value, true); - return true; - } catch (Exception) { - result = default(TEnum); - return false; - } - } + public static int Clamp(int value, int lo, int hi) { return Math.Max(Math.Min(value, hi), lo);