Minecraft-SSHD/Minecraft-SSHD-Bukkit/src/main/java/com/ryanmichela/sshd/ConsoleCommandCompleter.java

56 lines
1.6 KiB
Java
Raw Normal View History

2013-11-14 07:17:51 +00:00
package com.ryanmichela.sshd;
/**
* Copyright 2013 Ryan Michela
*/
2018-05-06 16:42:57 +00:00
import jline.console.completer.Completer;
2013-11-14 07:17:51 +00:00
import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
2019-11-24 05:53:58 +00:00
public class ConsoleCommandCompleter implements Completer
{
public int complete(final String buffer, final int cursor, final List<CharSequence> candidates)
{
Waitable<List<String>> waitable = new Waitable<List<String>>()
{
2013-11-14 07:17:51 +00:00
@Override
2019-11-24 05:53:58 +00:00
protected List<String> evaluate()
{
2013-11-14 07:17:51 +00:00
CommandMap commandMap = ReflectionUtil.getProtectedValue(Bukkit.getServer(), "commandMap");
return commandMap.tabComplete(Bukkit.getServer().getConsoleSender(), buffer);
}
};
2019-11-24 05:53:58 +00:00
2013-11-14 07:17:51 +00:00
Bukkit.getScheduler().runTask(SshdPlugin.instance, waitable);
2019-11-24 05:53:58 +00:00
try
{
2013-11-14 07:17:51 +00:00
List<String> offers = waitable.get();
2019-11-24 05:53:58 +00:00
if (offers == null)
2013-11-14 07:17:51 +00:00
return cursor;
2019-11-24 05:53:58 +00:00
2013-11-14 07:17:51 +00:00
candidates.addAll(offers);
final int lastSpace = buffer.lastIndexOf(' ');
2019-11-24 05:53:58 +00:00
if (lastSpace == -1)
2013-11-14 07:17:51 +00:00
return cursor - buffer.length();
2019-11-24 05:53:58 +00:00
else
2013-11-14 07:17:51 +00:00
return cursor - (buffer.length() - lastSpace - 1);
2019-11-24 05:53:58 +00:00
}
catch (ExecutionException e)
{
2013-11-14 07:17:51 +00:00
SshdPlugin.instance.getLogger().log(Level.WARNING, "Unhandled exception when tab completing", e);
2019-11-24 05:53:58 +00:00
}
catch (InterruptedException e)
{
2013-11-14 07:17:51 +00:00
Thread.currentThread().interrupt();
}
return cursor;
}
}