| 1 | package net.minecraft.world.entity.ai.sensing;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.core.BlockPos;
|
| 9 | import net.minecraft.server.level.ServerLevel;
|
| 10 | import net.minecraft.tags.BlockTags;
|
| 11 | import net.minecraft.world.entity.LivingEntity;
|
| 12 | import net.minecraft.world.entity.ai.Brain;
|
| 13 | import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
| 14 | import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
|
| 15 | import net.minecraft.world.entity.monster.hoglin.Hoglin;
|
| 16 | import net.minecraft.world.entity.monster.piglin.Piglin;
|
| 17 |
|
| 18 | public class HoglinSpecificSensor extends Sensor<Hoglin> {
|
| 19 | @Override
|
| 20 | public Set<MemoryModuleType<?>> requires() {
|
| 21 | return ImmutableSet.of(
|
| 22 | MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
|
| 23 | MemoryModuleType.NEAREST_REPELLENT,
|
| 24 | MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLIN,
|
| 25 | MemoryModuleType.NEAREST_VISIBLE_ADULT_HOGLINS,
|
| 26 | MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT,
|
| 27 | MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT
|
| 28 | );
|
| 29 | }
|
| 30 |
|
| 31 | protected void doTick(final ServerLevel level, final Hoglin body) {
|
| 32 | Brain<?> brain = body.getBrain();
|
| 33 | brain.setMemory(MemoryModuleType.NEAREST_REPELLENT, this.findNearestRepellent(level, body));
|
| 34 | Optional<Piglin> adultPiglin = Optional.empty();
|
| 35 | int adultPiglinCount = 0;
|
| 36 | List<Hoglin> adultHoglins = Lists.newArrayList();
|
| 37 | NearestVisibleLivingEntities visibleLivingEntities = brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
|
| 38 | .orElse(NearestVisibleLivingEntities.empty());
|
| 39 |
|
| 40 | for (LivingEntity entity : visibleLivingEntities.findAll(entityx -> !entityx.isBaby() && (entityx instanceof Piglin || entityx instanceof Hoglin))) {
|
| 41 | if (entity instanceof Piglin piglin) {
|
| 42 | adultPiglinCount++;
|
| 43 | if (adultPiglin.isEmpty()) {
|
| 44 | adultPiglin = Optional.of(piglin);
|
| 45 | }
|
| 46 | }
|
| 47 |
|
| 48 | if (entity instanceof Hoglin hoglin) {
|
| 49 | adultHoglins.add(hoglin);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLIN, adultPiglin);
|
| 54 | brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_HOGLINS, adultHoglins);
|
| 55 | brain.setMemory(MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT, adultPiglinCount);
|
| 56 | brain.setMemory(MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT, adultHoglins.size());
|
| 57 | }
|
| 58 |
|
| 59 | private Optional<BlockPos> findNearestRepellent(final ServerLevel level, final Hoglin body) {
|
| 60 | return level.findBlocksInBoxByManhattanDistance(body.blockPosition(), 8, 4).filterState(state -> state.is(BlockTags.HOGLIN_REPELLENTS)).findFirst();
|
| 61 | }
|
| 62 | }
|
| 63 |
|