Make a callValueOf() method to reduce code repetition
This commit is contained in:
parent
1207d70d6e
commit
983026a898
@ -289,58 +289,55 @@ 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];
|
||||||
// Parse to number
|
|
||||||
if (int.class == param) {
|
if (int.class == param) {
|
||||||
|
// Parse to integer
|
||||||
if (isNumeric(valueString)) {
|
if (isNumeric(valueString)) {
|
||||||
value = (int) Integer.parseInt(valueString);
|
value = (int) Integer.parseInt(valueString);
|
||||||
} else {
|
} else {
|
||||||
throw parseToException("number", valueString, methodName);
|
throw parseToException("number", valueString, methodName);
|
||||||
}
|
}
|
||||||
// Parse to boolean
|
|
||||||
} else if (float.class == param || double.class == param) {
|
} else if (float.class == param || double.class == param) {
|
||||||
|
// Parse to number
|
||||||
if (isDouble(valueString)) {
|
if (isDouble(valueString)) {
|
||||||
float obj = Float.parseFloat(valueString);
|
float obj = Float.parseFloat(valueString);
|
||||||
if (param == float.class) {
|
if (param == float.class) {
|
||||||
value = (float) obj;
|
value = (float) obj;
|
||||||
} else if (param == int.class) {
|
|
||||||
value = (int) obj;
|
|
||||||
} else if (param == double.class) {
|
} else if (param == double.class) {
|
||||||
value = (double) obj;
|
value = (double) obj;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw parseToException("number.0", valueString, methodName);
|
throw parseToException("number.0", valueString, methodName);
|
||||||
}
|
}
|
||||||
// Parse to boolean
|
|
||||||
} else if (boolean.class == param) {
|
} else if (boolean.class == param) {
|
||||||
|
// Parse to boolean
|
||||||
if (!("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString)))
|
if (!("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString)))
|
||||||
throw parseToException("true/false", valueString, methodName);
|
throw parseToException("true/false", valueString, methodName);
|
||||||
value = (boolean) "true".equalsIgnoreCase(valueString);
|
value = (boolean) "true".equalsIgnoreCase(valueString);
|
||||||
// Parse to string
|
|
||||||
} else if (param == String.class) {
|
} else if (param == String.class) {
|
||||||
|
// Parse to string
|
||||||
value = ChatColor.translateAlternateColorCodes('&', valueString);
|
value = ChatColor.translateAlternateColorCodes('&', valueString);
|
||||||
// Parse to animal color
|
|
||||||
} else if (param == AnimalColor.class) {
|
} else if (param == AnimalColor.class) {
|
||||||
|
// Parse to animal color
|
||||||
try {
|
try {
|
||||||
value = AnimalColor.valueOf(valueString.toUpperCase());
|
value = AnimalColor.valueOf(valueString.toUpperCase());
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw parseToException("animal color", valueString, methodName);
|
throw parseToException("animal color", valueString, methodName);
|
||||||
}
|
}
|
||||||
// Parse to itemstack
|
|
||||||
} else if (param == ItemStack.class) {
|
} else if (param == ItemStack.class) {
|
||||||
|
// Parse to itemstack
|
||||||
try {
|
try {
|
||||||
value = parseToItemstack(valueString);
|
value = parseToItemstack(valueString);
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw new Exception(String.format(ex.getMessage(), methodName));
|
throw new Exception(String.format(ex.getMessage(), methodName));
|
||||||
}
|
}
|
||||||
// Parse to itemstack array
|
|
||||||
} else if (param == ItemStack[].class) {
|
} else if (param == ItemStack[].class) {
|
||||||
|
// Parse to itemstack array
|
||||||
ItemStack[] items = new ItemStack[4];
|
ItemStack[] items = new ItemStack[4];
|
||||||
String[] split = valueString.split(",");
|
String[] split = valueString.split(",");
|
||||||
if (split.length == 4) {
|
if (split.length == 4) {
|
||||||
for (int a = 0; a < 4; a++) {
|
for (int a = 0; a < 4; a++) {
|
||||||
try {
|
try {
|
||||||
ItemStack item = parseToItemstack(split[a]);
|
items[a] = parseToItemstack(split[a]);
|
||||||
items[a] = item;
|
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
throw parseToException("item ID,ID,ID,ID" + ChatColor.RED + " or " + ChatColor.GREEN
|
||||||
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
||||||
@ -351,45 +348,23 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
|
|||||||
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
+ "ID:Data,ID:Data,ID:Data,ID:Data combo", valueString, methodName);
|
||||||
}
|
}
|
||||||
value = items;
|
value = items;
|
||||||
// Parse to horse color
|
|
||||||
} else if (param.getSimpleName().equals("Color")) {
|
} else if (param.getSimpleName().equals("Color")) {
|
||||||
try {
|
// Parse to horse color
|
||||||
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
|
value = callValueOf(param, valueString, methodName, "a horse color");
|
||||||
} catch (Exception ex) {
|
|
||||||
throw parseToException("a horse color", valueString, methodName);
|
|
||||||
}
|
|
||||||
// Parse to horse style
|
|
||||||
} else if (param.getSimpleName().equals("Style")) {
|
} else if (param.getSimpleName().equals("Style")) {
|
||||||
try {
|
// Parse to horse style
|
||||||
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
|
value = callValueOf(param, valueString, methodName, "a horse style");
|
||||||
} catch (Exception ex) {
|
|
||||||
throw parseToException("a horse style", valueString, methodName);
|
|
||||||
}
|
|
||||||
// Parse to villager profession
|
|
||||||
} else if (param.getSimpleName().equals("Profession")) {
|
} else if (param.getSimpleName().equals("Profession")) {
|
||||||
try {
|
// Parse to villager profession
|
||||||
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
|
value = callValueOf(param, valueString, methodName, "a villager profession");
|
||||||
} catch (Exception ex) {
|
|
||||||
throw parseToException("a villager profession", valueString, methodName);
|
|
||||||
}
|
|
||||||
// Parse to ocelot type
|
|
||||||
} else if (param.getSimpleName().equals("Art")) {
|
} else if (param.getSimpleName().equals("Art")) {
|
||||||
try {
|
// Parse to art type
|
||||||
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
|
value = callValueOf(param, valueString, methodName, "a painting art");
|
||||||
} catch (Exception ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
throw parseToException("a painting art", valueString, methodName);
|
|
||||||
}
|
|
||||||
// Parse to ocelot type
|
|
||||||
} else if (param.getSimpleName().equals("Type")) {
|
} else if (param.getSimpleName().equals("Type")) {
|
||||||
try {
|
// Parse to ocelot type
|
||||||
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
|
value = callValueOf(param, valueString, methodName, "a ocelot type");
|
||||||
} catch (Exception ex) {
|
|
||||||
throw parseToException("a ocelot type", valueString, methodName);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Parse to potion effect
|
|
||||||
} else if (param == PotionEffectType.class) {
|
} else if (param == PotionEffectType.class) {
|
||||||
|
// Parse to potion effect
|
||||||
try {
|
try {
|
||||||
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
|
PotionEffectType potionType = PotionEffectType.getByName(valueString.toUpperCase());
|
||||||
if (potionType == null && isNumeric(valueString)) {
|
if (potionType == null && isNumeric(valueString)) {
|
||||||
@ -413,6 +388,16 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
|
|||||||
return disguise;
|
return disguise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object callValueOf(Class<?> param, String valueString, String methodName, String description) throws Exception {
|
||||||
|
Object value;
|
||||||
|
try {
|
||||||
|
value = param.getMethod("valueOf", String.class).invoke(null, valueString.toUpperCase());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
throw parseToException(description, valueString, methodName);
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
private void doCheck(HashSet<HashSet<String>> optionPermissions, ArrayList<String> usedOptions) throws Exception {
|
private void doCheck(HashSet<HashSet<String>> optionPermissions, ArrayList<String> usedOptions) throws Exception {
|
||||||
if (!optionPermissions.isEmpty()) {
|
if (!optionPermissions.isEmpty()) {
|
||||||
boolean hasPermission = true;
|
boolean hasPermission = true;
|
||||||
|
Loading…
Reference in New Issue
Block a user