From d1129b0ded492f46989fc3e2ce890294bee03f76 Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Fri, 9 Sep 2022 14:25:54 -0700 Subject: [PATCH] Fix console completer/highlighter having invalid source stack (#8346) --- ...tab-completions-for-brigadier-comman.patch | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/patches/server/Enhance-console-tab-completions-for-brigadier-comman.patch b/patches/server/Enhance-console-tab-completions-for-brigadier-comman.patch index 54539bb1ec..e9b95ee394 100644 --- a/patches/server/Enhance-console-tab-completions-for-brigadier-comman.patch +++ b/patches/server/Enhance-console-tab-completions-for-brigadier-comman.patch @@ -20,7 +20,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 - ); + .option(LineReader.Option.COMPLETE_IN_WORD, true); + if (io.papermc.paper.configuration.GlobalConfiguration.get().console.enableBrigadierHighlighting) { -+ builder.highlighter(new io.papermc.paper.console.BrigadierCommandHighlighter(this.server, this.server.createCommandSourceStack())); ++ builder.highlighter(new io.papermc.paper.console.BrigadierCommandHighlighter(this.server)); + } + return super.buildReader(builder); } @@ -35,11 +35,16 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +package io.papermc.paper.console; + +import com.destroystokyo.paper.event.server.AsyncTabCompleteEvent.Completion; ++import com.google.common.base.Suppliers; +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.ParseResults; +import com.mojang.brigadier.StringReader; +import com.mojang.brigadier.suggestion.Suggestion; +import io.papermc.paper.adventure.PaperAdventure; ++import java.util.ArrayList; ++import java.util.Collections; ++import java.util.List; ++import java.util.function.Supplier; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.network.chat.ComponentUtils; +import net.minecraft.server.dedicated.DedicatedServer; @@ -48,28 +53,27 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 +import org.jline.reader.LineReader; +import org.jline.reader.ParsedLine; + -+import java.util.ArrayList; -+import java.util.Collections; -+import java.util.List; -+ +import static com.destroystokyo.paper.event.server.AsyncTabCompleteEvent.Completion.completion; + +public final class BrigadierCommandCompleter { -+ private final CommandSourceStack commandSourceStack; ++ private final Supplier commandSourceStack; + private final DedicatedServer server; + -+ public BrigadierCommandCompleter(final @NonNull DedicatedServer server, final @NonNull CommandSourceStack commandSourceStack) { ++ public BrigadierCommandCompleter(final @NonNull DedicatedServer server) { + this.server = server; -+ this.commandSourceStack = commandSourceStack; ++ this.commandSourceStack = Suppliers.memoize(this.server::createCommandSourceStack); + } + + public void complete(final @NonNull LineReader reader, final @NonNull ParsedLine line, final @NonNull List candidates, final @NonNull List existing) { -+ if (!io.papermc.paper.configuration.GlobalConfiguration.get().console.enableBrigadierCompletions) { ++ //noinspection ConstantConditions ++ if (this.server.overworld() == null) { // check if overworld is null, as worlds haven't been loaded yet ++ return; ++ } else if (!io.papermc.paper.configuration.GlobalConfiguration.get().console.enableBrigadierCompletions) { + this.addCandidates(candidates, Collections.emptyList(), existing); + return; + } + final CommandDispatcher dispatcher = this.server.getCommands().getDispatcher(); -+ final ParseResults results = dispatcher.parse(prepareStringReader(line.line()), this.commandSourceStack); ++ final ParseResults results = dispatcher.parse(prepareStringReader(line.line()), this.commandSourceStack.get()); + this.addCandidates( + candidates, + dispatcher.getCompletionSuggestions(results, line.cursor()).join().getList(), @@ -135,9 +139,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 @@ -0,0 +0,0 @@ +package io.papermc.paper.console; + ++import com.google.common.base.Suppliers; +import com.mojang.brigadier.ParseResults; +import com.mojang.brigadier.context.ParsedCommandNode; +import com.mojang.brigadier.tree.LiteralCommandNode; ++import java.util.function.Supplier; +import java.util.regex.Pattern; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.server.dedicated.DedicatedServer; @@ -150,18 +156,22 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + +public final class BrigadierCommandHighlighter implements Highlighter { + private static final int[] COLORS = {AttributedStyle.CYAN, AttributedStyle.YELLOW, AttributedStyle.GREEN, AttributedStyle.MAGENTA, /* Client uses GOLD here, not BLUE, however there is no GOLD AttributedStyle. */ AttributedStyle.BLUE}; -+ private final CommandSourceStack commandSourceStack; ++ private final Supplier commandSourceStack; + private final DedicatedServer server; + -+ public BrigadierCommandHighlighter(final @NonNull DedicatedServer server, final @NonNull CommandSourceStack commandSourceStack) { ++ public BrigadierCommandHighlighter(final @NonNull DedicatedServer server) { + this.server = server; -+ this.commandSourceStack = commandSourceStack; ++ this.commandSourceStack = Suppliers.memoize(this.server::createCommandSourceStack); + } + + @Override + public AttributedString highlight(final @NonNull LineReader reader, final @NonNull String buffer) { ++ //noinspection ConstantConditions ++ if (this.server.overworld() == null) { // check if overworld is null, as worlds haven't been loaded yet ++ return new AttributedString(buffer, AttributedStyle.DEFAULT.foreground(AttributedStyle.RED)); ++ } + final AttributedStringBuilder builder = new AttributedStringBuilder(); -+ final ParseResults results = this.server.getCommands().getDispatcher().parse(BrigadierCommandCompleter.prepareStringReader(buffer), this.commandSourceStack); ++ final ParseResults results = this.server.getCommands().getDispatcher().parse(BrigadierCommandCompleter.prepareStringReader(buffer), this.commandSourceStack.get()); + int pos = 0; + if (buffer.startsWith("/")) { + builder.append("/", AttributedStyle.DEFAULT); @@ -230,7 +240,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 public ConsoleCommandCompleter(DedicatedServer server) { // Paper - CraftServer -> DedicatedServer this.server = server; -+ this.brigadierCompleter = new io.papermc.paper.console.BrigadierCommandCompleter(this.server, this.server.createCommandSourceStack()); // Paper ++ this.brigadierCompleter = new io.papermc.paper.console.BrigadierCommandCompleter(this.server); // Paper } // Paper start - Change method signature for JLine update