consolidate error handling for commands

This commit is contained in:
Charlie Wang 2015-08-19 12:02:27 -04:00
parent 78cba2b29a
commit e8b7c3b6cf
3 changed files with 26 additions and 17 deletions

View File

@ -57,11 +57,15 @@ public class DiscoCommands implements CommandExecutor {
} }
} }
if (args.length == 0) {
return plugin.helpCommand(sender);
}
PartyBuilder builder = new PartyBuilder(player); PartyBuilder builder = new PartyBuilder(player);
// ctor takes "program name" as first arg so we pass the sub-command as that // ctor takes "program name" as first arg so we pass the sub-command as that
// args then start at args[1] so we slice args[1:] // args then start at args[1] so we slice args[1:]
Getopt g = new Getopt(args[0], Arrays.copyOfRange(args, 1, args.length), "n:t:p:r:g:lfjPb:"); Getopt g = new Getopt(args[0], Arrays.copyOfRange(args, 1, args.length), "n:t:p:r:g:lfjPb:");
g.setOpterr(false); // do own error handling
int c; int c;
while ((c = g.getopt()) != -1) { while ((c = g.getopt()) != -1) {
try { try {
@ -95,8 +99,8 @@ public class DiscoCommands implements CommandExecutor {
try { try {
String[] pair = tokens.nextToken().split(":"); String[] pair = tokens.nextToken().split(":");
builder.guests(pair[0], Integer.parseInt(pair[1])); builder.guests(pair[0], Integer.parseInt(pair[1]));
} catch (Exception e) { } catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("Bad guest arguments"); throw new IllegalArgumentException("a valid list of guest:number pairs was not found.");
} }
} }
} }
@ -117,9 +121,18 @@ public class DiscoCommands implements CommandExecutor {
case 'b': case 'b':
builder.baby(Integer.parseInt(g.getOptarg())); builder.baby(Integer.parseInt(g.getOptarg()));
break; break;
case '?': // handle invalid switch
// need to cast getOptopt() to char because it returns a string representation of an integer
sender.sendMessage(String.format("Invalid switch '%s' was found", (char)g.getOptopt()));
return false;
} }
} catch (IllegalArgumentException e) { } catch (NumberFormatException e) {
String badNumber = e.getMessage().replace("For input string: ", "");
sender.sendMessage(String.format("Error: %s is not a valid number", badNumber));
return false;
} catch (IllegalArgumentException e) { // handle invalid arguments
sender.sendMessage("Bad command: " + e.getMessage()); sender.sendMessage("Bad command: " + e.getMessage());
return false;
} }
} }

View File

@ -175,13 +175,9 @@ public final class DiscoSheep extends JavaPlugin {
} }
} }
public boolean hasParty(String name) { public boolean hasParty(String name) { return parties.containsKey(name); }
return parties.containsKey(name);
}
public AbstractParty getParty(String name) { public AbstractParty getParty(String name) { return parties.get(name); }
return parties.get(name);
}
public void removeParty(String name) { public void removeParty(String name) {
if (this.hasParty(name)) { if (this.hasParty(name)) {
@ -234,7 +230,7 @@ public final class DiscoSheep extends JavaPlugin {
if (!hasParty(s.getName())) { if (!hasParty(s.getName())) {
builder.build().startDisco(); builder.build().startDisco();
} else { } else {
s.sendMessage(ChatColor.RED + "You already have a party. Are you underground?"); s.sendMessage(ChatColor.RED + "You already have a party.");
} }
return true; return true;
} }

View File

@ -30,7 +30,7 @@ public class PartyBuilder {
public PartyBuilder duration(int duration) throws IllegalArgumentException { public PartyBuilder duration(int duration) throws IllegalArgumentException {
if (duration < 0 || duration > AbstractParty.maxDuration) { if (duration < 0 || duration > AbstractParty.maxDuration) {
throw new IllegalArgumentException("Invalid duration."); throw new IllegalArgumentException("nvalid duration");
} }
this.duration = duration; this.duration = duration;
return this; return this;
@ -38,7 +38,7 @@ public class PartyBuilder {
public PartyBuilder radius(int radius) throws IllegalArgumentException { public PartyBuilder radius(int radius) throws IllegalArgumentException {
if (radius < 0 || radius > AbstractParty.maxRadius) { if (radius < 0 || radius > AbstractParty.maxRadius) {
throw new IllegalArgumentException("Invalid radius."); throw new IllegalArgumentException("Invalid radius");
} }
this.radius = radius; this.radius = radius;
return this; return this;
@ -51,7 +51,7 @@ public class PartyBuilder {
public PartyBuilder period(int period) throws IllegalArgumentException { public PartyBuilder period(int period) throws IllegalArgumentException {
if (period < 0 || period > AbstractParty.maxPeriod) { if (period < 0 || period > AbstractParty.maxPeriod) {
throw new IllegalArgumentException("Invalid period."); throw new IllegalArgumentException("invalid period");
} }
this.period = period; this.period = period;
return this; return this;
@ -59,7 +59,7 @@ public class PartyBuilder {
public PartyBuilder sheep(int sheep) throws IllegalArgumentException { public PartyBuilder sheep(int sheep) throws IllegalArgumentException {
if (sheep < 0 || sheep > AbstractParty.maxSheep) { if (sheep < 0 || sheep > AbstractParty.maxSheep) {
throw new IllegalArgumentException("Invalid sheep number"); throw new IllegalArgumentException("invalid sheep number");
} }
this.sheep = sheep; this.sheep = sheep;
return this; return this;
@ -71,7 +71,7 @@ public class PartyBuilder {
try { try {
type = EntityType.valueOf(key); type = EntityType.valueOf(key);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
throw new IllegalArgumentException(String.format("Invalid guest: %s", key)); throw new IllegalArgumentException(String.format("invalid guest %s", key));
} }
if (guests.containsKey(type)) { if (guests.containsKey(type)) {
if (n <= AbstractParty.getMaxGuestNumbers().get(type) && n >= 0) { // so that /ds defaults can take 0 as arg if (n <= AbstractParty.getMaxGuestNumbers().get(type) && n >= 0) { // so that /ds defaults can take 0 as arg
@ -79,7 +79,7 @@ public class PartyBuilder {
return this; return this;
} }
} }
throw new IllegalArgumentException(String.format("Invalid number for %s", key)); throw new IllegalArgumentException(String.format("invalid number for %s", key));
} }
public PartyBuilder noGuests() { public PartyBuilder noGuests() {