| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.brigadier.StringReader;
|
| 5 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 6 | import com.mojang.brigadier.context.CommandContext;
|
| 7 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 8 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 9 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 10 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.List;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import net.minecraft.commands.CommandSourceStack;
|
| 16 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 17 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.stats.Stat;
|
| 20 | import net.minecraft.stats.StatType;
|
| 21 | import net.minecraft.world.scores.criteria.ObjectiveCriteria;
|
| 22 |
|
| 23 | public class ObjectiveCriteriaArgument implements ArgumentType<ObjectiveCriteria> {
|
| 24 | private static final Collection<String> EXAMPLES = Arrays.asList("foo", "foo.bar.baz", "minecraft:foo");
|
| 25 | public static final DynamicCommandExceptionType ERROR_INVALID_VALUE = new DynamicCommandExceptionType(
|
| 26 | value -> Component.translatableEscape("argument.criteria.invalid", value)
|
| 27 | );
|
| 28 |
|
| 29 | private ObjectiveCriteriaArgument() {
|
| 30 | }
|
| 31 |
|
| 32 | public static ObjectiveCriteriaArgument criteria() {
|
| 33 | return new ObjectiveCriteriaArgument();
|
| 34 | }
|
| 35 |
|
| 36 | public static ObjectiveCriteria getCriteria(final CommandContext<CommandSourceStack> context, final String name) {
|
| 37 | return context.getArgument(name, ObjectiveCriteria.class);
|
| 38 | }
|
| 39 |
|
| 40 | public ObjectiveCriteria parse(final StringReader reader) throws CommandSyntaxException {
|
| 41 | int start = reader.getCursor();
|
| 42 |
|
| 43 | while (reader.canRead() && reader.peek() != ' ') {
|
| 44 | reader.skip();
|
| 45 | }
|
| 46 |
|
| 47 | String id = reader.getString().substring(start, reader.getCursor());
|
| 48 | return ObjectiveCriteria.byName(id).orElseThrow(() -> {
|
| 49 | reader.setCursor(start);
|
| 50 | return ERROR_INVALID_VALUE.createWithContext(reader, id);
|
| 51 | });
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 56 | List<String> ids = Lists.newArrayList(ObjectiveCriteria.getCustomCriteriaNames());
|
| 57 |
|
| 58 | for (StatType<?> type : BuiltInRegistries.STAT_TYPE) {
|
| 59 | for (Object value : type.getRegistry()) {
|
| 60 | String name = this.getName(type, value);
|
| 61 | ids.add(name);
|
| 62 | }
|
| 63 | }
|
| 64 |
|
| 65 | return SharedSuggestionProvider.suggest(ids, builder);
|
| 66 | }
|
| 67 |
|
| 68 | public <T> String getName(final StatType<T> type, final Object value) {
|
| 69 | return Stat.buildName(type, (T)value);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public Collection<String> getExamples() {
|
| 74 | return EXAMPLES;
|
| 75 | }
|
| 76 | }
|
| 77 |
|