| 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.PlayerTeam;
|
| 17 | import net.minecraft.world.scores.Scoreboard;
|
| 18 |
|
| 19 | public class TeamArgument implements ArgumentType<String> {
|
| 20 | private static final Collection<String> EXAMPLES = Arrays.asList("foo", "123");
|
| 21 | private static final DynamicCommandExceptionType ERROR_TEAM_NOT_FOUND = new DynamicCommandExceptionType(
|
| 22 | name -> Component.translatableEscape("team.notFound", name)
|
| 23 | );
|
| 24 |
|
| 25 | public static TeamArgument team() {
|
| 26 | return new TeamArgument();
|
| 27 | }
|
| 28 |
|
| 29 | public static PlayerTeam getTeam(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 30 | String id = context.getArgument(name, String.class);
|
| 31 | Scoreboard scoreboard = context.getSource().getServer().getScoreboard();
|
| 32 | PlayerTeam team = scoreboard.getPlayerTeam(id);
|
| 33 | if (team == null) {
|
| 34 | throw ERROR_TEAM_NOT_FOUND.create(id);
|
| 35 | } else {
|
| 36 | return team;
|
| 37 | }
|
| 38 | }
|
| 39 |
|
| 40 | public String parse(final StringReader reader) throws CommandSyntaxException {
|
| 41 | return reader.readUnquotedString();
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> contextBuilder, final SuggestionsBuilder builder) {
|
| 46 | return contextBuilder.getSource() instanceof SharedSuggestionProvider
|
| 47 | ? SharedSuggestionProvider.suggest(((SharedSuggestionProvider)contextBuilder.getSource()).getAllTeams(), builder)
|
| 48 | : Suggestions.empty();
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public Collection<String> getExamples() {
|
| 53 | return EXAMPLES;
|
| 54 | }
|
| 55 | }
|
| 56 |
|