Improve command handling and exception structure.

This commit is contained in:
AppleDash 2016-06-14 04:52:21 -04:00
parent 85eff7a354
commit f940d301b7
8 changed files with 42 additions and 14 deletions

View File

@ -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("<command>", 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, '<command>' 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;
}

View File

@ -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.

View File

@ -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!";

View File

@ -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.";

View File

@ -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!";

View File

@ -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 {
}

View File

@ -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;

View File

@ -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;