mirror of
https://github.com/ClassiCube/MCGalaxy.git
synced 2025-09-24 05:03:34 -04:00
Add CommandParser.GetEnum
This commit is contained in:
parent
1e2342368c
commit
0241d151d2
@ -18,124 +18,136 @@
|
||||
using System;
|
||||
|
||||
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>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary> Attempts to parse the given argument as an integer, returning whether that succeeded. </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary> Attempts to parse the given argument as an byte, returning whether that succeeded. </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;
|
||||
if (!GetInt(p, input, type, ref temp, min, max)) return false;
|
||||
|
||||
result = (byte)temp; return true;
|
||||
}
|
||||
|
||||
/// <summary> Attempts to parse the given argument as an byte, returning whether that succeeded. </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;
|
||||
if (!GetInt(p, input, type, ref temp, min, max)) return false;
|
||||
|
||||
result = (ushort)temp; return true;
|
||||
}
|
||||
|
||||
/// <summary> Attempts to parse the given argument as a hex color, returning whether that succeeded. </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary> Attempts to parse the given argument as either a block name or a block ID,
|
||||
/// returning whether that succeeded. </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary> Provides helper methods for parsing arguments for commands. </summary>
|
||||
public static class CommandParser {
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/// <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;
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <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;
|
||||
if (!GetInt(p, input, type, ref temp, min, max)) return false;
|
||||
|
||||
result = (byte)temp; return true;
|
||||
}
|
||||
|
||||
/// <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;
|
||||
if (!GetInt(p, input, type, ref temp, min, max)) return false;
|
||||
|
||||
result = (ushort)temp; return true;
|
||||
}
|
||||
|
||||
/// <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);
|
||||
|
||||
if (!Utils.IsValidHex(input)) {
|
||||
Player.Message(p, "\"#{0}\" is not a valid HEX color.", input); return false;
|
||||
}
|
||||
|
||||
col = Colors.ParseHex(input); return true;
|
||||
}
|
||||
|
||||
|
||||
/// <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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
/// <summary> Attempts to parse the given argument as either a block name or a block ID,
|
||||
/// returning whether that succeeded. </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) {
|
||||
if (!GetBlock(p, input, out block, out extBlock, allowSkip)) return false;
|
||||
if (allowSkip && block == Block.Invalid) return true;
|
||||
return IsBlockAllowed(p, "draw with ", block);
|
||||
}
|
||||
|
||||
/// <summary> Returns whether the player is allowed to place/modify/delete the given block. </summary>
|
||||
/// <remarks> Outputs information of which ranks can modify the block if not. </remarks>
|
||||
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;
|
||||
}
|
||||
}
|
||||
/// <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) {
|
||||
if (!GetBlock(p, input, out block, out extBlock, allowSkip)) return false;
|
||||
if (allowSkip && block == Block.Invalid) return true;
|
||||
return IsBlockAllowed(p, "draw with ", block);
|
||||
}
|
||||
|
||||
/// <summary> Returns whether the player is allowed to place/modify/delete the given block. </summary>
|
||||
/// <remarks> Outputs information of which ranks can modify the block if not. </remarks>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -67,16 +67,7 @@ namespace MCGalaxy {
|
||||
*srcByte = value; srcByte++;
|
||||
}
|
||||
}
|
||||
|
||||
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