package net.minecraft.world.level; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList.Builder; import java.util.List; import java.util.UUID; import java.util.function.Predicate; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntitySelector; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.entity.EntityTypeTest; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.shapes.BooleanOp; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import org.jetbrains.annotations.Nullable; public interface EntityGetter { /** * Gets all entities within the specified AABB excluding the one passed into it. */ List getEntities(@Nullable Entity entity, AABB area, Predicate predicate); List getEntities(EntityTypeTest entityTypeTest, AABB bounds, Predicate predicate); default List getEntitiesOfClass(Class entityClass, AABB area, Predicate filter) { return this.getEntities(EntityTypeTest.forClass(entityClass), area, filter); } List players(); /** * Will get all entities within the specified AABB excluding the one passed into it. Args: entityToExclude, aabb */ default List getEntities(@Nullable Entity entity, AABB area) { return this.getEntities(entity, area, EntitySelector.NO_SPECTATORS); } default boolean isUnobstructed(@Nullable Entity entity, VoxelShape shape) { if (shape.isEmpty()) { return true; } else { for (Entity entity2 : this.getEntities(entity, shape.bounds())) { if (!entity2.isRemoved() && entity2.blocksBuilding && (entity == null || !entity2.isPassengerOfSameVehicle(entity)) && Shapes.joinIsNotEmpty(shape, Shapes.create(entity2.getBoundingBox()), BooleanOp.AND)) { return false; } } return true; } } default List getEntitiesOfClass(Class entityClass, AABB area) { return this.getEntitiesOfClass(entityClass, area, EntitySelector.NO_SPECTATORS); } default List getEntityCollisions(@Nullable Entity entity, AABB collisionBox) { if (collisionBox.getSize() < 1.0E-7) { return List.of(); } else { Predicate predicate = entity == null ? EntitySelector.CAN_BE_COLLIDED_WITH : EntitySelector.NO_SPECTATORS.and(entity::canCollideWith); List list = this.getEntities(entity, collisionBox.inflate(1.0E-7), predicate); if (list.isEmpty()) { return List.of(); } else { Builder builder = ImmutableList.builderWithExpectedSize(list.size()); for (Entity entity2 : list) { builder.add(Shapes.create(entity2.getBoundingBox())); } return builder.build(); } } } @Nullable default Player getNearestPlayer(double x, double y, double z, double distance, @Nullable Predicate predicate) { double d = -1.0; Player player = null; for (Player player2 : this.players()) { if (predicate == null || predicate.test(player2)) { double e = player2.distanceToSqr(x, y, z); if ((distance < 0.0 || e < distance * distance) && (d == -1.0 || e < d)) { d = e; player = player2; } } } return player; } @Nullable default Player getNearestPlayer(Entity entity, double distance) { return this.getNearestPlayer(entity.getX(), entity.getY(), entity.getZ(), distance, false); } @Nullable default Player getNearestPlayer(double x, double y, double z, double distance, boolean creativePlayers) { Predicate predicate = creativePlayers ? EntitySelector.NO_CREATIVE_OR_SPECTATOR : EntitySelector.NO_SPECTATORS; return this.getNearestPlayer(x, y, z, distance, predicate); } default boolean hasNearbyAlivePlayer(double x, double y, double z, double distance) { for (Player player : this.players()) { if (EntitySelector.NO_SPECTATORS.test(player) && EntitySelector.LIVING_ENTITY_STILL_ALIVE.test(player)) { double d = player.distanceToSqr(x, y, z); if (distance < 0.0 || d < distance * distance) { return true; } } } return false; } @Nullable default Player getPlayerByUUID(UUID uniqueId) { for (int i = 0; i < this.players().size(); i++) { Player player = (Player)this.players().get(i); if (uniqueId.equals(player.getUUID())) { return player; } } return null; } }