| 1 | package net.minecraft.world.entity.ai.sensing;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.google.common.collect.Iterables;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.minecraft.server.level.ServerLevel;
|
| 11 | import net.minecraft.world.entity.EntityTypes;
|
| 12 | import net.minecraft.world.entity.LivingEntity;
|
| 13 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 14 | import net.minecraft.world.entity.monster.warden.Warden;
|
| 15 |
|
| 16 | public class WardenEntitySensor extends NearestLivingEntitySensor<Warden> {
|
| 17 | @Override
|
| 18 | public Set<MemoryModuleType<?>> requires() {
|
| 19 | return ImmutableSet.copyOf(Iterables.concat(super.requires(), List.of(MemoryModuleType.NEAREST_ATTACKABLE)));
|
| 20 | }
|
| 21 |
|
| 22 | protected void doTick(final ServerLevel level, final Warden body) {
|
| 23 | super.doTick(level, body);
|
| 24 | getClosest(body, e -> e.is(EntityTypes.PLAYER))
|
| 25 | .or(() -> getClosest(body, e -> !e.is(EntityTypes.PLAYER)))
|
| 26 | .ifPresentOrElse(
|
| 27 | entity -> body.getBrain().setMemory(MemoryModuleType.NEAREST_ATTACKABLE, entity), () -> body.getBrain().eraseMemory(MemoryModuleType.NEAREST_ATTACKABLE)
|
| 28 | );
|
| 29 | }
|
| 30 |
|
| 31 | private static Optional<LivingEntity> getClosest(final Warden body, final Predicate<LivingEntity> test) {
|
| 32 | return body.getBrain()
|
| 33 | .getMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES)
|
| 34 | .stream()
|
| 35 | .flatMap(Collection::stream)
|
| 36 | .filter(body::canTargetEntity)
|
| 37 | .filter(test)
|
| 38 | .findFirst();
|
| 39 | }
|
| 40 | }
|
| 41 |
|