| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.tags.TagKey;
|
| 6 | import net.minecraft.world.entity.Entity;
|
| 7 | import net.minecraft.world.entity.EntityType;
|
| 8 | import net.minecraft.world.entity.LivingEntity;
|
| 9 | import net.minecraft.world.entity.ai.targeting.TargetingConditions;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.level.EntityGetter;
|
| 12 | import net.minecraft.world.phys.AABB;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public interface ServerEntityGetter extends EntityGetter {
|
| 16 | ServerLevel getLevel();
|
| 17 |
|
| 18 | default @Nullable Player getNearestPlayer(final TargetingConditions targetConditions, final LivingEntity source) {
|
| 19 | return this.getNearestEntity(this.players(), targetConditions, source, source.getX(), source.getY(), source.getZ());
|
| 20 | }
|
| 21 |
|
| 22 | default @Nullable Player getNearestPlayer(
|
| 23 | final TargetingConditions targetConditions, final LivingEntity source, final double x, final double y, final double z
|
| 24 | ) {
|
| 25 | return this.getNearestEntity(this.players(), targetConditions, source, x, y, z);
|
| 26 | }
|
| 27 |
|
| 28 | default @Nullable Player getNearestPlayer(final TargetingConditions targetConditions, final double x, final double y, final double z) {
|
| 29 | return this.getNearestEntity(this.players(), targetConditions, null, x, y, z);
|
| 30 | }
|
| 31 |
|
| 32 | default <T extends LivingEntity> @Nullable T getNearestEntity(
|
| 33 | final Class<? extends T> type,
|
| 34 | final TargetingConditions targetConditions,
|
| 35 | final @Nullable LivingEntity source,
|
| 36 | final double x,
|
| 37 | final double y,
|
| 38 | final double z,
|
| 39 | final AABB bb
|
| 40 | ) {
|
| 41 | return this.getNearestEntity(this.getEntitiesOfClass(type, bb, entity -> true), targetConditions, source, x, y, z);
|
| 42 | }
|
| 43 |
|
| 44 | default @Nullable LivingEntity getNearestEntity(
|
| 45 | final TagKey<EntityType<?>> tag,
|
| 46 | final TargetingConditions targetConditions,
|
| 47 | final @Nullable LivingEntity source,
|
| 48 | final double x,
|
| 49 | final double y,
|
| 50 | final double z,
|
| 51 | final AABB bb
|
| 52 | ) {
|
| 53 | double bestDistance = Double.MAX_VALUE;
|
| 54 | LivingEntity nearestEntity = null;
|
| 55 |
|
| 56 | for (LivingEntity entity : this.getEntitiesOfClass(LivingEntity.class, bb, e -> e.is(tag))) {
|
| 57 | if (targetConditions.test(this.getLevel(), source, entity)) {
|
| 58 | double distance = entity.distanceToSqr(x, y, z);
|
| 59 | if (distance < bestDistance) {
|
| 60 | bestDistance = distance;
|
| 61 | nearestEntity = entity;
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | return nearestEntity;
|
| 67 | }
|
| 68 |
|
| 69 | default <T extends Entity> @Nullable T getNearestEntity(final List<? extends T> entities, final double x, final double y, final double z) {
|
| 70 | double best = -1.0;
|
| 71 | T result = null;
|
| 72 |
|
| 73 | for (T entity : entities) {
|
| 74 | double dist = entity.distanceToSqr(x, y, z);
|
| 75 | if (best == -1.0 || dist < best) {
|
| 76 | best = dist;
|
| 77 | result = entity;
|
| 78 | }
|
| 79 | }
|
| 80 |
|
| 81 | return result;
|
| 82 | }
|
| 83 |
|
| 84 | default <T extends LivingEntity> @Nullable T getNearestEntity(
|
| 85 | final List<? extends T> entities,
|
| 86 | final TargetingConditions targetConditions,
|
| 87 | final @Nullable LivingEntity source,
|
| 88 | final double x,
|
| 89 | final double y,
|
| 90 | final double z
|
| 91 | ) {
|
| 92 | double best = -1.0;
|
| 93 | T result = null;
|
| 94 |
|
| 95 | for (T entity : entities) {
|
| 96 | if (targetConditions.test(this.getLevel(), source, entity)) {
|
| 97 | double dist = entity.distanceToSqr(x, y, z);
|
| 98 | if (best == -1.0 || dist < best) {
|
| 99 | best = dist;
|
| 100 | result = entity;
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | return result;
|
| 106 | }
|
| 107 |
|
| 108 | default List<Player> getNearbyPlayers(final TargetingConditions targetConditions, final LivingEntity source, final AABB bb) {
|
| 109 | List<Player> foundPlayers = new ArrayList<>();
|
| 110 |
|
| 111 | for (Player player : this.players()) {
|
| 112 | if (bb.contains(player.getX(), player.getY(), player.getZ()) && targetConditions.test(this.getLevel(), source, player)) {
|
| 113 | foundPlayers.add(player);
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | return foundPlayers;
|
| 118 | }
|
| 119 |
|
| 120 | default <T extends LivingEntity> List<T> getNearbyEntities(
|
| 121 | final Class<T> type, final TargetingConditions targetConditions, final LivingEntity source, final AABB bb
|
| 122 | ) {
|
| 123 | List<T> nearby = this.getEntitiesOfClass(type, bb, entityx -> true);
|
| 124 | List<T> entities = new ArrayList<>();
|
| 125 |
|
| 126 | for (T entity : nearby) {
|
| 127 | if (targetConditions.test(this.getLevel(), source, entity)) {
|
| 128 | entities.add(entity);
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | return entities;
|
| 133 | }
|
| 134 | }
|
| 135 |
|