| 1 | package net.minecraft.world.entity.ai.sensing;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import java.util.Comparator;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.world.entity.LivingEntity;
|
| 9 | import net.minecraft.world.entity.ai.Brain;
|
| 10 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 11 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 12 | import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
|
| 13 | import net.minecraft.world.phys.AABB;
|
| 14 |
|
| 15 | public class NearestLivingEntitySensor<T extends LivingEntity> extends Sensor<T> {
|
| 16 | @Override
|
| 17 | protected void doTick(final ServerLevel level, final T body) {
|
| 18 | double followRange = body.getAttributeValue(Attributes.FOLLOW_RANGE);
|
| 19 | AABB boundingBox = body.getBoundingBox().inflate(followRange, followRange, followRange);
|
| 20 | List<LivingEntity> livingEntities = level.getEntitiesOfClass(LivingEntity.class, boundingBox, mob -> mob != body && mob.isAlive());
|
| 21 | livingEntities.sort(Comparator.comparingDouble(body::distanceToSqr));
|
| 22 | Brain<?> brain = body.getBrain();
|
| 23 | brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, livingEntities);
|
| 24 | brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(level, body, livingEntities));
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public Set<MemoryModuleType<?>> requires() {
|
| 29 | return ImmutableSet.of(MemoryModuleType.NEAREST_LIVING_ENTITIES, MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES);
|
| 30 | }
|
| 31 | }
|
| 32 |
|