mirror of
https://github.com/PaperMC/Paper.git
synced 2025-01-09 11:44:19 +01:00
Provide a tab completion handler for JLine. Adds BUKKIT-4168
By: Phillip Schichtel <quick_wango@code-infection.de>
This commit is contained in:
parent
84e8935a22
commit
6a8d0de50c
1 changed files with 47 additions and 0 deletions
|
@ -0,0 +1,47 @@
|
||||||
|
package org.bukkit.craftbukkit.command;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import org.bukkit.craftbukkit.CraftServer;
|
||||||
|
import org.bukkit.craftbukkit.util.Waitable;
|
||||||
|
|
||||||
|
import jline.console.completer.Completer;
|
||||||
|
|
||||||
|
public class ConsoleCommandCompleter implements Completer {
|
||||||
|
private final CraftServer server;
|
||||||
|
|
||||||
|
public ConsoleCommandCompleter(CraftServer server) {
|
||||||
|
this.server = server;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
|
||||||
|
Waitable<List<String>> waitable = new Waitable<List<String>>() {
|
||||||
|
@Override
|
||||||
|
protected List<String> evaluate() {
|
||||||
|
return server.getCommandMap().tabComplete(server.getConsoleSender(), buffer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.server.getServer().processQueue.add(waitable);
|
||||||
|
try {
|
||||||
|
List<String> offers = waitable.get();
|
||||||
|
if (offers == null) {
|
||||||
|
return cursor;
|
||||||
|
}
|
||||||
|
candidates.addAll(offers);
|
||||||
|
|
||||||
|
final int lastSpace = buffer.lastIndexOf(' ');
|
||||||
|
if (lastSpace == -1) {
|
||||||
|
return cursor - buffer.length();
|
||||||
|
} else {
|
||||||
|
return cursor - (buffer.length() - lastSpace - 1);
|
||||||
|
}
|
||||||
|
} catch (ExecutionException e) {
|
||||||
|
this.server.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
return cursor;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue