Set default start & length for the tab complete packet

This commit is contained in:
themode 2021-03-09 04:04:21 +01:00
parent 441cb5a1db
commit f98cfe1d54
4 changed files with 15 additions and 6 deletions

View File

@ -11,6 +11,11 @@ public class Suggestion {
private int length;
private final List<SuggestionEntry> suggestionEntries = new ArrayList<>();
public Suggestion(int start, int length) {
this.start = start;
this.length = length;
}
public int getStart() {
return start;
}

View File

@ -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);
}

View File

@ -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;

View File

@ -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")));
}));
}