Search in sources :

Example 1 with CustomCommand

use of gg.projecteden.nexus.framework.commands.models.CustomCommand in project Nexus by ProjectEdenGG.

the class CommandListener method onAsyncTabComplete.

@EventHandler
@SneakyThrows
public void onAsyncTabComplete(AsyncTabCompleteEvent event) {
    String buffer = event.getBuffer();
    if ((!event.isCommand() && !buffer.startsWith("/")) || buffer.indexOf(' ') == -1)
        return;
    List<String> args = new ArrayList<>(Arrays.asList(buffer.split(" ")));
    String alias = trimFirst(args.get(0));
    CustomCommand customCommand = Commands.get(alias);
    if (customCommand == null)
        return;
    boolean lastIndexIsEmpty = Strings.isNullOrEmpty(args.get(args.size() - 1));
    args.removeIf(Strings::isNullOrEmpty);
    if (lastIndexIsEmpty || buffer.endsWith(" "))
        args.add("");
    args.remove(0);
    CommandTabEvent tabEvent = new CommandTabEvent(event.getSender(), customCommand, alias, args, Collections.unmodifiableList(args));
    if (!tabEvent.callEvent())
        return;
    List<String> completions = customCommand.tabComplete(tabEvent);
    if (completions == null)
        return;
    event.setCompletions(completions.stream().distinct().collect(Collectors.toList()));
    event.setHandled(true);
}
Also used : CustomCommand(gg.projecteden.nexus.framework.commands.models.CustomCommand) CommandTabEvent(gg.projecteden.nexus.framework.commands.models.events.CommandTabEvent) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) EventHandler(org.bukkit.event.EventHandler) SneakyThrows(lombok.SneakyThrows)

Aggregations

Strings (com.google.common.base.Strings)1 CustomCommand (gg.projecteden.nexus.framework.commands.models.CustomCommand)1 CommandTabEvent (gg.projecteden.nexus.framework.commands.models.events.CommandTabEvent)1 ArrayList (java.util.ArrayList)1 SneakyThrows (lombok.SneakyThrows)1 EventHandler (org.bukkit.event.EventHandler)1