From 5c50fa7691d2b57a2da7e57be2f6de45fc9caa85 Mon Sep 17 00:00:00 2001 From: Searge Date: Thu, 26 Apr 2018 16:02:45 +0200 Subject: [PATCH] Support lists of integers as suggestions --- .../suggestion/IntegerSuggestion.java | 63 +++++++++++++++++++ .../brigadier/suggestion/Suggestion.java | 4 ++ .../brigadier/suggestion/Suggestions.java | 2 +- .../suggestion/SuggestionsBuilder.java | 10 +++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/mojang/brigadier/suggestion/IntegerSuggestion.java diff --git a/src/main/java/com/mojang/brigadier/suggestion/IntegerSuggestion.java b/src/main/java/com/mojang/brigadier/suggestion/IntegerSuggestion.java new file mode 100644 index 0000000..c16fc01 --- /dev/null +++ b/src/main/java/com/mojang/brigadier/suggestion/IntegerSuggestion.java @@ -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); + } +} diff --git a/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java b/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java index aa61f58..2c592e8 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java +++ b/src/main/java/com/mojang/brigadier/suggestion/Suggestion.java @@ -78,6 +78,10 @@ public class Suggestion implements Comparable { 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; diff --git a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java index 03fdd5e..61d4acd 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java +++ b/src/main/java/com/mojang/brigadier/suggestion/Suggestions.java @@ -94,7 +94,7 @@ public class Suggestions { texts.add(suggestion.expand(command, range)); } final List 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); } } diff --git a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java index 9e6518c..ce2836a 100644 --- a/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java +++ b/src/main/java/com/mojang/brigadier/suggestion/SuggestionsBuilder.java @@ -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;