boolean isOurCommand(Command command) { return command instanceof RootCommand && ((RootCommand) command).getManager() == manager; }
default List<String> getTabCompletions(CommandIssuer sender, String alias, String[] args) { return getTabCompletions(sender, alias, args, false); }
default String getDescription() { final RegisteredCommand cmd = this.getDefaultRegisteredCommand(); if (cmd != null) { return cmd.getHelpText(); } BaseCommand defCommand = getDefCommand(); if (defCommand != null && defCommand.description != null) { return defCommand.description; } return ""; }
default BaseCommand getBaseCommand(String[] args) { BaseCommand command = getDefCommand(); for (int i = args.length; i >= 0; i--) { String checkSub = ApacheCommonsLangUtil.join(args, " ", 0, i).toLowerCase(); Set<RegisteredCommand> registeredCommands = getSubCommands().get(checkSub); if (!registeredCommands.isEmpty()) { command = registeredCommands.iterator().next().scope; break; } } return command; }
public CommandHelp(CommandManager manager, RootCommand rootCommand, CommandIssuer issuer) { this.manager = manager; this.issuer = issuer; this.perPage = manager.defaultHelpPerPage; this.commandPrefix = manager.getCommandPrefix(issuer); this.commandName = rootCommand.getCommandName(); SetMultimap<String, RegisteredCommand> subCommands = rootCommand.getSubCommands(); Set<RegisteredCommand> seen = new HashSet<>(); if (!rootCommand.getDefCommand().hasHelpCommand) { helpEntries.add(new HelpEntry(this, rootCommand.getDefaultRegisteredCommand())); seen.add(rootCommand.getDefaultRegisteredCommand()); } subCommands.entries().forEach(e -> { String key = e.getKey(); if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHUNKNOWN)) { return; } RegisteredCommand regCommand = e.getValue(); if (!regCommand.isPrivate && regCommand.hasPermission(issuer) && !seen.contains(regCommand)) { this.helpEntries.add(new HelpEntry(this, regCommand)); seen.add(regCommand); } }); }
default void addChildShared(List<BaseCommand> children, SetMultimap<String, RegisteredCommand> subCommands, BaseCommand command) { command.subCommands.entries().forEach(e -> { String key = e.getKey(); RegisteredCommand registeredCommand = e.getValue(); if (key.equals(BaseCommand.DEFAULT) || key.equals(BaseCommand.CATCHUNKNOWN)) { return; } Set<RegisteredCommand> registered = subCommands.get(key); if (!registered.isEmpty()) { BaseCommand prevBase = registered.iterator().next().scope; if (prevBase != registeredCommand.scope) { this.getManager().log(LogLevel.ERROR, "ACF Error: " + command.getName() + " registered subcommand " + key + " for root command " + getCommandName() + " - but it is already defined in " + prevBase.getName()); this.getManager().log(LogLevel.ERROR, "2 subcommands of the same prefix may not be spread over 2 different classes. Ignoring this."); return; } } subCommands.put(key, registeredCommand); }); children.add(command); }
default BaseCommand execute(CommandIssuer sender, String commandLabel, String[] args) { BaseCommand command = getBaseCommand(args); command.execute(sender, commandLabel, args); return command; }
default String getUsage() { final RegisteredCommand cmd = this.getDefaultRegisteredCommand(); if (cmd != null) { return cmd.syntaxText != null ? cmd.syntaxText : ""; } return ""; } }
default RegisteredCommand getDefaultRegisteredCommand() { BaseCommand defCommand = this.getDefCommand(); if (defCommand != null) { return defCommand.getDefaultRegisteredCommand(); } return null; }
/** * Registers the given {@link BaseCommand cmd} as a child of the {@link RootCommand} linked to the name given. * * @param name * Name of the parent to cmd. * @param cmd * The {@link BaseCommand} to add as a child to the {@link RootCommand} owned name field. */ private void register(String name, BaseCommand cmd) { String nameLower = name.toLowerCase(); RootCommand rootCommand = manager.obtainRootCommand(nameLower); rootCommand.addChild(cmd); this.registeredCommands.put(nameLower, rootCommand); }
default List<String> getTabCompletions(CommandIssuer sender, String alias, String[] args, boolean commandsOnly) { Set<String> completions = new HashSet<>(); getChildren().forEach(child -> { if (!commandsOnly) { completions.addAll(child.tabComplete(sender, alias, args)); } completions.addAll(child.getCommandsForCompletion(sender, args)); }); return new ArrayList<>(completions); }
BaseCommand getBaseCommand(String commandLabel, @NotNull String[] args) { RootCommand rootCommand = obtainRootCommand(commandLabel); if (rootCommand == null) { return null; } return rootCommand.getBaseCommand(args); }
@EventHandler(ignoreCancelled = true) public void onTabComplete(TabCompleteEvent event) { String buffer = event.getBuffer(); if (!event.isCommand() && !buffer.startsWith("/")) { return; } String[] args = ACFPatterns.SPACE.split(buffer, -1); String commandLabel = args[0]; if (commandLabel.startsWith("/")) { commandLabel = commandLabel.substring(1); } RootCommand rootCommand = this.manager.getRootCommand(commandLabel); if (rootCommand != null) { args = args.length > 1 ? Arrays.copyOfRange(args, 1, args.length) : new String[]{""}; BukkitCommandIssuer issuer = this.manager.getCommandIssuer(event.getSender()); List<String> tabCompletions = rootCommand.getTabCompletions(issuer, commandLabel, args, true); event.setCompletions(ACFUtil.preformOnImmutable( event.getCompletions(), (list) -> list.addAll(tabCompletions))); } } }