Represents a Command belonging to a plugin
| 6 | * Represents a {@link Command} belonging to a plugin |
| 7 | */ |
| 8 | public final class PluginCommand extends Command { |
| 9 | private final Plugin owningPlugin; |
| 10 | private CommandExecutor executor; |
| 11 | |
| 12 | protected PluginCommand(String name, Plugin owner) { |
| 13 | super(name); |
| 14 | this.executor = owner; |
| 15 | this.owningPlugin = owner; |
| 16 | this.usageMessage = ""; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Executes the command, returning its success |
| 21 | * |
| 22 | * @param sender Source object which is executing this command |
| 23 | * @param commandLabel The alias of the command used |
| 24 | * @param args All arguments passed to the command, split via ' ' |
| 25 | * @return true if the command was successful, otherwise false |
| 26 | */ |
| 27 | @Override |
| 28 | public boolean execute(CommandSender sender, String commandLabel, String[] args) { |
| 29 | boolean success = false; |
| 30 | |
| 31 | if (!owningPlugin.isEnabled()) { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | if (!testPermission(sender)) { |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | try { |
| 40 | success = executor.onCommand(sender, this, commandLabel, args); |
| 41 | } catch (Throwable ex) { |
| 42 | throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex); |
| 43 | } |
| 44 | |
| 45 | if (!success && usageMessage.length() > 0) { |
| 46 | for (String line: usageMessage.replace("<command>", commandLabel).split("\n")) { |
| 47 | sender.sendMessage(line); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | return success; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Sets the {@link CommandExecutor} to run when parsing this command |
| 56 | * |
| 57 | * @param executor New executor to run |
| 58 | */ |
| 59 | public void setExecutor(CommandExecutor executor) { |
| 60 | this.executor = executor; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the {@link CommandExecutor} associated with this command |
| 65 | * |
nothing calls this directly
no outgoing calls
no test coverage detected