| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.google.common.collect.ImmutableList.Builder;
|
| 5 | import java.util.List;
|
| 6 | import java.util.UUID;
|
| 7 | import java.util.function.Predicate;
|
| 8 | import net.minecraft.world.entity.Entity;
|
| 9 | import net.minecraft.world.entity.EntitySelector;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.level.entity.EntityTypeTest;
|
| 12 | import net.minecraft.world.phys.AABB;
|
| 13 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 14 | import net.minecraft.world.phys.shapes.Shapes;
|
| 15 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | public interface EntityGetter {
|
| 19 | List<Entity> getEntities(@Nullable Entity except, AABB bb, Predicate<? super Entity> selector);
|
| 20 |
|
| 21 | <T extends Entity> List<T> getEntities(final EntityTypeTest<Entity, T> type, final AABB bb, final Predicate<? super T> selector);
|
| 22 |
|
| 23 | default <T extends Entity> List<T> getEntitiesOfClass(final Class<T> baseClass, final AABB bb, final Predicate<? super T> selector) {
|
| 24 | return this.getEntities(EntityTypeTest.forClass(baseClass), bb, selector);
|
| 25 | }
|
| 26 |
|
| 27 | List<? extends Player> players();
|
| 28 |
|
| 29 | default List<Entity> getEntities(final @Nullable Entity except, final AABB bb) {
|
| 30 | return this.getEntities(except, bb, EntitySelector.NO_SPECTATORS);
|
| 31 | }
|
| 32 |
|
| 33 | default boolean isUnobstructed(final @Nullable Entity source, final VoxelShape shape) {
|
| 34 | if (shape.isEmpty()) {
|
| 35 | return true;
|
| 36 | }
|
| 37 |
|
| 38 | for (Entity entity : this.getEntities(source, shape.bounds())) {
|
| 39 | if (!entity.isRemoved()
|
| 40 | && entity.blocksBuilding
|
| 41 | && (source == null || !entity.isPassengerOfSameVehicle(source))
|
| 42 | && Shapes.joinIsNotEmpty(shape, Shapes.create(entity.getBoundingBox()), BooleanOp.AND)) {
|
| 43 | return false;
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | return true;
|
| 48 | }
|
| 49 |
|
| 50 | default <T extends Entity> List<T> getEntitiesOfClass(final Class<T> baseClass, final AABB bb) {
|
| 51 | return this.getEntitiesOfClass(baseClass, bb, EntitySelector.NO_SPECTATORS);
|
| 52 | }
|
| 53 |
|
| 54 | default List<VoxelShape> getEntityCollisions(final @Nullable Entity source, final AABB testArea) {
|
| 55 | if (testArea.getSize() < 1.0E-7) {
|
| 56 | return List.of();
|
| 57 | }
|
| 58 |
|
| 59 | Predicate<Entity> canCollide = source == null ? EntitySelector.CAN_BE_COLLIDED_WITH : EntitySelector.NO_SPECTATORS.and(source::canCollideWith);
|
| 60 | List<Entity> collidingEntities = this.getEntities(source, testArea.inflate(1.0E-7), canCollide);
|
| 61 | if (collidingEntities.isEmpty()) {
|
| 62 | return List.of();
|
| 63 | }
|
| 64 |
|
| 65 | Builder<VoxelShape> shapes = ImmutableList.builderWithExpectedSize(collidingEntities.size());
|
| 66 |
|
| 67 | for (Entity entity : collidingEntities) {
|
| 68 | shapes.add(Shapes.create(entity.getBoundingBox()));
|
| 69 | }
|
| 70 |
|
| 71 | return shapes.build();
|
| 72 | }
|
| 73 |
|
| 74 | default @Nullable Player getNearestPlayer(final double x, final double y, final double z, final double range, final @Nullable Predicate<Entity> predicate) {
|
| 75 | double best = -1.0;
|
| 76 | Player result = null;
|
| 77 |
|
| 78 | for (Player player : this.players()) {
|
| 79 | if (predicate == null || predicate.test(player)) {
|
| 80 | double dist = player.distanceToSqr(x, y, z);
|
| 81 | if ((range < 0.0 || dist < range * range) && (best == -1.0 || dist < best)) {
|
| 82 | best = dist;
|
| 83 | result = player;
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | return result;
|
| 89 | }
|
| 90 |
|
| 91 | default @Nullable Player getNearestPlayer(final Entity source, final double maxDist) {
|
| 92 | return this.getNearestPlayer(source.getX(), source.getY(), source.getZ(), maxDist, false);
|
| 93 | }
|
| 94 |
|
| 95 | default @Nullable Player getNearestPlayer(final double x, final double y, final double z, final double maxDist, final boolean filterOutCreative) {
|
| 96 | Predicate<Entity> predicate = filterOutCreative ? EntitySelector.NO_CREATIVE_OR_SPECTATOR : EntitySelector.NO_SPECTATORS;
|
| 97 | return this.getNearestPlayer(x, y, z, maxDist, predicate);
|
| 98 | }
|
| 99 |
|
| 100 | default boolean hasNearbyAlivePlayer(final double x, final double y, final double z, final double range) {
|
| 101 | for (Player player : this.players()) {
|
| 102 | if (EntitySelector.NO_SPECTATORS.test(player) && EntitySelector.LIVING_ENTITY_STILL_ALIVE.test(player)) {
|
| 103 | double playerDist = player.distanceToSqr(x, y, z);
|
| 104 | if (range < 0.0 || playerDist < range * range) {
|
| 105 | return true;
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | return false;
|
| 111 | }
|
| 112 |
|
| 113 | default @Nullable Player getPlayerByUUID(final UUID uuid) {
|
| 114 | for (int i = 0; i < this.players().size(); i++) {
|
| 115 | Player player = this.players().get(i);
|
| 116 | if (uuid.equals(player.getUUID())) {
|
| 117 | return player;
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | return null;
|
| 122 | }
|
| 123 | }
|
| 124 |
|