| 1 | package net.minecraft.commands.arguments;
|
| 2 |
|
| 3 | import com.google.common.collect.Iterables;
|
| 4 | import com.google.gson.JsonObject;
|
| 5 | import com.mojang.brigadier.StringReader;
|
| 6 | import com.mojang.brigadier.arguments.ArgumentType;
|
| 7 | import com.mojang.brigadier.context.CommandContext;
|
| 8 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 9 | import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
| 10 | import com.mojang.brigadier.suggestion.Suggestions;
|
| 11 | import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
| 12 | import java.util.Arrays;
|
| 13 | import java.util.Collection;
|
| 14 | import java.util.List;
|
| 15 | import java.util.concurrent.CompletableFuture;
|
| 16 | import net.minecraft.commands.CommandBuildContext;
|
| 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.commands.synchronization.ArgumentTypeInfo;
|
| 22 | import net.minecraft.network.FriendlyByteBuf;
|
| 23 | import net.minecraft.network.chat.Component;
|
| 24 | import net.minecraft.server.level.ServerPlayer;
|
| 25 | import net.minecraft.server.permissions.Permissions;
|
| 26 | import net.minecraft.world.entity.Entity;
|
| 27 |
|
| 28 | public class EntityArgument implements ArgumentType<EntitySelector> {
|
| 29 | private static final Collection<String> EXAMPLES = Arrays.asList("Player", "0123", "@e", "@e[type=foo]", "dd12be42-52a9-4a91-a8a1-11c01849e498");
|
| 30 | public static final SimpleCommandExceptionType ERROR_NOT_SINGLE_ENTITY = new SimpleCommandExceptionType(Component.translatable("argument.entity.toomany"));
|
| 31 | public static final SimpleCommandExceptionType ERROR_NOT_SINGLE_PLAYER = new SimpleCommandExceptionType(Component.translatable("argument.player.toomany"));
|
| 32 | public static final SimpleCommandExceptionType ERROR_ONLY_PLAYERS_ALLOWED = new SimpleCommandExceptionType(Component.translatable("argument.player.entities"));
|
| 33 | public static final SimpleCommandExceptionType NO_ENTITIES_FOUND = new SimpleCommandExceptionType(Component.translatable("argument.entity.notfound.entity"));
|
| 34 | public static final SimpleCommandExceptionType NO_PLAYERS_FOUND = new SimpleCommandExceptionType(Component.translatable("argument.entity.notfound.player"));
|
| 35 | public static final SimpleCommandExceptionType ERROR_SELECTORS_NOT_ALLOWED = new SimpleCommandExceptionType(
|
| 36 | Component.translatable("argument.entity.selector.not_allowed")
|
| 37 | );
|
| 38 | private final boolean single;
|
| 39 | private final boolean playersOnly;
|
| 40 |
|
| 41 | protected EntityArgument(final boolean single, final boolean playersOnly) {
|
| 42 | this.single = single;
|
| 43 | this.playersOnly = playersOnly;
|
| 44 | }
|
| 45 |
|
| 46 | public static EntityArgument entity() {
|
| 47 | return new EntityArgument(true, false);
|
| 48 | }
|
| 49 |
|
| 50 | public static Entity getEntity(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 51 | return context.getArgument(name, EntitySelector.class).findSingleEntity(context.getSource());
|
| 52 | }
|
| 53 |
|
| 54 | public static EntityArgument entities() {
|
| 55 | return new EntityArgument(false, false);
|
| 56 | }
|
| 57 |
|
| 58 | public static Collection<? extends Entity> getEntities(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 59 | Collection<? extends Entity> result = getOptionalEntities(context, name);
|
| 60 | if (result.isEmpty()) {
|
| 61 | throw NO_ENTITIES_FOUND.create();
|
| 62 | } else {
|
| 63 | return result;
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | public static Collection<? extends Entity> getOptionalEntities(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 68 | return context.getArgument(name, EntitySelector.class).findEntities(context.getSource());
|
| 69 | }
|
| 70 |
|
| 71 | public static Collection<ServerPlayer> getOptionalPlayers(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 72 | return context.getArgument(name, EntitySelector.class).findPlayers(context.getSource());
|
| 73 | }
|
| 74 |
|
| 75 | public static EntityArgument player() {
|
| 76 | return new EntityArgument(true, true);
|
| 77 | }
|
| 78 |
|
| 79 | public static ServerPlayer getPlayer(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 80 | return context.getArgument(name, EntitySelector.class).findSinglePlayer(context.getSource());
|
| 81 | }
|
| 82 |
|
| 83 | public static EntityArgument players() {
|
| 84 | return new EntityArgument(false, true);
|
| 85 | }
|
| 86 |
|
| 87 | public static Collection<ServerPlayer> getPlayers(final CommandContext<CommandSourceStack> context, final String name) throws CommandSyntaxException {
|
| 88 | List<ServerPlayer> players = context.getArgument(name, EntitySelector.class).findPlayers(context.getSource());
|
| 89 | if (players.isEmpty()) {
|
| 90 | throw NO_PLAYERS_FOUND.create();
|
| 91 | } else {
|
| 92 | return players;
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | public EntitySelector parse(final StringReader reader) throws CommandSyntaxException {
|
| 97 | return this.parse(reader, true);
|
| 98 | }
|
| 99 |
|
| 100 | public <S> EntitySelector parse(final StringReader reader, final S source) throws CommandSyntaxException {
|
| 101 | return this.parse(reader, EntitySelectorParser.allowSelectors(source));
|
| 102 | }
|
| 103 |
|
| 104 | private EntitySelector parse(final StringReader reader, final boolean allowSelectors) throws CommandSyntaxException {
|
| 105 | int start = 0;
|
| 106 | EntitySelectorParser parser = new EntitySelectorParser(reader, allowSelectors);
|
| 107 | EntitySelector selector = parser.parse();
|
| 108 | if (selector.getMaxResults() > 1 && this.single) {
|
| 109 | if (this.playersOnly) {
|
| 110 | reader.setCursor(0);
|
| 111 | throw ERROR_NOT_SINGLE_PLAYER.createWithContext(reader);
|
| 112 | } else {
|
| 113 | reader.setCursor(0);
|
| 114 | throw ERROR_NOT_SINGLE_ENTITY.createWithContext(reader);
|
| 115 | }
|
| 116 | } else if (selector.includesEntities() && this.playersOnly && !selector.isSelfSelector()) {
|
| 117 | reader.setCursor(0);
|
| 118 | throw ERROR_ONLY_PLAYERS_ALLOWED.createWithContext(reader);
|
| 119 | } else {
|
| 120 | return selector;
|
| 121 | }
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | public <S> CompletableFuture<Suggestions> listSuggestions(final CommandContext<S> contextBuilder, final SuggestionsBuilder builder) {
|
| 126 | if (contextBuilder.getSource() instanceof SharedSuggestionProvider source) {
|
| 127 | StringReader reader = new StringReader(builder.getInput());
|
| 128 | reader.setCursor(builder.getStart());
|
| 129 | EntitySelectorParser parser = new EntitySelectorParser(reader, source.permissions().hasPermission(Permissions.COMMANDS_ENTITY_SELECTORS));
|
| 130 |
|
| 131 | try {
|
| 132 | parser.parse();
|
| 133 | } catch (CommandSyntaxException var7) {
|
| 134 | }
|
| 135 |
|
| 136 | return parser.fillSuggestions(builder, suggestions -> {
|
| 137 | Collection<String> onlinePlayerNames = source.getOnlinePlayerNames();
|
| 138 | Iterable<String> suggestedNames = this.playersOnly ? onlinePlayerNames : Iterables.concat(onlinePlayerNames, source.getSelectedEntities());
|
| 139 | SharedSuggestionProvider.suggest(suggestedNames, suggestions);
|
| 140 | });
|
| 141 | } else {
|
| 142 | return Suggestions.empty();
|
| 143 | }
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public Collection<String> getExamples() {
|
| 148 | return EXAMPLES;
|
| 149 | }
|
| 150 |
|
| 151 | public static class Info implements ArgumentTypeInfo<EntityArgument, EntityArgument.Info.Template> {
|
| 152 | private static final byte FLAG_SINGLE = 1;
|
| 153 | private static final byte FLAG_PLAYERS_ONLY = 2;
|
| 154 |
|
| 155 | public void serializeToNetwork(final EntityArgument.Info.Template template, final FriendlyByteBuf out) {
|
| 156 | int flags = 0;
|
| 157 | if (template.single) {
|
| 158 | flags |= 1;
|
| 159 | }
|
| 160 |
|
| 161 | if (template.playersOnly) {
|
| 162 | flags |= 2;
|
| 163 | }
|
| 164 |
|
| 165 | out.writeByte(flags);
|
| 166 | }
|
| 167 |
|
| 168 | public EntityArgument.Info.Template deserializeFromNetwork(final FriendlyByteBuf in) {
|
| 169 | byte flags = in.readByte();
|
| 170 | return new EntityArgument.Info.Template((flags & 1) != 0, (flags & 2) != 0);
|
| 171 | }
|
| 172 |
|
| 173 | public void serializeToJson(final EntityArgument.Info.Template template, final JsonObject out) {
|
| 174 | out.addProperty("amount", template.single ? "single" : "multiple");
|
| 175 | out.addProperty("type", template.playersOnly ? "players" : "entities");
|
| 176 | }
|
| 177 |
|
| 178 | public EntityArgument.Info.Template unpack(final EntityArgument argument) {
|
| 179 | return new EntityArgument.Info.Template(argument.single, argument.playersOnly);
|
| 180 | }
|
| 181 |
|
| 182 | public final class Template implements ArgumentTypeInfo.Template<EntityArgument> {
|
| 183 | private final boolean single;
|
| 184 | private final boolean playersOnly;
|
| 185 |
|
| 186 | private Template(final boolean single, final boolean playersOnly) {
|
| 187 | this.single = single;
|
| 188 | this.playersOnly = playersOnly;
|
| 189 | }
|
| 190 |
|
| 191 | public EntityArgument instantiate(final CommandBuildContext context) {
|
| 192 | return new EntityArgument(this.single, this.playersOnly);
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public ArgumentTypeInfo<EntityArgument, ?> type() {
|
| 197 | return Info.this;
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 | }
|
| 202 |
|