Made it throw IllegalArugment exceptions instead of exceptions. Fixed previous commit

This commit is contained in:
libraryaddict 2014-09-15 01:42:08 +12:00
parent 304558cc6e
commit a34ede1104

View File

@ -282,11 +282,11 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
protected Disguise parseDisguise(CommandSender sender, String[] args, protected Disguise parseDisguise(CommandSender sender, String[] args,
HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) throws Exception { HashMap<DisguiseType, HashMap<ArrayList<String>, Boolean>> map) throws Exception {
if (map.isEmpty()) { if (map.isEmpty()) {
throw new Exception(ChatColor.RED + "You are forbidden to use this command."); throw new IllegalArgumentException(ChatColor.RED + "You are forbidden to use this command.");
} }
if (args.length == 0) { if (args.length == 0) {
sendCommandUsage(sender, map); sendCommandUsage(sender, map);
throw new Exception(); throw new IllegalArgumentException();
} }
// How many args to skip due to the disugise being constructed // How many args to skip due to the disugise being constructed
// Time to start constructing the disguise. // Time to start constructing the disguise.
@ -299,10 +299,10 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) { if (sender.hasPermission("libsdisguises.disguise.disguiseclone")) {
disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase()); disguise = DisguiseUtilities.getClonedDisguise(args[0].toLowerCase());
if (disguise == null) { if (disguise == null) {
throw new Exception(ChatColor.RED + "Cannot find a disguise under the reference " + args[0]); throw new IllegalArgumentException(ChatColor.RED + "Cannot find a disguise under the reference " + args[0]);
} }
} else { } else {
throw new Exception(ChatColor.RED + "You do not have perimssion to use disguise references!"); throw new IllegalArgumentException(ChatColor.RED + "You do not have perimssion to use disguise references!");
} }
optionPermissions = (map.containsKey(disguise.getType()) ? map.get(disguise.getType()) optionPermissions = (map.containsKey(disguise.getType()) ? map.get(disguise.getType())
: new HashMap<ArrayList<String>, Boolean>()); : new HashMap<ArrayList<String>, Boolean>());
@ -319,26 +319,27 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
} }
} }
if (disguiseType == null) { if (disguiseType == null) {
throw new Exception(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0] + ChatColor.RED throw new IllegalArgumentException(ChatColor.RED + "Error! The disguise " + ChatColor.GREEN + args[0]
+ " doesn't exist!"); + ChatColor.RED + " doesn't exist!");
} }
if (disguiseType.getEntityType() == null) { if (disguiseType.getEntityType() == null) {
throw new Exception(ChatColor.RED + "Error! This version of minecraft does not have that disguise!"); throw new IllegalArgumentException(ChatColor.RED
+ "Error! This version of minecraft does not have that disguise!");
} }
if (!map.containsKey(disguiseType)) { if (!map.containsKey(disguiseType)) {
throw new Exception(ChatColor.RED + "You are forbidden to use this disguise."); throw new IllegalArgumentException(ChatColor.RED + "You are forbidden to use this disguise.");
} }
optionPermissions = map.get(disguiseType); optionPermissions = map.get(disguiseType);
HashMap<String, Boolean> disguiseOptions = this.getDisguisePermission(sender, disguiseType); HashMap<String, Boolean> disguiseOptions = this.getDisguisePermission(sender, disguiseType);
if (disguiseType.isPlayer()) {// If he is doing a player disguise if (disguiseType.isPlayer()) {// If he is doing a player disguise
if (args.length == 1) { if (args.length == 1) {
// He needs to give the player name // He needs to give the player name
throw new Exception(ChatColor.RED + "Error! You need to give a player name!"); throw new IllegalArgumentException(ChatColor.RED + "Error! You need to give a player name!");
} else { } else {
if (!disguiseOptions.isEmpty() if (!disguiseOptions.isEmpty()
&& (!disguiseOptions.containsKey(args[1].toLowerCase()) || !disguiseOptions && (!disguiseOptions.containsKey(args[1].toLowerCase()) || !disguiseOptions
.get(args[1].toLowerCase()))) { .get(args[1].toLowerCase()))) {
throw new Exception(ChatColor.RED + "Error! You don't have permission to use that name!"); throw new IllegalArgumentException(ChatColor.RED + "Error! You don't have permission to use that name!");
} }
// Construct the player disguise // Construct the player disguise
disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1])); disguise = new PlayerDisguise(ChatColor.translateAlternateColorCodes('&', args[1]));
@ -396,7 +397,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
case WITHER_SKULL: case WITHER_SKULL:
break; break;
default: default:
throw new Exception(ChatColor.RED + "Error! " + disguiseType.toReadable() throw new IllegalArgumentException(ChatColor.RED + "Error! " + disguiseType.toReadable()
+ " doesn't know what to do with " + args[1] + "!"); + " doesn't know what to do with " + args[1] + "!");
} }
toSkip++; toSkip++;
@ -407,7 +408,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
} }
if (secondArg != null) { if (secondArg != null) {
if (disguiseType != DisguiseType.FALLING_BLOCK && disguiseType != DisguiseType.DROPPED_ITEM) { if (disguiseType != DisguiseType.FALLING_BLOCK && disguiseType != DisguiseType.DROPPED_ITEM) {
throw new Exception(ChatColor.RED + "Error! Only the disguises " throw new IllegalArgumentException(ChatColor.RED + "Error! Only the disguises "
+ DisguiseType.FALLING_BLOCK.toReadable() + " and " + DisguiseType.FALLING_BLOCK.toReadable() + " and "
+ DisguiseType.DROPPED_ITEM.toReadable() + " uses a second number!"); + DisguiseType.DROPPED_ITEM.toReadable() + " uses a second number!");
} }
@ -425,8 +426,9 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
toCheck += ":" + miscData; toCheck += ":" + miscData;
} }
if (!disguiseOptions.containsKey(toCheck) || !disguiseOptions.get(toCheck)) { if (!disguiseOptions.containsKey(toCheck) || !disguiseOptions.get(toCheck)) {
throw new Exception(ChatColor.RED + "Error! You do not have permission to use the parameter " throw new IllegalArgumentException(ChatColor.RED
+ toCheck + " on the " + disguiseType.toReadable() + " disguise!"); + "Error! You do not have permission to use the parameter " + toCheck + " on the "
+ disguiseType.toReadable() + " disguise!");
} }
} }
if (miscId != -1) { if (miscId != -1) {
@ -456,7 +458,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
String valueString = (args.length - 1 == i ? null : args[i + 1]); String valueString = (args.length - 1 == i ? null : args[i + 1]);
Method methodToUse = null; Method methodToUse = null;
Object value = null; Object value = null;
Exception storedEx = null; IllegalArgumentException storedEx = null;
for (Method method : methods) { for (Method method : methods) {
if (!method.getName().startsWith("get") && method.getName().equalsIgnoreCase(methodName) if (!method.getName().startsWith("get") && method.getName().equalsIgnoreCase(methodName)
&& method.getAnnotation(Deprecated.class) == null && method.getParameterTypes().length == 1) { && method.getAnnotation(Deprecated.class) == null && method.getParameterTypes().length == 1) {
@ -465,19 +467,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
methodName = methodToUse.getName(); methodName = methodToUse.getName();
Class<?>[] types = methodToUse.getParameterTypes(); Class<?>[] types = methodToUse.getParameterTypes();
Class param = types[0]; Class param = types[0];
if (boolean.class == param) { if (valueString != null) {
// Parse to boolean
if (valueString == null
|| !("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString))) {
value = true;
i--;
} else {
value = "true".equalsIgnoreCase(valueString);
}
} else {
if (valueString == null) {
throw new Exception(ChatColor.RED + "No value was given for the option " + methodName);
}
if (int.class == param) { if (int.class == param) {
// Parse to integer // Parse to integer
if (isNumeric(valueString)) { if (isNumeric(valueString)) {
@ -512,7 +502,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
try { try {
value = parseToItemstack(valueString); value = parseToItemstack(valueString);
} catch (Exception ex) { } catch (Exception ex) {
throw new Exception(String.format(ex.getMessage(), methodName)); throw new IllegalArgumentException(String.format(ex.getMessage(), methodName));
} }
} else if (param == ItemStack[].class) { } else if (param == ItemStack[].class) {
// Parse to itemstack array // Parse to itemstack array
@ -555,7 +545,7 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
potionType = PotionEffectType.getById(Integer.parseInt(valueString)); potionType = PotionEffectType.getById(Integer.parseInt(valueString));
} }
if (potionType == null) if (potionType == null)
throw new Exception(); throw new IllegalArgumentException();
value = potionType; value = potionType;
} catch (Exception ex) { } catch (Exception ex) {
throw parseToException("a potioneffect type", valueString, methodName); throw parseToException("a potioneffect type", valueString, methodName);
@ -575,17 +565,32 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
try { try {
BlockFace face = BlockFace.valueOf(valueString.toUpperCase()); BlockFace face = BlockFace.valueOf(valueString.toUpperCase());
if (face.ordinal() > 3) if (face.ordinal() > 3)
throw new Exception(); throw new IllegalArgumentException();
value = face; value = face;
} catch (Exception ex) { } catch (Exception ex) {
throw parseToException("a direction (north, east, south, west)", valueString, methodName); throw parseToException("a direction (north, east, south, west)", valueString, methodName);
} }
} }
} }
if (value == null && boolean.class == param) {
// Parse to boolean
if (valueString == null
|| !("true".equalsIgnoreCase(valueString) || "false".equalsIgnoreCase(valueString))) {
value = true;
i--;
} else {
value = "true".equalsIgnoreCase(valueString);
}
}
if (value != null) {
break; break;
} catch (Exception ex) { }
methodToUse = null; } catch (IllegalArgumentException ex) {
storedEx = ex; storedEx = ex;
methodToUse = null;
} catch (Exception ex) {
ex.printStackTrace();
methodToUse = null;
} }
} }
} }
@ -593,7 +598,10 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
if (storedEx != null) { if (storedEx != null) {
throw storedEx; throw storedEx;
} }
throw new Exception(ChatColor.RED + "Cannot find the option " + methodName); throw new IllegalArgumentException(ChatColor.RED + "Cannot find the option " + methodName);
}
if (value == null) {
throw new IllegalArgumentException(ChatColor.RED + "No value was given for the option " + methodName);
} }
if (!usedOptions.contains(methodName.toLowerCase())) { if (!usedOptions.contains(methodName.toLowerCase())) {
usedOptions.add(methodName.toLowerCase()); usedOptions.add(methodName.toLowerCase());
@ -637,14 +645,15 @@ public abstract class BaseDisguiseCommand implements CommandExecutor {
private void doCheck(HashMap<ArrayList<String>, Boolean> optionPermissions, ArrayList<String> usedOptions) throws Exception { private void doCheck(HashMap<ArrayList<String>, Boolean> optionPermissions, ArrayList<String> usedOptions) throws Exception {
if (!passesCheck(optionPermissions, usedOptions)) { if (!passesCheck(optionPermissions, usedOptions)) {
throw new Exception(ChatColor.RED + "You do not have the permission to use the option " throw new IllegalArgumentException(ChatColor.RED + "You do not have the permission to use the option "
+ usedOptions.get(usedOptions.size() - 1)); + usedOptions.get(usedOptions.size() - 1));
} }
} }
private Exception parseToException(String expectedValue, String receivedInstead, String methodName) { private IllegalArgumentException parseToException(String expectedValue, String receivedInstead, String methodName) {
return new Exception(ChatColor.RED + "Expected " + ChatColor.GREEN + expectedValue + ChatColor.RED + ", received " return new IllegalArgumentException(ChatColor.RED + "Expected " + ChatColor.GREEN + expectedValue + ChatColor.RED
+ ChatColor.GREEN + receivedInstead + ChatColor.RED + " instead for " + ChatColor.GREEN + methodName); + ", received " + ChatColor.GREEN + receivedInstead + ChatColor.RED + " instead for " + ChatColor.GREEN
+ methodName);
} }
private ItemStack parseToItemstack(String string) throws Exception { private ItemStack parseToItemstack(String string) throws Exception {