Fix up basecommand and booleans

This commit is contained in:
libraryaddict 2014-09-27 15:41:30 +12:00
parent ddac7feda3
commit 702a58c3bf

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map.Entry;
import me.libraryaddict.disguise.disguisetypes.AnimalColor; import me.libraryaddict.disguise.disguisetypes.AnimalColor;
import me.libraryaddict.disguise.disguisetypes.Disguise; import me.libraryaddict.disguise.disguisetypes.Disguise;
@ -473,140 +474,149 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
Method methodToUse = null; Method methodToUse = null;
Object value = null; Object value = null;
DisguiseParseException storedEx = null; DisguiseParseException storedEx = null;
for (Method method : methods) { int c = 0;
if (!method.getName().startsWith("get") && method.getName().equalsIgnoreCase(methodName) while (c < methods.length) {
&& method.getAnnotation(Deprecated.class) == null && method.getParameterTypes().length == 1) { try {
try { Entry<Method, Integer> entry = getMethod(methods, methodName, c);
methodToUse = method; if (entry == null) {
methodName = methodToUse.getName(); break;
Class<?>[] types = methodToUse.getParameterTypes(); }
Class param = types[0]; methodToUse = entry.getKey();
if (valueString != null) { c = entry.getValue();
if (int.class == param) { methodName = methodToUse.getName();
// Parse to integer Class<?>[] types = methodToUse.getParameterTypes();
if (isNumeric(valueString)) { Class param = types[0];
value = (int) Integer.parseInt(valueString); if (valueString != null) {
} else { if (int.class == param) {
throw parseToException("number", valueString, methodName); // 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 if (float.class == param || double.class == param) { } else {
// Parse to number throw parseToException("number.0", valueString, methodName);
if (isDouble(valueString)) { }
float obj = Float.parseFloat(valueString); } else if (param == String.class) {
if (param == float.class) { // Parse to string
value = (float) obj; value = ChatColor.translateAlternateColorCodes('&', valueString);
} else if (param == double.class) { } else if (param == AnimalColor.class) {
value = (double) obj; // Parse to animal color
} try {
} else { value = AnimalColor.valueOf(valueString.toUpperCase());
throw parseToException("number.0", valueString, methodName); } catch (Exception ex) {
} throw parseToException("animal color", valueString, methodName);
} else if (param == String.class) { }
// Parse to string } else if (param == ItemStack.class) {
value = ChatColor.translateAlternateColorCodes('&', valueString); // Parse to itemstack
} else if (param == AnimalColor.class) { try {
// Parse to animal color value = parseToItemstack(valueString);
try { } catch (Exception ex) {
value = AnimalColor.valueOf(valueString.toUpperCase()); throw new DisguiseParseException(String.format(ex.getMessage(), methodName));
} catch (Exception ex) { }
throw parseToException("animal color", valueString, methodName); } else if (param == ItemStack[].class) {
} // Parse to itemstack array
} else if (param == ItemStack.class) { ItemStack[] items = new ItemStack[4];
// Parse to itemstack String[] split = valueString.split(",");
try { if (split.length == 4) {
value = parseToItemstack(valueString); for (int a = 0; a < 4; a++) {
} catch (Exception ex) {
throw new DisguiseParseException(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 DisguiseParseException();
value = potionType;
} catch (Exception ex) {
throw parseToException("a potioneffect type", valueString, methodName);
}
} else if (param == int[].class) {
String[] split = valueString.split(",");
int[] values = new int[split.length];
for (int b = 0; b < values.length; b++) {
try { try {
values[b] = Integer.parseInt(split[b]); items[a] = parseToItemstack(split[a]);
} catch (NumberFormatException ex) { } catch (Exception ex) {
throw parseToException("Number,Number,Number...", valueString, methodName); throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
} }
} }
value = values; } else {
} else if (param == BlockFace.class) { 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 DisguiseParseException();
value = potionType;
} catch (Exception ex) {
throw parseToException("a potioneffect type", valueString, methodName);
}
} else if (param == int[].class) {
String[] split = valueString.split(",");
int[] values = new int[split.length];
for (int b = 0; b < values.length; b++) {
try { try {
BlockFace face = BlockFace.valueOf(valueString.toUpperCase()); values[b] = Integer.parseInt(split[b]);
if (face.ordinal() > 3) } catch (NumberFormatException ex) {
throw new DisguiseParseException(); throw parseToException("Number,Number,Number...", valueString, methodName);
value = face;
} catch (Exception ex) {
throw parseToException("a direction (north, east, south, west)", valueString, methodName);
} }
} }
value = values;
} else if (param == BlockFace.class) {
try {
BlockFace face = BlockFace.valueOf(valueString.toUpperCase());
if (face.ordinal() > 3)
throw new DisguiseParseException();
value = face;
} catch (Exception ex) {
throw parseToException("a direction (north, east, south, west)", valueString, methodName);
}
} }
if (value == null && boolean.class == param) { }
if (valueString == null) { if (value == null && boolean.class == param) {
if (valueString == null) {
value = true;
i--;
} else if (valueString.equalsIgnoreCase("true")) {
value = true;
} else if (valueString.equalsIgnoreCase("false")) {
value = false;
} else {
if (getMethod(methods, valueString, 0) == null) {
throw parseToException("true/false", valueString, methodName);
} else {
value = true; value = true;
i--; i--;
} else if (valueString.equalsIgnoreCase("true")) { }
value = true;
} else if (valueString.equalsIgnoreCase("false")) {
value = false;
} else
throw parseToException("true/false", valueString, methodName);
} }
if (value != null) {
break;
}
} catch (DisguiseParseException ex) {
storedEx = ex;
methodToUse = null;
} catch (Exception ex) {
ex.printStackTrace();
methodToUse = null;
} }
if (value != null) {
break;
}
} catch (DisguiseParseException ex) {
storedEx = ex;
methodToUse = null;
} catch (Exception ex) {
ex.printStackTrace();
methodToUse = null;
} }
} }
if (methodToUse == null) { if (methodToUse == null) {
@ -631,6 +641,17 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
return disguise; return disguise;
} }
private Entry<Method, Integer> getMethod(Method[] methods, String methodName, int toStart) {
for (int i = toStart; i < methods.length; i++) {
Method method = methods[i];
if (!method.getName().startsWith("get") && method.getName().equalsIgnoreCase(methodName)
&& method.getAnnotation(Deprecated.class) == null && method.getParameterTypes().length == 1) {
return new HashMap.SimpleEntry(method, ++i);
}
}
return null;
}
private Object callValueOf(Class<?> param, String valueString, String methodName, String description) private Object callValueOf(Class<?> param, String valueString, String methodName, String description)
throws DisguiseParseException { throws DisguiseParseException {
Object value; Object value;