Search in sources :

Example 1 with LanternScheduler

use of org.lanternpowered.server.scheduler.LanternScheduler in project LanternServer by LanternPowered.

the class ConsoleCommandCompleter method complete.

@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
    String buffer = line.line();
    // The content with normalized spaces, the spaces are trimmed
    // from the ends and there are never two spaces directly after each other
    String command = StringUtils.normalizeSpace(buffer);
    boolean hasPrefix = command.startsWith("/");
    // Don't include the '/'
    if (hasPrefix) {
        command = command.substring(1);
    }
    // Keep the last space, it must be there!
    if (buffer.endsWith(" ")) {
        command = command + " ";
    }
    final String command0 = command;
    final Future<List<String>> tabComplete = ((LanternScheduler) Sponge.getScheduler()).callSync(() -> Sponge.getCommandManager().getSuggestions(LanternConsoleSource.INSTANCE, command0, null));
    try {
        // Get the suggestions
        final List<String> suggestions = tabComplete.get();
        // If the suggestions are for the command and there was a prefix, then append the prefix
        if (hasPrefix && command.split(" ").length == 1 && !command.endsWith(" ")) {
            for (String completion : suggestions) {
                if (!completion.isEmpty()) {
                    candidates.add(new Candidate('/' + completion));
                }
            }
        } else {
            for (String completion : suggestions) {
                if (!completion.isEmpty()) {
                    candidates.add(new Candidate(completion));
                }
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        Lantern.getLogger().error("Failed to tab complete", e);
    }
}
Also used : Candidate(org.jline.reader.Candidate) LanternScheduler(org.lanternpowered.server.scheduler.LanternScheduler) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 Candidate (org.jline.reader.Candidate)1 LanternScheduler (org.lanternpowered.server.scheduler.LanternScheduler)1