From ed3ecfbae3853160b1a303a33ccd3c279221a8f7 Mon Sep 17 00:00:00 2001 From: Nathan Adams Date: Tue, 6 Feb 2018 11:30:39 +0100 Subject: [PATCH] Add tooltips to command suggestions --- .../java/com/mojang/brigadier/Message.java | 5 +++++ .../brigadier/suggestion/Suggestion.java | 18 +++++++++++++++--- .../suggestion/SuggestionsBuilder.java | 10 +++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/mojang/brigadier/Message.java diff --git a/src/main/java/com/mojang/brigadier/Message.java b/src/main/java/com/mojang/brigadier/Message.java new file mode 100644 index 0000000..b6ee219 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/Message.java @@ -0,0 +1,5 @@ +package com.mojang.brigadier; + +public interface Message { + String getString(); +} diff --git a/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java b/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java index 7fd7370..aa61f58 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java +++ b/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java @@ -1,5 +1,6 @@ package com.mojang.brigadier.suggestion; +import com.mojang.brigadier.Message; import com.mojang.brigadier.context.StringRange; import java.util.Objects; @@ -7,10 +8,16 @@ import java.util.Objects; public class Suggestion implements Comparable { private final StringRange range; private final String text; + private final Message tooltip; public Suggestion(final StringRange range, final String text) { + this(range, text, null); + } + + public Suggestion(final StringRange range, final String text, final Message tooltip) { this.range = range; this.text = text; + this.tooltip = tooltip; } public StringRange getRange() { @@ -21,6 +28,10 @@ public class Suggestion implements Comparable { return text; } + public Message getTooltip() { + return tooltip; + } + public String apply(final String input) { if (range.getStart() == 0 && range.getEnd() == input.length()) { return text; @@ -45,12 +56,12 @@ public class Suggestion implements Comparable { return false; } final Suggestion that = (Suggestion) o; - return Objects.equals(range, that.range) && Objects.equals(text, that.text); + return Objects.equals(range, that.range) && Objects.equals(text, that.text) && Objects.equals(tooltip, that.tooltip); } @Override public int hashCode() { - return Objects.hash(range, text); + return Objects.hash(range, text, tooltip); } @Override @@ -58,6 +69,7 @@ public class Suggestion implements Comparable { return "Suggestion{" + "range=" + range + ", text='" + text + '\'' + + ", tooltip='" + tooltip + '\'' + '}'; } @@ -78,6 +90,6 @@ public class Suggestion implements Comparable { if (range.getEnd() > this.range.getEnd()) { result.append(command.substring(this.range.getEnd(), range.getEnd())); } - return new Suggestion(range, result.toString()); + return new Suggestion(range, result.toString(), tooltip); } } diff --git a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java index f8993b4..58ce6b5 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java +++ b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java @@ -1,6 +1,6 @@ package com.mojang.brigadier.suggestion; -import com.google.common.base.Strings; +import com.mojang.brigadier.Message; import com.mojang.brigadier.context.StringRange; import java.util.ArrayList; @@ -47,6 +47,14 @@ public class SuggestionsBuilder { return this; } + public SuggestionsBuilder suggest(final String text, final Message tooltip) { + if (text.equals(remaining)) { + return this; + } + result.add(new Suggestion(StringRange.between(start, input.length()), text, tooltip)); + return this; + } + public SuggestionsBuilder restart() { return new SuggestionsBuilder(input, start); }