HoglinSpecificSensor .java

net.minecraft.world.entity.ai.sensing.HoglinSpecificSensor (class, 63 lines): members: doTick, requires. imports: Piglin, Hoglin, ImmutableSet, Brain.

net.minecraft.world.entity.ai.sensingclass63 linescommonextends Sensor<Hoglin>2.5 KB

API Surface (2)

Set<MemoryModuleType<?>> requires()
void doTick(final ServerLevel level, final Hoglin body)

Full Source

HoglinSpecificSensor.java63 lines · Minecraft 26.3 · verbatim
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

Decompiled Minecraft 26.3 source of HoglinSpecificSensor (net/minecraft/world/entity/ai/sensing/HoglinSpecificSensor.java) with Mojang official mappings, stored verbatim on the site.