mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-23 12:42:22 -04:00
Add CommandParser.GetEnum
This commit is contained in:
parent
1e2342368c
commit
0241d151d2
@ -22,7 +22,7 @@ namespace MCGalaxy {
|
||||
/// <summary> Provides helper methods for parsing arguments for commands. </summary>
|
||||
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) {
|
||||
if (input == "1" || input.CaselessEq("true") || input.CaselessEq("yes") || input.CaselessEq("on")) {
|
||||
result = true; return true;
|
||||
@ -37,7 +37,21 @@ namespace MCGalaxy {
|
||||
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,
|
||||
int min = int.MinValue, int max = int.MaxValue) {
|
||||
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,
|
||||
byte min = byte.MinValue, byte max = byte.MaxValue) {
|
||||
int temp = 0;
|
||||
@ -70,7 +84,7 @@ namespace MCGalaxy {
|
||||
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,
|
||||
ushort min = ushort.MinValue, ushort max = ushort.MaxValue) {
|
||||
int temp = 0;
|
||||
@ -79,7 +93,7 @@ namespace MCGalaxy {
|
||||
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) {
|
||||
if (input.Length > 0 && input[0] == '#')
|
||||
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,
|
||||
/// returning whether that succeeded. </summary>
|
||||
/// <summary> Attempts to parse the given argument as either a block name or a block ID. </summary>
|
||||
public static bool GetBlock(Player p, string input, out byte block,
|
||||
out byte extBlock, bool allowSkip = false) {
|
||||
block = 0; extBlock = 0;
|
||||
@ -120,8 +133,7 @@ namespace MCGalaxy {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary> Attempts to parse the given argument as either a block name or a block ID,
|
||||
/// returning whether that succeeded. </summary>
|
||||
/// <summary> Attempts to parse the given argument as either a block name or a block ID. </summary>
|
||||
/// <remarks> Also ensures the player is allowed to place the given block. </remarks>
|
||||
public static bool GetBlockIfAllowed(Player p, string input, out byte block,
|
||||
out byte extBlock, bool allowSkip = false) {
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -22,6 +22,7 @@
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
|
@ -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) {
|
||||
return Math.Max(Math.Min(value, hi), lo);
|
||||
|
Loading…
x
Reference in New Issue
Block a user