| 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.SimpleCommandExceptionType;
|
| 8 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 9 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.Arrays;
|
| 12 | import java.util.Collection;
|
| 13 | import java.util.Collections;
|
| 14 | import java.util.List;
|
| 15 | import java.util.Optional;
|
| 16 | import java.util.concurrent.CompletableFuture;
|
| 17 | import net.minecraft.commands.CommandSourceStack;
|
| 18 | import net.minecraft.commands.SharedSuggestionProvider;
|
| 19 | import net.minecraft.commands.arguments.selector.EntitySelector;
|
| 20 | import net.minecraft.commands.arguments.selector.EntitySelectorParser;
|
| 21 | import net.minecraft.network.chat.Component;
|
| 22 | import net.minecraft.server.level.ServerPlayer;
|
| 23 | import net.minecraft.server.permissions.Permissions;
|
| 24 | import net.minecraft.server.players.NameAndId;
|
| 25 |
|
| 26 | public class GameProfileArgument implements ArgumentType<GameProfileArgument.Result> {
|
| 27 | private static final Collection<String> EXAMPLES = Arrays.asList("Player", "0123", "dd12be42-52a9-4a91-a8a1-11c01849e498", "@e");
|
| 28 | public static final SimpleCommandExceptionType ERROR_UNKNOWN_PLAYER = new SimpleCommandExceptionType(Component.translatable("argument.player.unknown"));
|
| 29 |
|
| 30 | public static Collection<NameAndId> getGameProfiles(final CommandContext<CommandSourceStack> source, final String name) throws CommandSyntaxException {
|
| 31 | return source.getArgument(name, GameProfileArgument.Result.class).getNames(source.getSource());
|
| 32 | }
|
| 33 |
|
| 34 | public static GameProfileArgument gameProfile() {
|
| 35 | return new GameProfileArgument();
|
| 36 | }
|
| 37 |
|
| 38 | public <S> GameProfileArgument.Result parse(final StringReader reader, final S source) throws CommandSyntaxException {
|
| 39 | return parse(reader, EntitySelectorParser.allowSelectors(source));
|
| 40 | }
|
| 41 |
|
| 42 | public GameProfileArgument.Result parse(final StringReader reader) throws CommandSyntaxException {
|
| 43 | return parse(reader, true);
|
| 44 | }
|
| 45 |
|
| 46 | private static GameProfileArgument.Result parse(final StringReader reader, final boolean allowSelectors) throws CommandSyntaxException {
|
| 47 | if (reader.canRead() && reader.peek() == '@') {
|
| 48 | EntitySelectorParser parser = new EntitySelectorParser(reader, allowSelectors);
|
| 49 | EntitySelector parse = parser.parse();
|
| 50 | if (parse.includesEntities()) {
|
| 51 | throw EntityArgument.ERROR_ONLY_PLAYERS_ALLOWED.createWithContext(reader);
|
| 52 | } else {
|
| 53 | return new GameProfileArgument.SelectorResult(parse);
|
| 54 | }
|
| 55 | } else {
|
| 56 | int start = reader.getCursor();
|
| 57 |
|
| 58 | while (reader.canRead() && reader.peek() != ' ') {
|
| 59 | reader.skip();
|
| 60 | }
|
| 61 |
|
| 62 | String name = reader.getString().substring(start, reader.getCursor());
|
| 63 | return c -> {
|
| 64 | Optional<NameAndId> result = c.getServer().services().nameToIdCache().get(name);
|
| 65 | return Collections.singleton(result.orElseThrow(ERROR_UNKNOWN_PLAYER::create));
|
| 66 | };
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> contextBuilder, final SuggestionsBuilder builder) {
|
| 72 | if (contextBuilder.getSource() instanceof SharedSuggestionProvider source) {
|
| 73 | StringReader reader = new StringReader(builder.getInput());
|
| 74 | reader.setCursor(builder.getStart());
|
| 75 | EntitySelectorParser parser = new EntitySelectorParser(reader, source.permissions().hasPermission(Permissions.COMMANDS_ENTITY_SELECTORS));
|
| 76 |
|
| 77 | try {
|
| 78 | parser.parse();
|
| 79 | } catch (CommandSyntaxException var7) {
|
| 80 | }
|
| 81 |
|
| 82 | return parser.fillSuggestions(builder, suggestions -> SharedSuggestionProvider.suggest(source.getOnlinePlayerNames(), suggestions));
|
| 83 | } else {
|
| 84 | return Suggestions.empty();
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public Collection<String> getExamples() {
|
| 90 | return EXAMPLES;
|
| 91 | }
|
| 92 |
|
| 93 | @FunctionalInterface
|
| 94 | public interface Result {
|
| 95 | Collection<NameAndId> getNames(final CommandSourceStack sender) throws CommandSyntaxException;
|
| 96 | }
|
| 97 |
|
| 98 | public static class SelectorResult implements GameProfileArgument.Result {
|
| 99 | private final EntitySelector selector;
|
| 100 |
|
| 101 | public SelectorResult(final EntitySelector selector) {
|
| 102 | this.selector = selector;
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public Collection<NameAndId> getNames(final CommandSourceStack sender) throws CommandSyntaxException {
|
| 107 | List<ServerPlayer> players = this.selector.findPlayers(sender);
|
| 108 | if (players.isEmpty()) {
|
| 109 | throw EntityArgument.NO_PLAYERS_FOUND.create();
|
| 110 | }
|
| 111 |
|
| 112 | List<NameAndId> result = new ArrayList<>();
|
| 113 |
|
| 114 | for (ServerPlayer entity : players) {
|
| 115 | result.add(entity.nameAndId());
|
| 116 | }
|
| 117 |
|
| 118 | return result;
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|