PiglinAi .java

net.minecraft.world.entity.monster.piglin.PiglinAi (class): members: HOW_LONG_TIME_TO_DISABLE_ADMIRE_WALKING_IF_CANT_REACH_ITEM, isIdle, canAdmire.

net.minecraft.world.entity.monster.piglinclass864 linescommon35.0 KB

API Surface (26)

List<ActivityData<Piglin>> getActivities(final Piglin piglin)
void initMemories(final Piglin body, final RandomSource random)
void updateActivity(final Piglin body)
void pickUpItem(final ServerLevel level, final Piglin body, final ItemEntity itemEntity)
void stopHoldingOffHandItem(final ServerLevel level, final Piglin body, final boolean barteringEnabled)
void cancelAdmiring(final ServerLevel level, final Piglin body)
boolean wantsToPickup(final Piglin body, final ItemStack itemStack)
boolean isLovedItem(final ItemStack itemStack)
void angerNearbyPiglins(final ServerLevel level, final Player player, final boolean onlyIfTheySeeThePlayer)
InteractionResult mobInteract(final ServerLevel level, final Piglin body, final Player player, final InteractionHand hand)
boolean canAdmire(final Piglin body, final ItemStack playerHeldItemStack)
void wasHurtBy(final ServerLevel level, final Piglin body, final LivingEntity attacker)
void maybeRetaliate(final ServerLevel level, final AbstractPiglin body, final LivingEntity attacker)
Optional<SoundEvent> getSoundForCurrentActivity(final Piglin body)
List<AbstractPiglin> getVisibleAdultPiglins(final Piglin body)
boolean isWearingSafeArmor(final LivingEntity livingEntity)
void broadcastAngerTarget(final ServerLevel level, final AbstractPiglin body, final LivingEntity target)
void broadcastUniversalAnger(final ServerLevel level, final AbstractPiglin body)
void setAngerTarget(final ServerLevel level, final AbstractPiglin body, final LivingEntity target)
Optional<LivingEntity> getAvoidTarget(final Piglin body)
Optional<Player> getNearestVisibleTargetablePlayer(final AbstractPiglin body)
void dontKillAnyMoreHoglinsForAWhile(final AbstractPiglin body)
boolean isIdle(final AbstractPiglin body)
boolean isPlayerHoldingLovedItem(final LivingEntity entity)
boolean isZombified(final Entity entity)
List<AbstractPiglin> findNearbyAdultPiglins(final Brain<?> brain)

Fields & Constants (38)

REPELLENT_DETECTION_RANGE_HORIZONTAL
REPELLENT_DETECTION_RANGE_VERTICAL
BARTERING_ITEM
PLAYER_ANGER_RANGE
ANGER_DURATION
ADMIRE_DURATION
MAX_DISTANCE_TO_WALK_TO_ITEM
MAX_TIME_TO_WALK_TO_ITEM
HOW_LONG_TIME_TO_DISABLE_ADMIRE_WALKING_IF_CANT_REACH_ITEM
CELEBRATION_TIME
MAX_TIME_BETWEEN_HUNTS
TIME_BETWEEN_HUNTS
BABY_FLEE_DURATION_AFTER_GETTING_HIT
HIT_BY_PLAYER_MEMORY_TIMEOUT
MAX_WALK_DISTANCE_TO_START_RIDING
RIDE_START_INTERVAL
RIDE_DURATION
RETREAT_DURATION
MELEE_ATTACK_COOLDOWN
EAT_COOLDOWN
DESIRED_DISTANCE_FROM_ENTITY_WHEN_AVOIDING
MAX_LOOK_DIST
MAX_LOOK_DIST_FOR_PLAYER_HOLDING_LOVED_ITEM
INTERACTION_RANGE
MIN_DESIRED_DIST_FROM_TARGET_WHEN_HOLDING_CROSSBOW
SPEED_WHEN_STRAFING_BACK_FROM_TARGET
DESIRED_DISTANCE_FROM_ZOMBIFIED
AVOID_ZOMBIFIED_DURATION
BABY_AVOID_NEMESIS_DURATION
PROBABILITY_OF_CELEBRATION_DANCE
SPEED_MULTIPLIER_WHEN_AVOIDING
SPEED_MULTIPLIER_WHEN_RETREATING
SPEED_MULTIPLIER_WHEN_MOUNTING
SPEED_MULTIPLIER_WHEN_GOING_TO_WANTED_ITEM
SPEED_MULTIPLIER_WHEN_GOING_TO_CELEBRATE_LOCATION
SPEED_MULTIPLIER_WHEN_DANCING
SPEED_MULTIPLIER_WHEN_IDLING
body

Code Graph: Dependencies (79)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

PiglinAi.java864 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster.piglin;
2
3
import com.google.common.collect.ImmutableList;
4
import com.mojang.datafixers.util.Pair;
5
import java.util.ArrayList;
6
import java.util.Collections;
7
import java.util.List;
8
import java.util.Optional;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.sounds.SoundEvent;
11
import net.minecraft.sounds.SoundEvents;
12
import net.minecraft.tags.ItemTags;
13
import net.minecraft.util.RandomSource;
14
import net.minecraft.util.TimeUtil;
15
import net.minecraft.util.valueproviders.UniformInt;
16
import net.minecraft.world.InteractionHand;
17
import net.minecraft.world.InteractionResult;
18
import net.minecraft.world.entity.Entity;
19
import net.minecraft.world.entity.EntityTypes;
20
import net.minecraft.world.entity.EquipmentSlot;
21
import net.minecraft.world.entity.EquipmentSlotGroup;
22
import net.minecraft.world.entity.LivingEntity;
23
import net.minecraft.world.entity.Mob;
24
import net.minecraft.world.entity.PathfinderMob;
25
import net.minecraft.world.entity.ai.ActivityData;
26
import net.minecraft.world.entity.ai.Brain;
27
import net.minecraft.world.entity.ai.behavior.BackUpIfTooClose;
28
import net.minecraft.world.entity.ai.behavior.BehaviorControl;
29
import net.minecraft.world.entity.ai.behavior.BehaviorUtils;
30
import net.minecraft.world.entity.ai.behavior.CopyMemoryWithExpiry;
31
import net.minecraft.world.entity.ai.behavior.CrossbowAttack;
32
import net.minecraft.world.entity.ai.behavior.DismountOrSkipMounting;
33
import net.minecraft.world.entity.ai.behavior.DoNothing;
34
import net.minecraft.world.entity.ai.behavior.EraseMemoryIf;
35
import net.minecraft.world.entity.ai.behavior.GoToTargetLocation;
36
import net.minecraft.world.entity.ai.behavior.GoToWantedItem;
37
import net.minecraft.world.entity.ai.behavior.InteractWith;
38
import net.minecraft.world.entity.ai.behavior.InteractWithDoor;
39
import net.minecraft.world.entity.ai.behavior.LookAtTargetSink;
40
import net.minecraft.world.entity.ai.behavior.MeleeAttack;
41
import net.minecraft.world.entity.ai.behavior.Mount;
42
import net.minecraft.world.entity.ai.behavior.MoveToTargetSink;
43
import net.minecraft.world.entity.ai.behavior.OneShot;
44
import net.minecraft.world.entity.ai.behavior.RandomStroll;
45
import net.minecraft.world.entity.ai.behavior.RunOne;
46
import net.minecraft.world.entity.ai.behavior.SetEntityLookTarget;
47
import net.minecraft.world.entity.ai.behavior.SetEntityLookTargetSometimes;
48
import net.minecraft.world.entity.ai.behavior.SetLookAndInteract;
49
import net.minecraft.world.entity.ai.behavior.SetWalkTargetAwayFrom;
50
import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromAttackTargetIfTargetOutOfReach;
51
import net.minecraft.world.entity.ai.behavior.SetWalkTargetFromLookTarget;
52
import net.minecraft.world.entity.ai.behavior.SpearApproach;
53
import net.minecraft.world.entity.ai.behavior.SpearAttack;
54
import net.minecraft.world.entity.ai.behavior.SpearRetreat;
55
import net.minecraft.world.entity.ai.behavior.StartAttacking;
56
import net.minecraft.world.entity.ai.behavior.StartCelebratingIfTargetDead;
57
import net.minecraft.world.entity.ai.behavior.StopAttackingIfTargetInvalid;
58
import net.minecraft.world.entity.ai.behavior.StopBeingAngryIfTargetDead;
59
import net.minecraft.world.entity.ai.behavior.TriggerGate;
60
import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
61
import net.minecraft.world.entity.ai.behavior.declarative.Trigger;
62
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
63
import net.minecraft.world.entity.ai.sensing.Sensor;
64
import net.minecraft.world.entity.ai.util.LandRandomPos;
65
import net.minecraft.world.entity.item.ItemEntity;
66
import net.minecraft.world.entity.monster.hoglin.Hoglin;
67
import net.minecraft.world.entity.player.Player;
68
import net.minecraft.world.entity.schedule.Activity;
69
import net.minecraft.world.item.Item;
70
import net.minecraft.world.item.ItemStack;
71
import net.minecraft.world.item.Items;
72
import net.minecraft.world.item.component.SwingAnimation;
73
import net.minecraft.world.level.gamerules.GameRules;
74
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
75
import net.minecraft.world.level.storage.loot.LootParams;
76
import net.minecraft.world.level.storage.loot.LootTable;
77
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
78
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
79
import net.minecraft.world.phys.Vec3;
80
81
public class PiglinAi {
82
	public static final int REPELLENT_DETECTION_RANGE_HORIZONTAL = 8;
83
	public static final int REPELLENT_DETECTION_RANGE_VERTICAL = 4;
84
	public static final Item BARTERING_ITEM = Items.GOLD_INGOT;
85
	private static final int PLAYER_ANGER_RANGE = 16;
86
	private static final int ANGER_DURATION = 600;
87
	private static final int ADMIRE_DURATION = 119;
88
	private static final int MAX_DISTANCE_TO_WALK_TO_ITEM = 9;
89
	private static final int MAX_TIME_TO_WALK_TO_ITEM = 200;
90
	private static final int HOW_LONG_TIME_TO_DISABLE_ADMIRE_WALKING_IF_CANT_REACH_ITEM = 200;
91
	private static final int CELEBRATION_TIME = 300;
92
	public static final int MAX_TIME_BETWEEN_HUNTS = 120;
93
	protected static final UniformInt TIME_BETWEEN_HUNTS = TimeUtil.rangeOfSeconds(30, 120);
94
	private static final int BABY_FLEE_DURATION_AFTER_GETTING_HIT = 100;
95
	private static final int HIT_BY_PLAYER_MEMORY_TIMEOUT = 400;
96
	private static final int MAX_WALK_DISTANCE_TO_START_RIDING = 8;
97
	private static final UniformInt RIDE_START_INTERVAL = TimeUtil.rangeOfSeconds(10, 40);
98
	private static final UniformInt RIDE_DURATION = TimeUtil.rangeOfSeconds(10, 30);
99
	private static final UniformInt RETREAT_DURATION = TimeUtil.rangeOfSeconds(5, 20);
100
	private static final int MELEE_ATTACK_COOLDOWN = 20;
101
	private static final int EAT_COOLDOWN = 200;
102
	private static final int DESIRED_DISTANCE_FROM_ENTITY_WHEN_AVOIDING = 12;
103
	private static final int MAX_LOOK_DIST = 8;
104
	private static final int MAX_LOOK_DIST_FOR_PLAYER_HOLDING_LOVED_ITEM = 14;
105
	private static final int INTERACTION_RANGE = 8;
106
	private static final int MIN_DESIRED_DIST_FROM_TARGET_WHEN_HOLDING_CROSSBOW = 5;
107
	private static final float SPEED_WHEN_STRAFING_BACK_FROM_TARGET = 0.75F;
108
	private static final int DESIRED_DISTANCE_FROM_ZOMBIFIED = 6;
109
	private static final UniformInt AVOID_ZOMBIFIED_DURATION = TimeUtil.rangeOfSeconds(5, 7);
110
	private static final UniformInt BABY_AVOID_NEMESIS_DURATION = TimeUtil.rangeOfSeconds(5, 7);
111
	private static final float PROBABILITY_OF_CELEBRATION_DANCE = 0.1F;
112
	private static final float SPEED_MULTIPLIER_WHEN_AVOIDING = 1.0F;
113
	private static final float SPEED_MULTIPLIER_WHEN_RETREATING = 1.0F;
114
	private static final float SPEED_MULTIPLIER_WHEN_MOUNTING = 0.8F;
115
	private static final float SPEED_MULTIPLIER_WHEN_GOING_TO_WANTED_ITEM = 1.0F;
116
	private static final float SPEED_MULTIPLIER_WHEN_GOING_TO_CELEBRATE_LOCATION = 1.0F;
117
	private static final float SPEED_MULTIPLIER_WHEN_DANCING = 0.6F;
118
	private static final float SPEED_MULTIPLIER_WHEN_IDLING = 0.6F;
119
120
	public static List<ActivityData<Piglin>> getActivities(final Piglin piglin) {
121
		return List.of(
122
			initCoreActivity(),
123
			initIdleActivity(),
124
			initAdmireItemActivity(),
125
			initFightActivity(piglin),
126
			initCelebrateActivity(),
127
			initRetreatActivity(),
128
			initRideHoglinActivity()
129
		);
130
	}
131
132
	protected static void initMemories(final Piglin body, final RandomSource random) {
133
		int delayUntilFirstHunt = TIME_BETWEEN_HUNTS.sample(random);
134
		body.getBrain().setMemoryWithExpiry(MemoryModuleType.HUNTED_RECENTLY, true, delayUntilFirstHunt);
135
	}
136
137
	private static ActivityData<Piglin> initCoreActivity() {
138
		return ActivityData.create(
139
			Activity.CORE,
140
			0,
141
			ImmutableList.of(
142
				new LookAtTargetSink(45, 90),
143
				new MoveToTargetSink(),
144
				InteractWithDoor.create(),
145
				babyAvoidNemesis(),
146
				avoidZombified(),
147
				StopHoldingItemIfNoLongerAdmiring.create(),
148
				StartAdmiringItemIfSeen.create(119),
149
				StartCelebratingIfTargetDead.create(300, PiglinAi::wantsToDance),
150
				StopBeingAngryIfTargetDead.create()
151
			)
152
		);
153
	}
154
155
	private static ActivityData<Piglin> initIdleActivity() {
156
		return ActivityData.create(
157
			Activity.IDLE,
158
			10,
159
			ImmutableList.of(
160
				SetEntityLookTarget.create(PiglinAi::isPlayerHoldingLovedItem, 14.0F),
161
				StartAttacking.<Piglin>create((level, piglin) -> piglin.isAdult(), PiglinAi::findNearestValidAttackTarget),
162
				BehaviorBuilder.triggerIf(Piglin::canHunt, StartHuntingHoglin.create()),
163
				avoidRepellent(),
164
				babySometimesRideBabyHoglin(),
165
				createIdleLookBehaviors(),
166
				createIdleMovementBehaviors(),
167
				SetLookAndInteract.create(EntityTypes.PLAYER, 4)
168
			)
169
		);
170
	}
171
172
	private static ActivityData<Piglin> initFightActivity(final Piglin body) {
173
		return ActivityData.create(
174
			Activity.FIGHT,
175
			10,
176
			ImmutableList.of(
177
				StopAttackingIfTargetInvalid.create((level, target) -> !isNearestValidAttackTarget(level, body, target)),
178
				BehaviorBuilder.triggerIf(PiglinAi::hasCrossbow, BackUpIfTooClose.create(5, 0.75F)),
179
				SetWalkTargetFromAttackTargetIfTargetOutOfReach.create(1.0F),
180
				new SpearApproach(1.0, 10.0F),
181
				new SpearAttack(1.0, 1.0, 2.0F),
182
				new SpearRetreat(1.0),
183
				MeleeAttack.create(20),
184
				new CrossbowAttack(),
185
				RememberIfHoglinWasKilled.create(),
186
				EraseMemoryIf.create(PiglinAi::isNearZombified, MemoryModuleType.ATTACK_TARGET)
187
			),
188
			MemoryModuleType.ATTACK_TARGET
189
		);
190
	}
191
192
	private static ActivityData<Piglin> initCelebrateActivity() {
193
		return ActivityData.create(
194
			Activity.CELEBRATE,
195
			10,
196
			ImmutableList.of(
197
				avoidRepellent(),
198
				SetEntityLookTarget.create(PiglinAi::isPlayerHoldingLovedItem, 14.0F),
199
				StartAttacking.<Piglin>create((level, piglin) -> piglin.isAdult(), PiglinAi::findNearestValidAttackTarget),
200
				BehaviorBuilder.triggerIf(piglin -> !piglin.isDancing(), GoToTargetLocation.create(MemoryModuleType.CELEBRATE_LOCATION, 2, 1.0F)),
201
				BehaviorBuilder.triggerIf(Piglin::isDancing, GoToTargetLocation.create(MemoryModuleType.CELEBRATE_LOCATION, 4, 0.6F)),
202
				new RunOne<LivingEntity>(
203
					ImmutableList.of(
204
						Pair.of(SetEntityLookTarget.create(EntityTypes.PIGLIN, 8.0F), 1), Pair.of(RandomStroll.stroll(0.6F, 2, 1), 1), Pair.of(new DoNothing(10, 20), 1)
205
					)
206
				)
207
			),
208
			MemoryModuleType.CELEBRATE_LOCATION
209
		);
210
	}
211
212
	private static ActivityData<Piglin> initAdmireItemActivity() {
213
		return ActivityData.create(
214
			Activity.ADMIRE_ITEM,
215
			10,
216
			ImmutableList.of(
217
				GoToWantedItem.create(PiglinAi::isNotHoldingLovedItemInOffHand, 1.0F, true, 9),
218
				StopAdmiringIfItemTooFarAway.create(9),
219
				StopAdmiringIfTiredOfTryingToReachItem.create(200, 200)
220
			),
221
			MemoryModuleType.ADMIRING_ITEM
222
		);
223
	}
224
225
	private static ActivityData<Piglin> initRetreatActivity() {
226
		return ActivityData.create(
227
			Activity.AVOID,
228
			10,
229
			ImmutableList.of(
230
				SetWalkTargetAwayFrom.entity(MemoryModuleType.AVOID_TARGET, 1.0F, 12, true),
231
				createIdleLookBehaviors(),
232
				createIdleMovementBehaviors(),
233
				EraseMemoryIf.<PathfinderMob>create(PiglinAi::wantsToStopFleeing, MemoryModuleType.AVOID_TARGET)
234
			),
235
			MemoryModuleType.AVOID_TARGET
236
		);
237
	}
238
239
	private static ActivityData<Piglin> initRideHoglinActivity() {
240
		return ActivityData.create(
241
			Activity.RIDE,
242
			10,
243
			ImmutableList.of(
244
				Mount.create(0.8F),
245
				SetEntityLookTarget.create(PiglinAi::isPlayerHoldingLovedItem, 8.0F),
246
				BehaviorBuilder.sequence(
247
					BehaviorBuilder.triggerIf(Entity::isPassenger),
248
					TriggerGate.triggerOneShuffled(
249
						ImmutableList.<Pair<? extends Trigger<? super LivingEntity>, Integer>>builder()
250
							.addAll(createLookBehaviors())
251
							.add(Pair.of(BehaviorBuilder.triggerIf(e -> true), 1))
252
							.build()
253
					)
254
				),
255
				DismountOrSkipMounting.<LivingEntity>create(8, PiglinAi::wantsToStopRiding)
256
			),
257
			MemoryModuleType.RIDE_TARGET
258
		);
259
	}
260
261
	private static ImmutableList<Pair<OneShot<LivingEntity>, Integer>> createLookBehaviors() {
262
		return ImmutableList.of(
263
			Pair.of(SetEntityLookTarget.create(EntityTypes.PLAYER, 8.0F), 1),
264
			Pair.of(SetEntityLookTarget.create(EntityTypes.PIGLIN, 8.0F), 1),
265
			Pair.of(SetEntityLookTarget.create(8.0F), 1)
266
		);
267
	}
268
269
	private static RunOne<LivingEntity> createIdleLookBehaviors() {
270
		return new RunOne<>(
271
			ImmutableList.<Pair<? extends BehaviorControl<? super LivingEntity>, Integer>>builder()
272
				.addAll(createLookBehaviors())
273
				.add(Pair.of(new DoNothing(30, 60), 1))
274
				.build()
275
		);
276
	}
277
278
	private static RunOne<Piglin> createIdleMovementBehaviors() {
279
		return new RunOne<>(
280
			ImmutableList.of(
281
				Pair.of(RandomStroll.stroll(0.6F), 2),
282
				Pair.of(InteractWith.of(EntityTypes.PIGLIN, 8, MemoryModuleType.INTERACTION_TARGET, 0.6F, 2), 2),
283
				Pair.of(BehaviorBuilder.triggerIf(PiglinAi::doesntSeeAnyPlayerHoldingLovedItem, SetWalkTargetFromLookTarget.create(0.6F, 3)), 2),
284
				Pair.of(new DoNothing(30, 60), 1)
285
			)
286
		);
287
	}
288
289
	private static BehaviorControl<PathfinderMob> avoidRepellent() {
290
		return SetWalkTargetAwayFrom.pos(MemoryModuleType.NEAREST_REPELLENT, 1.0F, 8, false);
291
	}
292
293
	private static BehaviorControl<Piglin> babyAvoidNemesis() {
294
		return CopyMemoryWithExpiry.create(Piglin::isBaby, MemoryModuleType.NEAREST_VISIBLE_NEMESIS, MemoryModuleType.AVOID_TARGET, BABY_AVOID_NEMESIS_DURATION);
295
	}
296
297
	private static BehaviorControl<Piglin> avoidZombified() {
298
		return CopyMemoryWithExpiry.create(
299
			PiglinAi::isNearZombified, MemoryModuleType.NEAREST_VISIBLE_ZOMBIFIED, MemoryModuleType.AVOID_TARGET, AVOID_ZOMBIFIED_DURATION
300
		);
301
	}
302
303
	protected static void updateActivity(final Piglin body) {
304
		Brain<Piglin> brain = body.getBrain();
305
		Activity oldActivity = brain.getActiveNonCoreActivity().orElse(null);
306
		brain.setActiveActivityToFirstValid(ImmutableList.of(Activity.ADMIRE_ITEM, Activity.FIGHT, Activity.AVOID, Activity.CELEBRATE, Activity.RIDE, Activity.IDLE));
307
		Activity newActivity = brain.getActiveNonCoreActivity().orElse(null);
308
		if (oldActivity != newActivity) {
309
			getSoundForCurrentActivity(body).ifPresent(body::makeSound);
310
		}
311
312
		body.setAggressive(brain.hasMemoryValue(MemoryModuleType.ATTACK_TARGET));
313
		if (!brain.hasMemoryValue(MemoryModuleType.RIDE_TARGET) && isBabyRidingBaby(body)) {
314
			body.stopRiding();
315
		}
316
317
		if (!brain.hasMemoryValue(MemoryModuleType.CELEBRATE_LOCATION)) {
318
			brain.eraseMemory(MemoryModuleType.DANCING);
319
		}
320
321
		body.setDancing(brain.hasMemoryValue(MemoryModuleType.DANCING));
322
	}
323
324
	private static boolean isBabyRidingBaby(final Piglin body) {
325
		if (!body.isBaby()) {
326
			return false;
327
		}
328
329
		Entity vehicle = body.getVehicle();
330
		return vehicle instanceof Piglin riddenPiglin && riddenPiglin.isBaby() || vehicle instanceof Hoglin riddenHoglin && riddenHoglin.isBaby();
331
	}
332
333
	protected static void pickUpItem(final ServerLevel level, final Piglin body, final ItemEntity itemEntity) {
334
		stopWalking(body);
335
		ItemStack taken;
336
		if (itemEntity.getItem().is(Items.GOLD_NUGGET)) {
337
			body.take(itemEntity, itemEntity.getItem().getCount());
338
			taken = itemEntity.getItem();
339
			itemEntity.discard();
340
		} else {
341
			body.take(itemEntity, 1);
342
			taken = removeOneItemFromItemEntity(itemEntity);
343
		}
344
345
		if (isLovedItem(taken)) {
346
			body.getBrain().eraseMemory(MemoryModuleType.TIME_TRYING_TO_REACH_ADMIRE_ITEM);
347
			holdInOffhand(level, body, taken);
348
			admireGoldItem(body);
349
		} else if (isFood(taken) && !hasEatenRecently(body)) {
350
			eat(body);
351
		} else {
352
			boolean itemEquipped = !body.equipItemIfPossible(level, taken).equals(ItemStack.EMPTY);
353
			if (!itemEquipped) {
354
				putInInventory(body, taken);
355
			}
356
		}
357
	}
358
359
	private static void holdInOffhand(final ServerLevel level, final Piglin body, final ItemStack itemStack) {
360
		if (isHoldingItemInOffHand(body)) {
361
			body.spawnAtLocation(level, body.getItemInHand(InteractionHand.OFF_HAND));
362
		}
363
364
		body.holdInOffHand(itemStack);
365
	}
366
367
	private static ItemStack removeOneItemFromItemEntity(final ItemEntity itemEntity) {
368
		ItemStack sourceStack = itemEntity.getItem();
369
		ItemStack removedStack = sourceStack.split(1);
370
		if (sourceStack.isEmpty()) {
371
			itemEntity.discard();
372
		} else {
373
			itemEntity.setItem(sourceStack);
374
		}
375
376
		return removedStack;
377
	}
378
379
	protected static void stopHoldingOffHandItem(final ServerLevel level, final Piglin body, final boolean barteringEnabled) {
380
		ItemStack itemStack = body.getItemInHand(InteractionHand.OFF_HAND);
381
		body.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
382
		if (body.isAdult()) {
383
			boolean barterCurrency = isBarterCurrency(itemStack);
384
			if (barteringEnabled && barterCurrency) {
385
				throwItems(body, getBarterResponseItems(body));
386
			} else if (!barterCurrency) {
387
				boolean equipped = !body.equipItemIfPossible(level, itemStack).isEmpty();
388
				if (!equipped) {
389
					putInInventory(body, itemStack);
390
				}
391
			}
392
		} else {
393
			boolean equipped = !body.equipItemIfPossible(level, itemStack).isEmpty();
394
			if (!equipped) {
395
				ItemStack mainHandItem = body.getMainHandItem();
396
				if (isLovedItem(mainHandItem)) {
397
					putInInventory(body, mainHandItem);
398
				} else {
399
					throwItems(body, Collections.singletonList(mainHandItem));
400
				}
401
402
				body.holdInMainHand(itemStack);
403
			}
404
		}
405
	}
406
407
	protected static void cancelAdmiring(final ServerLevel level, final Piglin body) {
408
		if (isAdmiringItem(body) && !body.getOffhandItem().isEmpty()) {
409
			body.spawnAtLocation(level, body.getOffhandItem());
410
			body.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
411
		}
412
	}
413
414
	private static void putInInventory(final Piglin body, final ItemStack itemStack) {
415
		ItemStack stuffThatCouldntFitInMyInventory = body.addToInventory(itemStack);
416
		throwItemsTowardRandomPos(body, Collections.singletonList(stuffThatCouldntFitInMyInventory));
417
	}
418
419
	private static void throwItems(final Piglin body, final List<ItemStack> itemStacks) {
420
		Optional<Player> player = body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_PLAYER);
421
		if (player.isPresent()) {
422
			throwItemsTowardPlayer(body, player.get(), itemStacks);
423
		} else {
424
			throwItemsTowardRandomPos(body, itemStacks);
425
		}
426
	}
427
428
	private static void throwItemsTowardRandomPos(final Piglin body, final List<ItemStack> itemStacks) {
429
		throwItemsTowardPos(body, itemStacks, getRandomNearbyPos(body));
430
	}
431
432
	private static void throwItemsTowardPlayer(final Piglin body, final Player player, final List<ItemStack> itemStacks) {
433
		throwItemsTowardPos(body, itemStacks, player.position());
434
	}
435
436
	private static void throwItemsTowardPos(final Piglin body, final List<ItemStack> itemStacks, final Vec3 targetPos) {
437
		if (!itemStacks.isEmpty()) {
438
			body.swing(InteractionHand.OFF_HAND, SwingAnimation.DEFAULT);
439
440
			for (ItemStack itemStack : itemStacks) {
441
				BehaviorUtils.throwItem(body, itemStack, targetPos.add(0.0, 1.0, 0.0));
442
			}
443
		}
444
	}
445
446
	private static List<ItemStack> getBarterResponseItems(final Piglin body) {
447
		LootTable lootTable = body.level().getServer().reloadableRegistries().getLootTable(BuiltInLootTables.PIGLIN_BARTERING);
448
		return lootTable.getRandomItems(
449
			new LootParams.Builder((ServerLevel)body.level()).withParameter(LootContextParams.THIS_ENTITY, body).create(LootContextParamSets.PIGLIN_BARTER)
450
		);
451
	}
452
453
	private static boolean wantsToDance(final LivingEntity body, final LivingEntity killedTarget) {
454
		return !killedTarget.is(EntityTypes.HOGLIN) ? false : RandomSource.createThreadLocalInstance(body.level().getGameTime()).nextFloat() < 0.1F;
455
	}
456
457
	protected static boolean wantsToPickup(final Piglin body, final ItemStack itemStack) {
458
		if (body.isBaby() && itemStack.is(ItemTags.IGNORED_BY_PIGLIN_BABIES)) {
459
			return false;
460
		} else if (itemStack.is(ItemTags.PIGLIN_REPELLENTS)) {
461
			return false;
462
		} else if (isAdmiringDisabled(body) && body.getBrain().hasMemoryValue(MemoryModuleType.ATTACK_TARGET)) {
463
			return false;
464
		} else if (isBarterCurrency(itemStack)) {
465
			return isNotHoldingLovedItemInOffHand(body);
466
		} else {
467
			boolean hasSpace = body.canAddToInventory(itemStack);
468
			if (itemStack.is(Items.GOLD_NUGGET)) {
469
				return hasSpace;
470
			} else if (isFood(itemStack)) {
471
				return !hasEatenRecently(body) && hasSpace;
472
			} else {
473
				return !isLovedItem(itemStack) ? body.canReplaceCurrentItem(itemStack) : isNotHoldingLovedItemInOffHand(body) && hasSpace;
474
			}
475
		}
476
	}
477
478
	protected static boolean isLovedItem(final ItemStack itemStack) {
479
		return itemStack.is(ItemTags.PIGLIN_LOVED);
480
	}
481
482
	private static boolean wantsToStopRiding(final Piglin body, final Entity entityBeingRidden) {
483
		return !(entityBeingRidden instanceof Mob mobBeingRidden)
484
			? false
485
			: !mobBeingRidden.isBaby()
486
				|| !mobBeingRidden.isAlive()
487
				|| wasHurtRecently(body)
488
				|| wasHurtRecently(mobBeingRidden)
489
				|| mobBeingRidden instanceof Piglin && mobBeingRidden.getVehicle() == null;
490
	}
491
492
	private static boolean isNearestValidAttackTarget(final ServerLevel level, final Piglin body, final LivingEntity target) {
493
		return findNearestValidAttackTarget(level, body).filter(nearestValidTarget -> nearestValidTarget == target).isPresent();
494
	}
495
496
	private static boolean isNearZombified(final Piglin body) {
497
		Brain<Piglin> brain = body.getBrain();
498
		if (brain.hasMemoryValue(MemoryModuleType.NEAREST_VISIBLE_ZOMBIFIED)) {
499
			LivingEntity zombified = brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_ZOMBIFIED).get();
500
			return body.closerThan(zombified, 6.0);
501
		} else {
502
			return false;
503
		}
504
	}
505
506
	private static Optional<? extends LivingEntity> findNearestValidAttackTarget(final ServerLevel level, final Piglin body) {
507
		Brain<Piglin> brain = body.getBrain();
508
		if (isNearZombified(body)) {
509
			return Optional.empty();
510
		}
511
512
		Optional<LivingEntity> angryAt = BehaviorUtils.getLivingEntityFromUUIDMemory(body, MemoryModuleType.ANGRY_AT);
513
		if (angryAt.isPresent() && Sensor.isEntityAttackableIgnoringLineOfSight(level, body, angryAt.get())) {
514
			return angryAt;
515
		}
516
517
		if (brain.hasMemoryValue(MemoryModuleType.UNIVERSAL_ANGER)) {
518
			Optional<Player> player = brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER);
519
			if (player.isPresent()) {
520
				return player;
521
			}
522
		}
523
524
		Optional<Mob> nemesis = brain.getMemory(MemoryModuleType.NEAREST_VISIBLE_NEMESIS);
525
		if (nemesis.isPresent()) {
526
			return nemesis;
527
		}
528
529
		Optional<Player> playerNotWearingGold = brain.getMemory(MemoryModuleType.NEAREST_TARGETABLE_PLAYER_NOT_WEARING_GOLD);
530
		return playerNotWearingGold.isPresent() && Sensor.isEntityAttackable(level, body, playerNotWearingGold.get()) ? playerNotWearingGold : Optional.empty();
531
	}
532
533
	public static void angerNearbyPiglins(final ServerLevel level, final Player player, final boolean onlyIfTheySeeThePlayer) {
534
		List<Piglin> nearbyPiglins = player.level().getEntitiesOfClass(Piglin.class, player.getBoundingBox().inflate(16.0));
535
		nearbyPiglins.stream().filter(PiglinAi::isIdle).filter(piglin -> !onlyIfTheySeeThePlayer || BehaviorUtils.canSee(piglin, player)).forEach(piglin -> {
536
			if (level.getGameRules().get(GameRules.UNIVERSAL_ANGER)) {
537
				setAngerTargetToNearestTargetablePlayerIfFound(level, piglin, player);
538
			} else {
539
				setAngerTarget(level, piglin, player);
540
			}
541
		});
542
	}
543
544
	public static InteractionResult mobInteract(final ServerLevel level, final Piglin body, final Player player, final InteractionHand hand) {
545
		ItemStack playerHeldItemStack = player.getItemInHand(hand);
546
		if (canAdmire(body, playerHeldItemStack)) {
547
			ItemStack taken = playerHeldItemStack.consumeAndReturn(1, player);
548
			holdInOffhand(level, body, taken);
549
			admireGoldItem(body);
550
			stopWalking(body);
551
			return InteractionResult.SUCCESS;
552
		} else {
553
			return InteractionResult.PASS;
554
		}
555
	}
556
557
	protected static boolean canAdmire(final Piglin body, final ItemStack playerHeldItemStack) {
558
		return !isAdmiringDisabled(body) && !isAdmiringItem(body) && body.isAdult() && isBarterCurrency(playerHeldItemStack);
559
	}
560
561
	protected static void wasHurtBy(final ServerLevel level, final Piglin body, final LivingEntity attacker) {
562
		if (!(attacker instanceof Piglin)) {
563
			if (isHoldingItemInOffHand(body)) {
564
				stopHoldingOffHandItem(level, body, false);
565
			}
566
567
			Brain<Piglin> brain = body.getBrain();
568
			brain.eraseMemory(MemoryModuleType.CELEBRATE_LOCATION);
569
			brain.eraseMemory(MemoryModuleType.DANCING);
570
			brain.eraseMemory(MemoryModuleType.ADMIRING_ITEM);
571
			if (attacker instanceof Player) {
572
				brain.setMemoryWithExpiry(MemoryModuleType.ADMIRING_DISABLED, true, 400L);
573
			}
574
575
			getAvoidTarget(body).ifPresent(avoidTarget -> {
576
				if (avoidTarget.getType() != attacker.getType()) {
577
					brain.eraseMemory(MemoryModuleType.AVOID_TARGET);
578
				}
579
			});
580
			if (body.isBaby()) {
581
				brain.setMemoryWithExpiry(MemoryModuleType.AVOID_TARGET, attacker, 100L);
582
				if (Sensor.isEntityAttackableIgnoringLineOfSight(level, body, attacker)) {
583
					broadcastAngerTarget(level, body, attacker);
584
				}
585
			} else if (attacker.is(EntityTypes.HOGLIN) && hoglinsOutnumberPiglins(body)) {
586
				setAvoidTargetAndDontHuntForAWhile(body, attacker);
587
				broadcastRetreat(body, attacker);
588
			} else {
589
				maybeRetaliate(level, body, attacker);
590
			}
591
		}
592
	}
593
594
	protected static void maybeRetaliate(final ServerLevel level, final AbstractPiglin body, final LivingEntity attacker) {
595
		if (!body.getBrain().isActive(Activity.AVOID)) {
596
			if (Sensor.isEntityAttackableIgnoringLineOfSight(level, body, attacker)) {
597
				if (!BehaviorUtils.isOtherTargetMuchFurtherAwayThanCurrentAttackTarget(body, attacker, 4.0)) {
598
					if (attacker.is(EntityTypes.PLAYER) && level.getGameRules().get(GameRules.UNIVERSAL_ANGER)) {
599
						setAngerTargetToNearestTargetablePlayerIfFound(level, body, attacker);
600
						broadcastUniversalAnger(level, body);
601
					} else {
602
						setAngerTarget(level, body, attacker);
603
						broadcastAngerTarget(level, body, attacker);
604
					}
605
				}
606
			}
607
		}
608
	}
609
610
	public static Optional<SoundEvent> getSoundForCurrentActivity(final Piglin body) {
611
		return body.getBrain().getActiveNonCoreActivity().map(activity -> getSoundForActivity(body, activity));
612
	}
613
614
	private static SoundEvent getSoundForActivity(final Piglin body, final Activity activity) {
615
		if (activity == Activity.FIGHT) {
616
			return SoundEvents.PIGLIN_ANGRY;
617
		} else if (body.isConverting()) {
618
			return SoundEvents.PIGLIN_RETREAT;
619
		} else if (activity == Activity.AVOID && isNearAvoidTarget(body)) {
620
			return SoundEvents.PIGLIN_RETREAT;
621
		} else if (activity == Activity.ADMIRE_ITEM) {
622
			return SoundEvents.PIGLIN_ADMIRING_ITEM;
623
		} else if (activity == Activity.CELEBRATE) {
624
			return SoundEvents.PIGLIN_CELEBRATE;
625
		} else if (seesPlayerHoldingLovedItem(body)) {
626
			return SoundEvents.PIGLIN_JEALOUS;
627
		} else {
628
			return isNearRepellent(body) ? SoundEvents.PIGLIN_RETREAT : SoundEvents.PIGLIN_AMBIENT;
629
		}
630
	}
631
632
	private static boolean isNearAvoidTarget(final Piglin body) {
633
		Brain<Piglin> brain = body.getBrain();
634
		return !brain.hasMemoryValue(MemoryModuleType.AVOID_TARGET) ? false : brain.getMemory(MemoryModuleType.AVOID_TARGET).get().closerThan(body, 12.0);
635
	}
636
637
	protected static List<AbstractPiglin> getVisibleAdultPiglins(final Piglin body) {
638
		return body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_ADULT_PIGLINS).orElse(ImmutableList.of());
639
	}
640
641
	private static List<AbstractPiglin> getAdultPiglins(final AbstractPiglin body) {
642
		return body.getBrain().getMemory(MemoryModuleType.NEARBY_ADULT_PIGLINS).orElse(ImmutableList.of());
643
	}
644
645
	public static boolean isWearingSafeArmor(final LivingEntity livingEntity) {
646
		for (EquipmentSlot slot : EquipmentSlotGroup.ARMOR) {
647
			if (livingEntity.getItemBySlot(slot).is(ItemTags.PIGLIN_SAFE_ARMOR)) {
648
				return true;
649
			}
650
		}
651
652
		return false;
653
	}
654
655
	private static void stopWalking(final Piglin body) {
656
		body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
657
		body.getNavigation().stop();
658
	}
659
660
	private static BehaviorControl<LivingEntity> babySometimesRideBabyHoglin() {
661
		SetEntityLookTargetSometimes.Ticker ticker = new SetEntityLookTargetSometimes.Ticker(RIDE_START_INTERVAL);
662
		return CopyMemoryWithExpiry.create(
663
			e -> e.isBaby() && ticker.tickDownAndCheck(e.level().getRandom()), MemoryModuleType.NEAREST_VISIBLE_BABY_HOGLIN, MemoryModuleType.RIDE_TARGET, RIDE_DURATION
664
		);
665
	}
666
667
	protected static void broadcastAngerTarget(final ServerLevel level, final AbstractPiglin body, final LivingEntity target) {
668
		getAdultPiglins(body).forEach(piglin -> {
669
			if (!(target instanceof Hoglin hoglin && (!piglin.canHunt() || !hoglin.canBeHunted()))) {
670
				setAngerTargetIfCloserThanCurrent(level, piglin, target);
671
			}
672
		});
673
	}
674
675
	protected static void broadcastUniversalAnger(final ServerLevel level, final AbstractPiglin body) {
676
		getAdultPiglins(body).forEach(piglin -> getNearestVisibleTargetablePlayer(piglin).ifPresent(player -> setAngerTarget(level, piglin, player)));
677
	}
678
679
	protected static void setAngerTarget(final ServerLevel level, final AbstractPiglin body, final LivingEntity target) {
680
		if (Sensor.isEntityAttackableIgnoringLineOfSight(level, body, target)) {
681
			body.getBrain().eraseMemory(MemoryModuleType.CANT_REACH_WALK_TARGET_SINCE);
682
			body.getBrain().setMemoryWithExpiry(MemoryModuleType.ANGRY_AT, target.getUUID(), 600L);
683
			if (target.is(EntityTypes.HOGLIN) && body.canHunt()) {
684
				dontKillAnyMoreHoglinsForAWhile(body);
685
			}
686
687
			if (target.is(EntityTypes.PLAYER) && level.getGameRules().get(GameRules.UNIVERSAL_ANGER)) {
688
				body.getBrain().setMemoryWithExpiry(MemoryModuleType.UNIVERSAL_ANGER, true, 600L);
689
			}
690
		}
691
	}
692
693
	private static void setAngerTargetToNearestTargetablePlayerIfFound(
694
		final ServerLevel level, final AbstractPiglin body, final LivingEntity targetIfNoPlayerFound
695
	) {
696
		Optional<Player> nearestPlayer = getNearestVisibleTargetablePlayer(body);
697
		if (nearestPlayer.isPresent()) {
698
			setAngerTarget(level, body, nearestPlayer.get());
699
		} else {
700
			setAngerTarget(level, body, targetIfNoPlayerFound);
701
		}
702
	}
703
704
	private static void setAngerTargetIfCloserThanCurrent(final ServerLevel level, final AbstractPiglin body, final LivingEntity newTarget) {
705
		Optional<LivingEntity> currentTarget = getAngerTarget(body);
706
		LivingEntity nearest = BehaviorUtils.getNearestTarget(body, currentTarget, newTarget);
707
		if (!currentTarget.isPresent() || currentTarget.get() != nearest) {
708
			setAngerTarget(level, body, nearest);
709
		}
710
	}
711
712
	private static Optional<LivingEntity> getAngerTarget(final AbstractPiglin body) {
713
		return BehaviorUtils.getLivingEntityFromUUIDMemory(body, MemoryModuleType.ANGRY_AT);
714
	}
715
716
	public static Optional<LivingEntity> getAvoidTarget(final Piglin body) {
717
		return body.getBrain().hasMemoryValue(MemoryModuleType.AVOID_TARGET) ? body.getBrain().getMemory(MemoryModuleType.AVOID_TARGET) : Optional.empty();
718
	}
719
720
	public static Optional<Player> getNearestVisibleTargetablePlayer(final AbstractPiglin body) {
721
		return body.getBrain().hasMemoryValue(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER)
722
			? body.getBrain().getMemory(MemoryModuleType.NEAREST_VISIBLE_ATTACKABLE_PLAYER)
723
			: Optional.empty();
724
	}
725
726
	private static void broadcastRetreat(final Piglin body, final LivingEntity target) {
727
		getVisibleAdultPiglins(body).forEach(abstractPiglin -> {
728
			if (abstractPiglin instanceof Piglin piglin) {
729
				retreatFromNearestTarget(piglin, target);
730
			}
731
		});
732
	}
733
734
	private static void retreatFromNearestTarget(final Piglin body, final LivingEntity newAvoidTarget) {
735
		Brain<Piglin> brain = body.getBrain();
736
		LivingEntity nearest = newAvoidTarget;
737
		nearest = BehaviorUtils.getNearestTarget(body, brain.getMemory(MemoryModuleType.AVOID_TARGET), nearest);
738
		nearest = BehaviorUtils.getNearestTarget(body, brain.getMemory(MemoryModuleType.ATTACK_TARGET), nearest);
739
		setAvoidTargetAndDontHuntForAWhile(body, nearest);
740
	}
741
742
	private static boolean wantsToStopFleeing(final Piglin body) {
743
		Brain<Piglin> brain = body.getBrain();
744
		if (!brain.hasMemoryValue(MemoryModuleType.AVOID_TARGET)) {
745
			return true;
746
		} else {
747
			LivingEntity avoidedEntity = brain.getMemory(MemoryModuleType.AVOID_TARGET).get();
748
			if (avoidedEntity.is(EntityTypes.HOGLIN)) {
749
				return piglinsEqualOrOutnumberHoglins(body);
750
			} else {
751
				return isZombified(avoidedEntity) ? !brain.isMemoryValue(MemoryModuleType.NEAREST_VISIBLE_ZOMBIFIED, avoidedEntity) : false;
752
			}
753
		}
754
	}
755
756
	private static boolean piglinsEqualOrOutnumberHoglins(final Piglin body) {
757
		return !hoglinsOutnumberPiglins(body);
758
	}
759
760
	private static boolean hoglinsOutnumberPiglins(final Piglin body) {
761
		int piglinCount = body.getBrain().getMemory(MemoryModuleType.VISIBLE_ADULT_PIGLIN_COUNT).orElse(0) + 1;
762
		int hoglinCount = body.getBrain().getMemory(MemoryModuleType.VISIBLE_ADULT_HOGLIN_COUNT).orElse(0);
763
		return hoglinCount > piglinCount;
764
	}
765
766
	private static void setAvoidTargetAndDontHuntForAWhile(final Piglin body, final LivingEntity target) {
767
		body.getBrain().eraseMemory(MemoryModuleType.ANGRY_AT);
768
		body.getBrain().eraseMemory(MemoryModuleType.ATTACK_TARGET);
769
		body.getBrain().eraseMemory(MemoryModuleType.WALK_TARGET);
770
		body.getBrain().setMemoryWithExpiry(MemoryModuleType.AVOID_TARGET, target, RETREAT_DURATION.sample(body.level().getRandom()));
771
		dontKillAnyMoreHoglinsForAWhile(body);
772
	}
773
774
	protected static void dontKillAnyMoreHoglinsForAWhile(final AbstractPiglin body) {
775
		body.getBrain().setMemoryWithExpiry(MemoryModuleType.HUNTED_RECENTLY, true, TIME_BETWEEN_HUNTS.sample(body.level().getRandom()));
776
	}
777
778
	private static void eat(final Piglin body) {
779
		body.getBrain().setMemoryWithExpiry(MemoryModuleType.ATE_RECENTLY, true, 200L);
780
	}
781
782
	private static Vec3 getRandomNearbyPos(final Piglin body) {
783
		Vec3 targetVec = LandRandomPos.getPos(body, 4, 2);
784
		return targetVec == null ? body.position() : targetVec;
785
	}
786
787
	private static boolean hasEatenRecently(final Piglin body) {
788
		return body.getBrain().hasMemoryValue(MemoryModuleType.ATE_RECENTLY);
789
	}
790
791
	protected static boolean isIdle(final AbstractPiglin body) {
792
		return body.getBrain().isActive(Activity.IDLE);
793
	}
794
795
	private static boolean hasCrossbow(final LivingEntity body) {
796
		return body.isHolding(Items.CROSSBOW);
797
	}
798
799
	private static void admireGoldItem(final LivingEntity body) {
800
		body.getBrain().setMemoryWithExpiry(MemoryModuleType.ADMIRING_ITEM, true, 119L);
801
	}
802
803
	private static boolean isAdmiringItem(final Piglin body) {
804
		return body.getBrain().hasMemoryValue(MemoryModuleType.ADMIRING_ITEM);
805
	}
806
807
	private static boolean isBarterCurrency(final ItemStack itemStack) {
808
		return itemStack.is(BARTERING_ITEM);
809
	}
810
811
	private static boolean isFood(final ItemStack itemStack) {
812
		return itemStack.is(ItemTags.PIGLIN_FOOD);
813
	}
814
815
	private static boolean isNearRepellent(final Piglin body) {
816
		return body.getBrain().hasMemoryValue(MemoryModuleType.NEAREST_REPELLENT);
817
	}
818
819
	private static boolean seesPlayerHoldingLovedItem(final LivingEntity body) {
820
		return body.getBrain().hasMemoryValue(MemoryModuleType.NEAREST_PLAYER_HOLDING_WANTED_ITEM);
821
	}
822
823
	private static boolean doesntSeeAnyPlayerHoldingLovedItem(final LivingEntity body) {
824
		return !seesPlayerHoldingLovedItem(body);
825
	}
826
827
	public static boolean isPlayerHoldingLovedItem(final LivingEntity entity) {
828
		return entity.is(EntityTypes.PLAYER) && entity.isHolding(PiglinAi::isLovedItem);
829
	}
830
831
	private static boolean isAdmiringDisabled(final Piglin body) {
832
		return body.getBrain().hasMemoryValue(MemoryModuleType.ADMIRING_DISABLED);
833
	}
834
835
	private static boolean wasHurtRecently(final LivingEntity body) {
836
		return body.getBrain().hasMemoryValue(MemoryModuleType.HURT_BY);
837
	}
838
839
	private static boolean isHoldingItemInOffHand(final Piglin body) {
840
		return !body.getOffhandItem().isEmpty();
841
	}
842
843
	private static boolean isNotHoldingLovedItemInOffHand(final Piglin body) {
844
		return body.getOffhandItem().isEmpty() || !isLovedItem(body.getOffhandItem());
845
	}
846
847
	public static boolean isZombified(final Entity entity) {
848
		return entity.is(EntityTypes.ZOMBIFIED_PIGLIN) || entity.is(EntityTypes.ZOGLIN);
849
	}
850
851
	public static List<AbstractPiglin> findNearbyAdultPiglins(final Brain<?> brain) {
852
		List<LivingEntity> livingEntities = brain.getMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES).orElse(List.of());
853
		List<AbstractPiglin> adultPiglins = new ArrayList<>();
854
855
		for (LivingEntity entity : livingEntities) {
856
			if (entity instanceof AbstractPiglin piglin && piglin.isAdult()) {
857
				adultPiglins.add(piglin);
858
			}
859
		}
860
861
		return adultPiglins;
862
	}
863
}
864

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