PiglinSpecificSensor .java

net.minecraft.world.entity.ai.sensing.PiglinSpecificSensor (class, 120 lines): members: doTick, requires. imports: PiglinBrute, WitherSkeleton, Piglin.

net.minecraft.world.entity.ai.sensingclass120 linescommonextends Sensor<LivingEntity>5.5 KB

API Surface (2)

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

Full Source

PiglinSpecificSensor.java120 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.ai.sensing;
2
3
import com.google.common.collect.ImmutableSet;
4
import java.util.ArrayList;
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.Mob;
13
import net.minecraft.world.entity.ai.Brain;
14
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
15
import net.minecraft.world.entity.ai.memory.NearestVisibleLivingEntities;
16
import net.minecraft.world.entity.boss.wither.WitherBoss;
17
import net.minecraft.world.entity.monster.hoglin.Hoglin;
18
import net.minecraft.world.entity.monster.piglin.AbstractPiglin;
19
import net.minecraft.world.entity.monster.piglin.Piglin;
20
import net.minecraft.world.entity.monster.piglin.PiglinAi;
21
import net.minecraft.world.entity.monster.piglin.PiglinBrute;
22
import net.minecraft.world.entity.monster.skeleton.WitherSkeleton;
23
import net.minecraft.world.entity.player.Player;
24
import net.minecraft.world.level.block.Blocks;
25
import net.minecraft.world.level.block.CampfireBlock;
26
import net.minecraft.world.level.block.state.BlockState;
27
28
public class PiglinSpecificSensor extends Sensor<LivingEntity> {
29
	@Override
30
	public Set<MemoryModuleType<?>> requires() {
31
		return ImmutableSet.of(
32
			MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES,
33
			MemoryModuleType.NEAREST_LIVING_ENTITIES,
34
			MemoryModuleType.NEAREST_VISIBLE_NEMESIS,
35
			MemoryModuleType.NEAREST_TARGETABLE_PLAYER_NOT_WEARING_GOLD,
36
			MemoryModuleType.NEAREST_PLAYER_HOLDING_WANTED_ITEM,
37
			MemoryModuleType.NEAREST_VISIBLE_HUNTABLE_HOGLIN,
38
			MemoryModuleType.NEAREST_VISIBLE_BABY_HOGLIN,
39
			MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS,
40
			MemoryModuleType.NEARBY_ADULT_PIGLINS,
41
			MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT,
42
			MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT,
43
			MemoryModuleType.NEAREST_REPELLENT
44
		);
45
	}
46
47
	@Override
48
	protected void doTick(final ServerLevel level, final LivingEntity body) {
49
		Brain<?> brain = body.getBrain();
50
		brain.setMemory(MemoryModuleType.NEAREST_REPELLENT, findNearestRepellent(level, body));
51
		Optional<Mob> nemesis = Optional.empty();
52
		Optional<Hoglin> huntableHoglin = Optional.empty();
53
		Optional<Hoglin> babyHoglin = Optional.empty();
54
		Optional<Piglin> babyPiglin = Optional.empty();
55
		Optional<LivingEntity> zombified = Optional.empty();
56
		Optional<Player> playerNotWearingGold = Optional.empty();
57
		Optional<Player> playerHoldingWantedItem = Optional.empty();
58
		int visibleAdultHoglinCount = 0;
59
		List<AbstractPiglin> visibleAdultPiglins = new ArrayList<>();
60
		NearestVisibleLivingEntities visibleLivingEntities = brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES)
61
			.orElse(NearestVisibleLivingEntities.empty());
62
63
		for (LivingEntity entity : visibleLivingEntities.findAll(ignored -> true)) {
64
			if (entity instanceof Hoglin hoglin) {
65
				if (hoglin.isBaby() && babyHoglin.isEmpty()) {
66
					babyHoglin = Optional.of(hoglin);
67
				} else if (hoglin.isAdult()) {
68
					visibleAdultHoglinCount++;
69
					if (huntableHoglin.isEmpty() && hoglin.canBeHunted()) {
70
						huntableHoglin = Optional.of(hoglin);
71
					}
72
				}
73
			} else if (entity instanceof PiglinBrute piglinBrute) {
74
				visibleAdultPiglins.add(piglinBrute);
75
			} else if (entity instanceof Piglin piglin) {
76
				if (piglin.isBaby() && babyPiglin.isEmpty()) {
77
					babyPiglin = Optional.of(piglin);
78
				} else if (piglin.isAdult()) {
79
					visibleAdultPiglins.add(piglin);
80
				}
81
			} else if (entity instanceof Player player) {
82
				if (playerNotWearingGold.isEmpty() && !PiglinAi.isWearingSafeArmor(player) && body.canAttack(entity)) {
83
					playerNotWearingGold = Optional.of(player);
84
				}
85
86
				if (playerHoldingWantedItem.isEmpty() && !player.isSpectator() && PiglinAi.isPlayerHoldingLovedItem(player)) {
87
					playerHoldingWantedItem = Optional.of(player);
88
				}
89
			} else if (!nemesis.isEmpty() || !(entity instanceof WitherSkeleton) && !(entity instanceof WitherBoss)) {
90
				if (zombified.isEmpty() && PiglinAi.isZombified(entity)) {
91
					zombified = Optional.of(entity);
92
				}
93
			} else {
94
				nemesis = Optional.of((Mob)entity);
95
			}
96
		}
97
98
		List<AbstractPiglin> adultPiglins = PiglinAi.findNearbyAdultPiglins(brain);
99
		brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_NEMESIS, nemesis);
100
		brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_HUNTABLE_HOGLIN, huntableHoglin);
101
		brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_BABY_HOGLIN, babyHoglin);
102
		brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ZOMBIFIED, zombified);
103
		brain.setMemory(MemoryModuleType.NEAREST_TARGETABLE_PLAYER_NOT_WEARING_GOLD, playerNotWearingGold);
104
		brain.setMemory(MemoryModuleType.NEAREST_PLAYER_HOLDING_WANTED_ITEM, playerHoldingWantedItem);
105
		brain.setMemory(MemoryModuleType.NEARBY_ADULT_PIGLINS, adultPiglins);
106
		brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS, visibleAdultPiglins);
107
		brain.setMemory(MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT, visibleAdultPiglins.size());
108
		brain.setMemory(MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT, visibleAdultHoglinCount);
109
	}
110
111
	private static Optional<BlockPos> findNearestRepellent(final ServerLevel level, final LivingEntity body) {
112
		return level.findBlocksInBoxByManhattanDistance(body.blockPosition(), 8, 4).filterState(PiglinSpecificSensor::isValidRepellent).findFirst();
113
	}
114
115
	private static boolean isValidRepellent(final BlockState state) {
116
		boolean isRepellent = state.is(BlockTags.PIGLIN_REPELLENTS);
117
		return isRepellent && state.is(Blocks.SOUL_CAMPFIRE) ? CampfireBlock.isLitCampfire(state) : isRepellent;
118
	}
119
}
120

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