mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 09:01:54 +01:00
Set default start & length for the tab complete packet
This commit is contained in:
parent
441cb5a1db
commit
f98cfe1d54
@ -11,6 +11,11 @@ public class Suggestion {
|
|||||||
private int length;
|
private int length;
|
||||||
private final List<SuggestionEntry> suggestionEntries = new ArrayList<>();
|
private final List<SuggestionEntry> suggestionEntries = new ArrayList<>();
|
||||||
|
|
||||||
|
public Suggestion(int start, int length) {
|
||||||
|
this.start = start;
|
||||||
|
this.length = length;
|
||||||
|
}
|
||||||
|
|
||||||
public int getStart() {
|
public int getStart() {
|
||||||
return start;
|
return start;
|
||||||
}
|
}
|
||||||
|
@ -4,5 +4,5 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface SuggestionCallback {
|
public interface SuggestionCallback {
|
||||||
void apply(@NotNull Suggestion suggestion);
|
void apply(@NotNull Suggestion suggestion, @NotNull String input);
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import net.minestom.server.command.builder.Command;
|
|||||||
import net.minestom.server.command.builder.CommandDispatcher;
|
import net.minestom.server.command.builder.CommandDispatcher;
|
||||||
import net.minestom.server.command.builder.CommandSyntax;
|
import net.minestom.server.command.builder.CommandSyntax;
|
||||||
import net.minestom.server.command.builder.arguments.Argument;
|
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.CommandParser;
|
||||||
import net.minestom.server.command.builder.parser.CommandSuggestionHolder;
|
import net.minestom.server.command.builder.parser.CommandSuggestionHolder;
|
||||||
import net.minestom.server.command.builder.parser.ValidSyntaxHolder;
|
import net.minestom.server.command.builder.parser.ValidSyntaxHolder;
|
||||||
@ -54,13 +55,17 @@ public class TabCompleteListener {
|
|||||||
final int max = syntaxesSuggestions.firstIntKey();
|
final int max = syntaxesSuggestions.firstIntKey();
|
||||||
final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max);
|
final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max);
|
||||||
final CommandSyntax syntax = suggestionHolder.syntax;
|
final CommandSyntax syntax = suggestionHolder.syntax;
|
||||||
|
final ArgumentSyntaxException argumentSyntaxException = suggestionHolder.argumentSyntaxException;
|
||||||
final int argIndex = suggestionHolder.argIndex;
|
final int argIndex = suggestionHolder.argIndex;
|
||||||
final Argument<?> argument = syntax.getArguments()[argIndex];
|
final Argument<?> argument = syntax.getArguments()[argIndex];
|
||||||
|
|
||||||
final SuggestionCallback suggestionCallback = argument.suggestionCallback;
|
final SuggestionCallback suggestionCallback = argument.suggestionCallback;
|
||||||
if (suggestionCallback != null) {
|
if (suggestionCallback != null) {
|
||||||
Suggestion suggestion = new Suggestion();
|
final int argumentLength = argumentSyntaxException != null ? argumentSyntaxException.getInput().length() :
|
||||||
suggestionCallback.apply(suggestion);
|
Integer.MAX_VALUE;
|
||||||
|
final int start = text.length() - argumentLength;
|
||||||
|
Suggestion suggestion = new Suggestion(start, argumentLength);
|
||||||
|
suggestionCallback.apply(suggestion, commandString);
|
||||||
|
|
||||||
TabCompletePacket tabCompletePacket = new TabCompletePacket();
|
TabCompletePacket tabCompletePacket = new TabCompletePacket();
|
||||||
tabCompletePacket.transactionId = packet.transactionId;
|
tabCompletePacket.transactionId = packet.transactionId;
|
||||||
|
@ -17,9 +17,8 @@ public class TestCommand extends Command {
|
|||||||
|
|
||||||
addSyntax((sender, args) -> {
|
addSyntax((sender, args) -> {
|
||||||
System.out.println("test: " + args.get("msg"));
|
System.out.println("test: " + args.get("msg"));
|
||||||
}, String("msg").setSuggestionCallback(suggestion -> {
|
}, String("msg").setSuggestionCallback((suggestion, input) -> {
|
||||||
suggestion.setLength(999);
|
suggestion.addEntry(new SuggestionEntry(input, ColoredText.of(ChatColor.RED, "Hover")));
|
||||||
suggestion.addEntry(new SuggestionEntry("Match", ColoredText.of(ChatColor.RED, "Hover")));
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user