Add tooltips to command suggestions

This commit is contained in:
Nathan Adams 2018-02-06 11:30:39 +01:00
parent ecfa4b31dd
commit ed3ecfbae3
3 changed files with 29 additions and 4 deletions

View file

@ -0,0 +1,5 @@
package com.mojang.brigadier;
public interface Message {
String getString();
}

View file

@ -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<Suggestion> {
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<Suggestion> {
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<Suggestion> {
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<Suggestion> {
return "Suggestion{" +
"range=" + range +
", text='" + text + '\'' +
", tooltip='" + tooltip + '\'' +
'}';
}
@ -78,6 +90,6 @@ public class Suggestion implements Comparable<Suggestion> {
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);
}
}

View file

@ -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);
}