Add CommandParser.GetEnum

This commit is contained in:
UnknownShadow200 2017-02-28 16:47:39 +11:00
parent 1e2342368c
commit 0241d151d2
5 changed files with 137 additions and 138 deletions

View File

@ -22,7 +22,7 @@ namespace MCGalaxy {
/// <summary> Provides helper methods for parsing arguments for commands. </summary> /// <summary> Provides helper methods for parsing arguments for commands. </summary>
public static class CommandParser { public static class CommandParser {
/// <summary> Attempts to parse the given argument as a boolean, returning whether that succeeded. </summary> /// <summary> Attempts to parse the given argument as a boolean. </summary>
public static bool GetBool(Player p, string input, ref bool result) { public static bool GetBool(Player p, string input, ref bool result) {
if (input == "1" || input.CaselessEq("true") || input.CaselessEq("yes") || input.CaselessEq("on")) { if (input == "1" || input.CaselessEq("true") || input.CaselessEq("yes") || input.CaselessEq("on")) {
result = true; return true; result = true; return true;
@ -37,7 +37,21 @@ namespace MCGalaxy {
return false; return false;
} }
/// <summary> Attempts to parse the given argument as an integer, returning whether that succeeded. </summary> /// <summary> Attempts to parse the given argument as an enumeration member. </summary>
public static bool GetEnum<TEnum>(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;
}
}
/// <summary> Attempts to parse the given argument as an integer. </summary>
public static bool GetInt(Player p, string input, string type, ref int result, public static bool GetInt(Player p, string input, string type, ref int result,
int min = int.MinValue, int max = int.MaxValue) { int min = int.MinValue, int max = int.MaxValue) {
int value; int value;
@ -61,7 +75,7 @@ namespace MCGalaxy {
} }
/// <summary> Attempts to parse the given argument as an byte, returning whether that succeeded. </summary> /// <summary> Attempts to parse the given argument as an byte. </summary>
public static bool GetByte(Player p, string input, string type, ref byte result, public static bool GetByte(Player p, string input, string type, ref byte result,
byte min = byte.MinValue, byte max = byte.MaxValue) { byte min = byte.MinValue, byte max = byte.MaxValue) {
int temp = 0; int temp = 0;
@ -70,7 +84,7 @@ namespace MCGalaxy {
result = (byte)temp; return true; result = (byte)temp; return true;
} }
/// <summary> Attempts to parse the given argument as an byte, returning whether that succeeded. </summary> /// <summary> Attempts to parse the given argument as an byte. </summary>
public static bool GetUShort(Player p, string input, string type, ref ushort result, public static bool GetUShort(Player p, string input, string type, ref ushort result,
ushort min = ushort.MinValue, ushort max = ushort.MaxValue) { ushort min = ushort.MinValue, ushort max = ushort.MaxValue) {
int temp = 0; int temp = 0;
@ -79,7 +93,7 @@ namespace MCGalaxy {
result = (ushort)temp; return true; result = (ushort)temp; return true;
} }
/// <summary> Attempts to parse the given argument as a hex color, returning whether that succeeded. </summary> /// <summary> Attempts to parse the given argument as a hex color. </summary>
public static bool GetHex(Player p, string input, ref CustomColor col) { public static bool GetHex(Player p, string input, ref CustomColor col) {
if (input.Length > 0 && input[0] == '#') if (input.Length > 0 && input[0] == '#')
input = input.Substring(1); input = input.Substring(1);
@ -92,8 +106,7 @@ namespace MCGalaxy {
} }
/// <summary> Attempts to parse the given argument as either a block name or a block ID, /// <summary> Attempts to parse the given argument as either a block name or a block ID. </summary>
/// returning whether that succeeded. </summary>
public static bool GetBlock(Player p, string input, out byte block, public static bool GetBlock(Player p, string input, out byte block,
out byte extBlock, bool allowSkip = false) { out byte extBlock, bool allowSkip = false) {
block = 0; extBlock = 0; block = 0; extBlock = 0;
@ -120,8 +133,7 @@ namespace MCGalaxy {
return true; return true;
} }
/// <summary> Attempts to parse the given argument as either a block name or a block ID, /// <summary> Attempts to parse the given argument as either a block name or a block ID. </summary>
/// returning whether that succeeded. </summary>
/// <remarks> Also ensures the player is allowed to place the given block. </remarks> /// <remarks> Also ensures the player is allowed to place the given block. </remarks>
public static bool GetBlockIfAllowed(Player p, string input, out byte block, public static bool GetBlockIfAllowed(Player p, string input, out byte block,
out byte extBlock, bool allowSkip = false) { out byte extBlock, bool allowSkip = false) {

View File

@ -56,10 +56,8 @@ namespace MCGalaxy.Commands {
Player.Message(p, "Set pillaring allowed to: " + value); Player.Message(p, "Set pillaring allowed to: " + value);
HUD.UpdateAllSecondary(Server.zombie); HUD.UpdateAllSecondary(Server.zombie);
} else if (args[0].CaselessEq("build") || args[0].CaselessEq("buildtype")) { } else if (args[0].CaselessEq("build") || args[0].CaselessEq("buildtype")) {
BuildType value; BuildType value = BuildType.Normal;
if (!Utils.TryParseEnum(args[1], out value)) { if (!CommandParser.GetEnum(p, args[1], "Build type", ref value)) return;
Player.Message(p, "Value must be 'normal', 'modifyonly', or 'nomodify'"); return;
}
p.level.BuildType = value; p.level.BuildType = value;
p.level.UpdateBlockPermissions(); p.level.UpdateBlockPermissions();

View File

@ -401,11 +401,8 @@ namespace MCGalaxy.Generator {
static bool GenerateMap(MapGenArgs genArgs) { static bool GenerateMap(MapGenArgs genArgs) {
MapGenTheme theme = MapGenTheme.Forest; MapGenTheme theme = MapGenTheme.Forest;
if (genArgs.Args != "" && !Utils.TryParseEnum(genArgs.Args, out theme)) { if (genArgs.Args != "" &&
string[] themes = Enum.GetNames(typeof(MapGenTheme)); !CommandParser.GetEnum(genArgs.Player, genArgs.Args, "Seed", ref theme)) return false;
Player.Message(genArgs.Player, "Seed must be one of the following themes: " + themes.Join());
return false;
}
MapGenTemplate templ = (MapGenTemplate)Enum.Parse(typeof(MapGenTemplate), genArgs.Theme.Substring(3), true); MapGenTemplate templ = (MapGenTemplate)Enum.Parse(typeof(MapGenTemplate), genArgs.Theme.Substring(3), true);
fCraftMapGeneratorArgs args = fCraftMapGeneratorArgs.MakeTemplate(templ); fCraftMapGeneratorArgs args = fCraftMapGeneratorArgs.MakeTemplate(templ);

View File

@ -22,6 +22,7 @@
using System; using System;
using System.IO; using System.IO;
using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Diagnostics; using System.Diagnostics;

View File

@ -68,15 +68,6 @@ namespace MCGalaxy {
} }
} }
public static bool TryParseEnum<TEnum>(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) { public static int Clamp(int value, int lo, int hi) {
return Math.Max(Math.Min(value, hi), lo); return Math.Max(Math.Min(value, hi), lo);