From f1e5a61bfdcebd67a1f4ef322ab557c468acb13d Mon Sep 17 00:00:00 2001 From: aromaa Date: Wed, 25 Oct 2023 21:04:40 +0300 Subject: [PATCH] Use SpongeCommandDispatcher for JLine --- .../chat/console/BrigadierHighlighter.java | 15 ++++++++------- .../chat/console/BrigadierJLineCompleter.java | 13 +++++++------ .../vanilla/chat/console/VanillaConsole.java | 4 ++-- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierHighlighter.java b/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierHighlighter.java index a017c75733f..896fe0ce6a0 100644 --- a/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierHighlighter.java +++ b/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierHighlighter.java @@ -24,10 +24,10 @@ */ package org.spongepowered.vanilla.chat.console; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.ImmutableStringReader; import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.context.ParsedCommandNode; +import net.minecraft.commands.CommandSourceStack; import org.checkerframework.checker.nullness.qual.Nullable; import org.jline.reader.Highlighter; import org.jline.reader.LineReader; @@ -35,6 +35,7 @@ import org.jline.utils.AttributedStringBuilder; import org.jline.utils.AttributedStyle; import org.spongepowered.common.SpongeCommon; +import org.spongepowered.common.command.brigadier.dispatcher.SpongeCommandDispatcher; import java.util.function.Supplier; import java.util.regex.Pattern; @@ -53,29 +54,29 @@ public class BrigadierHighlighter implements Highlighter { .mapToObj(AttributedStyle.DEFAULT::foreground) .toArray(AttributedStyle[]::new); - private final Supplier<@Nullable CommandDispatcher> dispatcherProvider; - private final Supplier commandSourceProvider; + private final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider; + private final Supplier commandSourceProvider; - public BrigadierHighlighter(final Supplier<@Nullable CommandDispatcher> dispatcherProvider, final Supplier commandSourceProvider) { + public BrigadierHighlighter(final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider, final Supplier commandSourceProvider) { this.dispatcherProvider = dispatcherProvider; this.commandSourceProvider = commandSourceProvider; } @Override public AttributedString highlight(final LineReader lineReader, final String buffer) { - final CommandDispatcher dispatcher = this.dispatcherProvider.get(); + final SpongeCommandDispatcher dispatcher = this.dispatcherProvider.get(); if (dispatcher == null) { return new AttributedString(buffer); } try { - final ParseResults results = dispatcher.parse(buffer, this.commandSourceProvider.get()); + final ParseResults results = dispatcher.parse(buffer, this.commandSourceProvider.get(), true); final ImmutableStringReader reader = results.getReader(); final AttributedStringBuilder builder = new AttributedStringBuilder(); int lastPos = 0; int argColorIdx = 0; - for (final ParsedCommandNode node : results.getContext().getLastChild().getNodes()) { + for (final ParsedCommandNode node : results.getContext().getLastChild().getNodes()) { // Sometimes Brigadier will spit out ranges that are invalid for the current input string???? final int start = Math.min(node.getRange().getStart(), reader.getTotalLength()); final int end = Math.min(node.getRange().getEnd(), reader.getTotalLength()); diff --git a/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierJLineCompleter.java b/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierJLineCompleter.java index 877ac3e393f..b8a37f06073 100644 --- a/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierJLineCompleter.java +++ b/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/BrigadierJLineCompleter.java @@ -24,17 +24,18 @@ */ package org.spongepowered.vanilla.chat.console; -import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.Message; import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; +import net.minecraft.commands.CommandSourceStack; import org.checkerframework.checker.nullness.qual.Nullable; import org.jline.reader.Candidate; import org.jline.reader.Completer; import org.jline.reader.LineReader; import org.jline.reader.ParsedLine; import org.spongepowered.common.SpongeCommon; +import org.spongepowered.common.command.brigadier.dispatcher.SpongeCommandDispatcher; import java.util.List; import java.util.concurrent.CompletableFuture; @@ -43,23 +44,23 @@ final class BrigadierJLineCompleter implements Completer { - private final Supplier<@Nullable CommandDispatcher> dispatcherProvider; - private final Supplier commandSourceProvider; + private final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider; + private final Supplier commandSourceProvider; - public BrigadierJLineCompleter(final Supplier<@Nullable CommandDispatcher> dispatcherProvider, final Supplier commandSourceProvider) { + public BrigadierJLineCompleter(final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider, final Supplier commandSourceProvider) { this.dispatcherProvider = dispatcherProvider; this.commandSourceProvider = commandSourceProvider; } @Override public void complete(LineReader reader, ParsedLine line, List candidates) { - final CommandDispatcher dispatcher = this.dispatcherProvider.get(); + final SpongeCommandDispatcher dispatcher = this.dispatcherProvider.get(); if (dispatcher == null) { return; } final String input = line.line(); - final ParseResults parseResult = dispatcher.parse(input, this.commandSourceProvider.get()); + final ParseResults parseResult = dispatcher.parse(input, this.commandSourceProvider.get(), true); final CompletableFuture suggestions = dispatcher.getCompletionSuggestions( parseResult, line.cursor() diff --git a/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/VanillaConsole.java b/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/VanillaConsole.java index 1df7df4f441..52d070260c1 100644 --- a/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/VanillaConsole.java +++ b/vanilla/src/main/java/org/spongepowered/vanilla/chat/console/VanillaConsole.java @@ -24,13 +24,13 @@ */ package org.spongepowered.vanilla.chat.console; -import com.mojang.brigadier.CommandDispatcher; import net.minecraft.commands.CommandSourceStack; import net.minecraft.server.dedicated.DedicatedServer; import net.minecrell.terminalconsole.SimpleTerminalConsole; import org.checkerframework.checker.nullness.qual.Nullable; import org.jline.reader.LineReader; import org.jline.reader.LineReaderBuilder; +import org.spongepowered.common.command.brigadier.dispatcher.SpongeCommandDispatcher; import org.spongepowered.common.command.manager.SpongeCommandManager; import org.spongepowered.common.launch.Launch; @@ -46,7 +46,7 @@ public VanillaConsole(DedicatedServer server) { @Override protected LineReader buildReader(LineReaderBuilder builder) { - final Supplier<@Nullable CommandDispatcher> dispatcherProvider = () -> { + final Supplier<@Nullable SpongeCommandDispatcher> dispatcherProvider = () -> { final SpongeCommandManager manager = SpongeCommandManager.get(this.server); return manager == null ? null : manager.getDispatcher(); };