diff --git a/MCGalaxy/Commands/CPE/CmdCustomColors.cs b/MCGalaxy/Commands/CPE/CmdCustomColors.cs
index 25bd410a3..8513553e8 100644
--- a/MCGalaxy/Commands/CPE/CmdCustomColors.cs
+++ b/MCGalaxy/Commands/CPE/CmdCustomColors.cs
@@ -70,10 +70,10 @@ namespace MCGalaxy.Commands.CPE {
}
char fallback;
- if (!CheckName(p, args[2]) || !CheckFallback(p, args[3], out fallback)
- || !Utils.CheckHex(p, ref args[4])) return;
-
+ if (!CheckName(p, args[2]) || !CheckFallback(p, args[3], out fallback)) return;
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];
Colors.AddExtColor(col);
Player.Message(p, "Successfully added a custom color.");
@@ -145,8 +145,8 @@ namespace MCGalaxy.Commands.CPE {
col.Fallback = fallback; break;
case "hex":
case "color":
- if (!Utils.CheckHex(p, ref args[3])) return;
- CustomColor rgb = Colors.ParseHex(args[3]);
+ CustomColor rgb = default(CustomColor);
+ if (!CommandParser.GetHex(p, args[3], ref rgb)) return;
col.R = rgb.R; col.G = rgb.G; col.B = rgb.B;
break;
default:
diff --git a/MCGalaxy/Commands/CPE/CustomBlockCommand.cs b/MCGalaxy/Commands/CPE/CustomBlockCommand.cs
index 4dfce1061..ee8b0d06f 100644
--- a/MCGalaxy/Commands/CPE/CustomBlockCommand.cs
+++ b/MCGalaxy/Commands/CPE/CustomBlockCommand.cs
@@ -262,8 +262,8 @@ namespace MCGalaxy.Commands.CPE {
step += (bd.FogDensity == 0 ? 2 : 1);
}
} else if (step == 16) {
- if (Utils.CheckHex(p, ref value)) {
- CustomColor rgb = Colors.ParseHex(value);
+ CustomColor rgb = default(CustomColor);
+ if (CommandParser.GetHex(p, value, ref rgb)) {
bd.FogR = rgb.R; bd.FogG = rgb.G; bd.FogB = rgb.B;
step++;
}
@@ -421,8 +421,8 @@ namespace MCGalaxy.Commands.CPE {
case "col":
case "fogcol":
case "fogcolor":
- if (!Utils.CheckHex(p, ref value)) return;
- CustomColor rgb = Colors.ParseHex(value);
+ CustomColor rgb = default(CustomColor);
+ if (!CommandParser.GetHex(p, value, ref rgb)) return;
def.FogR = rgb.R; def.FogG = rgb.G; def.FogB = rgb.B;
break;
diff --git a/MCGalaxy/Commands/CommandParser.cs b/MCGalaxy/Commands/CommandParser.cs
index 62c26aafa..20145a885 100644
--- a/MCGalaxy/Commands/CommandParser.cs
+++ b/MCGalaxy/Commands/CommandParser.cs
@@ -37,6 +37,7 @@ namespace MCGalaxy {
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) {
@@ -48,11 +49,11 @@ namespace MCGalaxy {
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);
+ 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);
+ 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);
+ Player.Message(p, "{0} must be between {1} and {2}", type, min, max);
}
return false;
}
@@ -77,5 +78,18 @@ namespace MCGalaxy {
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;
+ }
}
}
diff --git a/MCGalaxy/Commands/Fun/CmdTntWars.cs b/MCGalaxy/Commands/Fun/CmdTntWars.cs
index 93bb96a38..f9e409191 100644
--- a/MCGalaxy/Commands/Fun/CmdTntWars.cs
+++ b/MCGalaxy/Commands/Fun/CmdTntWars.cs
@@ -760,15 +760,11 @@ namespace MCGalaxy.Commands {
text[3] = text[4];
}
int numb = -1;
- if (!int.TryParse(text[3], out numb))
- { 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;
- Player.Message(p, "TNT Wars: Score per kill is now " + numb + " points!");
- return;
- }
+ if (!CommandParser.GetInt(p, text[3], "Score per kill", ref numb, 0)) return;
+
+ it.ScorePerKill = numb;
+ Player.Message(p, "TNT Wars: Score per kill is now " + numb + " points!");
+ return;
//break;
}
break;
diff --git a/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs b/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs
index 9ffa8aa04..17dde41e1 100644
--- a/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs
+++ b/MCGalaxy/Commands/Fun/ZombieSurvival/CmdZombieGame.cs
@@ -35,13 +35,13 @@ namespace MCGalaxy.Commands {
if (message == "") { Help(p); return; }
string[] args = message.ToLower().Split(' ');
switch (args[0]) {
- case "go": HandleGo(p, message, args); break;
- case "status": HandleStatus(p, message, args); break;
- case "start": HandleStart(p, message, args); break;
- case "stop": HandleStop(p, message, args); break;
- case "force": HandleForceStop(p, message, args); break;
- case "hitbox": HandleHitbox(p, message, args); break;
- case "maxmove": HandleMaxMove(p, message, args); break;
+ case "go": HandleGo(p, message, args); break;
+ case "status": HandleStatus(p, message, args); break;
+ case "start": HandleStart(p, message, args); break;
+ case "stop": HandleStop(p, message, args); break;
+ case "force": HandleForceStop(p, message, args); break;
+ case "hitbox": HandleHitbox(p, message, args); break;
+ case "maxmove": HandleMaxMove(p, message, args); break;
}
}
@@ -108,29 +108,31 @@ namespace MCGalaxy.Commands {
}
static void HandleHitbox(Player p, string message, string[] args) {
- byte precision;
if (args.Length == 1) {
Player.Message(p, "Hitbox detection is currently &a" + ZombieGameProps.HitboxPrecision + " %Sunits apart.");
- } else if (!byte.TryParse(args[1], out precision)) {
- Player.Message(p, "Hitbox detection must be an integer between 0 and 256.");
- } else {
- ZombieGameProps.HitboxPrecision = precision;
- Player.Message(p, "Hitbox detection set to &a" + precision + " %Sunits apart.");
- SrvProperties.Save();
+ return;
}
+
+ byte precision = 0;
+ if (!CommandParser.GetByte(p, args[1], "Hitbox detection", ref precision)) return;
+
+ ZombieGameProps.HitboxPrecision = precision;
+ Player.Message(p, "Hitbox detection set to &a" + precision + " %Sunits apart.");
+ SrvProperties.Save();
}
static void HandleMaxMove(Player p, string message, string[] args) {
- byte distance;
if (args.Length == 1) {
Player.Message(p, "Maxmium move distance is currently &a" + ZombieGameProps.MaxMoveDistance + " %Sunits apart.");
- } else if (!byte.TryParse(args[1], out distance)) {
- Player.Message(p, "Maximum move distance must be an integer between 0 and 256.");
- } else {
- ZombieGameProps.MaxMoveDistance = distance;
- Player.Message(p, "Maximum move distance set to &a" + distance + " %Sunits apart.");
- SrvProperties.Save();
+ return;
}
+
+ byte distance = 0;
+ if (!CommandParser.GetByte(p, args[1], "Maxmimum move distance", ref distance)) return;
+
+ ZombieGameProps.MaxMoveDistance = distance;
+ Player.Message(p, "Maximum move distance set to &a" + distance + " %Sunits apart.");
+ SrvProperties.Save();
}
public override void Help(Player p) {
diff --git a/MCGalaxy/Commands/Moderation/CmdXmute.cs b/MCGalaxy/Commands/Moderation/CmdXmute.cs
index f7aa8268e..75b2cc926 100644
--- a/MCGalaxy/Commands/Moderation/CmdXmute.cs
+++ b/MCGalaxy/Commands/Moderation/CmdXmute.cs
@@ -47,12 +47,7 @@ namespace MCGalaxy.Commands {
Command.all.Find("mute").Use(p, muter.name);
int time = 120;
- if (args.Length > 1 && !int.TryParse(args[1], out time)) {
- Player.Message(p, "Invalid time given."); Help(p); return;
- }
- if (time <= 0) {
- Player.Message(p, "Time must be positive and greater than zero."); return;
- }
+ if (args.Length > 1 && !CommandParser.GetInt(p, args[1], "Time", ref time, 1)) return;
Chat.MessageAll("{0} %Shas been muted for {1} seconds", muter.ColoredName, time);
Player.Message(muter, "You have been muted for " + time + " seconds");
diff --git a/MCGalaxy/Commands/building/CmdPalette.cs b/MCGalaxy/Commands/building/CmdPalette.cs
index cd7fa3d33..51f30049f 100644
--- a/MCGalaxy/Commands/building/CmdPalette.cs
+++ b/MCGalaxy/Commands/building/CmdPalette.cs
@@ -82,8 +82,8 @@ namespace MCGalaxy.Commands.Building {
byte block = GetBlock(p, args[2]);
if (block == Block.Invalid) return;
- if (!Utils.CheckHex(p, ref args[3])) return;
- CustomColor rgb = Colors.ParseHex(args[3]);
+ CustomColor rgb = default(CustomColor);
+ if (!CommandParser.GetHex(p, args[3], ref rgb)) return;
PaletteEntry entry = new PaletteEntry(rgb.R, rgb.G, rgb.B, block);
AddEntry(p, palette, entry);
}
diff --git a/MCGalaxy/Commands/building/CmdRestartPhysics.cs b/MCGalaxy/Commands/building/CmdRestartPhysics.cs
index d21bfa7d5..4d22b2eaa 100644
--- a/MCGalaxy/Commands/building/CmdRestartPhysics.cs
+++ b/MCGalaxy/Commands/building/CmdRestartPhysics.cs
@@ -67,13 +67,8 @@ namespace MCGalaxy.Commands.Building {
return true;
}
- int temp;
- if (!int.TryParse(arg, out temp)) {
- 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;
- }
+ byte temp = 0;
+ if (!CommandParser.GetByte(p, arg, "Value", ref temp)) return false;
value = (byte)temp;
switch (name) {
diff --git a/MCGalaxy/Drawing/BrushFactories/FrequencyBrushes.cs b/MCGalaxy/Drawing/BrushFactories/FrequencyBrushes.cs
index 051813bf8..3a990d3a6 100644
--- a/MCGalaxy/Drawing/BrushFactories/FrequencyBrushes.cs
+++ b/MCGalaxy/Drawing/BrushFactories/FrequencyBrushes.cs
@@ -53,11 +53,9 @@ namespace MCGalaxy.Drawing.Brushes {
blocks[j].Block = (byte)block; blocks[j].Ext = extBlock;
if (sepIndex < 0) { j++; continue; }
- int chance;
- if (!int.TryParse(parts[i].Substring(sepIndex + 1), out chance)
- || chance <= 0 || chance > 10000) {
- Player.Message(p, "frequency must be an integer between 1 and 10,000."); return null;
- }
+ int chance = 0;
+ if (!CommandParser.GetInt(p, parts[i].Substring(sepIndex + 1), "Frequency", ref chance, 1, 10000)) return null;
+
count[j] = chance;
j++;
}
diff --git a/MCGalaxy/Economy/ZombieItems.cs b/MCGalaxy/Economy/ZombieItems.cs
index bf298b2df..ad65ee8eb 100644
--- a/MCGalaxy/Economy/ZombieItems.cs
+++ b/MCGalaxy/Economy/ZombieItems.cs
@@ -33,9 +33,9 @@ namespace MCGalaxy.Eco {
protected override void DoPurchase(Player p, string message, string[] args) {
byte count = 1;
- if (args.Length >= 2 && !byte.TryParse(args[1], out count) || count == 0 || count > 10) {
- Player.Message(p, "Number of groups of 10 blocks to buy must be an integer between 1 and 10."); return;
- }
+ const string group = "Number of groups of 10 blocks";
+ if (args.Length >= 2 && !CommandParser.GetByte(p, args[1], group, ref count, 0, 10)) return;
+
if (p.money < Price * count) {
Player.Message(p, "&cYou don't have enough &3{2} &cto buy {1} {0}.",
Name, count * 10, Server.moneys); return;
diff --git a/MCGalaxy/Levels/LevelEnv.cs b/MCGalaxy/Levels/LevelEnv.cs
index acce4050c..568ab7c9f 100644
--- a/MCGalaxy/Levels/LevelEnv.cs
+++ b/MCGalaxy/Levels/LevelEnv.cs
@@ -86,9 +86,11 @@ namespace MCGalaxy {
Player.Message(p, "Reset {0} color for {1} %Sto normal", envTypeName, p.level.ColoredName);
target = "";
} 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);
- target = value;
+ target = Utils.Hex(rgb.R, rgb.G, rgb.B);
}
UpdateEnvColor(p, envType, value);
}
@@ -98,10 +100,10 @@ namespace MCGalaxy {
if (IsResetString(value)) {
Player.Message(p, "Reset {0} for {1} %Sto normal", variable, p.level.ColoredName);
target = defValue;
- } else {
+ } else {
if (!CommandParser.GetBool(p, value, ref target)) return;
- Player.Message(p, "Set {0} for {1} %Sto {2}", variable,
+ Player.Message(p, "Set {0} for {1} %Sto {2}", variable,
p.level.ColoredName, target ? "&aON" : "&cOFF");
}
SendCurrentMapAppearance(p.level, prop, target ? 1 : 0);
@@ -129,15 +131,12 @@ namespace MCGalaxy {
}
static bool CheckShort(Player p, string raw, string variable, ref int modify) {
- short value;
- if (!short.TryParse(raw, out value)) {
- Player.Message(p, "Env: \"{0}\" is not a valid integer.", value);
- return false;
- } else {
- modify = value;
- Player.Message(p, "Set {0} for {1} %Sto {2}", variable, p.level.ColoredName, value);
- return true;
- }
+ int value = 0;
+ if (!CommandParser.GetInt(p, raw, variable, ref value, short.MinValue, short.MaxValue)) return false;
+
+ modify = (short)value;
+ Player.Message(p, "Set {0} for {1} %Sto {2}", variable, p.level.ColoredName, value);
+ return true;
}
static bool CheckFloat(Player p, string raw, string variable,
diff --git a/MCGalaxy/util/Utils.cs b/MCGalaxy/util/Utils.cs
index 015cc6f5b..9d44d35b2 100644
--- a/MCGalaxy/util/Utils.cs
+++ b/MCGalaxy/util/Utils.cs
@@ -25,16 +25,6 @@ namespace MCGalaxy {
/// The absolute path on disc of the folder MCGalaxy.exe is currently running from.
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) {
if (hex.Length != 6) return false;