Animal .java

net.minecraft.world.entity.animal.Animal (class): members: finalizeSpawnChildFromBreeding, PARENT_AGE_AFTER_BREEDING, createAnimalAttributes, getLoveCause.

net.minecraft.world.entity.animalclass279 linescommonextends AgeableMob9.2 KB

API Surface (25)

AttributeSupplier.Builder createAnimalAttributes()
void customServerAiStep(final ServerLevel level)
void aiStep()
void actuallyHurt(final ServerLevel level, final DamageSource source, final float dmg)
float getWalkTargetValue(final BlockPos pos, final LevelReader level)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
boolean checkAnimalSpawnRules(final EntityType<? extends Animal> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random)
boolean isBrightEnoughToSpawn(final BlockAndLightGetter level, final BlockPos pos)
int getAmbientSoundInterval()
boolean removeWhenFarAway(final double distSqr)
int getBaseExperienceReward(final ServerLevel level)
InteractionResult mobInteract(final Player player, final InteractionHand hand)
void playEatingSound()
boolean canFallInLove()
void setInLove(final @Nullable Player player)
void setInLoveTime(final int time)
int getInLoveTime()
boolean isInLove()
void resetLove()
boolean canMate(final Animal partner)
void spawnChildFromBreeding(final ServerLevel level, final Animal partner)
void finalizeSpawnChildFromBreeding(final ServerLevel level, final Animal partner, final @Nullable AgeableMob offspring)
void handleEntityEvent(final @EntityEvent.Value byte id)
Vec3 getDismountLocationForPassenger(final LivingEntity passenger)

Fields & Constants (5)

PARENT_AGE_AFTER_BREEDING
DEFAULT_IN_LOVE_TIME
inLove
loveCause
pos

String literals (2)

"InLove"
"LoveCause"

Full Source

Animal.java279 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal;
2
3
import java.util.Optional;
4
import net.minecraft.advancements.triggers.CriteriaTriggers;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.core.particles.ParticleTypes;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.server.level.ServerPlayer;
10
import net.minecraft.stats.Stats;
11
import net.minecraft.tags.BlockTags;
12
import net.minecraft.util.RandomSource;
13
import net.minecraft.world.InteractionHand;
14
import net.minecraft.world.InteractionResult;
15
import net.minecraft.world.damagesource.DamageSource;
16
import net.minecraft.world.entity.AgeableMob;
17
import net.minecraft.world.entity.EntityEvent;
18
import net.minecraft.world.entity.EntityReference;
19
import net.minecraft.world.entity.EntitySpawnReason;
20
import net.minecraft.world.entity.EntityType;
21
import net.minecraft.world.entity.ExperienceOrb;
22
import net.minecraft.world.entity.LivingEntity;
23
import net.minecraft.world.entity.Mob;
24
import net.minecraft.world.entity.Pose;
25
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
26
import net.minecraft.world.entity.ai.attributes.Attributes;
27
import net.minecraft.world.entity.player.Player;
28
import net.minecraft.world.entity.vehicle.DismountHelper;
29
import net.minecraft.world.item.ItemStack;
30
import net.minecraft.world.level.BlockAndLightGetter;
31
import net.minecraft.world.level.Level;
32
import net.minecraft.world.level.LevelAccessor;
33
import net.minecraft.world.level.LevelReader;
34
import net.minecraft.world.level.block.Blocks;
35
import net.minecraft.world.level.gamerules.GameRules;
36
import net.minecraft.world.level.pathfinder.PathType;
37
import net.minecraft.world.level.storage.ValueInput;
38
import net.minecraft.world.level.storage.ValueOutput;
39
import net.minecraft.world.phys.AABB;
40
import net.minecraft.world.phys.Vec3;
41
import org.jspecify.annotations.Nullable;
42
43
public abstract class Animal extends AgeableMob {
44
	protected static final int PARENT_AGE_AFTER_BREEDING = 6000;
45
	private static final int DEFAULT_IN_LOVE_TIME = 0;
46
	private int inLove = 0;
47
	private @Nullable EntityReference<ServerPlayer> loveCause;
48
49
	protected Animal(final EntityType<? extends Animal> type, final Level level) {
50
		super(type, level);
51
		this.setPathfindingMalus(PathType.FIRE_IN_NEIGHBOR, 16.0F);
52
		this.setPathfindingMalus(PathType.FIRE, -1.0F);
53
	}
54
55
	public static AttributeSupplier.Builder createAnimalAttributes() {
56
		return Mob.createMobAttributes().add(Attributes.TEMPT_RANGE, 10.0);
57
	}
58
59
	@Override
60
	protected void customServerAiStep(final ServerLevel level) {
61
		if (this.getAge() != 0) {
62
			this.inLove = 0;
63
		}
64
65
		super.customServerAiStep(level);
66
	}
67
68
	@Override
69
	public void aiStep() {
70
		super.aiStep();
71
		if (this.getAge() != 0) {
72
			this.inLove = 0;
73
		}
74
75
		if (this.inLove > 0) {
76
			this.inLove--;
77
			if (this.inLove % 10 == 0) {
78
				double xa = this.random.nextGaussian() * 0.02;
79
				double ya = this.random.nextGaussian() * 0.02;
80
				double za = this.random.nextGaussian() * 0.02;
81
				this.level().addParticle(ParticleTypes.HEART, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), xa, ya, za);
82
			}
83
		}
84
	}
85
86
	@Override
87
	protected void actuallyHurt(final ServerLevel level, final DamageSource source, final float dmg) {
88
		this.resetLove();
89
		super.actuallyHurt(level, source, dmg);
90
	}
91
92
	@Override
93
	public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
94
		return level.getBlockState(pos.below()).is(Blocks.GRASS_BLOCK) ? 10.0F : level.getPathfindingCostFromLightLevels(pos);
95
	}
96
97
	@Override
98
	protected void addAdditionalSaveData(final ValueOutput output) {
99
		super.addAdditionalSaveData(output);
100
		output.putInt("InLove", this.inLove);
101
		EntityReference.store(this.loveCause, output, "LoveCause");
102
	}
103
104
	@Override
105
	protected void readAdditionalSaveData(final ValueInput input) {
106
		super.readAdditionalSaveData(input);
107
		this.inLove = input.getIntOr("InLove", 0);
108
		this.loveCause = EntityReference.read(input, "LoveCause");
109
	}
110
111
	public static boolean checkAnimalSpawnRules(
112
		final EntityType<? extends Animal> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
113
	) {
114
		boolean brightEnoughToSpawn = EntitySpawnReason.ignoresLightRequirements(spawnReason) || isBrightEnoughToSpawn(level, pos);
115
		return level.getBlockState(pos.below()).is(BlockTags.ANIMALS_SPAWNABLE_ON) && brightEnoughToSpawn;
116
	}
117
118
	protected static boolean isBrightEnoughToSpawn(final BlockAndLightGetter level, final BlockPos pos) {
119
		return level.getRawBrightness(pos, 0) > 8;
120
	}
121
122
	@Override
123
	public int getAmbientSoundInterval() {
124
		return 120;
125
	}
126
127
	@Override
128
	public boolean removeWhenFarAway(final double distSqr) {
129
		return false;
130
	}
131
132
	@Override
133
	protected int getBaseExperienceReward(final ServerLevel level) {
134
		return 1 + this.random.nextInt(3);
135
	}
136
137
	public abstract boolean isFood(final ItemStack itemStack);
138
139
	@Override
140
	public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
141
		ItemStack itemStack = player.getItemInHand(hand);
142
		if (this.isFood(itemStack)) {
143
			int age = this.getAge();
144
			if (player instanceof ServerPlayer serverPlayer && age == 0 && this.canFallInLove()) {
145
				this.usePlayerItem(player, hand, itemStack);
146
				this.setInLove(serverPlayer);
147
				this.playEatingSound();
148
				return InteractionResult.SUCCESS_SERVER;
149
			}
150
151
			if (this.canAgeUp()) {
152
				this.usePlayerItem(player, hand, itemStack);
153
				this.ageUp(getSpeedUpSecondsWhenFeeding(-age), true);
154
				this.playEatingSound();
155
				return InteractionResult.SUCCESS;
156
			}
157
158
			if (this.level().isClientSide()) {
159
				return InteractionResult.CONSUME;
160
			}
161
		}
162
163
		return super.mobInteract(player, hand);
164
	}
165
166
	protected void playEatingSound() {
167
	}
168
169
	public boolean canFallInLove() {
170
		return this.inLove <= 0;
171
	}
172
173
	public void setInLove(final @Nullable Player player) {
174
		this.inLove = 600;
175
		if (player instanceof ServerPlayer serverPlayer) {
176
			this.loveCause = EntityReference.of(serverPlayer);
177
		}
178
179
		this.level().broadcastEntityEvent(this, (byte)18);
180
	}
181
182
	public void setInLoveTime(final int time) {
183
		this.inLove = time;
184
	}
185
186
	public int getInLoveTime() {
187
		return this.inLove;
188
	}
189
190
	public @Nullable ServerPlayer getLoveCause() {
191
		return EntityReference.get(this.loveCause, this.level(), ServerPlayer.class);
192
	}
193
194
	public boolean isInLove() {
195
		return this.inLove > 0;
196
	}
197
198
	public void resetLove() {
199
		this.inLove = 0;
200
	}
201
202
	public boolean canMate(final Animal partner) {
203
		if (partner == this) {
204
			return false;
205
		} else {
206
			return partner.getClass() != this.getClass() ? false : this.isInLove() && partner.isInLove();
207
		}
208
	}
209
210
	public void spawnChildFromBreeding(final ServerLevel level, final Animal partner) {
211
		AgeableMob offspring = this.getBreedOffspring(level, partner);
212
		if (offspring != null) {
213
			offspring.setBaby(true);
214
			offspring.snapTo(this.getX(), this.getY(), this.getZ(), 0.0F, 0.0F);
215
			this.finalizeSpawnChildFromBreeding(level, partner, offspring);
216
			level.addFreshEntityWithPassengers(offspring);
217
		}
218
	}
219
220
	public void finalizeSpawnChildFromBreeding(final ServerLevel level, final Animal partner, final @Nullable AgeableMob offspring) {
221
		Optional.ofNullable(this.getLoveCause()).or(() -> Optional.ofNullable(partner.getLoveCause())).ifPresent(cause -> {
222
			cause.awardStat(Stats.ANIMALS_BRED);
223
			CriteriaTriggers.BRED_ANIMALS.trigger(cause, this, partner, offspring);
224
		});
225
		this.setAge(6000);
226
		partner.setAge(6000);
227
		this.resetLove();
228
		partner.resetLove();
229
		level.broadcastEntityEvent(this, (byte)18);
230
		if (level.getGameRules().get(GameRules.MOB_DROPS)) {
231
			level.addFreshEntity(new ExperienceOrb(level, this.getX(), this.getY(), this.getZ(), this.getRandom().nextInt(7) + 1));
232
		}
233
	}
234
235
	@Override
236
	public void handleEntityEvent(final @EntityEvent.Value byte id) {
237
		if (id == 18) {
238
			for (int i = 0; i < 7; i++) {
239
				double xa = this.random.nextGaussian() * 0.02;
240
				double ya = this.random.nextGaussian() * 0.02;
241
				double za = this.random.nextGaussian() * 0.02;
242
				this.level().addParticle(ParticleTypes.HEART, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), xa, ya, za);
243
			}
244
		} else {
245
			super.handleEntityEvent(id);
246
		}
247
	}
248
249
	@Override
250
	public Vec3 getDismountLocationForPassenger(final LivingEntity passenger) {
251
		Direction forward = this.getMotionDirection();
252
		if (forward.getAxis() == Direction.Axis.Y) {
253
			return super.getDismountLocationForPassenger(passenger);
254
		}
255
256
		int[][] offsets = DismountHelper.offsetsForDirection(forward);
257
		BlockPos vehicleBlockPos = this.blockPosition();
258
		BlockPos.MutableBlockPos targetBlockPos = new BlockPos.MutableBlockPos();
259
260
		for (Pose dismountPose : passenger.getDismountPoses()) {
261
			AABB poseCollisionBox = passenger.getLocalBoundsForPose(dismountPose);
262
263
			for (int[] offsetXZ : offsets) {
264
				targetBlockPos.set(vehicleBlockPos.getX() + offsetXZ[0], vehicleBlockPos.getY(), vehicleBlockPos.getZ() + offsetXZ[1]);
265
				double blockFloorHeight = this.level().getBlockFloorHeight(targetBlockPos);
266
				if (DismountHelper.isBlockFloorValid(blockFloorHeight)) {
267
					Vec3 location = Vec3.upFromBottomCenterOf(targetBlockPos, blockFloorHeight);
268
					if (DismountHelper.canDismountTo(this.level(), passenger, poseCollisionBox.move(location))) {
269
						passenger.setPose(dismountPose);
270
						return location;
271
					}
272
				}
273
			}
274
		}
275
276
		return super.getDismountLocationForPassenger(passenger);
277
	}
278
}
279

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