Fox .java

net.minecraft.world.entity.animal.fox.Fox (class): members: FoxAlertableEntitiesSelector, FoxStrollThroughVillageGoal, DefendTrustedTargetGoal, SleepGoal.

net.minecraft.world.entity.animal.foxclass1,523 linescommonextends Animal48.2 KB

API Surface (104)

void defineSynchedData(final SynchedEntityData.Builder entityData)
void registerGoals()
void aiStep()
boolean isImmobile()
void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty)
void handleEntityEvent(final @EntityEvent.Value byte id)
AttributeSupplier.Builder createAttributes()
boolean checkFoxSpawnRules(final EntityType<Fox> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random)
void playEatingSound()
EntityDimensions getDefaultDimensions(final Pose pose)
Fox.Variant getVariant()
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
boolean isSitting()
void setSitting(final boolean value)
boolean isFaceplanted()
boolean isSleeping()
boolean canDispenserEquipIntoSlot(final EquipmentSlot slot)
boolean canHoldItem(final ItemStack itemStack)
void pickUpItem(final ServerLevel level, final ItemEntity entity)
void tick()
boolean isFood(final ItemStack itemStack)
void onOffspringSpawnedFromEgg(final Player spawner, final Mob offspring)
boolean isPouncing()
void setIsPouncing(final boolean pouncing)
boolean isFullyCrouched()
void setIsCrouching(final boolean isCrouching)
boolean isCrouching()
void setIsInterested(final boolean value)
boolean isInterested()
float getHeadRollAngle(final float a)
float getCrouchAmount(final float a)
void setTarget(final @Nullable LivingEntity target)
boolean canMove()
void playAmbientSound()
void dropAllDeathLoot(final ServerLevel level, final DamageSource source)
boolean isPathClear(final Fox fox, final LivingEntity target)
Vec3 getLeashOffset()
boolean canUse()
void start()
boolean canUse()
boolean canContinueToUse()
void start()
void stop()
void tick()
boolean test(final LivingEntity target, final ServerLevel level)
boolean hasShelter()
boolean alertable()
void start()
void breed()
double acceptedDistance()
boolean shouldRecalculatePath()
boolean isValidTarget(final LevelReader level, final BlockPos pos)
void tick()
void onReachedTarget()
boolean canUse()
void start()
void start()
boolean canUse()
boolean canUse()
boolean canContinueToUse()
void start()
boolean canUse()
boolean canContinueToUse()
void tick()
boolean resetXRotOnTick()
void checkAndPerformAttack(final LivingEntity target)
void start()
boolean canUse()
void tick()
boolean shouldPanic()
boolean canUse()
boolean canContinueToUse()
boolean isInterruptable()
void start()
void stop()
void tick()
boolean canUse()
void tick()
void start()
void start()
boolean canUse()
boolean canContinueToUse()
boolean canUse()
boolean canContinueToUse()
void start()
void stop()
void tick()
boolean canUse()
void start()
boolean canUse()
boolean canContinueToUse()
void stop()
void start()
boolean canUse()
void start()
void stop()
void tick()
String getSerializedName()
int getId()
Fox.Variant byId(final int id)
Fox.Variant byBiome(final Holder<Biome> biome)

Fields & Constants (33)

DATA_TYPE_ID
DATA_FLAGS_ID
BABY_SCALE
FLAG_SITTING
FLAG_CROUCHING
FLAG_INTERESTED
FLAG_POUNCING
FLAG_SLEEPING
FLAG_FACEPLANTED
FLAG_DEFENDING
DATA_TRUSTED_ID_0
DATA_TRUSTED_ID_1
ALLOWED_ITEMS
TRUSTED_TARGET_SELECTOR
STALKABLE_PREY
AVOID_PLAYERS
MIN_TICKS_BEFORE_EAT
BABY_DIMENSIONS
TRUSTED_LIST_CODEC
DEFAULT_SLEEPING
DEFAULT_SITTING
DEFAULT_CROUCHING
MAX_CROUCH_AMOUNT
landTargetGoal
turtleEggTargetGoal
fishTargetGoal
interestedAngle
interestedAngleO
crouchAmount
crouchAmountO
ticksSinceEaten
pos
spawnReason

String literals (5)

"Trusted"
"Sleeping"
"Sitting"
"Crouching"
"snow"

Same Package (1)

Code Graph: Dependencies (80)

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

Full Source

Fox.java1523 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.fox;
2
3
import com.mojang.serialization.Codec;
4
import io.netty.buffer.ByteBuf;
5
import java.util.EnumSet;
6
import java.util.List;
7
import java.util.Optional;
8
import java.util.function.IntFunction;
9
import java.util.function.Predicate;
10
import java.util.stream.Stream;
11
import net.minecraft.advancements.triggers.CriteriaTriggers;
12
import net.minecraft.core.BlockPos;
13
import net.minecraft.core.Holder;
14
import net.minecraft.core.component.DataComponentGetter;
15
import net.minecraft.core.component.DataComponentType;
16
import net.minecraft.core.component.DataComponents;
17
import net.minecraft.core.particles.ItemParticleOption;
18
import net.minecraft.core.particles.ParticleTypes;
19
import net.minecraft.network.codec.ByteBufCodecs;
20
import net.minecraft.network.codec.StreamCodec;
21
import net.minecraft.network.syncher.EntityDataAccessor;
22
import net.minecraft.network.syncher.EntityDataSerializers;
23
import net.minecraft.network.syncher.SynchedEntityData;
24
import net.minecraft.server.level.ServerLevel;
25
import net.minecraft.server.level.ServerPlayer;
26
import net.minecraft.sounds.SoundEvent;
27
import net.minecraft.sounds.SoundEvents;
28
import net.minecraft.stats.Stats;
29
import net.minecraft.tags.BiomeTags;
30
import net.minecraft.tags.BlockTags;
31
import net.minecraft.tags.ItemTags;
32
import net.minecraft.util.ByIdMap;
33
import net.minecraft.util.Mth;
34
import net.minecraft.util.RandomSource;
35
import net.minecraft.util.StringRepresentable;
36
import net.minecraft.world.DifficultyInstance;
37
import net.minecraft.world.damagesource.DamageSource;
38
import net.minecraft.world.entity.AgeableMob;
39
import net.minecraft.world.entity.Entity;
40
import net.minecraft.world.entity.EntityAttachment;
41
import net.minecraft.world.entity.EntityAttachments;
42
import net.minecraft.world.entity.EntityDimensions;
43
import net.minecraft.world.entity.EntityEvent;
44
import net.minecraft.world.entity.EntityReference;
45
import net.minecraft.world.entity.EntitySelector;
46
import net.minecraft.world.entity.EntitySpawnReason;
47
import net.minecraft.world.entity.EntityType;
48
import net.minecraft.world.entity.EntityTypes;
49
import net.minecraft.world.entity.EquipmentSlot;
50
import net.minecraft.world.entity.ExperienceOrb;
51
import net.minecraft.world.entity.LivingEntity;
52
import net.minecraft.world.entity.Mob;
53
import net.minecraft.world.entity.Pose;
54
import net.minecraft.world.entity.SpawnGroupData;
55
import net.minecraft.world.entity.TamableAnimal;
56
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
57
import net.minecraft.world.entity.ai.attributes.Attributes;
58
import net.minecraft.world.entity.ai.control.LookControl;
59
import net.minecraft.world.entity.ai.control.MoveControl;
60
import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
61
import net.minecraft.world.entity.ai.goal.BreedGoal;
62
import net.minecraft.world.entity.ai.goal.ClimbOnTopOfPowderSnowGoal;
63
import net.minecraft.world.entity.ai.goal.FleeSunGoal;
64
import net.minecraft.world.entity.ai.goal.FloatGoal;
65
import net.minecraft.world.entity.ai.goal.FollowParentGoal;
66
import net.minecraft.world.entity.ai.goal.Goal;
67
import net.minecraft.world.entity.ai.goal.JumpGoal;
68
import net.minecraft.world.entity.ai.goal.LeapAtTargetGoal;
69
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
70
import net.minecraft.world.entity.ai.goal.MeleeAttackGoal;
71
import net.minecraft.world.entity.ai.goal.MoveToBlockGoal;
72
import net.minecraft.world.entity.ai.goal.PanicGoal;
73
import net.minecraft.world.entity.ai.goal.StrollThroughVillageGoal;
74
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
75
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
76
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
77
import net.minecraft.world.entity.animal.Animal;
78
import net.minecraft.world.entity.animal.chicken.Chicken;
79
import net.minecraft.world.entity.animal.fish.AbstractFish;
80
import net.minecraft.world.entity.animal.fish.AbstractSchoolingFish;
81
import net.minecraft.world.entity.animal.polarbear.PolarBear;
82
import net.minecraft.world.entity.animal.rabbit.Rabbit;
83
import net.minecraft.world.entity.animal.turtle.Turtle;
84
import net.minecraft.world.entity.animal.wolf.Wolf;
85
import net.minecraft.world.entity.item.ItemEntity;
86
import net.minecraft.world.entity.monster.Monster;
87
import net.minecraft.world.entity.player.Player;
88
import net.minecraft.world.item.ItemStack;
89
import net.minecraft.world.item.ItemStackTemplate;
90
import net.minecraft.world.item.Items;
91
import net.minecraft.world.level.Level;
92
import net.minecraft.world.level.LevelAccessor;
93
import net.minecraft.world.level.LevelReader;
94
import net.minecraft.world.level.ServerLevelAccessor;
95
import net.minecraft.world.level.biome.Biome;
96
import net.minecraft.world.level.block.Block;
97
import net.minecraft.world.level.block.Blocks;
98
import net.minecraft.world.level.block.CaveVines;
99
import net.minecraft.world.level.block.SweetBerryBushBlock;
100
import net.minecraft.world.level.block.state.BlockState;
101
import net.minecraft.world.level.gameevent.GameEvent;
102
import net.minecraft.world.level.gamerules.GameRules;
103
import net.minecraft.world.level.pathfinder.PathType;
104
import net.minecraft.world.level.storage.ValueInput;
105
import net.minecraft.world.level.storage.ValueOutput;
106
import net.minecraft.world.phys.Vec3;
107
import org.jspecify.annotations.Nullable;
108
109
public class Fox extends Animal {
110
	private static final EntityDataAccessor<Integer> DATA_TYPE_ID = SynchedEntityData.defineId(Fox.class, EntityDataSerializers.INT);
111
	private static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(Fox.class, EntityDataSerializers.BYTE);
112
	private static final float BABY_SCALE = 0.6F;
113
	private static final int FLAG_SITTING = 1;
114
	public static final int FLAG_CROUCHING = 4;
115
	public static final int FLAG_INTERESTED = 8;
116
	public static final int FLAG_POUNCING = 16;
117
	private static final int FLAG_SLEEPING = 32;
118
	private static final int FLAG_FACEPLANTED = 64;
119
	private static final int FLAG_DEFENDING = 128;
120
	private static final EntityDataAccessor<Optional<EntityReference<LivingEntity>>> DATA_TRUSTED_ID_0 = SynchedEntityData.defineId(
121
		Fox.class, EntityDataSerializers.OPTIONAL_LIVING_ENTITY_REFERENCE
122
	);
123
	private static final EntityDataAccessor<Optional<EntityReference<LivingEntity>>> DATA_TRUSTED_ID_1 = SynchedEntityData.defineId(
124
		Fox.class, EntityDataSerializers.OPTIONAL_LIVING_ENTITY_REFERENCE
125
	);
126
	private static final Predicate<ItemEntity> ALLOWED_ITEMS = e -> !e.hasPickUpDelay() && e.isAlive();
127
	private static final Predicate<Entity> TRUSTED_TARGET_SELECTOR = entity -> !(entity instanceof LivingEntity livingEntity)
128
		? false
129
		: livingEntity.getLastHurtMob() != null && livingEntity.getLastHurtMobTimestamp() < livingEntity.tickCount + 600;
130
	private static final Predicate<Entity> STALKABLE_PREY = entity -> entity instanceof Chicken || entity instanceof Rabbit;
131
	private static final Predicate<Entity> AVOID_PLAYERS = entity -> !entity.isDiscrete() && EntitySelector.NO_CREATIVE_OR_SPECTATOR.test(entity);
132
	private static final int MIN_TICKS_BEFORE_EAT = 600;
133
	private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.FOX
134
		.getDimensions()
135
		.scale(0.6F)
136
		.withEyeHeight(0.34375F)
137
		.withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.375F, 0.0F));
138
	private static final Codec<List<EntityReference<LivingEntity>>> TRUSTED_LIST_CODEC = EntityReference.<LivingEntity>codec().listOf();
139
	private static final boolean DEFAULT_SLEEPING = false;
140
	private static final boolean DEFAULT_SITTING = false;
141
	private static final boolean DEFAULT_CROUCHING = false;
142
	private Goal landTargetGoal;
143
	private Goal turtleEggTargetGoal;
144
	private Goal fishTargetGoal;
145
	private float interestedAngle;
146
	private float interestedAngleO;
147
	private float crouchAmount;
148
	private float crouchAmountO;
149
	private static final float MAX_CROUCH_AMOUNT = 5.0F;
150
	private int ticksSinceEaten;
151
152
	public Fox(final EntityType<? extends Fox> type, final Level level) {
153
		super(type, level);
154
		this.lookControl = new Fox.FoxLookControl();
155
		this.moveControl = new Fox.FoxMoveControl<>(this);
156
		this.setPathfindingMalus(PathType.DAMAGING_IN_NEIGHBOR, 0.0F);
157
		this.setPathfindingMalus(PathType.DAMAGING, 0.0F);
158
		this.setCanPickUpLoot(true);
159
		this.getNavigation().setRequiredPathLength(32.0F);
160
	}
161
162
	@Override
163
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
164
		super.defineSynchedData(entityData);
165
		entityData.define(DATA_TRUSTED_ID_0, Optional.empty());
166
		entityData.define(DATA_TRUSTED_ID_1, Optional.empty());
167
		entityData.define(DATA_TYPE_ID, Fox.Variant.DEFAULT.getId());
168
		entityData.define(DATA_FLAGS_ID, (byte)0);
169
	}
170
171
	@Override
172
	protected void registerGoals() {
173
		this.landTargetGoal = new NearestAttackableTargetGoal<>(
174
			this, Animal.class, 10, false, false, (target, level) -> target instanceof Chicken || target instanceof Rabbit
175
		);
176
		this.turtleEggTargetGoal = new NearestAttackableTargetGoal<>(this, Turtle.class, 10, false, false, Turtle.BABY_ON_LAND_SELECTOR);
177
		this.fishTargetGoal = new NearestAttackableTargetGoal<>(
178
			this, AbstractFish.class, 20, false, false, (target, level) -> target instanceof AbstractSchoolingFish
179
		);
180
		this.goalSelector.addGoal(0, new Fox.FoxFloatGoal());
181
		this.goalSelector.addGoal(0, new ClimbOnTopOfPowderSnowGoal(this, this.level()));
182
		this.goalSelector.addGoal(1, new Fox.FaceplantGoal());
183
		this.goalSelector.addGoal(2, new Fox.FoxPanicGoal(2.2));
184
		this.goalSelector.addGoal(3, new Fox.FoxBreedGoal(1.0));
185
		this.goalSelector
186
			.addGoal(4, new AvoidEntityGoal<>(this, Player.class, 16.0F, 1.6, 1.4, entity -> AVOID_PLAYERS.test(entity) && !this.trusts(entity) && !this.isDefending()));
187
		this.goalSelector.addGoal(4, new AvoidEntityGoal<>(this, Wolf.class, 8.0F, 1.6, 1.4, entity -> !((Wolf)entity).isTame() && !this.isDefending()));
188
		this.goalSelector.addGoal(4, new AvoidEntityGoal<>(this, PolarBear.class, 8.0F, 1.6, 1.4, entity -> !this.isDefending()));
189
		this.goalSelector.addGoal(5, new Fox.StalkPreyGoal());
190
		this.goalSelector.addGoal(6, new Fox.FoxPounceGoal());
191
		this.goalSelector.addGoal(6, new Fox.SeekShelterGoal(1.25));
192
		this.goalSelector.addGoal(7, new Fox.FoxMeleeAttackGoal(1.2F, true));
193
		this.goalSelector.addGoal(7, new Fox.SleepGoal());
194
		this.goalSelector.addGoal(8, new Fox.FoxFollowParentGoal(this, 1.25));
195
		this.goalSelector.addGoal(9, new Fox.FoxStrollThroughVillageGoal(32, 200));
196
		this.goalSelector.addGoal(10, new Fox.FoxEatBerriesGoal(1.2F, 12, 1));
197
		this.goalSelector.addGoal(10, new LeapAtTargetGoal(this, 0.4F));
198
		this.goalSelector.addGoal(11, new WaterAvoidingRandomStrollGoal(this, 1.0));
199
		this.goalSelector.addGoal(11, new Fox.FoxSearchForItemsGoal());
200
		this.goalSelector.addGoal(12, new Fox.FoxLookAtPlayerGoal(this, Player.class, 24.0F));
201
		this.goalSelector.addGoal(13, new Fox.PerchAndSearchGoal());
202
		this.targetSelector
203
			.addGoal(
204
				3, new Fox.DefendTrustedTargetGoal(LivingEntity.class, false, false, (target, level) -> TRUSTED_TARGET_SELECTOR.test(target) && !this.trusts(target))
205
			);
206
	}
207
208
	@Override
209
	public void aiStep() {
210
		if (!this.level().isClientSide() && this.isAlive() && this.isEffectiveAi()) {
211
			this.ticksSinceEaten++;
212
			ItemStack itemInMouth = this.getItemBySlot(EquipmentSlot.MAINHAND);
213
			if (this.canEat(itemInMouth)) {
214
				if (this.ticksSinceEaten > 600) {
215
					ItemStack remainingFood = itemInMouth.finishUsingItem(this.level(), this);
216
					if (!remainingFood.isEmpty()) {
217
						this.setItemSlot(EquipmentSlot.MAINHAND, remainingFood);
218
					}
219
220
					this.ticksSinceEaten = 0;
221
				} else if (this.ticksSinceEaten > 560 && this.random.nextFloat() < 0.1F) {
222
					this.playEatingSound();
223
					this.level().broadcastEntityEvent(this, (byte)45);
224
				}
225
			}
226
227
			LivingEntity target = this.getTarget();
228
			if (target == null || !target.isAlive()) {
229
				this.setIsCrouching(false);
230
				this.setIsInterested(false);
231
			}
232
		}
233
234
		if (this.isSleeping() || this.isImmobile()) {
235
			this.jumping = false;
236
			this.xxa = 0.0F;
237
			this.zza = 0.0F;
238
		}
239
240
		super.aiStep();
241
		if (this.isDefending() && this.random.nextFloat() < 0.05F) {
242
			this.playSound(SoundEvents.FOX_AGGRO, 1.0F, 1.0F);
243
		}
244
	}
245
246
	@Override
247
	protected boolean isImmobile() {
248
		return this.isDeadOrDying();
249
	}
250
251
	private boolean canEat(final ItemStack itemInMouth) {
252
		return this.isConsumableFood(itemInMouth) && this.getTarget() == null && this.onGround() && !this.isSleeping();
253
	}
254
255
	private boolean isConsumableFood(final ItemStack itemStack) {
256
		return itemStack.has(DataComponents.FOOD) && itemStack.has(DataComponents.CONSUMABLE);
257
	}
258
259
	@Override
260
	protected void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty) {
261
		if (random.nextFloat() < 0.2F) {
262
			float odds = random.nextFloat();
263
			ItemStack heldInMouth;
264
			if (odds < 0.05F) {
265
				heldInMouth = new ItemStack(Items.EMERALD);
266
			} else if (odds < 0.2F) {
267
				heldInMouth = new ItemStack(Items.EGG);
268
			} else if (odds < 0.4F) {
269
				heldInMouth = random.nextBoolean() ? new ItemStack(Items.RABBIT_FOOT) : new ItemStack(Items.RABBIT_HIDE);
270
			} else if (odds < 0.6F) {
271
				heldInMouth = new ItemStack(Items.WHEAT);
272
			} else if (odds < 0.8F) {
273
				heldInMouth = new ItemStack(Items.LEATHER);
274
			} else {
275
				heldInMouth = new ItemStack(Items.FEATHER);
276
			}
277
278
			this.setItemSlot(EquipmentSlot.MAINHAND, heldInMouth);
279
		}
280
	}
281
282
	@Override
283
	public void handleEntityEvent(final @EntityEvent.Value byte id) {
284
		if (id == 45) {
285
			ItemStack mouthItem = this.getItemBySlot(EquipmentSlot.MAINHAND);
286
			if (!mouthItem.isEmpty()) {
287
				ItemParticleOption breakParticle = new ItemParticleOption(ParticleTypes.ITEM, ItemStackTemplate.fromNonEmptyStack(mouthItem));
288
289
				for (int i = 0; i < 8; i++) {
290
					Vec3 direction = new Vec3((this.random.nextFloat() - 0.5) * 0.1, this.random.nextFloat() * 0.1 + 0.1, 0.0)
291
						.xRot(-this.getXRot() * (float) (Math.PI / 180.0))
292
						.yRot(-this.getYRot() * (float) (Math.PI / 180.0));
293
					this.level()
294
						.addParticle(
295
							breakParticle,
296
							this.getX() + this.getLookAngle().x / 2.0,
297
							this.getY(),
298
							this.getZ() + this.getLookAngle().z / 2.0,
299
							direction.x,
300
							direction.y + 0.05,
301
							direction.z
302
						);
303
				}
304
			}
305
		} else {
306
			super.handleEntityEvent(id);
307
		}
308
	}
309
310
	public static AttributeSupplier.Builder createAttributes() {
311
		return Animal.createAnimalAttributes()
312
			.add(Attributes.MOVEMENT_SPEED, 0.3F)
313
			.add(Attributes.MAX_HEALTH, 10.0)
314
			.add(Attributes.ATTACK_DAMAGE, 2.0)
315
			.add(Attributes.SAFE_FALL_DISTANCE, 5.0)
316
			.add(Attributes.FOLLOW_RANGE, 32.0);
317
	}
318
319
	public @Nullable Fox getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
320
		Fox baby = EntityTypes.FOX.create(level, EntitySpawnReason.BREEDING);
321
		if (baby != null) {
322
			baby.setVariant(this.random.nextBoolean() ? this.getVariant() : ((Fox)partner).getVariant());
323
		}
324
325
		return baby;
326
	}
327
328
	public static boolean checkFoxSpawnRules(
329
		final EntityType<Fox> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
330
	) {
331
		return level.getBlockState(pos.below()).is(BlockTags.FOXES_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
332
	}
333
334
	@Override
335
	public @Nullable SpawnGroupData finalizeSpawn(
336
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
337
	) {
338
		Holder<Biome> biome = level.getBiome(this.blockPosition());
339
		Fox.Variant variant = Fox.Variant.byBiome(biome);
340
		boolean isBaby = false;
341
		if (groupData instanceof Fox.FoxGroupData foxGroupData) {
342
			variant = foxGroupData.variant;
343
			if (foxGroupData.getGroupSize() >= 2) {
344
				isBaby = true;
345
			}
346
		} else {
347
			groupData = new Fox.FoxGroupData(variant);
348
		}
349
350
		this.setVariant(variant);
351
		if (isBaby) {
352
			this.setAge(-24000);
353
		}
354
355
		if (level instanceof ServerLevel) {
356
			this.setTargetGoals();
357
		}
358
359
		this.populateDefaultEquipmentSlots(level.getRandom(), difficulty);
360
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
361
	}
362
363
	private void setTargetGoals() {
364
		if (this.getVariant() == Fox.Variant.RED) {
365
			this.targetSelector.addGoal(4, this.landTargetGoal);
366
			this.targetSelector.addGoal(4, this.turtleEggTargetGoal);
367
			this.targetSelector.addGoal(6, this.fishTargetGoal);
368
		} else {
369
			this.targetSelector.addGoal(4, this.fishTargetGoal);
370
			this.targetSelector.addGoal(6, this.landTargetGoal);
371
			this.targetSelector.addGoal(6, this.turtleEggTargetGoal);
372
		}
373
	}
374
375
	@Override
376
	protected void playEatingSound() {
377
		this.playSound(SoundEvents.FOX_EAT, 1.0F, 1.0F);
378
	}
379
380
	@Override
381
	public EntityDimensions getDefaultDimensions(final Pose pose) {
382
		return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
383
	}
384
385
	public Fox.Variant getVariant() {
386
		return Fox.Variant.byId(this.entityData.get(DATA_TYPE_ID));
387
	}
388
389
	private void setVariant(final Fox.Variant variant) {
390
		this.entityData.set(DATA_TYPE_ID, variant.getId());
391
	}
392
393
	@Override
394
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
395
		return type == DataComponents.FOX_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
396
	}
397
398
	@Override
399
	protected void applyImplicitComponents(final DataComponentGetter components) {
400
		this.applyImplicitComponentIfPresent(components, DataComponents.FOX_VARIANT);
401
		super.applyImplicitComponents(components);
402
	}
403
404
	@Override
405
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
406
		if (type == DataComponents.FOX_VARIANT) {
407
			this.setVariant(castComponentValue(DataComponents.FOX_VARIANT, value));
408
			return true;
409
		} else {
410
			return super.applyImplicitComponent(type, value);
411
		}
412
	}
413
414
	private Stream<EntityReference<LivingEntity>> getTrustedEntities() {
415
		return Stream.concat(this.entityData.get(DATA_TRUSTED_ID_0).stream(), this.entityData.get(DATA_TRUSTED_ID_1).stream());
416
	}
417
418
	private void addTrustedEntity(final LivingEntity entity) {
419
		this.addTrustedEntity(EntityReference.of(entity));
420
	}
421
422
	private void addTrustedEntity(final EntityReference<LivingEntity> reference) {
423
		if (this.entityData.get(DATA_TRUSTED_ID_0).isPresent()) {
424
			this.entityData.set(DATA_TRUSTED_ID_1, Optional.of(reference));
425
		} else {
426
			this.entityData.set(DATA_TRUSTED_ID_0, Optional.of(reference));
427
		}
428
	}
429
430
	@Override
431
	protected void addAdditionalSaveData(final ValueOutput output) {
432
		super.addAdditionalSaveData(output);
433
		output.store("Trusted", TRUSTED_LIST_CODEC, this.getTrustedEntities().toList());
434
		output.putBoolean("Sleeping", this.isSleeping());
435
		output.store("Type", Fox.Variant.CODEC, this.getVariant());
436
		output.putBoolean("Sitting", this.isSitting());
437
		output.putBoolean("Crouching", this.isCrouching());
438
	}
439
440
	@Override
441
	protected void readAdditionalSaveData(final ValueInput input) {
442
		super.readAdditionalSaveData(input);
443
		this.clearTrusted();
444
		input.read("Trusted", TRUSTED_LIST_CODEC).orElse(List.of()).forEach(this::addTrustedEntity);
445
		this.setSleeping(input.getBooleanOr("Sleeping", false));
446
		this.setVariant(input.read("Type", Fox.Variant.CODEC).orElse(Fox.Variant.DEFAULT));
447
		this.setSitting(input.getBooleanOr("Sitting", false));
448
		this.setIsCrouching(input.getBooleanOr("Crouching", false));
449
		if (this.level() instanceof ServerLevel) {
450
			this.setTargetGoals();
451
		}
452
	}
453
454
	private void clearTrusted() {
455
		this.entityData.set(DATA_TRUSTED_ID_0, Optional.empty());
456
		this.entityData.set(DATA_TRUSTED_ID_1, Optional.empty());
457
	}
458
459
	public boolean isSitting() {
460
		return this.getFlag(1);
461
	}
462
463
	public void setSitting(final boolean value) {
464
		this.setFlag(1, value);
465
	}
466
467
	public boolean isFaceplanted() {
468
		return this.getFlag(64);
469
	}
470
471
	private void setFaceplanted(final boolean faceplanted) {
472
		this.setFlag(64, faceplanted);
473
	}
474
475
	private boolean isDefending() {
476
		return this.getFlag(128);
477
	}
478
479
	private void setDefending(final boolean defending) {
480
		this.setFlag(128, defending);
481
	}
482
483
	@Override
484
	public boolean isSleeping() {
485
		return this.getFlag(32);
486
	}
487
488
	private void setSleeping(final boolean sleeping) {
489
		this.setFlag(32, sleeping);
490
	}
491
492
	private void setFlag(final int flag, final boolean value) {
493
		if (value) {
494
			this.entityData.set(DATA_FLAGS_ID, (byte)(this.entityData.get(DATA_FLAGS_ID) | flag));
495
		} else {
496
			this.entityData.set(DATA_FLAGS_ID, (byte)(this.entityData.get(DATA_FLAGS_ID) & ~flag));
497
		}
498
	}
499
500
	private boolean getFlag(final int flag) {
501
		return (this.entityData.get(DATA_FLAGS_ID) & flag) != 0;
502
	}
503
504
	@Override
505
	protected boolean canDispenserEquipIntoSlot(final EquipmentSlot slot) {
506
		return slot == EquipmentSlot.MAINHAND && this.canPickUpLoot();
507
	}
508
509
	@Override
510
	public boolean canHoldItem(final ItemStack itemStack) {
511
		ItemStack heldItemStack = this.getItemBySlot(EquipmentSlot.MAINHAND);
512
		return heldItemStack.isEmpty() || this.ticksSinceEaten > 0 && this.isConsumableFood(itemStack) && !this.isConsumableFood(heldItemStack);
513
	}
514
515
	private void spitOutItem(final ItemStack itemStack) {
516
		if (!itemStack.isEmpty() && !this.level().isClientSide()) {
517
			ItemEntity thrownItem = new ItemEntity(this.level(), this.getX() + this.getLookAngle().x, this.getY() + 1.0, this.getZ() + this.getLookAngle().z, itemStack);
518
			thrownItem.setPickUpDelay(40);
519
			thrownItem.setThrower(this);
520
			this.playSound(SoundEvents.FOX_SPIT, 1.0F, 1.0F);
521
			this.level().addFreshEntity(thrownItem);
522
		}
523
	}
524
525
	private void dropItemStack(final ItemStack itemStack) {
526
		ItemEntity itemEntity = new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), itemStack);
527
		this.level().addFreshEntity(itemEntity);
528
	}
529
530
	@Override
531
	protected void pickUpItem(final ServerLevel level, final ItemEntity entity) {
532
		ItemStack itemStack = entity.getItem();
533
		if (this.canHoldItem(itemStack)) {
534
			int count = itemStack.getCount();
535
			if (count > 1) {
536
				this.dropItemStack(itemStack.split(count - 1));
537
			}
538
539
			this.spitOutItem(this.getItemBySlot(EquipmentSlot.MAINHAND));
540
			this.onItemPickup(entity);
541
			this.setItemSlot(EquipmentSlot.MAINHAND, itemStack.split(1));
542
			this.setGuaranteedDrop(EquipmentSlot.MAINHAND);
543
			this.take(entity, itemStack.getCount());
544
			entity.discard();
545
			this.ticksSinceEaten = 0;
546
		}
547
	}
548
549
	@Override
550
	public void tick() {
551
		super.tick();
552
		if (this.isEffectiveAi()) {
553
			boolean inWater = this.isInWater();
554
			if (inWater || this.getTarget() != null || this.level().isThundering()) {
555
				this.wakeUp();
556
			}
557
558
			if (inWater || this.isSleeping()) {
559
				this.setSitting(false);
560
			}
561
562
			if (this.isFaceplanted() && this.level().getRandom().nextFloat() < 0.2F) {
563
				BlockPos pos = this.blockPosition();
564
				BlockState state = this.level().getBlockState(pos);
565
				this.level().levelEvent(2001, pos, Block.getId(state));
566
			}
567
		}
568
569
		this.interestedAngleO = this.interestedAngle;
570
		if (this.isInterested()) {
571
			this.interestedAngle = this.interestedAngle + (1.0F - this.interestedAngle) * 0.4F;
572
		} else {
573
			this.interestedAngle = this.interestedAngle + (0.0F - this.interestedAngle) * 0.4F;
574
		}
575
576
		this.crouchAmountO = this.crouchAmount;
577
		if (this.isCrouching()) {
578
			this.crouchAmount += 0.2F;
579
			if (this.crouchAmount > 5.0F) {
580
				this.crouchAmount = 5.0F;
581
			}
582
		} else {
583
			this.crouchAmount = 0.0F;
584
		}
585
	}
586
587
	@Override
588
	public boolean isFood(final ItemStack itemStack) {
589
		return itemStack.is(ItemTags.FOX_FOOD);
590
	}
591
592
	@Override
593
	protected void onOffspringSpawnedFromEgg(final Player spawner, final Mob offspring) {
594
		((Fox)offspring).addTrustedEntity(spawner);
595
	}
596
597
	public boolean isPouncing() {
598
		return this.getFlag(16);
599
	}
600
601
	public void setIsPouncing(final boolean pouncing) {
602
		this.setFlag(16, pouncing);
603
	}
604
605
	public boolean isFullyCrouched() {
606
		return this.crouchAmount == 5.0F;
607
	}
608
609
	public void setIsCrouching(final boolean isCrouching) {
610
		this.setFlag(4, isCrouching);
611
	}
612
613
	@Override
614
	public boolean isCrouching() {
615
		return this.getFlag(4);
616
	}
617
618
	public void setIsInterested(final boolean value) {
619
		this.setFlag(8, value);
620
	}
621
622
	public boolean isInterested() {
623
		return this.getFlag(8);
624
	}
625
626
	public float getHeadRollAngle(final float a) {
627
		return Mth.lerp(a, this.interestedAngleO, this.interestedAngle) * 0.11F * (float) Math.PI;
628
	}
629
630
	public float getCrouchAmount(final float a) {
631
		return Mth.lerp(a, this.crouchAmountO, this.crouchAmount);
632
	}
633
634
	@Override
635
	public void setTarget(final @Nullable LivingEntity target) {
636
		if (this.isDefending() && target == null) {
637
			this.setDefending(false);
638
		}
639
640
		super.setTarget(target);
641
	}
642
643
	private void wakeUp() {
644
		this.setSleeping(false);
645
	}
646
647
	private void clearStates() {
648
		this.setIsInterested(false);
649
		this.setIsCrouching(false);
650
		this.setSitting(false);
651
		this.setSleeping(false);
652
		this.setDefending(false);
653
		this.setFaceplanted(false);
654
	}
655
656
	public boolean canMove() {
657
		return !this.isSleeping() && !this.isSitting() && !this.isFaceplanted();
658
	}
659
660
	@Override
661
	public void playAmbientSound() {
662
		SoundEvent ambient = this.getAmbientSound();
663
		if (ambient == SoundEvents.FOX_SCREECH) {
664
			this.playSound(ambient, 2.0F, this.getVoicePitch());
665
		} else {
666
			super.playAmbientSound();
667
		}
668
	}
669
670
	@Override
671
	protected @Nullable SoundEvent getAmbientSound() {
672
		if (this.isSleeping()) {
673
			return SoundEvents.FOX_SLEEP;
674
		}
675
676
		if (!this.level().isBrightOutside() && this.random.nextFloat() < 0.1F) {
677
			List<Player> nearbyEntities = this.level().getEntitiesOfClass(Player.class, this.getBoundingBox().inflate(16.0, 16.0, 16.0), EntitySelector.NO_SPECTATORS);
678
			if (nearbyEntities.isEmpty()) {
679
				return SoundEvents.FOX_SCREECH;
680
			}
681
		}
682
683
		return SoundEvents.FOX_AMBIENT;
684
	}
685
686
	@Override
687
	protected @Nullable SoundEvent getHurtSound(final DamageSource source) {
688
		return SoundEvents.FOX_HURT;
689
	}
690
691
	@Override
692
	protected @Nullable SoundEvent getDeathSound() {
693
		return SoundEvents.FOX_DEATH;
694
	}
695
696
	private boolean trusts(final LivingEntity entity) {
697
		return this.getTrustedEntities().anyMatch(trusted -> trusted.matches(entity));
698
	}
699
700
	@Override
701
	protected void dropAllDeathLoot(final ServerLevel level, final DamageSource source) {
702
		ItemStack itemStack = this.getItemBySlot(EquipmentSlot.MAINHAND);
703
		if (!itemStack.isEmpty()) {
704
			this.spawnAtLocation(level, itemStack);
705
			this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
706
		}
707
708
		super.dropAllDeathLoot(level, source);
709
	}
710
711
	public static boolean isPathClear(final Fox fox, final LivingEntity target) {
712
		double zdiff = target.getZ() - fox.getZ();
713
		double xdiff = target.getX() - fox.getX();
714
		double slope = zdiff / xdiff;
715
		int increments = 6;
716
717
		for (int i = 0; i < 6; i++) {
718
			double z = slope == 0.0 ? 0.0 : zdiff * (i / 6.0F);
719
			double x = slope == 0.0 ? xdiff * (i / 6.0F) : z / slope;
720
721
			for (int j = 1; j < 4; j++) {
722
				if (!fox.level().getBlockState(BlockPos.containing(fox.getX() + x, fox.getY() + j, fox.getZ() + z)).canBeReplaced()) {
723
					return false;
724
				}
725
			}
726
		}
727
728
		return true;
729
	}
730
731
	@Override
732
	public Vec3 getLeashOffset() {
733
		return new Vec3(0.0, 0.55F * this.getEyeHeight(), this.getBbWidth() * 0.4F);
734
	}
735
736
	private class DefendTrustedTargetGoal extends NearestAttackableTargetGoal<LivingEntity> {
737
		private @Nullable LivingEntity trustedLastHurtBy;
738
		private @Nullable LivingEntity trustedLastHurt;
739
		private int timestamp;
740
741
		public DefendTrustedTargetGoal(
742
			final Class<LivingEntity> targetType, final boolean mustSee, final boolean mustReach, final TargetingConditions.@Nullable Selector subselector
743
		) {
744
			super(Fox.this, targetType, 10, mustSee, mustReach, subselector);
745
		}
746
747
		@Override
748
		public boolean canUse() {
749
			if (this.randomInterval > 0 && this.mob.getRandom().nextInt(this.randomInterval) != 0) {
750
				return false;
751
			}
752
753
			ServerLevel level = getServerLevel(Fox.this.level());
754
755
			for (EntityReference<LivingEntity> trustedReference : Fox.this.getTrustedEntities().toList()) {
756
				LivingEntity trustedEntity = trustedReference.getEntity(level, LivingEntity.class);
757
				if (trustedEntity != null) {
758
					this.trustedLastHurt = trustedEntity;
759
					this.trustedLastHurtBy = trustedEntity.getLastHurtByMob();
760
					int timestamp = trustedEntity.getLastHurtByMobTimestamp();
761
					return timestamp != this.timestamp && this.canAttack(this.trustedLastHurtBy, this.targetConditions);
762
				}
763
			}
764
765
			return false;
766
		}
767
768
		@Override
769
		public void start() {
770
			this.setTarget(this.trustedLastHurtBy);
771
			this.target = this.trustedLastHurtBy;
772
			if (this.trustedLastHurt != null) {
773
				this.timestamp = this.trustedLastHurt.getLastHurtByMobTimestamp();
774
			}
775
776
			Fox.this.playSound(SoundEvents.FOX_AGGRO, 1.0F, 1.0F);
777
			Fox.this.setDefending(true);
778
			Fox.this.wakeUp();
779
			super.start();
780
		}
781
	}
782
783
	private class FaceplantGoal extends Goal {
784
		private int countdown;
785
786
		public FaceplantGoal() {
787
			this.setFlags(EnumSet.of(Goal.Flag.LOOK, Goal.Flag.JUMP, Goal.Flag.MOVE));
788
		}
789
790
		@Override
791
		public boolean canUse() {
792
			return Fox.this.isFaceplanted();
793
		}
794
795
		@Override
796
		public boolean canContinueToUse() {
797
			return this.canUse() && this.countdown > 0;
798
		}
799
800
		@Override
801
		public void start() {
802
			this.countdown = this.adjustedTickDelay(40);
803
		}
804
805
		@Override
806
		public void stop() {
807
			Fox.this.setFaceplanted(false);
808
		}
809
810
		@Override
811
		public void tick() {
812
			this.countdown--;
813
		}
814
	}
815
816
	public class FoxAlertableEntitiesSelector implements TargetingConditions.Selector {
817
		@Override
818
		public boolean test(final LivingEntity target, final ServerLevel level) {
819
			if (target instanceof Fox) {
820
				return false;
821
			} else if (target instanceof Chicken || target instanceof Rabbit || target instanceof Monster) {
822
				return true;
823
			} else if (target instanceof TamableAnimal tamableAnimal) {
824
				return !tamableAnimal.isTame();
825
			} else if (target instanceof Player player && (player.isSpectator() || player.isCreative())) {
826
				return false;
827
			} else {
828
				return Fox.this.trusts(target) ? false : !target.isSleeping() && !target.isDiscrete();
829
			}
830
		}
831
	}
832
833
	private abstract class FoxBehaviorGoal extends Goal {
834
		private final TargetingConditions alertableTargeting = TargetingConditions.forCombat()
835
			.range(12.0)
836
			.ignoreLineOfSight()
837
			.selector(Fox.this.new FoxAlertableEntitiesSelector());
838
839
		protected boolean hasShelter() {
840
			BlockPos foxPos = BlockPos.containing(Fox.this.getX(), Fox.this.getBoundingBox().maxY, Fox.this.getZ());
841
			return !Fox.this.level().canSeeSky(foxPos) && Fox.this.getWalkTargetValue(foxPos) >= 0.0F;
842
		}
843
844
		protected boolean alertable() {
845
			return !getServerLevel(Fox.this.level())
846
				.getNearbyEntities(LivingEntity.class, this.alertableTargeting, Fox.this, Fox.this.getBoundingBox().inflate(12.0, 6.0, 12.0))
847
				.isEmpty();
848
		}
849
	}
850
851
	private class FoxBreedGoal extends BreedGoal {
852
		public FoxBreedGoal(final double speedModifier) {
853
			super(Fox.this, speedModifier);
854
		}
855
856
		@Override
857
		public void start() {
858
			((Fox)this.animal).clearStates();
859
			((Fox)this.partner).clearStates();
860
			super.start();
861
		}
862
863
		@Override
864
		protected void breed() {
865
			Fox offspring = (Fox)this.animal.getBreedOffspring(this.level, this.partner);
866
			if (offspring != null) {
867
				ServerPlayer animalLoveCause = this.animal.getLoveCause();
868
				ServerPlayer partnerLoveCause = this.partner.getLoveCause();
869
				ServerPlayer loveCause = animalLoveCause;
870
				if (animalLoveCause != null) {
871
					offspring.addTrustedEntity(animalLoveCause);
872
				} else {
873
					loveCause = partnerLoveCause;
874
				}
875
876
				if (partnerLoveCause != null && animalLoveCause != partnerLoveCause) {
877
					offspring.addTrustedEntity(partnerLoveCause);
878
				}
879
880
				if (loveCause != null) {
881
					loveCause.awardStat(Stats.ANIMALS_BRED);
882
					CriteriaTriggers.BRED_ANIMALS.trigger(loveCause, this.animal, this.partner, offspring);
883
				}
884
885
				this.animal.setAge(6000);
886
				this.partner.setAge(6000);
887
				this.animal.resetLove();
888
				this.partner.resetLove();
889
				offspring.setAge(-24000);
890
				offspring.snapTo(this.animal.getX(), this.animal.getY(), this.animal.getZ(), 0.0F, 0.0F);
891
				this.level.addFreshEntityWithPassengers(offspring);
892
				this.level.broadcastEntityEvent(this.animal, (byte)18);
893
				if (this.level.getGameRules().get(GameRules.MOB_DROPS)) {
894
					this.level
895
						.addFreshEntity(new ExperienceOrb(this.level, this.animal.getX(), this.animal.getY(), this.animal.getZ(), this.animal.getRandom().nextInt(7) + 1));
896
				}
897
			}
898
		}
899
	}
900
901
	public class FoxEatBerriesGoal extends MoveToBlockGoal {
902
		private static final int WAIT_TICKS = 40;
903
		protected int ticksWaited;
904
905
		public FoxEatBerriesGoal(final double speedModifier, final int searchRange, final int verticalSearchRange) {
906
			super(Fox.this, speedModifier, searchRange, verticalSearchRange);
907
		}
908
909
		@Override
910
		public double acceptedDistance() {
911
			return 2.0;
912
		}
913
914
		@Override
915
		public boolean shouldRecalculatePath() {
916
			return this.tryTicks % 100 == 0;
917
		}
918
919
		@Override
920
		protected boolean isValidTarget(final LevelReader level, final BlockPos pos) {
921
			BlockState blockState = level.getBlockState(pos);
922
			return blockState.is(Blocks.SWEET_BERRY_BUSH) && blockState.getValue(SweetBerryBushBlock.AGE) >= 2 || CaveVines.hasGlowBerries(blockState);
923
		}
924
925
		@Override
926
		public void tick() {
927
			if (this.isReachedTarget()) {
928
				if (this.ticksWaited >= 40) {
929
					this.onReachedTarget();
930
				} else {
931
					this.ticksWaited++;
932
				}
933
			} else if (!this.isReachedTarget() && Fox.this.random.nextFloat() < 0.05F) {
934
				Fox.this.playSound(SoundEvents.FOX_SNIFF, 1.0F, 1.0F);
935
			}
936
937
			super.tick();
938
		}
939
940
		protected void onReachedTarget() {
941
			if (getServerLevel(Fox.this.level()).getGameRules().get(GameRules.MOB_GRIEFING)) {
942
				BlockState state = Fox.this.level().getBlockState(this.blockPos);
943
				if (state.is(Blocks.SWEET_BERRY_BUSH)) {
944
					this.pickSweetBerries(state);
945
				} else if (CaveVines.hasGlowBerries(state)) {
946
					this.pickGlowBerry(state);
947
				}
948
			}
949
		}
950
951
		private void pickGlowBerry(final BlockState state) {
952
			CaveVines.use(Fox.this, state, Fox.this.level(), this.blockPos);
953
		}
954
955
		private void pickSweetBerries(final BlockState state) {
956
			int age = state.getValue(SweetBerryBushBlock.AGE);
957
			state.setValue(SweetBerryBushBlock.AGE, 1);
958
			int count = 1 + Fox.this.level().getRandom().nextInt(2) + (age == 3 ? 1 : 0);
959
			ItemStack heldItem = Fox.this.getItemBySlot(EquipmentSlot.MAINHAND);
960
			if (heldItem.isEmpty()) {
961
				Fox.this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.SWEET_BERRIES));
962
				count--;
963
			}
964
965
			if (count > 0) {
966
				Block.popResource(Fox.this.level(), this.blockPos, new ItemStack(Items.SWEET_BERRIES, count));
967
			}
968
969
			Fox.this.playSound(SoundEvents.SWEET_BERRY_BUSH_PICK_BERRIES, 1.0F, 1.0F);
970
			Fox.this.level().setBlock(this.blockPos, state.setValue(SweetBerryBushBlock.AGE, 1), 2);
971
			Fox.this.level().gameEvent(GameEvent.BLOCK_CHANGE, this.blockPos, GameEvent.Context.of(Fox.this));
972
		}
973
974
		@Override
975
		public boolean canUse() {
976
			return !Fox.this.isSleeping() && super.canUse();
977
		}
978
979
		@Override
980
		public void start() {
981
			this.ticksWaited = 0;
982
			Fox.this.setSitting(false);
983
			super.start();
984
		}
985
	}
986
987
	private class FoxFloatGoal extends FloatGoal {
988
		public FoxFloatGoal() {
989
			super(Fox.this);
990
		}
991
992
		@Override
993
		public void start() {
994
			super.start();
995
			Fox.this.clearStates();
996
		}
997
998
		@Override
999
		public boolean canUse() {
1000
			return Fox.this.isInFluidDeeperThan(0.25, this.fluid) || Fox.this.isInLava();
1001
		}
1002
	}
1003
1004
	private static class FoxFollowParentGoal extends FollowParentGoal {
1005
		private final Fox fox;
1006
1007
		public FoxFollowParentGoal(final Fox fox, final double speedModifier) {
1008
			super(fox, speedModifier);
1009
			this.fox = fox;
1010
		}
1011
1012
		@Override
1013
		public boolean canUse() {
1014
			return !this.fox.isDefending() && super.canUse();
1015
		}
1016
1017
		@Override
1018
		public boolean canContinueToUse() {
1019
			return !this.fox.isDefending() && super.canContinueToUse();
1020
		}
1021
1022
		@Override
1023
		public void start() {
1024
			this.fox.clearStates();
1025
			super.start();
1026
		}
1027
	}
1028
1029
	public static class FoxGroupData extends AgeableMob.AgeableMobGroupData {
1030
		public final Fox.Variant variant;
1031
1032
		public FoxGroupData(final Fox.Variant variant) {
1033
			super(false);
1034
			this.variant = variant;
1035
		}
1036
	}
1037
1038
	private class FoxLookAtPlayerGoal extends LookAtPlayerGoal {
1039
		public FoxLookAtPlayerGoal(final Mob mob, final Class<? extends LivingEntity> lookAtType, final float lookDistance) {
1040
			super(mob, lookAtType, lookDistance);
1041
		}
1042
1043
		@Override
1044
		public boolean canUse() {
1045
			return super.canUse() && !Fox.this.isFaceplanted() && !Fox.this.isInterested();
1046
		}
1047
1048
		@Override
1049
		public boolean canContinueToUse() {
1050
			return super.canContinueToUse() && !Fox.this.isFaceplanted() && !Fox.this.isInterested();
1051
		}
1052
	}
1053
1054
	public class FoxLookControl extends LookControl {
1055
		public FoxLookControl() {
1056
			super(Fox.this);
1057
		}
1058
1059
		@Override
1060
		public void tick() {
1061
			if (!Fox.this.isSleeping()) {
1062
				super.tick();
1063
			}
1064
		}
1065
1066
		@Override
1067
		protected boolean resetXRotOnTick() {
1068
			return !Fox.this.isPouncing() && !Fox.this.isCrouching() && !Fox.this.isInterested() && !Fox.this.isFaceplanted();
1069
		}
1070
	}
1071
1072
	private class FoxMeleeAttackGoal extends MeleeAttackGoal {
1073
		public FoxMeleeAttackGoal(final double speedModifier, final boolean trackTarget) {
1074
			super(Fox.this, speedModifier, trackTarget);
1075
		}
1076
1077
		@Override
1078
		protected void checkAndPerformAttack(final LivingEntity target) {
1079
			if (this.canPerformAttack(target)) {
1080
				this.resetAttackCooldown();
1081
				this.mob.doHurtTarget(getServerLevel(this.mob), target);
1082
				Fox.this.playSound(SoundEvents.FOX_BITE, 1.0F, 1.0F);
1083
			}
1084
		}
1085
1086
		@Override
1087
		public void start() {
1088
			Fox.this.setIsInterested(false);
1089
			super.start();
1090
		}
1091
1092
		@Override
1093
		public boolean canUse() {
1094
			return !Fox.this.isSitting() && !Fox.this.isSleeping() && !Fox.this.isCrouching() && !Fox.this.isFaceplanted() && super.canUse();
1095
		}
1096
	}
1097
1098
	private static class FoxMoveControl<T extends Fox> extends MoveControl<T> {
1099
		public FoxMoveControl(final T fox) {
1100
			super(fox);
1101
		}
1102
1103
		@Override
1104
		public void tick() {
1105
			if (this.mob.canMove()) {
1106
				super.tick();
1107
			}
1108
		}
1109
	}
1110
1111
	private class FoxPanicGoal extends PanicGoal {
1112
		public FoxPanicGoal(final double speedModifier) {
1113
			super(Fox.this, speedModifier);
1114
		}
1115
1116
		@Override
1117
		public boolean shouldPanic() {
1118
			return !Fox.this.isDefending() && super.shouldPanic();
1119
		}
1120
	}
1121
1122
	public class FoxPounceGoal extends JumpGoal {
1123
		@Override
1124
		public boolean canUse() {
1125
			if (!Fox.this.isFullyCrouched()) {
1126
				return false;
1127
			}
1128
1129
			LivingEntity target = Fox.this.getTarget();
1130
			if (target != null && target.isAlive()) {
1131
				if (target.getMotionDirection() != target.getDirection()) {
1132
					return false;
1133
				}
1134
1135
				boolean hasClearPath = Fox.isPathClear(Fox.this, target);
1136
				if (!hasClearPath) {
1137
					Fox.this.getNavigation().createPath(target, 0);
1138
					Fox.this.setIsCrouching(false);
1139
					Fox.this.setIsInterested(false);
1140
				}
1141
1142
				return hasClearPath;
1143
			} else {
1144
				return false;
1145
			}
1146
		}
1147
1148
		@Override
1149
		public boolean canContinueToUse() {
1150
			LivingEntity target = Fox.this.getTarget();
1151
			if (target != null && target.isAlive()) {
1152
				double yd = Fox.this.getDeltaMovement().y;
1153
				return (!(yd * yd < 0.05F) || !(Math.abs(Fox.this.getXRot()) < 15.0F) || !Fox.this.onGround()) && !Fox.this.isFaceplanted();
1154
			} else {
1155
				return false;
1156
			}
1157
		}
1158
1159
		@Override
1160
		public boolean isInterruptable() {
1161
			return false;
1162
		}
1163
1164
		@Override
1165
		public void start() {
1166
			Fox.this.setJumping(true);
1167
			Fox.this.setIsPouncing(true);
1168
			Fox.this.setIsInterested(false);
1169
			LivingEntity target = Fox.this.getTarget();
1170
			if (target != null) {
1171
				Fox.this.getLookControl().setLookAt(target, 60.0F, 30.0F);
1172
				Vec3 uv = new Vec3(target.getX() - Fox.this.getX(), target.getY() - Fox.this.getY(), target.getZ() - Fox.this.getZ()).normalize();
1173
				Fox.this.setDeltaMovement(Fox.this.getDeltaMovement().add(uv.x * 0.8, 0.9, uv.z * 0.8));
1174
			}
1175
1176
			Fox.this.getNavigation().stop();
1177
		}
1178
1179
		@Override
1180
		public void stop() {
1181
			Fox.this.setIsCrouching(false);
1182
			Fox.this.crouchAmount = 0.0F;
1183
			Fox.this.crouchAmountO = 0.0F;
1184
			Fox.this.setIsInterested(false);
1185
			Fox.this.setIsPouncing(false);
1186
		}
1187
1188
		@Override
1189
		public void tick() {
1190
			LivingEntity target = Fox.this.getTarget();
1191
			if (target != null) {
1192
				Fox.this.getLookControl().setLookAt(target, 60.0F, 30.0F);
1193
			}
1194
1195
			if (!Fox.this.isFaceplanted()) {
1196
				Vec3 movement = Fox.this.getDeltaMovement();
1197
				if (movement.y * movement.y < 0.03F && Fox.this.getXRot() != 0.0F) {
1198
					Fox.this.setXRot(Mth.rotLerp(0.2F, Fox.this.getXRot(), 0.0F));
1199
				} else {
1200
					double direction = movement.horizontalDistance();
1201
					float upwardsBias = Fox.this.jumping && movement.y > 0.0 ? 6.5F : 1.0F;
1202
					double biasedY = movement.y * upwardsBias;
1203
					double len = Math.sqrt(direction * direction + biasedY * biasedY);
1204
					if (len > 1.0E-5F) {
1205
						double rotation = Math.signum(-biasedY) * Math.acos(direction / len) * 180.0F / (float)Math.PI;
1206
						Fox.this.setXRot((float)rotation);
1207
					}
1208
				}
1209
			}
1210
1211
			if (target != null && Fox.this.distanceTo(target) <= 2.0F) {
1212
				Fox.this.doHurtTarget(getServerLevel(Fox.this.level()), target);
1213
			} else if (Fox.this.getXRot() > 0.0F
1214
				&& Fox.this.onGround()
1215
				&& (float)Fox.this.getDeltaMovement().y != 0.0F
1216
				&& Fox.this.level().getBlockState(Fox.this.blockPosition()).is(Blocks.SNOW)) {
1217
				Fox.this.setXRot(60.0F);
1218
				Fox.this.setTarget(null);
1219
				Fox.this.setFaceplanted(true);
1220
			}
1221
		}
1222
	}
1223
1224
	private class FoxSearchForItemsGoal extends Goal {
1225
		public FoxSearchForItemsGoal() {
1226
			this.setFlags(EnumSet.of(Goal.Flag.MOVE));
1227
		}
1228
1229
		@Override
1230
		public boolean canUse() {
1231
			if (!Fox.this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty()) {
1232
				return false;
1233
			}
1234
1235
			if (Fox.this.getTarget() != null || Fox.this.getLastHurtByMob() != null) {
1236
				return false;
1237
			}
1238
1239
			if (!Fox.this.canMove()) {
1240
				return false;
1241
			}
1242
1243
			if (Fox.this.getRandom().nextInt(reducedTickDelay(10)) != 0) {
1244
				return false;
1245
			}
1246
1247
			List<ItemEntity> items = Fox.this.level().getEntitiesOfClass(ItemEntity.class, Fox.this.getBoundingBox().inflate(8.0, 8.0, 8.0), Fox.ALLOWED_ITEMS);
1248
			return !items.isEmpty() && Fox.this.getItemBySlot(EquipmentSlot.MAINHAND).isEmpty();
1249
		}
1250
1251
		@Override
1252
		public void tick() {
1253
			List<ItemEntity> items = Fox.this.level().getEntitiesOfClass(ItemEntity.class, Fox.this.getBoundingBox().inflate(8.0, 8.0, 8.0), Fox.ALLOWED_ITEMS);
1254
			ItemStack itemStack = Fox.this.getItemBySlot(EquipmentSlot.MAINHAND);
1255
			if (itemStack.isEmpty() && !items.isEmpty()) {
1256
				Fox.this.getNavigation().moveTo(items.get(0), 1.2F);
1257
			}
1258
		}
1259
1260
		@Override
1261
		public void start() {
1262
			List<ItemEntity> items = Fox.this.level().getEntitiesOfClass(ItemEntity.class, Fox.this.getBoundingBox().inflate(8.0, 8.0, 8.0), Fox.ALLOWED_ITEMS);
1263
			if (!items.isEmpty()) {
1264
				Fox.this.getNavigation().moveTo(items.get(0), 1.2F);
1265
			}
1266
		}
1267
	}
1268
1269
	private class FoxStrollThroughVillageGoal extends StrollThroughVillageGoal {
1270
		public FoxStrollThroughVillageGoal(final int searchRadius, final int interval) {
1271
			super(Fox.this, interval);
1272
		}
1273
1274
		@Override
1275
		public void start() {
1276
			Fox.this.clearStates();
1277
			super.start();
1278
		}
1279
1280
		@Override
1281
		public boolean canUse() {
1282
			return super.canUse() && this.canFoxMove();
1283
		}
1284
1285
		@Override
1286
		public boolean canContinueToUse() {
1287
			return super.canContinueToUse() && this.canFoxMove();
1288
		}
1289
1290
		private boolean canFoxMove() {
1291
			return !Fox.this.isSleeping() && !Fox.this.isSitting() && !Fox.this.isDefending() && Fox.this.getTarget() == null;
1292
		}
1293
	}
1294
1295
	private class PerchAndSearchGoal extends Fox.FoxBehaviorGoal {
1296
		private double relX;
1297
		private double relZ;
1298
		private int lookTime;
1299
		private int looksRemaining;
1300
1301
		public PerchAndSearchGoal() {
1302
			this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
1303
		}
1304
1305
		@Override
1306
		public boolean canUse() {
1307
			return Fox.this.getLastHurtByMob() == null
1308
				&& Fox.this.getRandom().nextFloat() < 0.02F
1309
				&& !Fox.this.isSleeping()
1310
				&& Fox.this.getTarget() == null
1311
				&& Fox.this.getNavigation().isDone()
1312
				&& !this.alertable()
1313
				&& !Fox.this.isPouncing()
1314
				&& !Fox.this.isCrouching();
1315
		}
1316
1317
		@Override
1318
		public boolean canContinueToUse() {
1319
			return this.looksRemaining > 0;
1320
		}
1321
1322
		@Override
1323
		public void start() {
1324
			this.resetLook();
1325
			this.looksRemaining = 2 + Fox.this.getRandom().nextInt(3);
1326
			Fox.this.setSitting(true);
1327
			Fox.this.getNavigation().stop();
1328
		}
1329
1330
		@Override
1331
		public void stop() {
1332
			Fox.this.setSitting(false);
1333
		}
1334
1335
		@Override
1336
		public void tick() {
1337
			this.lookTime--;
1338
			if (this.lookTime <= 0) {
1339
				this.looksRemaining--;
1340
				this.resetLook();
1341
			}
1342
1343
			Fox.this.getLookControl()
1344
				.setLookAt(Fox.this.getX() + this.relX, Fox.this.getEyeY(), Fox.this.getZ() + this.relZ, Fox.this.getMaxHeadYRot(), Fox.this.getMaxHeadXRot());
1345
		}
1346
1347
		private void resetLook() {
1348
			double rnd = (Math.PI * 2) * Fox.this.getRandom().nextDouble();
1349
			this.relX = Math.cos(rnd);
1350
			this.relZ = Math.sin(rnd);
1351
			this.lookTime = this.adjustedTickDelay(80 + Fox.this.getRandom().nextInt(20));
1352
		}
1353
	}
1354
1355
	private class SeekShelterGoal extends FleeSunGoal {
1356
		private int interval = reducedTickDelay(100);
1357
1358
		public SeekShelterGoal(final double speedModifier) {
1359
			super(Fox.this, speedModifier);
1360
		}
1361
1362
		@Override
1363
		public boolean canUse() {
1364
			if (!Fox.this.isSleeping() && this.mob.getTarget() == null) {
1365
				if (Fox.this.level().isThundering() && Fox.this.level().canSeeSky(this.mob.blockPosition())) {
1366
					return this.setWantedPos();
1367
				} else if (this.interval > 0) {
1368
					this.interval--;
1369
					return false;
1370
				} else {
1371
					this.interval = 100;
1372
					BlockPos pos = this.mob.blockPosition();
1373
					return Fox.this.level().isBrightOutside() && Fox.this.level().canSeeSky(pos) && !((ServerLevel)Fox.this.level()).isVillage(pos) && this.setWantedPos();
1374
				}
1375
			} else {
1376
				return false;
1377
			}
1378
		}
1379
1380
		@Override
1381
		public void start() {
1382
			Fox.this.clearStates();
1383
			super.start();
1384
		}
1385
	}
1386
1387
	private class SleepGoal extends Fox.FoxBehaviorGoal {
1388
		private static final int WAIT_TIME_BEFORE_SLEEP = reducedTickDelay(140);
1389
		private int countdown = Fox.this.random.nextInt(WAIT_TIME_BEFORE_SLEEP);
1390
1391
		public SleepGoal() {
1392
			this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK, Goal.Flag.JUMP));
1393
		}
1394
1395
		@Override
1396
		public boolean canUse() {
1397
			return Fox.this.xxa == 0.0F && Fox.this.yya == 0.0F && Fox.this.zza == 0.0F ? this.canSleep() || Fox.this.isSleeping() : false;
1398
		}
1399
1400
		@Override
1401
		public boolean canContinueToUse() {
1402
			return this.canSleep();
1403
		}
1404
1405
		private boolean canSleep() {
1406
			if (this.countdown > 0) {
1407
				this.countdown--;
1408
				return false;
1409
			} else {
1410
				return Fox.this.level().isBrightOutside() && this.hasShelter() && !this.alertable() && !Fox.this.isInPowderSnow;
1411
			}
1412
		}
1413
1414
		@Override
1415
		public void stop() {
1416
			this.countdown = Fox.this.random.nextInt(WAIT_TIME_BEFORE_SLEEP);
1417
			Fox.this.clearStates();
1418
		}
1419
1420
		@Override
1421
		public void start() {
1422
			Fox.this.setSitting(false);
1423
			Fox.this.setIsCrouching(false);
1424
			Fox.this.setIsInterested(false);
1425
			Fox.this.setJumping(false);
1426
			Fox.this.setSleeping(true);
1427
			Fox.this.getNavigation().stop();
1428
			Fox.this.getMoveControl().setWantedPosition(Fox.this.getX(), Fox.this.getY(), Fox.this.getZ(), 0.0);
1429
		}
1430
	}
1431
1432
	private class StalkPreyGoal extends Goal {
1433
		public StalkPreyGoal() {
1434
			this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
1435
		}
1436
1437
		@Override
1438
		public boolean canUse() {
1439
			if (Fox.this.isSleeping()) {
1440
				return false;
1441
			}
1442
1443
			LivingEntity target = Fox.this.getTarget();
1444
			return target != null
1445
				&& target.isAlive()
1446
				&& Fox.STALKABLE_PREY.test(target)
1447
				&& Fox.this.distanceToSqr(target) > 36.0
1448
				&& !Fox.this.isCrouching()
1449
				&& !Fox.this.isInterested()
1450
				&& !Fox.this.jumping;
1451
		}
1452
1453
		@Override
1454
		public void start() {
1455
			Fox.this.setSitting(false);
1456
			Fox.this.setFaceplanted(false);
1457
		}
1458
1459
		@Override
1460
		public void stop() {
1461
			LivingEntity target = Fox.this.getTarget();
1462
			if (target != null && Fox.isPathClear(Fox.this, target)) {
1463
				Fox.this.setIsInterested(true);
1464
				Fox.this.setIsCrouching(true);
1465
				Fox.this.getNavigation().stop();
1466
				Fox.this.getLookControl().setLookAt(target, Fox.this.getMaxHeadYRot(), Fox.this.getMaxHeadXRot());
1467
			} else {
1468
				Fox.this.setIsInterested(false);
1469
				Fox.this.setIsCrouching(false);
1470
			}
1471
		}
1472
1473
		@Override
1474
		public void tick() {
1475
			LivingEntity target = Fox.this.getTarget();
1476
			if (target != null) {
1477
				Fox.this.getLookControl().setLookAt(target, Fox.this.getMaxHeadYRot(), Fox.this.getMaxHeadXRot());
1478
				if (Fox.this.distanceToSqr(target) <= 36.0) {
1479
					Fox.this.setIsInterested(true);
1480
					Fox.this.setIsCrouching(true);
1481
					Fox.this.getNavigation().stop();
1482
				} else {
1483
					Fox.this.getNavigation().moveTo(target, 1.5);
1484
				}
1485
			}
1486
		}
1487
	}
1488
1489
	public enum Variant implements StringRepresentable {
1490
		RED(0, "red"),
1491
		SNOW(1, "snow");
1492
1493
		public static final Fox.Variant DEFAULT = RED;
1494
		public static final Codec<Fox.Variant> CODEC = StringRepresentable.fromEnum(Fox.Variant::values);
1495
		private static final IntFunction<Fox.Variant> BY_ID = ByIdMap.continuous(Fox.Variant::getId, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
1496
		public static final StreamCodec<ByteBuf, Fox.Variant> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Fox.Variant::getId);
1497
		private final int id;
1498
		private final String name;
1499
1500
		Variant(final int id, final String name) {
1501
			this.id = id;
1502
			this.name = name;
1503
		}
1504
1505
		@Override
1506
		public String getSerializedName() {
1507
			return this.name;
1508
		}
1509
1510
		public int getId() {
1511
			return this.id;
1512
		}
1513
1514
		public static Fox.Variant byId(final int id) {
1515
			return BY_ID.apply(id);
1516
		}
1517
1518
		public static Fox.Variant byBiome(final Holder<Biome> biome) {
1519
			return biome.is(BiomeTags.SPAWNS_SNOW_FOXES) ? SNOW : RED;
1520
		}
1521
	}
1522
}
1523

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