Allow boolean params to not require a argument to be true
This commit is contained in:
parent
61d8ca7b42
commit
e940a9e0e9
@ -335,10 +335,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
|
|||||||
args = newArgs;
|
args = newArgs;
|
||||||
for (int i = 0; i < args.length; i += 2) {
|
for (int i = 0; i < args.length; i += 2) {
|
||||||
String methodName = args[i];
|
String methodName = args[i];
|
||||||
if (i + 1 >= args.length) {
|
String valueString = (args.length - 1 == i ? null : args[i + 1]);
|
||||||
throw new Exception(ChatColor.RED + "No value was given for the option " + methodName);
|
|
||||||
}
|
|
||||||
String valueString = args[i + 1];
|
|
||||||
Method methodToUse = null;
|
Method methodToUse = null;
|
||||||
Object value = null;
|
Object value = null;
|
||||||
for (Method method : disguise.getWatcher().getClass().getMethods()) {
|
for (Method method : disguise.getWatcher().getClass().getMethods()) {
|
||||||
@ -355,92 +352,100 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
|
|||||||
Class<?>[] types = methodToUse.getParameterTypes();
|
Class<?>[] types = methodToUse.getParameterTypes();
|
||||||
if (types.length == 1) {
|
if (types.length == 1) {
|
||||||
Class param = types[0];
|
Class param = types[0];
|
||||||
if (int.class == param) {
|
if (boolean.class == param) {
|
||||||
// Parse to integer
|
|
||||||
if (isNumeric(valueString)) {
|
|
||||||
value = (int) Integer.parseInt(valueString);
|
|
||||||
} else {
|
|
||||||
throw parseToException("number", valueString, methodName);
|
|
||||||
}
|
|
||||||
} else if (float.class == param || double.class == param) {
|
|
||||||
// Parse to number
|
|
||||||
if (isDouble(valueString)) {
|
|
||||||
float obj = Float.parseFloat(valueString);
|
|
||||||
if (param == float.class) {
|
|
||||||
value = (float) obj;
|
|
||||||
} else if (param == double.class) {
|
|
||||||
value = (double) obj;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw parseToException("number.0", valueString, methodName);
|
|
||||||
}
|
|
||||||
} else if (boolean.class == param) {
|
|
||||||
// Parse to boolean
|
// Parse to boolean
|
||||||
if (!("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString)))
|
if (valueString == null || !("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString))) {
|
||||||
throw parseToException("true/false", valueString, methodName);
|
value = true;
|
||||||
value = (boolean) "true".equalsIgnoreCase(valueString);
|
i--;
|
||||||
} else if (param == String.class) {
|
|
||||||
// Parse to string
|
|
||||||
value = ChatColor.translateAlternateColorCodes('&', valueString);
|
|
||||||
} else if (param == AnimalColor.class) {
|
|
||||||
// Parse to animal color
|
|
||||||
try {
|
|
||||||
value = AnimalColor.valueOf(valueString.toUpperCase());
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw parseToException("animal color", valueString, methodName);
|
|
||||||
}
|
|
||||||
} else if (param == ItemStack.class) {
|
|
||||||
// Parse to itemstack
|
|
||||||
try {
|
|
||||||
value = parseToItemstack(valueString);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw new Exception(String.format(ex.getMessage(), methodName));
|
|
||||||
}
|
|
||||||
} else if (param == ItemStack[].class) {
|
|
||||||
// Parse to itemstack array
|
|
||||||
ItemStack[] items = new ItemStack[4];
|
|
||||||
String[] split = valueString.split(",");
|
|
||||||
if (split.length == 4) {
|
|
||||||
for (int a = 0; a < 4; a++) {
|
|
||||||
try {
|
|
||||||
items[a] = parseToItemstack(split[a]);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
|
||||||
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
value = "true".equalsIgnoreCase(valueString);
|
||||||
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
|
||||||
}
|
}
|
||||||
value = items;
|
} else {
|
||||||
} else if (param.getSimpleName().equals("Color")) {
|
if (valueString == null) {
|
||||||
// Parse to horse color
|
throw new Exception(ChatColor.RED + "No value was given for the option " + methodName);
|
||||||
value = callValueOf(param, valueString, methodName, "a horse color");
|
}
|
||||||
} else if (param.getSimpleName().equals("Style")) {
|
if (int.class == param) {
|
||||||
// Parse to horse style
|
// Parse to integer
|
||||||
value = callValueOf(param, valueString, methodName, "a horse style");
|
if (isNumeric(valueString)) {
|
||||||
} else if (param.getSimpleName().equals("Profession")) {
|
value = (int) Integer.parseInt(valueString);
|
||||||
// Parse to villager profession
|
} else {
|
||||||
value = callValueOf(param, valueString, methodName, "a villager profession");
|
throw parseToException("number", valueString, methodName);
|
||||||
} else if (param.getSimpleName().equals("Art")) {
|
}
|
||||||
// Parse to art type
|
} else if (float.class == param || double.class == param) {
|
||||||
value = callValueOf(param, valueString, methodName, "a painting art");
|
// Parse to number
|
||||||
} else if (param.getSimpleName().equals("Type")) {
|
if (isDouble(valueString)) {
|
||||||
// Parse to ocelot type
|
float obj = Float.parseFloat(valueString);
|
||||||
value = callValueOf(param, valueString, methodName, "a ocelot type");
|
if (param == float.class) {
|
||||||
} else if (param == PotionEffectType.class) {
|
value = (float) obj;
|
||||||
// Parse to potion effect
|
} else if (param == double.class) {
|
||||||
try {
|
value = (double) obj;
|
||||||
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
|
}
|
||||||
if (potionType == null && isNumeric(valueString)) {
|
} else {
|
||||||
potionType = PotionEffectType.getById(Integer.parseInt(valueString));
|
throw parseToException("number.0", valueString, methodName);
|
||||||
|
}
|
||||||
|
} else if (param == String.class) {
|
||||||
|
// Parse to string
|
||||||
|
value = ChatColor.translateAlternateColorCodes('&', valueString);
|
||||||
|
} else if (param == AnimalColor.class) {
|
||||||
|
// Parse to animal color
|
||||||
|
try {
|
||||||
|
value = AnimalColor.valueOf(valueString.toUpperCase());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw parseToException("animal color", valueString, methodName);
|
||||||
|
}
|
||||||
|
} else if (param == ItemStack.class) {
|
||||||
|
// Parse to itemstack
|
||||||
|
try {
|
||||||
|
value = parseToItemstack(valueString);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw new Exception(String.format(ex.getMessage(), methodName));
|
||||||
|
}
|
||||||
|
} else if (param == ItemStack[].class) {
|
||||||
|
// Parse to itemstack array
|
||||||
|
ItemStack[] items = new ItemStack[4];
|
||||||
|
String[] split = valueString.split(",");
|
||||||
|
if (split.length == 4) {
|
||||||
|
for (int a = 0; a < 4; a++) {
|
||||||
|
try {
|
||||||
|
items[a] = parseToItemstack(split[a]);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
||||||
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
||||||
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
||||||
|
}
|
||||||
|
value = items;
|
||||||
|
} else if (param.getSimpleName().equals("Color")) {
|
||||||
|
// Parse to horse color
|
||||||
|
value = callValueOf(param, valueString, methodName, "a horse color");
|
||||||
|
} else if (param.getSimpleName().equals("Style")) {
|
||||||
|
// Parse to horse style
|
||||||
|
value = callValueOf(param, valueString, methodName, "a horse style");
|
||||||
|
} else if (param.getSimpleName().equals("Profession")) {
|
||||||
|
// Parse to villager profession
|
||||||
|
value = callValueOf(param, valueString, methodName, "a villager profession");
|
||||||
|
} else if (param.getSimpleName().equals("Art")) {
|
||||||
|
// Parse to art type
|
||||||
|
value = callValueOf(param, valueString, methodName, "a painting art");
|
||||||
|
} else if (param.getSimpleName().equals("Type")) {
|
||||||
|
// Parse to ocelot type
|
||||||
|
value = callValueOf(param, valueString, methodName, "a ocelot type");
|
||||||
|
} else if (param == PotionEffectType.class) {
|
||||||
|
// Parse to potion effect
|
||||||
|
try {
|
||||||
|
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
|
||||||
|
if (potionType == null && isNumeric(valueString)) {
|
||||||
|
potionType = PotionEffectType.getById(Integer.parseInt(valueString));
|
||||||
|
}
|
||||||
|
if (potionType == null)
|
||||||
|
throw new Exception();
|
||||||
|
value = potionType;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw parseToException("a potioneffect type", valueString, methodName);
|
||||||
}
|
}
|
||||||
if (potionType == null)
|
|
||||||
throw new Exception();
|
|
||||||
value = potionType;
|
|
||||||
} catch (Exception ex) {
|
|
||||||
throw parseToException("a potioneffect type", valueString, methodName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user