package net.minecraft.server.level; import java.util.ArrayList; import java.util.List; import net.minecraft.tags.TagKey; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.ai.targeting.TargetingConditions; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.EntityGetter; import net.minecraft.world.phys.AABB; import org.jspecify.annotations.Nullable; public interface ServerEntityGetter extends EntityGetter { ServerLevel getLevel(); @Nullable default Player getNearestPlayer(final TargetingConditions targetConditions, final LivingEntity source) { return this.getNearestEntity(this.players(), targetConditions, source, source.getX(), source.getY(), source.getZ()); } @Nullable default Player getNearestPlayer(final TargetingConditions targetConditions, final LivingEntity source, final double x, final double y, final double z) { return this.getNearestEntity(this.players(), targetConditions, source, x, y, z); } @Nullable default Player getNearestPlayer(final TargetingConditions targetConditions, final double x, final double y, final double z) { return this.getNearestEntity(this.players(), targetConditions, null, x, y, z); } @Nullable default T getNearestEntity( final Class type, final TargetingConditions targetConditions, @Nullable final LivingEntity source, final double x, final double y, final double z, final AABB bb ) { return this.getNearestEntity(this.getEntitiesOfClass(type, bb, entity -> true), targetConditions, source, x, y, z); } @Nullable default LivingEntity getNearestEntity( final TagKey> tag, final TargetingConditions targetConditions, @Nullable final LivingEntity source, final double x, final double y, final double z, final AABB bb ) { double bestDistance = Double.MAX_VALUE; LivingEntity nearestEntity = null; for (LivingEntity entity : this.getEntitiesOfClass(LivingEntity.class, bb, e -> e.is(tag))) { if (targetConditions.test(this.getLevel(), source, entity)) { double distance = entity.distanceToSqr(x, y, z); if (distance < bestDistance) { bestDistance = distance; nearestEntity = entity; } } } return nearestEntity; } @Nullable default T getNearestEntity(final List entities, final double x, final double y, final double z) { double best = -1.0; T result = null; for (T entity : entities) { double dist = entity.distanceToSqr(x, y, z); if (best == -1.0 || dist < best) { best = dist; result = entity; } } return result; } @Nullable default T getNearestEntity( final List entities, final TargetingConditions targetConditions, @Nullable final LivingEntity source, final double x, final double y, final double z ) { double best = -1.0; T result = null; for (T entity : entities) { if (targetConditions.test(this.getLevel(), source, entity)) { double dist = entity.distanceToSqr(x, y, z); if (best == -1.0 || dist < best) { best = dist; result = entity; } } } return result; } default List getNearbyPlayers(final TargetingConditions targetConditions, final LivingEntity source, final AABB bb) { List foundPlayers = new ArrayList(); for (Player player : this.players()) { if (bb.contains(player.getX(), player.getY(), player.getZ()) && targetConditions.test(this.getLevel(), source, player)) { foundPlayers.add(player); } } return foundPlayers; } default List getNearbyEntities( final Class type, final TargetingConditions targetConditions, final LivingEntity source, final AABB bb ) { List nearby = this.getEntitiesOfClass(type, bb, entityx -> true); List entities = new ArrayList(); for (T entity : nearby) { if (targetConditions.test(this.getLevel(), source, entity)) { entities.add(entity); } } return entities; } }