From 9efc5122538b63db9cf57e1a83048a4882169c0b Mon Sep 17 00:00:00 2001 From: ZacSharp <68165024+ZacSharp@users.noreply.github.com> Date: Sun, 19 Feb 2023 19:17:11 +0100 Subject: [PATCH] Make tab completion usable Now behaves almost identical to vanilla tab completion --- .../mixins/MixinCommandSuggestionHelper.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/launch/java/baritone/launch/mixins/MixinCommandSuggestionHelper.java b/src/launch/java/baritone/launch/mixins/MixinCommandSuggestionHelper.java index 00497ab6b..5603246d4 100644 --- a/src/launch/java/baritone/launch/mixins/MixinCommandSuggestionHelper.java +++ b/src/launch/java/baritone/launch/mixins/MixinCommandSuggestionHelper.java @@ -19,6 +19,7 @@ import baritone.api.BaritoneAPI; import baritone.api.event.events.TabCompleteEvent; +import com.mojang.brigadier.ParseResults; import com.mojang.brigadier.context.StringRange; import com.mojang.brigadier.suggestion.Suggestion; import com.mojang.brigadier.suggestion.Suggestions; @@ -51,9 +52,18 @@ public class MixinCommandSuggestionHelper { @Final private List exceptionList; + @Shadow + private ParseResults parseResults; + @Shadow private CompletableFuture suggestionsFuture; + @Shadow + private CommandSuggestionHelper.Suggestions suggestions; + + @Shadow + boolean isApplyingSuggestion; + @Inject( method = "init", at = @At("HEAD"), @@ -74,27 +84,32 @@ private void preUpdateSuggestion(CallbackInfo ci) { if (event.completions != null) { ci.cancel(); + this.parseResults = null; // stop coloring + + if (this.isApplyingSuggestion) { // Supress suggestions update when cycling suggestions. + return; + } + + this.inputField.setSuggestion(null); // clear old suggestions + this.suggestions = null; // TODO: Support populating the command usage this.exceptionList.clear(); if (event.completions.length == 0) { this.suggestionsFuture = Suggestions.empty(); } else { - int offset = this.inputField.getText().endsWith(" ") - ? this.inputField.getCursorPosition() - : this.inputField.getText().lastIndexOf(" ") + 1; // If there is no space this is still 0 haha yes + StringRange range = StringRange.between(prefix.lastIndexOf(" ") + 1, prefix.length()); // if there is no space this starts at 0 List suggestionList = Stream.of(event.completions) - .map(s -> new Suggestion(StringRange.between(offset, offset + s.length()), s)) + .map(s -> new Suggestion(range, s)) .collect(Collectors.toList()); - Suggestions suggestions = new Suggestions( - StringRange.between(offset, offset + suggestionList.stream().mapToInt(s -> s.getText().length()).max().orElse(0)), - suggestionList); + Suggestions suggestions = new Suggestions(range, suggestionList); this.suggestionsFuture = new CompletableFuture<>(); this.suggestionsFuture.complete(suggestions); } + ((CommandSuggestionHelper) (Object) this).updateSuggestions(true); // actually populate the suggestions list from the suggestions future } } }