| 1 | package net.minecraft.commands.arguments.selector;
|
| 2 |
|
| 3 | import com.mojang.brigadier.StringReader;
|
| 4 | import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 7 | import java.util.List;
|
| 8 | import java.util.UUID;
|
| 9 | import java.util.function.BiConsumer;
|
| 10 | import java.util.function.Function;
|
| 11 | import java.util.function.Predicate;
|
| 12 | import net.minecraft.advancements.predicates.MinMaxBounds;
|
| 13 | import net.minecraft.commands.CommandSourceStack;
|
| 14 | import net.minecraft.commands.arguments.EntityArgument;
|
| 15 | import net.minecraft.network.chat.Component;
|
| 16 | import net.minecraft.network.chat.ComponentUtils;
|
| 17 | import net.minecraft.server.level.ServerLevel;
|
| 18 | import net.minecraft.server.level.ServerPlayer;
|
| 19 | import net.minecraft.server.permissions.Permissions;
|
| 20 | import net.minecraft.util.CompilableString;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import net.minecraft.world.entity.Entity;
|
| 23 | import net.minecraft.world.entity.EntityType;
|
| 24 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 25 | import net.minecraft.world.level.entity.EntityTypeTest;
|
| 26 | import net.minecraft.world.phys.AABB;
|
| 27 | import net.minecraft.world.phys.Vec3;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | public class EntitySelector {
|
| 31 | public static final int INFINITE = Integer.MAX_VALUE;
|
| 32 | public static final BiConsumer<Vec3, List<? extends Entity>> ORDER_ARBITRARY = (p, c) -> {};
|
| 33 | private static final EntityTypeTest<Entity, ?> ANY_TYPE = new EntityTypeTest<Entity, Entity>() {
|
| 34 | public Entity tryCast(final Entity entity) {
|
| 35 | return entity;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public Class<? extends Entity> getBaseClass() {
|
| 40 | return Entity.class;
|
| 41 | }
|
| 42 | };
|
| 43 | public static final Codec<CompilableString<EntitySelector>> COMPILABLE_CODEC = CompilableString.codec(
|
| 44 | new CompilableString.CommandParserHelper<EntitySelector>() {
|
| 45 | protected EntitySelector parse(final StringReader reader) throws CommandSyntaxException {
|
| 46 | return new EntitySelectorParser(reader, true).parse();
|
| 47 | }
|
| 48 |
|
| 49 | @Override
|
| 50 | protected String errorMessage(final String original, final CommandSyntaxException exception) {
|
| 51 | return "Invalid selector component: " + original + ": " + exception.getMessage();
|
| 52 | }
|
| 53 | }
|
| 54 | );
|
| 55 | private final int maxResults;
|
| 56 | private final boolean includesEntities;
|
| 57 | private final boolean worldLimited;
|
| 58 | private final List<Predicate<Entity>> contextFreePredicates;
|
| 59 | private final MinMaxBounds.@Nullable Doubles range;
|
| 60 | private final Function<Vec3, Vec3> position;
|
| 61 | private final @Nullable AABB aabb;
|
| 62 | private final BiConsumer<Vec3, List<? extends Entity>> order;
|
| 63 | private final boolean currentEntity;
|
| 64 | private final @Nullable String playerName;
|
| 65 | private final @Nullable UUID entityUUID;
|
| 66 | private final EntityTypeTest<Entity, ?> type;
|
| 67 | private final boolean usesSelector;
|
| 68 |
|
| 69 | public EntitySelector(
|
| 70 | final int maxResults,
|
| 71 | final boolean includesEntities,
|
| 72 | final boolean worldLimited,
|
| 73 | final List<Predicate<Entity>> contextFreePredicates,
|
| 74 | final MinMaxBounds.@Nullable Doubles range,
|
| 75 | final Function<Vec3, Vec3> position,
|
| 76 | final @Nullable AABB aabb,
|
| 77 | final BiConsumer<Vec3, List<? extends Entity>> order,
|
| 78 | final boolean currentEntity,
|
| 79 | final @Nullable String playerName,
|
| 80 | final @Nullable UUID entityUUID,
|
| 81 | final @Nullable EntityType<?> type,
|
| 82 | final boolean usesSelector
|
| 83 | ) {
|
| 84 | this.maxResults = maxResults;
|
| 85 | this.includesEntities = includesEntities;
|
| 86 | this.worldLimited = worldLimited;
|
| 87 | this.contextFreePredicates = contextFreePredicates;
|
| 88 | this.range = range;
|
| 89 | this.position = position;
|
| 90 | this.aabb = aabb;
|
| 91 | this.order = order;
|
| 92 | this.currentEntity = currentEntity;
|
| 93 | this.playerName = playerName;
|
| 94 | this.entityUUID = entityUUID;
|
| 95 | this.type = type == null ? ANY_TYPE : type;
|
| 96 | this.usesSelector = usesSelector;
|
| 97 | }
|
| 98 |
|
| 99 | public int getMaxResults() {
|
| 100 | return this.maxResults;
|
| 101 | }
|
| 102 |
|
| 103 | public boolean includesEntities() {
|
| 104 | return this.includesEntities;
|
| 105 | }
|
| 106 |
|
| 107 | public boolean isSelfSelector() {
|
| 108 | return this.currentEntity;
|
| 109 | }
|
| 110 |
|
| 111 | public boolean isWorldLimited() {
|
| 112 | return this.worldLimited;
|
| 113 | }
|
| 114 |
|
| 115 | public boolean usesSelector() {
|
| 116 | return this.usesSelector;
|
| 117 | }
|
| 118 |
|
| 119 | private void checkPermissions(final CommandSourceStack sender) throws CommandSyntaxException {
|
| 120 | if (this.usesSelector && !sender.permissions().hasPermission(Permissions.COMMANDS_ENTITY_SELECTORS)) {
|
| 121 | throw EntityArgument.ERROR_SELECTORS_NOT_ALLOWED.create();
|
| 122 | }
|
| 123 | }
|
| 124 |
|
| 125 | public Entity findSingleEntity(final CommandSourceStack sender) throws CommandSyntaxException {
|
| 126 | this.checkPermissions(sender);
|
| 127 | List<? extends Entity> entities = this.findEntities(sender);
|
| 128 | if (entities.isEmpty()) {
|
| 129 | throw EntityArgument.NO_ENTITIES_FOUND.create();
|
| 130 | } else if (entities.size() > 1) {
|
| 131 | throw EntityArgument.ERROR_NOT_SINGLE_ENTITY.create();
|
| 132 | } else {
|
| 133 | return entities.get(0);
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public List<? extends Entity> findEntities(final CommandSourceStack sender) throws CommandSyntaxException {
|
| 138 | this.checkPermissions(sender);
|
| 139 | if (!this.includesEntities) {
|
| 140 | return this.findPlayers(sender);
|
| 141 | }
|
| 142 |
|
| 143 | if (this.playerName != null) {
|
| 144 | ServerPlayer result = sender.getServer().getPlayerList().getPlayerByName(this.playerName);
|
| 145 | return result == null ? List.of() : List.of(result);
|
| 146 | }
|
| 147 |
|
| 148 | if (this.entityUUID != null) {
|
| 149 | for (ServerLevel level : sender.getServer().getAllLevels()) {
|
| 150 | Entity entity = level.getEntity(this.entityUUID);
|
| 151 | if (entity != null) {
|
| 152 | if (entity.getType().isEnabled(sender.enabledFeatures())) {
|
| 153 | return List.of(entity);
|
| 154 | }
|
| 155 | break;
|
| 156 | }
|
| 157 | }
|
| 158 |
|
| 159 | return List.of();
|
| 160 | } else {
|
| 161 | Vec3 pos = this.position.apply(sender.getPosition());
|
| 162 | AABB absoluteAabb = this.getAbsoluteAabb(pos);
|
| 163 | if (this.currentEntity) {
|
| 164 | Predicate<Entity> predicate = this.getPredicate(pos, absoluteAabb, null);
|
| 165 | return sender.getEntity() != null && predicate.test(sender.getEntity()) ? List.of(sender.getEntity()) : List.of();
|
| 166 | }
|
| 167 |
|
| 168 | Predicate<Entity> predicate = this.getPredicate(pos, absoluteAabb, sender.enabledFeatures());
|
| 169 | List<Entity> result = new ObjectArrayList<>();
|
| 170 | if (this.isWorldLimited()) {
|
| 171 | this.addEntities(result, sender.getLevel(), absoluteAabb, predicate);
|
| 172 | } else {
|
| 173 | for (ServerLevel level : sender.getServer().getAllLevels()) {
|
| 174 | this.addEntities(result, level, absoluteAabb, predicate);
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | return this.sortAndLimit(pos, result);
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | private void addEntities(final List<Entity> result, final ServerLevel level, final @Nullable AABB absoluteAABB, final Predicate<Entity> predicate) {
|
| 183 | int limit = this.getResultLimit();
|
| 184 | if (result.size() < limit) {
|
| 185 | if (absoluteAABB != null) {
|
| 186 | level.getEntities(this.type, absoluteAABB, predicate, result, limit);
|
| 187 | } else {
|
| 188 | level.getEntities(this.type, predicate, result, limit);
|
| 189 | }
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | private int getResultLimit() {
|
| 194 | return this.order == ORDER_ARBITRARY ? this.maxResults : Integer.MAX_VALUE;
|
| 195 | }
|
| 196 |
|
| 197 | public ServerPlayer findSinglePlayer(final CommandSourceStack sender) throws CommandSyntaxException {
|
| 198 | this.checkPermissions(sender);
|
| 199 | List<ServerPlayer> players = this.findPlayers(sender);
|
| 200 | if (players.size() != 1) {
|
| 201 | throw EntityArgument.NO_PLAYERS_FOUND.create();
|
| 202 | } else {
|
| 203 | return players.get(0);
|
| 204 | }
|
| 205 | }
|
| 206 |
|
| 207 | public List<ServerPlayer> findPlayers(final CommandSourceStack sender) throws CommandSyntaxException {
|
| 208 | this.checkPermissions(sender);
|
| 209 | if (this.playerName != null) {
|
| 210 | ServerPlayer result = sender.getServer().getPlayerList().getPlayerByName(this.playerName);
|
| 211 | return result == null ? List.of() : List.of(result);
|
| 212 | }
|
| 213 |
|
| 214 | if (this.entityUUID != null) {
|
| 215 | ServerPlayer result = sender.getServer().getPlayerList().getPlayer(this.entityUUID);
|
| 216 | return result == null ? List.of() : List.of(result);
|
| 217 | }
|
| 218 |
|
| 219 | Vec3 pos = this.position.apply(sender.getPosition());
|
| 220 | AABB absoluteAabb = this.getAbsoluteAabb(pos);
|
| 221 | Predicate<Entity> predicate = this.getPredicate(pos, absoluteAabb, null);
|
| 222 | if (this.currentEntity) {
|
| 223 | return sender.getEntity() instanceof ServerPlayer player && predicate.test(player) ? List.of(player) : List.of();
|
| 224 | }
|
| 225 |
|
| 226 | int limit = this.getResultLimit();
|
| 227 | List<ServerPlayer> result;
|
| 228 | if (this.isWorldLimited()) {
|
| 229 | result = sender.getLevel().getPlayers(predicate, limit);
|
| 230 | } else {
|
| 231 | result = new ObjectArrayList<>();
|
| 232 |
|
| 233 | for (ServerPlayer player : sender.getServer().getPlayerList().getPlayers()) {
|
| 234 | if (predicate.test(player)) {
|
| 235 | result.add(player);
|
| 236 | if (result.size() >= limit) {
|
| 237 | return result;
|
| 238 | }
|
| 239 | }
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | return this.sortAndLimit(pos, result);
|
| 244 | }
|
| 245 |
|
| 246 | private @Nullable AABB getAbsoluteAabb(final Vec3 pos) {
|
| 247 | return this.aabb != null ? this.aabb.move(pos) : null;
|
| 248 | }
|
| 249 |
|
| 250 | private Predicate<Entity> getPredicate(final Vec3 pos, final @Nullable AABB absoluteAabb, final @Nullable FeatureFlagSet enabledFeatures) {
|
| 251 | boolean filterFeatures = enabledFeatures != null;
|
| 252 | boolean filterAabb = absoluteAabb != null;
|
| 253 | boolean filterRange = this.range != null;
|
| 254 | int extraCount = (filterFeatures ? 1 : 0) + (filterAabb ? 1 : 0) + (filterRange ? 1 : 0);
|
| 255 | List<Predicate<Entity>> completePredicates;
|
| 256 | if (extraCount == 0) {
|
| 257 | completePredicates = this.contextFreePredicates;
|
| 258 | } else {
|
| 259 | List<Predicate<Entity>> predicates = new ObjectArrayList<>(this.contextFreePredicates.size() + extraCount);
|
| 260 | predicates.addAll(this.contextFreePredicates);
|
| 261 | if (filterFeatures) {
|
| 262 | predicates.add(e -> e.getType().isEnabled(enabledFeatures));
|
| 263 | }
|
| 264 |
|
| 265 | if (filterAabb) {
|
| 266 | predicates.add(e -> absoluteAabb.intersects(e.getBoundingBox()));
|
| 267 | }
|
| 268 |
|
| 269 | if (filterRange) {
|
| 270 | predicates.add(e -> this.range.matchesSqr(e.distanceToSqr(pos)));
|
| 271 | }
|
| 272 |
|
| 273 | completePredicates = predicates;
|
| 274 | }
|
| 275 |
|
| 276 | return Util.allOf(completePredicates);
|
| 277 | }
|
| 278 |
|
| 279 | private <T extends Entity> List<T> sortAndLimit(final Vec3 pos, final List<T> result) {
|
| 280 | if (result.size() > 1) {
|
| 281 | this.order.accept(pos, result);
|
| 282 | }
|
| 283 |
|
| 284 | return result.subList(0, Math.min(this.maxResults, result.size()));
|
| 285 | }
|
| 286 |
|
| 287 | public static Component joinNames(final List<? extends Entity> entities) {
|
| 288 | return ComponentUtils.formatList(entities, Entity::getDisplayName);
|
| 289 | }
|
| 290 | }
|
| 291 |
|