More usage of CommandParser

This commit is contained in:
UnknownShadow200 2017-02-26 14:37:41 +11:00
parent 5a2f138252
commit 6e18f7df2b
12 changed files with 77 additions and 88 deletions

View File

@ -70,10 +70,10 @@ namespace MCGalaxy.Commands.CPE {
} }
char fallback; char fallback;
if (!CheckName(p, args[2]) || !CheckFallback(p, args[3], out fallback) if (!CheckName(p, args[2]) || !CheckFallback(p, args[3], out fallback)) return;
|| !Utils.CheckHex(p, ref args[4])) return;
CustomColor col = Colors.ParseHex(args[4]); CustomColor col = Colors.ParseHex(args[4]);
if (!CommandParser.GetHex(p, args[4], ref col)) return;
col.Code = code; col.Fallback = fallback; col.Name = args[2]; col.Code = code; col.Fallback = fallback; col.Name = args[2];
Colors.AddExtColor(col); Colors.AddExtColor(col);
Player.Message(p, "Successfully added a custom color."); Player.Message(p, "Successfully added a custom color.");
@ -145,8 +145,8 @@ namespace MCGalaxy.Commands.CPE {
col.Fallback = fallback; break; col.Fallback = fallback; break;
case "hex": case "hex":
case "color": case "color":
if (!Utils.CheckHex(p, ref args[3])) return; CustomColor rgb = default(CustomColor);
CustomColor rgb = Colors.ParseHex(args[3]); if (!CommandParser.GetHex(p, args[3], ref rgb)) return;
col.R = rgb.R; col.G = rgb.G; col.B = rgb.B; col.R = rgb.R; col.G = rgb.G; col.B = rgb.B;
break; break;
default: default:

View File

@ -262,8 +262,8 @@ namespace MCGalaxy.Commands.CPE {
step += (bd.FogDensity == 0 ? 2 : 1); step += (bd.FogDensity == 0 ? 2 : 1);
} }
} else if (step == 16) { } else if (step == 16) {
if (Utils.CheckHex(p, ref value)) { CustomColor rgb = default(CustomColor);
CustomColor rgb = Colors.ParseHex(value); if (CommandParser.GetHex(p, value, ref rgb)) {
bd.FogR = rgb.R; bd.FogG = rgb.G; bd.FogB = rgb.B; bd.FogR = rgb.R; bd.FogG = rgb.G; bd.FogB = rgb.B;
step++; step++;
} }
@ -421,8 +421,8 @@ namespace MCGalaxy.Commands.CPE {
case "col": case "col":
case "fogcol": case "fogcol":
case "fogcolor": case "fogcolor":
if (!Utils.CheckHex(p, ref value)) return; CustomColor rgb = default(CustomColor);
CustomColor rgb = Colors.ParseHex(value); if (!CommandParser.GetHex(p, value, ref rgb)) return;
def.FogR = rgb.R; def.FogG = rgb.G; def.FogB = rgb.B; def.FogR = rgb.R; def.FogG = rgb.G; def.FogB = rgb.B;
break; break;

View File

@ -37,6 +37,7 @@ 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 integer, returning whether that succeeded. </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) {
@ -77,5 +78,18 @@ 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>
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;
}
} }
} }

View File

@ -760,15 +760,11 @@ namespace MCGalaxy.Commands {
text[3] = text[4]; text[3] = text[4];
} }
int numb = -1; int numb = -1;
if (!int.TryParse(text[3], out numb)) if (!CommandParser.GetInt(p, text[3], "Score per kill", ref numb, 0)) return;
{ Player.Message(p, "TNT Wars Error: Invalid number '" + text[3] + "'"); return; }
if (numb <= -1) { Player.Message(p, "TNT Wars Error: Invalid number '" + text[3] + "'"); return; }
else
{
it.ScorePerKill = numb; it.ScorePerKill = numb;
Player.Message(p, "TNT Wars: Score per kill is now " + numb + " points!"); Player.Message(p, "TNT Wars: Score per kill is now " + numb + " points!");
return; return;
}
//break; //break;
} }
break; break;

View File

@ -108,30 +108,32 @@ namespace MCGalaxy.Commands {
} }
static void HandleHitbox(Player p, string message, string[] args) { static void HandleHitbox(Player p, string message, string[] args) {
byte precision;
if (args.Length == 1) { if (args.Length == 1) {
Player.Message(p, "Hitbox detection is currently &a" + ZombieGameProps.HitboxPrecision + " %Sunits apart."); Player.Message(p, "Hitbox detection is currently &a" + ZombieGameProps.HitboxPrecision + " %Sunits apart.");
} else if (!byte.TryParse(args[1], out precision)) { return;
Player.Message(p, "Hitbox detection must be an integer between 0 and 256."); }
} else {
byte precision = 0;
if (!CommandParser.GetByte(p, args[1], "Hitbox detection", ref precision)) return;
ZombieGameProps.HitboxPrecision = precision; ZombieGameProps.HitboxPrecision = precision;
Player.Message(p, "Hitbox detection set to &a" + precision + " %Sunits apart."); Player.Message(p, "Hitbox detection set to &a" + precision + " %Sunits apart.");
SrvProperties.Save(); SrvProperties.Save();
} }
}
static void HandleMaxMove(Player p, string message, string[] args) { static void HandleMaxMove(Player p, string message, string[] args) {
byte distance;
if (args.Length == 1) { if (args.Length == 1) {
Player.Message(p, "Maxmium move distance is currently &a" + ZombieGameProps.MaxMoveDistance + " %Sunits apart."); Player.Message(p, "Maxmium move distance is currently &a" + ZombieGameProps.MaxMoveDistance + " %Sunits apart.");
} else if (!byte.TryParse(args[1], out distance)) { return;
Player.Message(p, "Maximum move distance must be an integer between 0 and 256."); }
} else {
byte distance = 0;
if (!CommandParser.GetByte(p, args[1], "Maxmimum move distance", ref distance)) return;
ZombieGameProps.MaxMoveDistance = distance; ZombieGameProps.MaxMoveDistance = distance;
Player.Message(p, "Maximum move distance set to &a" + distance + " %Sunits apart."); Player.Message(p, "Maximum move distance set to &a" + distance + " %Sunits apart.");
SrvProperties.Save(); SrvProperties.Save();
} }
}
public override void Help(Player p) { public override void Help(Player p) {
Player.Message(p, "%T/zg status %H- Shows the current status of Zombie Survival."); Player.Message(p, "%T/zg status %H- Shows the current status of Zombie Survival.");

View File

@ -47,12 +47,7 @@ namespace MCGalaxy.Commands {
Command.all.Find("mute").Use(p, muter.name); Command.all.Find("mute").Use(p, muter.name);
int time = 120; int time = 120;
if (args.Length > 1 && !int.TryParse(args[1], out time)) { if (args.Length > 1 && !CommandParser.GetInt(p, args[1], "Time", ref time, 1)) return;
Player.Message(p, "Invalid time given."); Help(p); return;
}
if (time <= 0) {
Player.Message(p, "Time must be positive and greater than zero."); return;
}
Chat.MessageAll("{0} %Shas been muted for {1} seconds", muter.ColoredName, time); Chat.MessageAll("{0} %Shas been muted for {1} seconds", muter.ColoredName, time);
Player.Message(muter, "You have been muted for " + time + " seconds"); Player.Message(muter, "You have been muted for " + time + " seconds");

View File

@ -82,8 +82,8 @@ namespace MCGalaxy.Commands.Building {
byte block = GetBlock(p, args[2]); byte block = GetBlock(p, args[2]);
if (block == Block.Invalid) return; if (block == Block.Invalid) return;
if (!Utils.CheckHex(p, ref args[3])) return; CustomColor rgb = default(CustomColor);
CustomColor rgb = Colors.ParseHex(args[3]); if (!CommandParser.GetHex(p, args[3], ref rgb)) return;
PaletteEntry entry = new PaletteEntry(rgb.R, rgb.G, rgb.B, block); PaletteEntry entry = new PaletteEntry(rgb.R, rgb.G, rgb.B, block);
AddEntry(p, palette, entry); AddEntry(p, palette, entry);
} }

View File

@ -67,13 +67,8 @@ namespace MCGalaxy.Commands.Building {
return true; return true;
} }
int temp; byte temp = 0;
if (!int.TryParse(arg, out temp)) { if (!CommandParser.GetByte(p, arg, "Value", ref temp)) return false;
Player.Message(p, "/rp [type1] [num] [type2] [num]..."); return false;
}
if (temp < 0 || temp > 255) {
Player.Message(p, "Values must be between 0 and 255."); return false;
}
value = (byte)temp; value = (byte)temp;
switch (name) { switch (name) {

View File

@ -53,11 +53,9 @@ namespace MCGalaxy.Drawing.Brushes {
blocks[j].Block = (byte)block; blocks[j].Ext = extBlock; blocks[j].Block = (byte)block; blocks[j].Ext = extBlock;
if (sepIndex < 0) { j++; continue; } if (sepIndex < 0) { j++; continue; }
int chance; int chance = 0;
if (!int.TryParse(parts[i].Substring(sepIndex + 1), out chance) if (!CommandParser.GetInt(p, parts[i].Substring(sepIndex + 1), "Frequency", ref chance, 1, 10000)) return null;
|| chance <= 0 || chance > 10000) {
Player.Message(p, "frequency must be an integer between 1 and 10,000."); return null;
}
count[j] = chance; count[j] = chance;
j++; j++;
} }

View File

@ -33,9 +33,9 @@ namespace MCGalaxy.Eco {
protected override void DoPurchase(Player p, string message, string[] args) { protected override void DoPurchase(Player p, string message, string[] args) {
byte count = 1; byte count = 1;
if (args.Length >= 2 && !byte.TryParse(args[1], out count) || count == 0 || count > 10) { const string group = "Number of groups of 10 blocks";
Player.Message(p, "Number of groups of 10 blocks to buy must be an integer between 1 and 10."); return; if (args.Length >= 2 && !CommandParser.GetByte(p, args[1], group, ref count, 0, 10)) return;
}
if (p.money < Price * count) { if (p.money < Price * count) {
Player.Message(p, "&cYou don't have enough &3{2} &cto buy {1} {0}.", Player.Message(p, "&cYou don't have enough &3{2} &cto buy {1} {0}.",
Name, count * 10, Server.moneys); return; Name, count * 10, Server.moneys); return;

View File

@ -86,9 +86,11 @@ namespace MCGalaxy {
Player.Message(p, "Reset {0} color for {1} %Sto normal", envTypeName, p.level.ColoredName); Player.Message(p, "Reset {0} color for {1} %Sto normal", envTypeName, p.level.ColoredName);
target = ""; target = "";
} else { } else {
if (!Utils.CheckHex(p, ref value)) return; CustomColor rgb = default(CustomColor);
if (!CommandParser.GetHex(p, value, ref rgb)) return;
Player.Message(p, "Set {0} color for {1} %Sto #{2}", envTypeName, p.level.ColoredName, value); Player.Message(p, "Set {0} color for {1} %Sto #{2}", envTypeName, p.level.ColoredName, value);
target = value; target = Utils.Hex(rgb.R, rgb.G, rgb.B);
} }
UpdateEnvColor(p, envType, value); UpdateEnvColor(p, envType, value);
} }
@ -129,16 +131,13 @@ namespace MCGalaxy {
} }
static bool CheckShort(Player p, string raw, string variable, ref int modify) { static bool CheckShort(Player p, string raw, string variable, ref int modify) {
short value; int value = 0;
if (!short.TryParse(raw, out value)) { if (!CommandParser.GetInt(p, raw, variable, ref value, short.MinValue, short.MaxValue)) return false;
Player.Message(p, "Env: \"{0}\" is not a valid integer.", value);
return false; modify = (short)value;
} else {
modify = value;
Player.Message(p, "Set {0} for {1} %Sto {2}", variable, p.level.ColoredName, value); Player.Message(p, "Set {0} for {1} %Sto {2}", variable, p.level.ColoredName, value);
return true; return true;
} }
}
static bool CheckFloat(Player p, string raw, string variable, static bool CheckFloat(Player p, string raw, string variable,
ref int modify, int scale, float min, float max) { ref int modify, int scale, float min, float max) {

View File

@ -26,16 +26,6 @@ namespace MCGalaxy {
/// <summary> The absolute path on disc of the folder MCGalaxy.exe is currently running from. </summary> /// <summary> The absolute path on disc of the folder MCGalaxy.exe is currently running from. </summary>
public static string FolderPath { get { return AppDomain.CurrentDomain.BaseDirectory; } } public static string FolderPath { get { return AppDomain.CurrentDomain.BaseDirectory; } }
public static bool CheckHex(Player p, ref string arg) {
if (arg.Length > 0 && arg[0] == '#')
arg = arg.Substring(1);
if (!IsValidHex(arg)) {
Player.Message(p, "\"#{0}\" is not a valid HEX color.", arg); return false;
}
return true;
}
public static bool IsValidHex(string hex) { public static bool IsValidHex(string hex) {
if (hex.Length != 6) return false; if (hex.Length != 6) return false;