Parrot .java

net.minecraft.world.entity.animal.parrot.Parrot (class): members: checkParrotSpawnRules, NOT_PARROT_PREDICATE, imitateNearbyMobs, ParrotWanderGoal, oFlap.

net.minecraft.world.entity.animal.parrotclass552 linescommonextends ShoulderRidingEntity19.3 KB

API Surface (39)

boolean test(final @Nullable Mob input)
boolean canBeABaby()
void registerGoals()
AttributeSupplier.Builder createAttributes()
PathNavigation createNavigation(final Level level)
void aiStep()
void setRecordPlayingNearby(final BlockPos jukebox, final boolean isPlaying)
boolean isPartyParrot()
boolean imitateNearbyMobs(final Level level, final Entity entity)
InteractionResult mobInteract(final Player player, final InteractionHand hand)
boolean isFood(final ItemStack itemStack)
boolean checkParrotSpawnRules(final EntityType<Parrot> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random)
void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos)
boolean canMate(final Animal partner)
SoundEvent getAmbient(final Level level, final RandomSource random)
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
void playStepSound(final BlockPos pos, final BlockState blockState)
boolean isFlapping()
void onFlap()
float getVoicePitch()
float getPitch(final RandomSource random)
SoundSource getSoundSource()
boolean isPushable()
void doPush(final Entity entity)
boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage)
Parrot.Variant getVariant()
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)
void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
boolean isFlying()
boolean omnidirectionalAirMover()
boolean canFlyToOwner()
Vec3 getLeashOffset()
int getId()
Parrot.Variant byId(final int id)
String getSerializedName()

Fields & Constants (13)

DATA_VARIANT_ID
NOT_PARROT_PREDICATE
MOB_SOUND_MAP
flap
flapSpeed
oFlapSpeed
oFlap
flapping
nextFlap
partyParrot
jukebox
spawnReason
pos

String literals (6)

"Variant"
"red_blue"
"blue"
"green"
"yellow_blue"
"gray"

Code Graph: Dependencies (65)

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

Full Source

Parrot.java552 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.parrot;
2
3
import com.google.common.collect.Lists;
4
import com.google.common.collect.Maps;
5
import com.mojang.serialization.Codec;
6
import io.netty.buffer.ByteBuf;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.function.IntFunction;
10
import java.util.function.Predicate;
11
import net.minecraft.core.BlockPos;
12
import net.minecraft.core.Direction;
13
import net.minecraft.core.component.DataComponentGetter;
14
import net.minecraft.core.component.DataComponentType;
15
import net.minecraft.core.component.DataComponents;
16
import net.minecraft.network.codec.ByteBufCodecs;
17
import net.minecraft.network.codec.StreamCodec;
18
import net.minecraft.network.syncher.EntityDataAccessor;
19
import net.minecraft.network.syncher.EntityDataSerializers;
20
import net.minecraft.network.syncher.SynchedEntityData;
21
import net.minecraft.server.level.ServerLevel;
22
import net.minecraft.sounds.SoundEvent;
23
import net.minecraft.sounds.SoundEvents;
24
import net.minecraft.sounds.SoundSource;
25
import net.minecraft.tags.BlockTags;
26
import net.minecraft.tags.ItemTags;
27
import net.minecraft.util.ByIdMap;
28
import net.minecraft.util.Mth;
29
import net.minecraft.util.RandomSource;
30
import net.minecraft.util.StringRepresentable;
31
import net.minecraft.util.Util;
32
import net.minecraft.world.Difficulty;
33
import net.minecraft.world.DifficultyInstance;
34
import net.minecraft.world.InteractionHand;
35
import net.minecraft.world.InteractionResult;
36
import net.minecraft.world.damagesource.DamageSource;
37
import net.minecraft.world.effect.MobEffectInstance;
38
import net.minecraft.world.effect.MobEffects;
39
import net.minecraft.world.entity.AgeableMob;
40
import net.minecraft.world.entity.Entity;
41
import net.minecraft.world.entity.EntitySpawnReason;
42
import net.minecraft.world.entity.EntityType;
43
import net.minecraft.world.entity.EntityTypes;
44
import net.minecraft.world.entity.Mob;
45
import net.minecraft.world.entity.PathfinderMob;
46
import net.minecraft.world.entity.SpawnGroupData;
47
import net.minecraft.world.entity.TamableAnimal;
48
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
49
import net.minecraft.world.entity.ai.attributes.Attributes;
50
import net.minecraft.world.entity.ai.control.FlyingMoveControl;
51
import net.minecraft.world.entity.ai.goal.FloatGoal;
52
import net.minecraft.world.entity.ai.goal.FollowMobGoal;
53
import net.minecraft.world.entity.ai.goal.FollowOwnerGoal;
54
import net.minecraft.world.entity.ai.goal.LandOnOwnersShoulderGoal;
55
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
56
import net.minecraft.world.entity.ai.goal.SitWhenOrderedToGoal;
57
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomFlyingGoal;
58
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
59
import net.minecraft.world.entity.ai.navigation.PathNavigation;
60
import net.minecraft.world.entity.ai.util.LandRandomPos;
61
import net.minecraft.world.entity.animal.Animal;
62
import net.minecraft.world.entity.player.Player;
63
import net.minecraft.world.item.ItemStack;
64
import net.minecraft.world.level.Level;
65
import net.minecraft.world.level.LevelAccessor;
66
import net.minecraft.world.level.ServerLevelAccessor;
67
import net.minecraft.world.level.block.Blocks;
68
import net.minecraft.world.level.block.LeavesBlock;
69
import net.minecraft.world.level.block.state.BlockState;
70
import net.minecraft.world.level.pathfinder.PathType;
71
import net.minecraft.world.level.storage.ValueInput;
72
import net.minecraft.world.level.storage.ValueOutput;
73
import net.minecraft.world.phys.Vec3;
74
import org.jspecify.annotations.Nullable;
75
76
public class Parrot extends ShoulderRidingEntity {
77
	private static final EntityDataAccessor<Integer> DATA_VARIANT_ID = SynchedEntityData.defineId(Parrot.class, EntityDataSerializers.INT);
78
	private static final Predicate<Mob> NOT_PARROT_PREDICATE = new Predicate<Mob>() {
79
		public boolean test(final @Nullable Mob input) {
80
			return input != null && Parrot.MOB_SOUND_MAP.containsKey(input.getType());
81
		}
82
	};
83
	private static final Map<EntityType<?>, SoundEvent> MOB_SOUND_MAP = Util.make(Maps.newHashMap(), map -> {
84
		map.put(EntityTypes.BLAZE, SoundEvents.PARROT_IMITATE_BLAZE);
85
		map.put(EntityTypes.BOGGED, SoundEvents.PARROT_IMITATE_BOGGED);
86
		map.put(EntityTypes.BREEZE, SoundEvents.PARROT_IMITATE_BREEZE);
87
		map.put(EntityTypes.CAMEL_HUSK, SoundEvents.PARROT_IMITATE_CAMEL_HUSK);
88
		map.put(EntityTypes.CAVE_SPIDER, SoundEvents.PARROT_IMITATE_SPIDER);
89
		map.put(EntityTypes.CREAKING, SoundEvents.PARROT_IMITATE_CREAKING);
90
		map.put(EntityTypes.CREEPER, SoundEvents.PARROT_IMITATE_CREEPER);
91
		map.put(EntityTypes.DROWNED, SoundEvents.PARROT_IMITATE_DROWNED);
92
		map.put(EntityTypes.ELDER_GUARDIAN, SoundEvents.PARROT_IMITATE_ELDER_GUARDIAN);
93
		map.put(EntityTypes.ENDER_DRAGON, SoundEvents.PARROT_IMITATE_ENDER_DRAGON);
94
		map.put(EntityTypes.ENDERMITE, SoundEvents.PARROT_IMITATE_ENDERMITE);
95
		map.put(EntityTypes.EVOKER, SoundEvents.PARROT_IMITATE_EVOKER);
96
		map.put(EntityTypes.GHAST, SoundEvents.PARROT_IMITATE_GHAST);
97
		map.put(EntityTypes.HAPPY_GHAST, SoundEvents.EMPTY);
98
		map.put(EntityTypes.GUARDIAN, SoundEvents.PARROT_IMITATE_GUARDIAN);
99
		map.put(EntityTypes.HOGLIN, SoundEvents.PARROT_IMITATE_HOGLIN);
100
		map.put(EntityTypes.HUSK, SoundEvents.PARROT_IMITATE_HUSK);
101
		map.put(EntityTypes.ILLUSIONER, SoundEvents.PARROT_IMITATE_ILLUSIONER);
102
		map.put(EntityTypes.MAGMA_CUBE, SoundEvents.PARROT_IMITATE_MAGMA_CUBE);
103
		map.put(EntityTypes.PARCHED, SoundEvents.PARROT_IMITATE_PARCHED);
104
		map.put(EntityTypes.PHANTOM, SoundEvents.PARROT_IMITATE_PHANTOM);
105
		map.put(EntityTypes.PIGLIN, SoundEvents.PARROT_IMITATE_PIGLIN);
106
		map.put(EntityTypes.PIGLIN_BRUTE, SoundEvents.PARROT_IMITATE_PIGLIN_BRUTE);
107
		map.put(EntityTypes.PILLAGER, SoundEvents.PARROT_IMITATE_PILLAGER);
108
		map.put(EntityTypes.RAVAGER, SoundEvents.PARROT_IMITATE_RAVAGER);
109
		map.put(EntityTypes.SHULKER, SoundEvents.PARROT_IMITATE_SHULKER);
110
		map.put(EntityTypes.SILVERFISH, SoundEvents.PARROT_IMITATE_SILVERFISH);
111
		map.put(EntityTypes.SKELETON, SoundEvents.PARROT_IMITATE_SKELETON);
112
		map.put(EntityTypes.SLIME, SoundEvents.PARROT_IMITATE_SLIME);
113
		map.put(EntityTypes.SPIDER, SoundEvents.PARROT_IMITATE_SPIDER);
114
		map.put(EntityTypes.STRAY, SoundEvents.PARROT_IMITATE_STRAY);
115
		map.put(EntityTypes.VEX, SoundEvents.PARROT_IMITATE_VEX);
116
		map.put(EntityTypes.VINDICATOR, SoundEvents.PARROT_IMITATE_VINDICATOR);
117
		map.put(EntityTypes.WARDEN, SoundEvents.PARROT_IMITATE_WARDEN);
118
		map.put(EntityTypes.WITCH, SoundEvents.PARROT_IMITATE_WITCH);
119
		map.put(EntityTypes.WITHER, SoundEvents.PARROT_IMITATE_WITHER);
120
		map.put(EntityTypes.WITHER_SKELETON, SoundEvents.PARROT_IMITATE_WITHER_SKELETON);
121
		map.put(EntityTypes.ZOGLIN, SoundEvents.PARROT_IMITATE_ZOGLIN);
122
		map.put(EntityTypes.ZOMBIE, SoundEvents.PARROT_IMITATE_ZOMBIE);
123
		map.put(EntityTypes.ZOMBIE_HORSE, SoundEvents.PARROT_IMITATE_ZOMBIE_HORSE);
124
		map.put(EntityTypes.ZOMBIE_NAUTILUS, SoundEvents.PARROT_IMITATE_ZOMBIE_NAUTILUS);
125
		map.put(EntityTypes.ZOMBIE_VILLAGER, SoundEvents.PARROT_IMITATE_ZOMBIE_VILLAGER);
126
	});
127
	public float flap;
128
	public float flapSpeed;
129
	public float oFlapSpeed;
130
	public float oFlap;
131
	private float flapping = 1.0F;
132
	private float nextFlap = 1.0F;
133
	private boolean partyParrot;
134
	private @Nullable BlockPos jukebox;
135
136
	public Parrot(final EntityType<? extends Parrot> type, final Level level) {
137
		super(type, level);
138
		this.moveControl = new FlyingMoveControl<>(this, 10, false);
139
		this.setPathfindingMalus(PathType.FIRE_IN_NEIGHBOR, -1.0F);
140
		this.setPathfindingMalus(PathType.FIRE, -1.0F);
141
		this.setPathfindingMalus(PathType.COCOA, -1.0F);
142
	}
143
144
	@Override
145
	public @Nullable SpawnGroupData finalizeSpawn(
146
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
147
	) {
148
		this.setVariant(Util.getRandom(Parrot.Variant.values(), level.getRandom()));
149
		if (groupData == null) {
150
			groupData = new AgeableMob.AgeableMobGroupData(false);
151
		}
152
153
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
154
	}
155
156
	@Override
157
	protected boolean canBeABaby() {
158
		return false;
159
	}
160
161
	@Override
162
	protected void registerGoals() {
163
		this.goalSelector.addGoal(0, new TamableAnimal.TamableAnimalPanicGoal(1.25));
164
		this.goalSelector.addGoal(0, new FloatGoal(this));
165
		this.goalSelector.addGoal(1, new LookAtPlayerGoal(this, Player.class, 8.0F));
166
		this.goalSelector.addGoal(2, new SitWhenOrderedToGoal(this));
167
		this.goalSelector.addGoal(2, new FollowOwnerGoal(this, 1.0, 5.0F, 1.0F));
168
		this.goalSelector.addGoal(2, new Parrot.ParrotWanderGoal(this, 1.0));
169
		this.goalSelector.addGoal(3, new LandOnOwnersShoulderGoal(this));
170
		this.goalSelector.addGoal(3, new FollowMobGoal(this, 1.0, 3.0F, 7.0F));
171
	}
172
173
	public static AttributeSupplier.Builder createAttributes() {
174
		return Animal.createAnimalAttributes()
175
			.add(Attributes.MAX_HEALTH, 6.0)
176
			.add(Attributes.FLYING_SPEED, 0.4F)
177
			.add(Attributes.MOVEMENT_SPEED, 0.2F)
178
			.add(Attributes.ATTACK_DAMAGE, 3.0);
179
	}
180
181
	@Override
182
	protected PathNavigation createNavigation(final Level level) {
183
		FlyingPathNavigation flyingPathNavigation = new FlyingPathNavigation(this, level);
184
		flyingPathNavigation.setCanOpenDoors(false);
185
		flyingPathNavigation.setCanFloat(true);
186
		return flyingPathNavigation;
187
	}
188
189
	@Override
190
	public void aiStep() {
191
		if (this.jukebox == null || !this.jukebox.closerToCenterThan(this.position(), 3.46) || !this.level().getBlockState(this.jukebox).is(Blocks.JUKEBOX)) {
192
			this.partyParrot = false;
193
			this.jukebox = null;
194
		}
195
196
		if (this.level().getRandom().nextInt(400) == 0) {
197
			imitateNearbyMobs(this.level(), this);
198
		}
199
200
		super.aiStep();
201
		this.calculateFlapping();
202
	}
203
204
	@Override
205
	public void setRecordPlayingNearby(final BlockPos jukebox, final boolean isPlaying) {
206
		this.jukebox = jukebox;
207
		this.partyParrot = isPlaying;
208
	}
209
210
	public boolean isPartyParrot() {
211
		return this.partyParrot;
212
	}
213
214
	private void calculateFlapping() {
215
		this.oFlap = this.flap;
216
		this.oFlapSpeed = this.flapSpeed;
217
		this.flapSpeed = this.flapSpeed + (!this.onGround() && !this.isPassenger() ? 4 : -1) * 0.3F;
218
		this.flapSpeed = Mth.clamp(this.flapSpeed, 0.0F, 1.0F);
219
		if (!this.onGround() && this.flapping < 1.0F) {
220
			this.flapping = 1.0F;
221
		}
222
223
		this.flapping *= 0.9F;
224
		Vec3 movement = this.getDeltaMovement();
225
		if (!this.onGround() && movement.y < 0.0) {
226
			this.setDeltaMovement(movement.multiply(1.0, 0.6, 1.0));
227
		}
228
229
		this.flap = this.flap + this.flapping * 2.0F;
230
	}
231
232
	public static boolean imitateNearbyMobs(final Level level, final Entity entity) {
233
		RandomSource random = level.getRandom();
234
		if (entity.isAlive() && !entity.isSilent() && random.nextInt(2) == 0) {
235
			List<Mob> mobs = level.getEntitiesOfClass(Mob.class, entity.getBoundingBox().inflate(20.0), NOT_PARROT_PREDICATE);
236
			if (!mobs.isEmpty()) {
237
				Mob mob = mobs.get(random.nextInt(mobs.size()));
238
				if (!mob.isSilent()) {
239
					SoundEvent soundEvent = getImitatedSound(mob.getType());
240
					level.playSound(null, entity.getX(), entity.getY(), entity.getZ(), soundEvent, entity.getSoundSource(), 0.7F, getPitch(random));
241
					return true;
242
				}
243
			}
244
245
			return false;
246
		} else {
247
			return false;
248
		}
249
	}
250
251
	@Override
252
	public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
253
		ItemStack itemStack = player.getItemInHand(hand);
254
		if (!this.isTame() && itemStack.is(ItemTags.PARROT_FOOD)) {
255
			this.usePlayerItem(player, hand, itemStack);
256
			if (!this.isSilent()) {
257
				this.level()
258
					.playSound(
259
						null,
260
						this.getX(),
261
						this.getY(),
262
						this.getZ(),
263
						SoundEvents.PARROT_EAT,
264
						this.getSoundSource(),
265
						1.0F,
266
						1.0F + (this.random.nextFloat() - this.random.nextFloat()) * 0.2F
267
					);
268
			}
269
270
			if (!this.level().isClientSide()) {
271
				if (this.random.nextInt(10) == 0) {
272
					this.tame(player);
273
					this.level().broadcastEntityEvent(this, (byte)7);
274
				} else {
275
					this.level().broadcastEntityEvent(this, (byte)6);
276
				}
277
			}
278
279
			return InteractionResult.SUCCESS;
280
		} else if (!itemStack.is(ItemTags.PARROT_POISONOUS_FOOD)) {
281
			if (!this.isFlying() && this.isTame() && this.isOwnedBy(player)) {
282
				if (!this.level().isClientSide()) {
283
					this.setOrderedToSit(!this.isOrderedToSit());
284
				}
285
286
				return InteractionResult.SUCCESS;
287
			} else {
288
				return super.mobInteract(player, hand);
289
			}
290
		} else {
291
			this.usePlayerItem(player, hand, itemStack);
292
			this.addEffect(new MobEffectInstance(MobEffects.POISON, 900));
293
			if (player.isCreative() || !this.isInvulnerable()) {
294
				this.hurt(this.damageSources().playerAttack(player), Float.MAX_VALUE);
295
			}
296
297
			return InteractionResult.SUCCESS;
298
		}
299
	}
300
301
	@Override
302
	public boolean isFood(final ItemStack itemStack) {
303
		return false;
304
	}
305
306
	public static boolean checkParrotSpawnRules(
307
		final EntityType<Parrot> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
308
	) {
309
		return level.getBlockState(pos.below()).is(BlockTags.PARROTS_SPAWNABLE_ON) && isBrightEnoughToSpawn(level, pos);
310
	}
311
312
	@Override
313
	protected void checkFallDamage(final double ya, final boolean onGround, final BlockState onState, final BlockPos pos) {
314
	}
315
316
	@Override
317
	public boolean canMate(final Animal partner) {
318
		return false;
319
	}
320
321
	@Override
322
	public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
323
		return null;
324
	}
325
326
	@Override
327
	public @Nullable SoundEvent getAmbientSound() {
328
		return getAmbient(this.level(), this.level().getRandom());
329
	}
330
331
	public static SoundEvent getAmbient(final Level level, final RandomSource random) {
332
		if (level.getDifficulty() != Difficulty.PEACEFUL && random.nextInt(1000) == 0) {
333
			List<EntityType<?>> keys = Lists.newArrayList(MOB_SOUND_MAP.keySet());
334
			return getImitatedSound(keys.get(random.nextInt(keys.size())));
335
		} else {
336
			return SoundEvents.PARROT_AMBIENT;
337
		}
338
	}
339
340
	private static SoundEvent getImitatedSound(final EntityType<?> id) {
341
		return MOB_SOUND_MAP.getOrDefault(id, SoundEvents.PARROT_AMBIENT);
342
	}
343
344
	@Override
345
	protected SoundEvent getHurtSound(final DamageSource source) {
346
		return SoundEvents.PARROT_HURT;
347
	}
348
349
	@Override
350
	protected SoundEvent getDeathSound() {
351
		return SoundEvents.PARROT_DEATH;
352
	}
353
354
	@Override
355
	protected void playStepSound(final BlockPos pos, final BlockState blockState) {
356
		this.playSound(SoundEvents.PARROT_STEP, 0.15F, 1.0F);
357
	}
358
359
	@Override
360
	protected boolean isFlapping() {
361
		return this.flyDist > this.nextFlap;
362
	}
363
364
	@Override
365
	protected void onFlap() {
366
		this.playSound(SoundEvents.PARROT_FLY, 0.15F, 1.0F);
367
		this.nextFlap = this.flyDist + this.flapSpeed / 2.0F;
368
	}
369
370
	@Override
371
	public float getVoicePitch() {
372
		return getPitch(this.random);
373
	}
374
375
	public static float getPitch(final RandomSource random) {
376
		return (random.nextFloat() - random.nextFloat()) * 0.2F + 1.0F;
377
	}
378
379
	@Override
380
	public SoundSource getSoundSource() {
381
		return SoundSource.NEUTRAL;
382
	}
383
384
	@Override
385
	public boolean isPushable() {
386
		return true;
387
	}
388
389
	@Override
390
	protected void doPush(final Entity entity) {
391
		if (!(entity instanceof Player)) {
392
			super.doPush(entity);
393
		}
394
	}
395
396
	@Override
397
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
398
		if (this.isInvulnerableTo(level, source)) {
399
			return false;
400
		}
401
402
		this.setOrderedToSit(false);
403
		return super.hurtServer(level, source, damage);
404
	}
405
406
	public Parrot.Variant getVariant() {
407
		return Parrot.Variant.byId(this.entityData.get(DATA_VARIANT_ID));
408
	}
409
410
	private void setVariant(final Parrot.Variant variant) {
411
		this.entityData.set(DATA_VARIANT_ID, variant.id);
412
	}
413
414
	@Override
415
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
416
		return type == DataComponents.PARROT_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
417
	}
418
419
	@Override
420
	protected void applyImplicitComponents(final DataComponentGetter components) {
421
		this.applyImplicitComponentIfPresent(components, DataComponents.PARROT_VARIANT);
422
		super.applyImplicitComponents(components);
423
	}
424
425
	@Override
426
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
427
		if (type == DataComponents.PARROT_VARIANT) {
428
			this.setVariant(castComponentValue(DataComponents.PARROT_VARIANT, value));
429
			return true;
430
		} else {
431
			return super.applyImplicitComponent(type, value);
432
		}
433
	}
434
435
	@Override
436
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
437
		super.defineSynchedData(entityData);
438
		entityData.define(DATA_VARIANT_ID, Parrot.Variant.DEFAULT.id);
439
	}
440
441
	@Override
442
	protected void addAdditionalSaveData(final ValueOutput output) {
443
		super.addAdditionalSaveData(output);
444
		output.store("Variant", Parrot.Variant.LEGACY_CODEC, this.getVariant());
445
	}
446
447
	@Override
448
	protected void readAdditionalSaveData(final ValueInput input) {
449
		super.readAdditionalSaveData(input);
450
		this.setVariant(input.read("Variant", Parrot.Variant.LEGACY_CODEC).orElse(Parrot.Variant.DEFAULT));
451
	}
452
453
	public boolean isFlying() {
454
		return !this.onGround();
455
	}
456
457
	@Override
458
	protected boolean omnidirectionalAirMover() {
459
		return true;
460
	}
461
462
	@Override
463
	protected boolean canFlyToOwner() {
464
		return true;
465
	}
466
467
	@Override
468
	public Vec3 getLeashOffset() {
469
		return new Vec3(0.0, 0.5F * this.getEyeHeight(), this.getBbWidth() * 0.4F);
470
	}
471
472
	private static class ParrotWanderGoal extends WaterAvoidingRandomFlyingGoal {
473
		public ParrotWanderGoal(final PathfinderMob mob, final double speedModifier) {
474
			super(mob, speedModifier);
475
		}
476
477
		@Override
478
		protected @Nullable Vec3 getPosition() {
479
			Vec3 pos = null;
480
			if (this.mob.isInWater()) {
481
				pos = LandRandomPos.getPos(this.mob, 15, 15);
482
			}
483
484
			if (this.mob.getRandom().nextFloat() >= this.probability) {
485
				pos = this.getTreePos();
486
			}
487
488
			return pos == null ? super.getPosition() : pos;
489
		}
490
491
		private @Nullable Vec3 getTreePos() {
492
			BlockPos mobPos = this.mob.blockPosition();
493
			BlockPos.MutableBlockPos abovePos = new BlockPos.MutableBlockPos();
494
			BlockPos.MutableBlockPos belowPos = new BlockPos.MutableBlockPos();
495
496
			for (BlockPos pos : BlockPos.betweenClosed(
497
				Mth.floor(this.mob.getX() - 3.0),
498
				Mth.floor(this.mob.getY() - 6.0),
499
				Mth.floor(this.mob.getZ() - 3.0),
500
				Mth.floor(this.mob.getX() + 3.0),
501
				Mth.floor(this.mob.getY() + 6.0),
502
				Mth.floor(this.mob.getZ() + 3.0)
503
			)) {
504
				if (!mobPos.equals(pos)) {
505
					BlockState state = this.mob.level().getBlockState(belowPos.setWithOffset(pos, Direction.DOWN));
506
					boolean canSitOn = state.getBlock() instanceof LeavesBlock || state.is(BlockTags.LOGS);
507
					if (canSitOn && this.mob.level().isEmptyBlock(pos) && this.mob.level().isEmptyBlock(abovePos.setWithOffset(pos, Direction.UP))) {
508
						return Vec3.atBottomCenterOf(pos);
509
					}
510
				}
511
			}
512
513
			return null;
514
		}
515
	}
516
517
	public enum Variant implements StringRepresentable {
518
		RED_BLUE(0, "red_blue"),
519
		BLUE(1, "blue"),
520
		GREEN(2, "green"),
521
		YELLOW_BLUE(3, "yellow_blue"),
522
		GRAY(4, "gray");
523
524
		public static final Parrot.Variant DEFAULT = RED_BLUE;
525
		private static final IntFunction<Parrot.Variant> BY_ID = ByIdMap.continuous(Parrot.Variant::getId, values(), ByIdMap.OutOfBoundsStrategy.CLAMP);
526
		public static final Codec<Parrot.Variant> CODEC = StringRepresentable.fromEnum(Parrot.Variant::values);
527
		@Deprecated
528
		public static final Codec<Parrot.Variant> LEGACY_CODEC = Codec.INT.xmap(BY_ID::apply, Parrot.Variant::getId);
529
		public static final StreamCodec<ByteBuf, Parrot.Variant> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Parrot.Variant::getId);
530
		private final int id;
531
		private final String name;
532
533
		Variant(final int id, final String name) {
534
			this.id = id;
535
			this.name = name;
536
		}
537
538
		public int getId() {
539
			return this.id;
540
		}
541
542
		public static Parrot.Variant byId(final int id) {
543
			return BY_ID.apply(id);
544
		}
545
546
		@Override
547
		public String getSerializedName() {
548
			return this.name;
549
		}
550
	}
551
}
552

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