Ocelot .java

net.minecraft.world.entity.animal.feline.Ocelot (class, 317 lines): members: ocelotAvoidPlayersGoal, OcelotAvoidEntityGoal, checkOcelotSpawnRules, isFood.

net.minecraft.world.entity.animal.felineclass317 linescommonextends Animal10.9 KB

API Surface (22)

void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void defineSynchedData(final SynchedEntityData.Builder entityData)
void registerGoals()
void customServerAiStep(final ServerLevel level)
boolean removeWhenFarAway(final double distSqr)
AttributeSupplier.Builder createAttributes()
int getAmbientSoundInterval()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
InteractionResult mobInteract(final Player player, final InteractionHand hand)
void handleEntityEvent(final @EntityEvent.Value byte id)
void reassessTrustingGoals()
EntityDimensions getDefaultDimensions(final Pose pose)
boolean isFood(final ItemStack itemStack)
boolean checkOcelotSpawnRules(final EntityType<Ocelot> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random)
boolean checkSpawnObstruction(final LevelReader level)
Vec3 getLeashOffset()
boolean isSteppingCarefully()
boolean canUse()
boolean canContinueToUse()
boolean canScare()

Fields & Constants (10)

CROUCH_SPEED_MOD
WALK_SPEED_MOD
SPRINT_SPEED_MOD
DATA_TRUSTING
DEFAULT_TRUSTING
BABY_DIMENSIONS
ocelotAvoidPlayersGoal
temptGoal
pos
spawnReason

String literals (1)

"Trusting"

Code Graph: Dependencies (53)

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

Full Source

Ocelot.java317 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.feline;
2
3
import java.util.function.Predicate;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.particles.ParticleOptions;
6
import net.minecraft.core.particles.ParticleTypes;
7
import net.minecraft.network.syncher.EntityDataAccessor;
8
import net.minecraft.network.syncher.EntityDataSerializers;
9
import net.minecraft.network.syncher.SynchedEntityData;
10
import net.minecraft.server.level.ServerLevel;
11
import net.minecraft.sounds.SoundEvent;
12
import net.minecraft.sounds.SoundEvents;
13
import net.minecraft.tags.BlockTags;
14
import net.minecraft.tags.ItemTags;
15
import net.minecraft.util.RandomSource;
16
import net.minecraft.world.DifficultyInstance;
17
import net.minecraft.world.InteractionHand;
18
import net.minecraft.world.InteractionResult;
19
import net.minecraft.world.damagesource.DamageSource;
20
import net.minecraft.world.entity.AgeableMob;
21
import net.minecraft.world.entity.EntityAttachment;
22
import net.minecraft.world.entity.EntityAttachments;
23
import net.minecraft.world.entity.EntityDimensions;
24
import net.minecraft.world.entity.EntityEvent;
25
import net.minecraft.world.entity.EntitySelector;
26
import net.minecraft.world.entity.EntitySpawnReason;
27
import net.minecraft.world.entity.EntityType;
28
import net.minecraft.world.entity.EntityTypes;
29
import net.minecraft.world.entity.LivingEntity;
30
import net.minecraft.world.entity.Pose;
31
import net.minecraft.world.entity.SpawnGroupData;
32
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
33
import net.minecraft.world.entity.ai.attributes.Attributes;
34
import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
35
import net.minecraft.world.entity.ai.goal.BreedGoal;
36
import net.minecraft.world.entity.ai.goal.FloatGoal;
37
import net.minecraft.world.entity.ai.goal.LeapAtTargetGoal;
38
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
39
import net.minecraft.world.entity.ai.goal.OcelotAttackGoal;
40
import net.minecraft.world.entity.ai.goal.TemptGoal;
41
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
42
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
43
import net.minecraft.world.entity.animal.Animal;
44
import net.minecraft.world.entity.animal.chicken.Chicken;
45
import net.minecraft.world.entity.animal.turtle.Turtle;
46
import net.minecraft.world.entity.player.Player;
47
import net.minecraft.world.item.ItemStack;
48
import net.minecraft.world.level.Level;
49
import net.minecraft.world.level.LevelAccessor;
50
import net.minecraft.world.level.LevelReader;
51
import net.minecraft.world.level.ServerLevelAccessor;
52
import net.minecraft.world.level.block.Blocks;
53
import net.minecraft.world.level.block.state.BlockState;
54
import net.minecraft.world.level.storage.ValueInput;
55
import net.minecraft.world.level.storage.ValueOutput;
56
import net.minecraft.world.phys.Vec3;
57
import org.jspecify.annotations.Nullable;
58
59
public class Ocelot extends Animal {
60
	public static final double CROUCH_SPEED_MOD = 0.6;
61
	public static final double WALK_SPEED_MOD = 0.8;
62
	public static final double SPRINT_SPEED_MOD = 1.33;
63
	private static final EntityDataAccessor<Boolean> DATA_TRUSTING = SynchedEntityData.defineId(Ocelot.class, EntityDataSerializers.BOOLEAN);
64
	private static final boolean DEFAULT_TRUSTING = false;
65
	private static final EntityDimensions BABY_DIMENSIONS = EntityDimensions.scalable(0.3F, 0.35F)
66
		.withEyeHeight(0.34375F)
67
		.withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, 0.3125F, 0.0F));
68
	private Ocelot.@Nullable OcelotAvoidEntityGoal<Player> ocelotAvoidPlayersGoal;
69
	private Ocelot.@Nullable OcelotTemptGoal temptGoal;
70
71
	public Ocelot(final EntityType<? extends Ocelot> type, final Level level) {
72
		super(type, level);
73
		this.reassessTrustingGoals();
74
	}
75
76
	private boolean isTrusting() {
77
		return this.entityData.get(DATA_TRUSTING);
78
	}
79
80
	private void setTrusting(final boolean trusting) {
81
		this.entityData.set(DATA_TRUSTING, trusting);
82
		this.reassessTrustingGoals();
83
	}
84
85
	@Override
86
	protected void addAdditionalSaveData(final ValueOutput output) {
87
		super.addAdditionalSaveData(output);
88
		output.putBoolean("Trusting", this.isTrusting());
89
	}
90
91
	@Override
92
	protected void readAdditionalSaveData(final ValueInput input) {
93
		super.readAdditionalSaveData(input);
94
		this.setTrusting(input.getBooleanOr("Trusting", false));
95
	}
96
97
	@Override
98
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
99
		super.defineSynchedData(entityData);
100
		entityData.define(DATA_TRUSTING, false);
101
	}
102
103
	@Override
104
	protected void registerGoals() {
105
		this.temptGoal = new Ocelot.OcelotTemptGoal(this, 0.6, i -> i.is(ItemTags.OCELOT_FOOD), true);
106
		this.goalSelector.addGoal(1, new FloatGoal(this));
107
		this.goalSelector.addGoal(3, this.temptGoal);
108
		this.goalSelector.addGoal(7, new LeapAtTargetGoal(this, 0.3F));
109
		this.goalSelector.addGoal(8, new OcelotAttackGoal(this));
110
		this.goalSelector.addGoal(9, new BreedGoal(this, 0.8));
111
		this.goalSelector.addGoal(10, new WaterAvoidingRandomStrollGoal(this, 0.8, 1.0000001E-5F));
112
		this.goalSelector.addGoal(11, new LookAtPlayerGoal(this, Player.class, 10.0F));
113
		this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Chicken.class, false));
114
		this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Turtle.class, 10, false, false, Turtle.BABY_ON_LAND_SELECTOR));
115
	}
116
117
	@Override
118
	public void customServerAiStep(final ServerLevel level) {
119
		if (this.getMoveControl().hasWanted()) {
120
			double speed = this.getMoveControl().getSpeedModifier();
121
			if (speed == 0.6) {
122
				this.setPose(Pose.CROUCHING);
123
				this.setSprinting(false);
124
			} else if (speed == 1.33) {
125
				this.setPose(Pose.STANDING);
126
				this.setSprinting(true);
127
			} else {
128
				this.setPose(Pose.STANDING);
129
				this.setSprinting(false);
130
			}
131
		} else {
132
			this.setPose(Pose.STANDING);
133
			this.setSprinting(false);
134
		}
135
	}
136
137
	@Override
138
	public boolean removeWhenFarAway(final double distSqr) {
139
		return !this.isTrusting() && this.tickCount > 2400;
140
	}
141
142
	public static AttributeSupplier.Builder createAttributes() {
143
		return Animal.createAnimalAttributes().add(Attributes.MAX_HEALTH, 10.0).add(Attributes.MOVEMENT_SPEED, 0.3F).add(Attributes.ATTACK_DAMAGE, 3.0);
144
	}
145
146
	@Override
147
	protected @Nullable SoundEvent getAmbientSound() {
148
		return SoundEvents.OCELOT_AMBIENT;
149
	}
150
151
	@Override
152
	public int getAmbientSoundInterval() {
153
		return 900;
154
	}
155
156
	@Override
157
	protected SoundEvent getHurtSound(final DamageSource source) {
158
		return SoundEvents.OCELOT_HURT;
159
	}
160
161
	@Override
162
	protected SoundEvent getDeathSound() {
163
		return SoundEvents.OCELOT_DEATH;
164
	}
165
166
	@Override
167
	public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
168
		ItemStack itemStack = player.getItemInHand(hand);
169
		if ((this.temptGoal == null || this.temptGoal.isRunning()) && !this.isTrusting() && this.isFood(itemStack) && player.distanceToSqr(this) < 9.0) {
170
			this.usePlayerItem(player, hand, itemStack);
171
			if (!this.level().isClientSide()) {
172
				if (this.random.nextInt(3) == 0) {
173
					this.setTrusting(true);
174
					this.spawnTrustingParticles(true);
175
					this.level().broadcastEntityEvent(this, (byte)41);
176
				} else {
177
					this.spawnTrustingParticles(false);
178
					this.level().broadcastEntityEvent(this, (byte)40);
179
				}
180
			}
181
182
			return InteractionResult.SUCCESS;
183
		} else {
184
			return super.mobInteract(player, hand);
185
		}
186
	}
187
188
	@Override
189
	public void handleEntityEvent(final @EntityEvent.Value byte id) {
190
		if (id == 41) {
191
			this.spawnTrustingParticles(true);
192
		} else if (id == 40) {
193
			this.spawnTrustingParticles(false);
194
		} else {
195
			super.handleEntityEvent(id);
196
		}
197
	}
198
199
	private void spawnTrustingParticles(final boolean success) {
200
		ParticleOptions particle = ParticleTypes.HEART;
201
		if (!success) {
202
			particle = ParticleTypes.SMOKE;
203
		}
204
205
		for (int i = 0; i < 7; i++) {
206
			double xa = this.random.nextGaussian() * 0.02;
207
			double ya = this.random.nextGaussian() * 0.02;
208
			double za = this.random.nextGaussian() * 0.02;
209
			this.level().addParticle(particle, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), xa, ya, za);
210
		}
211
	}
212
213
	protected void reassessTrustingGoals() {
214
		if (this.ocelotAvoidPlayersGoal == null) {
215
			this.ocelotAvoidPlayersGoal = new Ocelot.OcelotAvoidEntityGoal<>(this, Player.class, 16.0F, 0.8, 1.33);
216
		}
217
218
		this.goalSelector.removeGoal(this.ocelotAvoidPlayersGoal);
219
		if (!this.isTrusting()) {
220
			this.goalSelector.addGoal(4, this.ocelotAvoidPlayersGoal);
221
		}
222
	}
223
224
	@Override
225
	public EntityDimensions getDefaultDimensions(final Pose pose) {
226
		return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
227
	}
228
229
	public @Nullable Ocelot getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
230
		return EntityTypes.OCELOT.create(level, EntitySpawnReason.BREEDING);
231
	}
232
233
	@Override
234
	public boolean isFood(final ItemStack itemStack) {
235
		return itemStack.is(ItemTags.OCELOT_FOOD);
236
	}
237
238
	public static boolean checkOcelotSpawnRules(
239
		final EntityType<Ocelot> type, final LevelAccessor level, final EntitySpawnReason spawnReason, final BlockPos pos, final RandomSource random
240
	) {
241
		return random.nextInt(3) != 0;
242
	}
243
244
	@Override
245
	public boolean checkSpawnObstruction(final LevelReader level) {
246
		if (level.isUnobstructed(this) && !level.containsAnyLiquid(this.getBoundingBox())) {
247
			BlockPos pos = this.blockPosition();
248
			if (pos.getY() < level.getSeaLevel()) {
249
				return false;
250
			}
251
252
			BlockState state = level.getBlockState(pos.below());
253
			if (state.is(Blocks.GRASS_BLOCK) || state.is(BlockTags.LEAVES)) {
254
				return true;
255
			}
256
		}
257
258
		return false;
259
	}
260
261
	@Override
262
	public @Nullable SpawnGroupData finalizeSpawn(
263
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
264
	) {
265
		if (groupData == null) {
266
			groupData = new AgeableMob.AgeableMobGroupData(1.0F);
267
		}
268
269
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
270
	}
271
272
	@Override
273
	public Vec3 getLeashOffset() {
274
		return new Vec3(0.0, 0.5F * this.getEyeHeight(), this.getBbWidth() * 0.4F);
275
	}
276
277
	@Override
278
	public boolean isSteppingCarefully() {
279
		return this.isCrouching() || super.isSteppingCarefully();
280
	}
281
282
	private static class OcelotAvoidEntityGoal<T extends LivingEntity> extends AvoidEntityGoal<T> {
283
		private final Ocelot ocelot;
284
285
		public OcelotAvoidEntityGoal(
286
			final Ocelot ocelot, final Class<T> avoidClass, final float maxDist, final double walkSpeedModifier, final double sprintSpeedModifier
287
		) {
288
			super(ocelot, avoidClass, maxDist, walkSpeedModifier, sprintSpeedModifier, EntitySelector.NO_CREATIVE_OR_SPECTATOR);
289
			this.ocelot = ocelot;
290
		}
291
292
		@Override
293
		public boolean canUse() {
294
			return !this.ocelot.isTrusting() && super.canUse();
295
		}
296
297
		@Override
298
		public boolean canContinueToUse() {
299
			return !this.ocelot.isTrusting() && super.canContinueToUse();
300
		}
301
	}
302
303
	private static class OcelotTemptGoal extends TemptGoal {
304
		private final Ocelot ocelot;
305
306
		public OcelotTemptGoal(final Ocelot ocelot, final double speedModifier, final Predicate<ItemStack> items, final boolean canScare) {
307
			super(ocelot, speedModifier, items, canScare);
308
			this.ocelot = ocelot;
309
		}
310
311
		@Override
312
		protected boolean canScare() {
313
			return super.canScare() && !this.ocelot.isTrusting();
314
		}
315
	}
316
}
317

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