| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 5 | import com.mojang.brigadier.context.CommandContext;
|
| 6 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 7 | import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
|
| 8 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 9 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 10 | import java.util.Arrays;
|
| 11 | import java.util.Collection;
|
| 12 | import java.util.concurrent.CompletableFuture;
|
| 13 | import net.minecraft.commands.CommandSourceStack;
|
| 14 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.world.scores.Objective;
|
| 17 | import net.minecraft.world.scores.Scoreboard;
|
| 18 |
|
| 19 | public class ObjectiveArgument implements ArgumentType<String> {
|
| 20 | private static final Collection<String> EXAMPLES = Arrays.asList("foo", "*", "012");
|
| 21 | private static final DynamicCommandExceptionType ERROR_OBJECTIVE_NOT_FOUND = new DynamicCommandExceptionType(
|
| 22 | name -> Component.translatableEscape("arguments.objective.notFound", name)
|
| 23 | );
|
| 24 | private static final DynamicCommandExceptionType ERROR_OBJECTIVE_READ_ONLY = new DynamicCommandExceptionType(
|
| 25 | name -> Component.translatableEscape("arguments.objective.readonly", name)
|
| 26 | );
|
| 27 |
|
| 28 | public static ObjectiveArgument objective() {
|
| 29 | return new ObjectiveArgument();
|
| 30 | }
|
| 31 |
|
| 32 | public static Objective getObjective(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 33 | String id = context.getArgument(name, String.class);
|
| 34 | Scoreboard scoreboard = context.getSource().getServer().getScoreboard();
|
| 35 | Objective objective = scoreboard.getObjective(id);
|
| 36 | if (objective == null) {
|
| 37 | throw ERROR_OBJECTIVE_NOT_FOUND.create(id);
|
| 38 | } else {
|
| 39 | return objective;
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | public static Objective getWritableObjective(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 44 | Objective objective = getObjective(context, name);
|
| 45 | if (objective.getCriteria().isReadOnly()) {
|
| 46 | throw ERROR_OBJECTIVE_READ_ONLY.create(objective.getName());
|
| 47 | } else {
|
| 48 | return objective;
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | public String parse(final StringReader reader) throws CommandSyntaxException {
|
| 53 | return reader.readUnquotedString();
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> context, final SuggestionsBuilder builder) {
|
| 58 | S rawSource = context.getSource();
|
| 59 | if (rawSource instanceof CommandSourceStack source) {
|
| 60 | return SharedSuggestionProvider.suggest(source.getServer().getScoreboard().getObjectiveNames(), builder);
|
| 61 | } else {
|
| 62 | return rawSource instanceof SharedSuggestionProvider source ? source.customSuggestion(context) : Suggestions.empty();
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public Collection<String> getExamples() {
|
| 68 | return EXAMPLES;
|
| 69 | }
|
| 70 | }
|
| 71 |
|