Support lists of integers as suggestions

This commit is contained in:
Searge 2018-04-26 16:02:45 +02:00
parent 9fd10a2f3b
commit a49e92840b
4 changed files with 78 additions and 1 deletions

View file

@ -0,0 +1,63 @@
package com.mojang.brigadier.suggestion;
import com.mojang.brigadier.Message;
import com.mojang.brigadier.context.StringRange;
import java.util.Objects;
public class IntegerSuggestion extends Suggestion {
private int value;
public IntegerSuggestion(final StringRange range, final int value) {
this(range, value, null);
}
public IntegerSuggestion(final StringRange range, final int value, final Message tooltip) {
super(range, Integer.toString(value), tooltip);
this.value = value;
}
public int getValue() {
return value;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (!(o instanceof IntegerSuggestion)) {
return false;
}
final IntegerSuggestion that = (IntegerSuggestion) o;
return value == that.value && super.equals(o);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), value);
}
@Override
public String toString() {
return "IntegerSuggestion{" +
"value=" + value +
", range=" + getRange() +
", text='" + getText() + '\'' +
", tooltip='" + getTooltip() + '\'' +
'}';
}
@Override
public int compareTo(final Suggestion o) {
if (o instanceof IntegerSuggestion) {
return Integer.compare(value, ((IntegerSuggestion) o).value);
}
return super.compareTo(o);
}
@Override
public int compareToIgnoreCase(final Suggestion b) {
return compareTo(b);
}
}

View file

@ -78,6 +78,10 @@ public class Suggestion implements Comparable<Suggestion> {
return text.compareTo(o.text);
}
public int compareToIgnoreCase(final Suggestion b) {
return text.compareToIgnoreCase(b.text);
}
public Suggestion expand(final String command, final StringRange range) {
if (range.equals(this.range)) {
return this;

View file

@ -94,7 +94,7 @@ public class Suggestions {
texts.add(suggestion.expand(command, range));
}
final List<Suggestion> sorted = Lists.newArrayList(texts);
sorted.sort((a, b) -> a.getText().compareToIgnoreCase(b.getText()));
sorted.sort((a, b) -> a.compareToIgnoreCase(b));
return new Suggestions(range, sorted);
}
}

View file

@ -55,6 +55,16 @@ public class SuggestionsBuilder {
return this;
}
public SuggestionsBuilder suggest(final int value) {
result.add(new IntegerSuggestion(StringRange.between(start, input.length()), value));
return this;
}
public SuggestionsBuilder suggest(final int value, final Message tooltip) {
result.add(new IntegerSuggestion(StringRange.between(start, input.length()), value, tooltip));
return this;
}
public SuggestionsBuilder add(final SuggestionsBuilder other) {
result.addAll(other.result);
return this;