Unicopia/src/main/java/com/minelittlepony/unicopia/command/RaceArgument.java

42 lines
1.5 KiB
Java
Raw Normal View History

package com.minelittlepony.unicopia.command;
2020-01-27 11:05:22 +01:00
import java.util.Arrays;
import java.util.Collection;
2020-05-02 18:51:19 +02:00
import java.util.concurrent.CompletableFuture;
2020-01-27 11:05:22 +01:00
import java.util.stream.Collectors;
import com.minelittlepony.unicopia.Race;
2020-01-27 11:05:22 +01:00
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
2020-05-02 18:51:19 +02:00
import com.mojang.brigadier.context.CommandContext;
2020-01-27 11:05:22 +01:00
import com.mojang.brigadier.exceptions.CommandSyntaxException;
2020-05-02 18:51:19 +02:00
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
2020-01-27 11:05:22 +01:00
class RaceArgument implements ArgumentType<Race> {
static final Collection<String> EXAMPLES = Arrays.stream(Race.values())
.filter(Race::isUsable)
.map(Race::name)
.collect(Collectors.toList());
@Override
public Race parse(StringReader reader) throws CommandSyntaxException {
return Race.fromName(reader.readUnquotedString(), Race.EARTH);
}
2020-05-02 18:51:19 +02:00
@Override
public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
Arrays.stream(Race.values())
.filter(Race::isUsable)
.map(i -> i.name().toLowerCase())
.filter(i -> i.startsWith(builder.getRemaining().toLowerCase()))
.forEach(i -> builder.suggest(i.toLowerCase()));
return builder.buildFuture();
}
2020-01-27 11:05:22 +01:00
@Override
public Collection<String> getExamples() {
return EXAMPLES;
}
}