Fix console completions on invalid commands (#7603)
This commit is contained in:
		
					parent
					
						
							
								d89b0fb649
							
						
					
				
			
			
				commit
				
					
						eba36b62bd
					
				
			
		
					 3 changed files with 7 additions and 19 deletions
				
			
		|  | @ -99,7 +99,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| @@ -0,0 +0,0 @@ public class ConsoleCommandCompleter implements Completer {
 | @@ -0,0 +0,0 @@ public class ConsoleCommandCompleter implements Completer {
 | ||||||
|      public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) { |      public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) { | ||||||
|          final CraftServer server = this.server.server; |          final CraftServer server = this.server.server; | ||||||
|          final String buffer = line.line(); |          final String buffer = "/" + line.line(); | ||||||
| +        // Async Tab Complete
 | +        // Async Tab Complete
 | ||||||
| +        final com.destroystokyo.paper.event.server.AsyncTabCompleteEvent event =
 | +        final com.destroystokyo.paper.event.server.AsyncTabCompleteEvent event =
 | ||||||
| +            new com.destroystokyo.paper.event.server.AsyncTabCompleteEvent(server.getConsoleSender(), buffer, true, null);
 | +            new com.destroystokyo.paper.event.server.AsyncTabCompleteEvent(server.getConsoleSender(), buffer, true, null);
 | ||||||
|  |  | ||||||
|  | @ -91,7 +91,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| +            return;
 | +            return;
 | ||||||
| +        }
 | +        }
 | ||||||
| +        final CommandDispatcher<CommandSourceStack> dispatcher = this.server.getCommands().getDispatcher();
 | +        final CommandDispatcher<CommandSourceStack> dispatcher = this.server.getCommands().getDispatcher();
 | ||||||
| +        final ParseResults<CommandSourceStack> results = dispatcher.parse(prepareStringReader(line.line()), this.commandSourceStack.get());
 | +        final ParseResults<CommandSourceStack> results = dispatcher.parse(new StringReader(line.line()), this.commandSourceStack.get());
 | ||||||
| +        this.addCandidates(
 | +        this.addCandidates(
 | ||||||
| +            candidates,
 | +            candidates,
 | ||||||
| +            dispatcher.getCompletionSuggestions(results, line.cursor()).join().getList(),
 | +            dispatcher.getCompletionSuggestions(results, line.cursor()).join().getList(),
 | ||||||
|  | @ -157,14 +157,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| +        return completion(suggestion.getText(), PaperAdventure.asAdventure(ComponentUtils.fromMessage(suggestion.getTooltip())));
 | +        return completion(suggestion.getText(), PaperAdventure.asAdventure(ComponentUtils.fromMessage(suggestion.getTooltip())));
 | ||||||
| +    }
 | +    }
 | ||||||
| +
 | +
 | ||||||
| +    static @NonNull StringReader prepareStringReader(final @NonNull String line) {
 |  | ||||||
| +        final StringReader stringReader = new StringReader(line);
 |  | ||||||
| +        if (stringReader.canRead() && stringReader.peek() == '/') {
 |  | ||||||
| +            stringReader.skip();
 |  | ||||||
| +        }
 |  | ||||||
| +        return stringReader;
 |  | ||||||
| +    }
 |  | ||||||
| +
 |  | ||||||
| +    private record ParseContext(String line, int suggestionStart) {
 | +    private record ParseContext(String line, int suggestionStart) {
 | ||||||
| +    }
 | +    }
 | ||||||
| +
 | +
 | ||||||
|  | @ -184,6 +176,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| +
 | +
 | ||||||
| +import com.google.common.base.Suppliers;
 | +import com.google.common.base.Suppliers;
 | ||||||
| +import com.mojang.brigadier.ParseResults;
 | +import com.mojang.brigadier.ParseResults;
 | ||||||
|  | +import com.mojang.brigadier.StringReader;
 | ||||||
| +import com.mojang.brigadier.context.ParsedCommandNode;
 | +import com.mojang.brigadier.context.ParsedCommandNode;
 | ||||||
| +import com.mojang.brigadier.tree.LiteralCommandNode;
 | +import com.mojang.brigadier.tree.LiteralCommandNode;
 | ||||||
| +import java.util.function.Supplier;
 | +import java.util.function.Supplier;
 | ||||||
|  | @ -214,12 +207,8 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| +            return new AttributedString(buffer, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
 | +            return new AttributedString(buffer, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED));
 | ||||||
| +        }
 | +        }
 | ||||||
| +        final AttributedStringBuilder builder = new AttributedStringBuilder();
 | +        final AttributedStringBuilder builder = new AttributedStringBuilder();
 | ||||||
| +        final ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(BrigadierCommandCompleter.prepareStringReader(buffer), this.commandSourceStack.get());
 | +        final ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(new StringReader(buffer), this.commandSourceStack.get());
 | ||||||
| +        int pos = 0;
 | +        int pos = 0;
 | ||||||
| +        if (buffer.startsWith("/")) {
 |  | ||||||
| +            builder.append("/", AttributedStyle.DEFAULT);
 |  | ||||||
| +            pos = 1;
 |  | ||||||
| +        }
 |  | ||||||
| +        int component = -1;
 | +        int component = -1;
 | ||||||
| +        for (final ParsedCommandNode<CommandSourceStack> node : results.getContext().getLastChild().getNodes()) {
 | +        for (final ParsedCommandNode<CommandSourceStack> node : results.getContext().getLastChild().getNodes()) {
 | ||||||
| +            if (node.getRange().getStart() >= buffer.length()) {
 | +            if (node.getRange().getStart() >= buffer.length()) {
 | ||||||
|  | @ -293,6 +282,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| +
 | +
 | ||||||
| +import com.mojang.brigadier.ImmutableStringReader;
 | +import com.mojang.brigadier.ImmutableStringReader;
 | ||||||
| +import com.mojang.brigadier.ParseResults;
 | +import com.mojang.brigadier.ParseResults;
 | ||||||
|  | +import com.mojang.brigadier.StringReader;
 | ||||||
| +import com.mojang.brigadier.context.CommandContextBuilder;
 | +import com.mojang.brigadier.context.CommandContextBuilder;
 | ||||||
| +import com.mojang.brigadier.context.ParsedCommandNode;
 | +import com.mojang.brigadier.context.ParsedCommandNode;
 | ||||||
| +import com.mojang.brigadier.context.StringRange;
 | +import com.mojang.brigadier.context.StringRange;
 | ||||||
|  | @ -304,8 +294,6 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| +import org.jline.reader.Parser;
 | +import org.jline.reader.Parser;
 | ||||||
| +import org.jline.reader.SyntaxError;
 | +import org.jline.reader.SyntaxError;
 | ||||||
| +
 | +
 | ||||||
| +import static io.papermc.paper.console.BrigadierCommandCompleter.prepareStringReader;
 |  | ||||||
| +
 |  | ||||||
| +public class BrigadierConsoleParser implements Parser {
 | +public class BrigadierConsoleParser implements Parser {
 | ||||||
| +
 | +
 | ||||||
| +    private final DedicatedServer server;
 | +    private final DedicatedServer server;
 | ||||||
|  | @ -316,7 +304,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| +
 | +
 | ||||||
| +    @Override
 | +    @Override
 | ||||||
| +    public ParsedLine parse(final String line, final int cursor, final ParseContext context) throws SyntaxError {
 | +    public ParsedLine parse(final String line, final int cursor, final ParseContext context) throws SyntaxError {
 | ||||||
| +        final ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(prepareStringReader(line), this.server.createCommandSourceStack());
 | +        final ParseResults<CommandSourceStack> results = this.server.getCommands().getDispatcher().parse(new StringReader(line), this.server.createCommandSourceStack());
 | ||||||
| +        final ImmutableStringReader reader = results.getReader();
 | +        final ImmutableStringReader reader = results.getReader();
 | ||||||
| +        final List<String> words = new ArrayList<>();
 | +        final List<String> words = new ArrayList<>();
 | ||||||
| +        CommandContextBuilder<CommandSourceStack> currentContext = results.getContext();
 | +        CommandContextBuilder<CommandSourceStack> currentContext = results.getContext();
 | ||||||
|  |  | ||||||
|  | @ -534,7 +534,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 | ||||||
| -    public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
 | -    public int complete(final String buffer, final int cursor, final List<CharSequence> candidates) {
 | ||||||
| +    public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
 | +    public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
 | ||||||
| +        final CraftServer server = this.server.server;
 | +        final CraftServer server = this.server.server;
 | ||||||
| +        final String buffer = line.line();
 | +        final String buffer = "/" + line.line();
 | ||||||
| +        // Paper end
 | +        // Paper end
 | ||||||
|          Waitable<List<String>> waitable = new Waitable<List<String>>() { |          Waitable<List<String>> waitable = new Waitable<List<String>>() { | ||||||
|              @Override |              @Override | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Jake Potrebic
				Jake Potrebic