2020-03-07 23:50:46 +01:00
|
|
|
From 3d39aab76fcf606f04df1c1e9e500009a2426a87 Mon Sep 17 00:00:00 2001
|
2018-07-06 23:00:08 +02:00
|
|
|
From: Minecrell <minecrell@minecrell.net>
|
2017-09-26 18:50:48 +02:00
|
|
|
Date: Tue, 26 Sep 2017 18:59:37 +0200
|
|
|
|
Subject: [PATCH] Add console command completion
|
|
|
|
|
|
|
|
Register command completer for JLine to complete command names and
|
|
|
|
command arguments (if supported).
|
|
|
|
|
|
|
|
diff --git a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java
|
2020-03-07 23:50:46 +01:00
|
|
|
index e7cd8a27..70d3736b 100644
|
2017-09-26 18:50:48 +02:00
|
|
|
--- a/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java
|
|
|
|
+++ b/api/src/main/java/net/md_5/bungee/api/plugin/PluginManager.java
|
2020-03-07 23:50:46 +01:00
|
|
|
@@ -205,6 +205,41 @@ public final class PluginManager
|
2017-09-26 18:50:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ // Waterfall start - Add method to tab-complete command names
|
|
|
|
+ /**
|
|
|
|
+ * Searches for tab-complete suggestions for the given command line.
|
|
|
|
+ *
|
|
|
|
+ * <p>This is similar to {@link #dispatchCommand(CommandSender, String, List)}
|
|
|
|
+ * called with a list, but it also handles completing the command names itself
|
|
|
|
+ * instead of just the arguments.</p>
|
|
|
|
+ *
|
|
|
|
+ * @param sender The command sender
|
|
|
|
+ * @param commandLine The current command line
|
|
|
|
+ * @return The tab-complete suggestions
|
|
|
|
+ */
|
|
|
|
+ public List<String> tabCompleteCommand(CommandSender sender, String commandLine) {
|
|
|
|
+ List<String> suggestions = new java.util.ArrayList<>();
|
|
|
|
+
|
|
|
|
+ if (commandLine.indexOf(' ') == -1) {
|
|
|
|
+ // Complete command name
|
|
|
|
+ for (Command command : this.commandMap.values()) {
|
|
|
|
+ if (command.getName().startsWith(commandLine)) {
|
|
|
|
+ // Check command permissions before adding it to the suggestions
|
|
|
|
+ String permission = command.getPermission();
|
|
|
|
+ if (permission == null || permission.isEmpty() || sender.hasPermission(permission)) {
|
|
|
|
+ suggestions.add(command.getName());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // Complete command arguments
|
|
|
|
+ dispatchCommand(sender, commandLine, suggestions);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return suggestions;
|
|
|
|
+ }
|
|
|
|
+ // Waterfall end
|
|
|
|
+
|
|
|
|
/**
|
|
|
|
* Returns the {@link Plugin} objects corresponding to all loaded plugins.
|
|
|
|
*
|
|
|
|
diff --git a/proxy/src/main/java/io/github/waterfallmc/waterfall/console/ConsoleCommandCompleter.java b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/ConsoleCommandCompleter.java
|
|
|
|
new file mode 100644
|
|
|
|
index 00000000..bfcb6e9f
|
|
|
|
--- /dev/null
|
|
|
|
+++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/ConsoleCommandCompleter.java
|
|
|
|
@@ -0,0 +1,31 @@
|
|
|
|
+package io.github.waterfallmc.waterfall.console;
|
|
|
|
+
|
|
|
|
+import net.md_5.bungee.api.ProxyServer;
|
|
|
|
+import org.jline.reader.Candidate;
|
|
|
|
+import org.jline.reader.Completer;
|
|
|
|
+import org.jline.reader.LineReader;
|
|
|
|
+import org.jline.reader.ParsedLine;
|
|
|
|
+
|
|
|
|
+import java.util.List;
|
|
|
|
+
|
|
|
|
+final class ConsoleCommandCompleter implements Completer {
|
|
|
|
+
|
|
|
|
+ private final ProxyServer proxy;
|
|
|
|
+
|
|
|
|
+ ConsoleCommandCompleter(ProxyServer proxy) {
|
|
|
|
+ this.proxy = proxy;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
|
|
|
|
+ List<String> suggestions = this.proxy.getPluginManager().tabCompleteCommand(this.proxy.getConsole(), line.line());
|
|
|
|
+ if (suggestions.isEmpty()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (String suggestion : suggestions) {
|
|
|
|
+ candidates.add(new Candidate(suggestion));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
diff --git a/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java
|
2018-07-06 23:00:08 +02:00
|
|
|
index 765d24bc..6cec0b5a 100644
|
2017-09-26 18:50:48 +02:00
|
|
|
--- a/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java
|
|
|
|
+++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/console/WaterfallConsole.java
|
2018-07-06 23:00:08 +02:00
|
|
|
@@ -15,6 +15,7 @@ public final class WaterfallConsole extends SimpleTerminalConsole {
|
|
|
|
ProxyServer proxy = ProxyServer.getInstance();
|
|
|
|
return super.buildReader(builder
|
|
|
|
.appName(proxy.getName())
|
|
|
|
+ .completer(new ConsoleCommandCompleter(proxy))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-09-26 18:50:48 +02:00
|
|
|
--
|
2020-03-07 23:50:46 +01:00
|
|
|
2.25.0
|
2017-09-26 18:50:48 +02:00
|
|
|
|