consolidate error handling for commands
This commit is contained in:
parent
78cba2b29a
commit
e8b7c3b6cf
@ -57,11 +57,15 @@ public class DiscoCommands implements CommandExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length == 0) {
|
||||
return plugin.helpCommand(sender);
|
||||
}
|
||||
|
||||
PartyBuilder builder = new PartyBuilder(player);
|
||||
// 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:]
|
||||
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;
|
||||
while ((c = g.getopt()) != -1) {
|
||||
try {
|
||||
@ -95,8 +99,8 @@ public class DiscoCommands implements CommandExecutor {
|
||||
try {
|
||||
String[] pair = tokens.nextToken().split(":");
|
||||
builder.guests(pair[0], Integer.parseInt(pair[1]));
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Bad guest arguments");
|
||||
} catch (ArrayIndexOutOfBoundsException e) {
|
||||
throw new IllegalArgumentException("a valid list of guest:number pairs was not found.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -117,9 +121,18 @@ public class DiscoCommands implements CommandExecutor {
|
||||
case 'b':
|
||||
builder.baby(Integer.parseInt(g.getOptarg()));
|
||||
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());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -175,13 +175,9 @@ public final class DiscoSheep extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasParty(String name) {
|
||||
return parties.containsKey(name);
|
||||
}
|
||||
public boolean hasParty(String name) { return parties.containsKey(name); }
|
||||
|
||||
public AbstractParty getParty(String name) {
|
||||
return parties.get(name);
|
||||
}
|
||||
public AbstractParty getParty(String name) { return parties.get(name); }
|
||||
|
||||
public void removeParty(String name) {
|
||||
if (this.hasParty(name)) {
|
||||
@ -234,7 +230,7 @@ public final class DiscoSheep extends JavaPlugin {
|
||||
if (!hasParty(s.getName())) {
|
||||
builder.build().startDisco();
|
||||
} else {
|
||||
s.sendMessage(ChatColor.RED + "You already have a party. Are you underground?");
|
||||
s.sendMessage(ChatColor.RED + "You already have a party.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public class PartyBuilder {
|
||||
|
||||
public PartyBuilder duration(int duration) throws IllegalArgumentException {
|
||||
if (duration < 0 || duration > AbstractParty.maxDuration) {
|
||||
throw new IllegalArgumentException("Invalid duration.");
|
||||
throw new IllegalArgumentException("nvalid duration");
|
||||
}
|
||||
this.duration = duration;
|
||||
return this;
|
||||
@ -38,7 +38,7 @@ public class PartyBuilder {
|
||||
|
||||
public PartyBuilder radius(int radius) throws IllegalArgumentException {
|
||||
if (radius < 0 || radius > AbstractParty.maxRadius) {
|
||||
throw new IllegalArgumentException("Invalid radius.");
|
||||
throw new IllegalArgumentException("Invalid radius");
|
||||
}
|
||||
this.radius = radius;
|
||||
return this;
|
||||
@ -51,7 +51,7 @@ public class PartyBuilder {
|
||||
|
||||
public PartyBuilder period(int period) throws IllegalArgumentException {
|
||||
if (period < 0 || period > AbstractParty.maxPeriod) {
|
||||
throw new IllegalArgumentException("Invalid period.");
|
||||
throw new IllegalArgumentException("invalid period");
|
||||
}
|
||||
this.period = period;
|
||||
return this;
|
||||
@ -59,7 +59,7 @@ public class PartyBuilder {
|
||||
|
||||
public PartyBuilder sheep(int sheep) throws IllegalArgumentException {
|
||||
if (sheep < 0 || sheep > AbstractParty.maxSheep) {
|
||||
throw new IllegalArgumentException("Invalid sheep number");
|
||||
throw new IllegalArgumentException("invalid sheep number");
|
||||
}
|
||||
this.sheep = sheep;
|
||||
return this;
|
||||
@ -71,7 +71,7 @@ public class PartyBuilder {
|
||||
try {
|
||||
type = EntityType.valueOf(key);
|
||||
} 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 (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;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException(String.format("Invalid number for %s", key));
|
||||
throw new IllegalArgumentException(String.format("invalid number for %s", key));
|
||||
}
|
||||
|
||||
public PartyBuilder noGuests() {
|
||||
|
Loading…
Reference in New Issue
Block a user