From f98cfe1d54677cb9399a506b054ace1fd1fa4f18 Mon Sep 17 00:00:00 2001 From: themode Date: Tue, 9 Mar 2021 04:04:21 +0100 Subject: [PATCH] Set default start & length for the tab complete packet --- .../server/command/builder/suggestion/Suggestion.java | 5 +++++ .../command/builder/suggestion/SuggestionCallback.java | 2 +- .../minestom/server/listener/TabCompleteListener.java | 9 +++++++-- src/test/java/demo/commands/TestCommand.java | 5 ++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/command/builder/suggestion/Suggestion.java b/src/main/java/net/minestom/server/command/builder/suggestion/Suggestion.java index 988e5ac46..3c951ba16 100644 --- a/src/main/java/net/minestom/server/command/builder/suggestion/Suggestion.java +++ b/src/main/java/net/minestom/server/command/builder/suggestion/Suggestion.java @@ -11,6 +11,11 @@ public class Suggestion { private int length; private final List suggestionEntries = new ArrayList<>(); + public Suggestion(int start, int length) { + this.start = start; + this.length = length; + } + public int getStart() { return start; } diff --git a/src/main/java/net/minestom/server/command/builder/suggestion/SuggestionCallback.java b/src/main/java/net/minestom/server/command/builder/suggestion/SuggestionCallback.java index f99c17798..f629b101b 100644 --- a/src/main/java/net/minestom/server/command/builder/suggestion/SuggestionCallback.java +++ b/src/main/java/net/minestom/server/command/builder/suggestion/SuggestionCallback.java @@ -4,5 +4,5 @@ import org.jetbrains.annotations.NotNull; @FunctionalInterface public interface SuggestionCallback { - void apply(@NotNull Suggestion suggestion); + void apply(@NotNull Suggestion suggestion, @NotNull String input); } diff --git a/src/main/java/net/minestom/server/listener/TabCompleteListener.java b/src/main/java/net/minestom/server/listener/TabCompleteListener.java index ef2819113..33dcf762c 100644 --- a/src/main/java/net/minestom/server/listener/TabCompleteListener.java +++ b/src/main/java/net/minestom/server/listener/TabCompleteListener.java @@ -8,6 +8,7 @@ import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandDispatcher; import net.minestom.server.command.builder.CommandSyntax; import net.minestom.server.command.builder.arguments.Argument; +import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.command.builder.parser.CommandParser; import net.minestom.server.command.builder.parser.CommandSuggestionHolder; import net.minestom.server.command.builder.parser.ValidSyntaxHolder; @@ -54,13 +55,17 @@ public class TabCompleteListener { final int max = syntaxesSuggestions.firstIntKey(); final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max); final CommandSyntax syntax = suggestionHolder.syntax; + final ArgumentSyntaxException argumentSyntaxException = suggestionHolder.argumentSyntaxException; final int argIndex = suggestionHolder.argIndex; final Argument argument = syntax.getArguments()[argIndex]; final SuggestionCallback suggestionCallback = argument.suggestionCallback; if (suggestionCallback != null) { - Suggestion suggestion = new Suggestion(); - suggestionCallback.apply(suggestion); + final int argumentLength = argumentSyntaxException != null ? argumentSyntaxException.getInput().length() : + Integer.MAX_VALUE; + final int start = text.length() - argumentLength; + Suggestion suggestion = new Suggestion(start, argumentLength); + suggestionCallback.apply(suggestion, commandString); TabCompletePacket tabCompletePacket = new TabCompletePacket(); tabCompletePacket.transactionId = packet.transactionId; diff --git a/src/test/java/demo/commands/TestCommand.java b/src/test/java/demo/commands/TestCommand.java index 861b51a6a..9e392c759 100644 --- a/src/test/java/demo/commands/TestCommand.java +++ b/src/test/java/demo/commands/TestCommand.java @@ -17,9 +17,8 @@ public class TestCommand extends Command { addSyntax((sender, args) -> { System.out.println("test: " + args.get("msg")); - }, String("msg").setSuggestionCallback(suggestion -> { - suggestion.setLength(999); - suggestion.addEntry(new SuggestionEntry("Match", ColoredText.of(ChatColor.RED, "Hover"))); + }, String("msg").setSuggestionCallback((suggestion, input) -> { + suggestion.addEntry(new SuggestionEntry(input, ColoredText.of(ChatColor.RED, "Hover"))); })); }