|
1 | 1 | package com.wizardlybump17.wlib.command.suggestion; |
2 | 2 |
|
| 3 | +import com.wizardlybump17.wlib.command.exception.SuggesterException; |
| 4 | +import com.wizardlybump17.wlib.command.node.CommandNode; |
| 5 | +import com.wizardlybump17.wlib.command.sender.CommandSender; |
3 | 6 | import org.jetbrains.annotations.NotNull; |
| 7 | +import org.jetbrains.annotations.Nullable; |
| 8 | +import org.jetbrains.annotations.Unmodifiable; |
4 | 9 |
|
5 | 10 | import java.util.List; |
| 11 | +import java.util.function.Function; |
6 | 12 |
|
7 | 13 | public interface ValuesSuggester<T> extends Suggester<T> { |
8 | 14 |
|
9 | 15 | @NotNull List<T> values(); |
| 16 | + |
| 17 | + final class Values<T> implements ValuesSuggester<T> { |
| 18 | + |
| 19 | + private final @NotNull List<T> values; |
| 20 | + private final @Nullable Function<T, String> stringRepresentationFunction; |
| 21 | + |
| 22 | + Values(@NotNull List<T> values, @NotNull Function<T, String> stringRepresentationFunction) { |
| 23 | + this.values = List.copyOf(values); |
| 24 | + this.stringRepresentationFunction = stringRepresentationFunction; |
| 25 | + } |
| 26 | + |
| 27 | + Values(@NotNull List<T> values) { |
| 28 | + this.values = List.copyOf(values); |
| 29 | + stringRepresentationFunction = null; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public @NotNull @Unmodifiable List<T> values() { |
| 34 | + return values; |
| 35 | + } |
| 36 | + |
| 37 | + public @Nullable Function<T, String> stringRepresentation() { |
| 38 | + return stringRepresentationFunction; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public @NotNull List<T> getSuggestions(@NotNull CommandSender<?> sender, @NotNull List<String> input, @NotNull String current, @NotNull CommandNode<?> currentNode) throws SuggesterException { |
| 43 | + return values; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public @NotNull String getStringRepresentation(@NotNull T value) { |
| 48 | + return stringRepresentationFunction == null ? ValuesSuggester.super.getStringRepresentation(value) : stringRepresentationFunction.apply(value); |
| 49 | + } |
| 50 | + } |
10 | 51 | } |
0 commit comments