From f940d301b76fde33ae462ffa9531ecefce96649e Mon Sep 17 00:00:00 2001 From: AppleDash Date: Tue, 14 Jun 2016 04:52:21 -0400 Subject: [PATCH] Improve command handling and exception structure. --- .../command/SaneEconomyCommand.java | 22 ++++++++++++++++--- .../{ => type}/NoPermissionException.java | 4 +++- .../usage}/InvalidUsageException.java | 4 ++-- .../{ => type/usage}/NeedPlayerException.java | 4 ++-- .../usage}/TooFewArgumentsException.java | 4 ++-- .../exception/type/usage/UsageException.java | 10 +++++++++ .../command/type/BalanceCommand.java | 2 +- .../command/type/EconomyAdminCommand.java | 6 ++--- 8 files changed, 42 insertions(+), 14 deletions(-) rename src/main/java/org/appledash/saneeconomy/command/exception/{ => type}/NoPermissionException.java (66%) rename src/main/java/org/appledash/saneeconomy/command/exception/{ => type/usage}/InvalidUsageException.java (60%) rename src/main/java/org/appledash/saneeconomy/command/exception/{ => type/usage}/NeedPlayerException.java (64%) rename src/main/java/org/appledash/saneeconomy/command/exception/{ => type/usage}/TooFewArgumentsException.java (60%) create mode 100644 src/main/java/org/appledash/saneeconomy/command/exception/type/usage/UsageException.java diff --git a/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java b/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java index b2892bc..448b8e7 100644 --- a/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java +++ b/src/main/java/org/appledash/saneeconomy/command/SaneEconomyCommand.java @@ -1,6 +1,8 @@ package org.appledash.saneeconomy.command; -import org.appledash.saneeconomy.command.exception.*; +import org.appledash.saneeconomy.command.exception.CommandException; +import org.appledash.saneeconomy.command.exception.type.NoPermissionException; +import org.appledash.saneeconomy.command.exception.type.usage.UsageException; import org.appledash.saneeconomy.utils.MessageUtils; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -19,11 +21,14 @@ public abstract class SaneEconomyCommand implements CommandExecutor { } return onCommand(sender, args); - } catch (TooFewArgumentsException | NeedPlayerException | InvalidUsageException e) { + } catch (UsageException e) { + /* Invalid usage in some way, print out exactly what went wrong along with the proper usage. */ MessageUtils.sendMessage(sender, e.getMessage()); + for (String s : getUsage()) { MessageUtils.sendMessage(sender, String.format("Usage: %s", s.replace("", label))); } + return true; } catch (CommandException e) { MessageUtils.sendMessage(sender, e.getMessage()); @@ -31,7 +36,18 @@ public abstract class SaneEconomyCommand implements CommandExecutor { } } + /** + * Get the permission node required to use the command. + * @return Permission node. + */ public abstract String getPermission(); + + /** + * Get the command's usage. + * When this is printed, '' will be replaced with the command name. + * @return Command usage examples + */ public abstract String[] getUsage(); - public abstract boolean onCommand(CommandSender sender, String[] args) throws CommandException; + + protected abstract boolean onCommand(CommandSender sender, String[] args) throws CommandException; } diff --git a/src/main/java/org/appledash/saneeconomy/command/exception/NoPermissionException.java b/src/main/java/org/appledash/saneeconomy/command/exception/type/NoPermissionException.java similarity index 66% rename from src/main/java/org/appledash/saneeconomy/command/exception/NoPermissionException.java rename to src/main/java/org/appledash/saneeconomy/command/exception/type/NoPermissionException.java index 7e3dd56..00b8fcc 100644 --- a/src/main/java/org/appledash/saneeconomy/command/exception/NoPermissionException.java +++ b/src/main/java/org/appledash/saneeconomy/command/exception/type/NoPermissionException.java @@ -1,4 +1,6 @@ -package org.appledash.saneeconomy.command.exception; +package org.appledash.saneeconomy.command.exception.type; + +import org.appledash.saneeconomy.command.exception.CommandException; /** * Created by AppleDash on 6/13/2016. diff --git a/src/main/java/org/appledash/saneeconomy/command/exception/InvalidUsageException.java b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/InvalidUsageException.java similarity index 60% rename from src/main/java/org/appledash/saneeconomy/command/exception/InvalidUsageException.java rename to src/main/java/org/appledash/saneeconomy/command/exception/type/usage/InvalidUsageException.java index 693c913..89d1117 100644 --- a/src/main/java/org/appledash/saneeconomy/command/exception/InvalidUsageException.java +++ b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/InvalidUsageException.java @@ -1,10 +1,10 @@ -package org.appledash.saneeconomy.command.exception; +package org.appledash.saneeconomy.command.exception.type.usage; /** * Created by AppleDash on 6/13/2016. * Blackjack is still best pony. */ -public class InvalidUsageException extends CommandException { +public class InvalidUsageException extends UsageException { @Override public String getMessage() { return "Invalid syntax for that command!"; diff --git a/src/main/java/org/appledash/saneeconomy/command/exception/NeedPlayerException.java b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/NeedPlayerException.java similarity index 64% rename from src/main/java/org/appledash/saneeconomy/command/exception/NeedPlayerException.java rename to src/main/java/org/appledash/saneeconomy/command/exception/type/usage/NeedPlayerException.java index 881bad2..5f0677c 100644 --- a/src/main/java/org/appledash/saneeconomy/command/exception/NeedPlayerException.java +++ b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/NeedPlayerException.java @@ -1,10 +1,10 @@ -package org.appledash.saneeconomy.command.exception; +package org.appledash.saneeconomy.command.exception.type.usage; /** * Created by AppleDash on 6/13/2016. * Blackjack is still best pony. */ -public class NeedPlayerException extends CommandException { +public class NeedPlayerException extends UsageException { @Override public String getMessage() { return "That command requires a player argument when not run by a player."; diff --git a/src/main/java/org/appledash/saneeconomy/command/exception/TooFewArgumentsException.java b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/TooFewArgumentsException.java similarity index 60% rename from src/main/java/org/appledash/saneeconomy/command/exception/TooFewArgumentsException.java rename to src/main/java/org/appledash/saneeconomy/command/exception/type/usage/TooFewArgumentsException.java index dcfcc74..e13511e 100644 --- a/src/main/java/org/appledash/saneeconomy/command/exception/TooFewArgumentsException.java +++ b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/TooFewArgumentsException.java @@ -1,10 +1,10 @@ -package org.appledash.saneeconomy.command.exception; +package org.appledash.saneeconomy.command.exception.type.usage; /** * Created by AppleDash on 6/13/2016. * Blackjack is still best pony. */ -public class TooFewArgumentsException extends CommandException { +public class TooFewArgumentsException extends UsageException { @Override public String getMessage() { return "Wrong number of arguments for that command!"; diff --git a/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/UsageException.java b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/UsageException.java new file mode 100644 index 0000000..125d72f --- /dev/null +++ b/src/main/java/org/appledash/saneeconomy/command/exception/type/usage/UsageException.java @@ -0,0 +1,10 @@ +package org.appledash.saneeconomy.command.exception.type.usage; + +import org.appledash.saneeconomy.command.exception.CommandException; + +/** + * Created by AppleDash on 6/14/2016. + * Blackjack is still best pony. + */ +public class UsageException extends CommandException { +} diff --git a/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java b/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java index 25e0fe4..54a6685 100644 --- a/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java +++ b/src/main/java/org/appledash/saneeconomy/command/type/BalanceCommand.java @@ -3,7 +3,7 @@ package org.appledash.saneeconomy.command.type; import org.appledash.saneeconomy.SaneEconomy; import org.appledash.saneeconomy.command.SaneEconomyCommand; import org.appledash.saneeconomy.command.exception.CommandException; -import org.appledash.saneeconomy.command.exception.NeedPlayerException; +import org.appledash.saneeconomy.command.exception.type.usage.NeedPlayerException; import org.appledash.saneeconomy.utils.MessageUtils; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; diff --git a/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java b/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java index b05a57e..ff01314 100644 --- a/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java +++ b/src/main/java/org/appledash/saneeconomy/command/type/EconomyAdminCommand.java @@ -3,9 +3,9 @@ package org.appledash.saneeconomy.command.type; import org.appledash.saneeconomy.SaneEconomy; import org.appledash.saneeconomy.command.SaneEconomyCommand; import org.appledash.saneeconomy.command.exception.CommandException; -import org.appledash.saneeconomy.command.exception.InvalidUsageException; -import org.appledash.saneeconomy.command.exception.NeedPlayerException; -import org.appledash.saneeconomy.command.exception.TooFewArgumentsException; +import org.appledash.saneeconomy.command.exception.type.usage.InvalidUsageException; +import org.appledash.saneeconomy.command.exception.type.usage.NeedPlayerException; +import org.appledash.saneeconomy.command.exception.type.usage.TooFewArgumentsException; import org.appledash.saneeconomy.economy.EconomyManager; import org.appledash.saneeconomy.utils.MessageUtils; import org.bukkit.Bukkit;