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