TamableAnimal .java

net.minecraft.world.entity.TamableAnimal (class): members: MAX_HORIZONTAL_DISTANCE_FROM_TARGET_AFTER_TELEPORTING, TELEPORT_WHEN_DISTANCE_IS_SQ, feed, tame.

net.minecraft.world.entityclass318 linescommonextends Animal10.0 KB

Implements

API Surface (27)

void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
boolean canBeLeashed()
void spawnTamingParticles(final boolean success)
void handleEntityEvent(final @EntityEvent.Value byte id)
boolean isTame()
void setTame(final boolean isTame, final boolean includeSideEffects)
void applyTamingSideEffects()
void feed(final Player player, final InteractionHand hand, final ItemStack itemStack, final float healingFactor, final float defaultHeal)
boolean isInSittingPose()
void setInSittingPose(final boolean value)
void setOwner(final @Nullable LivingEntity owner)
void setOwnerReference(final @Nullable EntityReference<LivingEntity> owner)
void tame(final Player player)
boolean canAttack(final LivingEntity target)
boolean isOwnedBy(final LivingEntity entity)
boolean wantsToAttack(final LivingEntity target, final LivingEntity owner)
boolean considersEntityAsAlly(final Entity other)
void die(final DamageSource source)
boolean isOrderedToSit()
void setOrderedToSit(final boolean orderedToSit)
void tryToTeleportToOwner()
boolean shouldTryTeleportToOwner()
boolean unableToMoveToOwner()
boolean canFlyToOwner()
void tick()

Fields & Constants (8)

TELEPORT_WHEN_DISTANCE_IS_SQ
MIN_HORIZONTAL_DISTANCE_FROM_TARGET_AFTER_TELEPORTING
MAX_HORIZONTAL_DISTANCE_FROM_TARGET_AFTER_TELEPORTING
MAX_VERTICAL_DISTANCE_FROM_TARGET_AFTER_TELEPORTING
DEFAULT_ORDERED_TO_SIT
DATA_FLAGS_ID
DATA_OWNERUUID_ID
orderedToSit

String literals (2)

"Owner"
"Sitting"

Full Source

TamableAnimal.java318 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity;
2
3
import java.util.Optional;
4
import net.minecraft.advancements.triggers.CriteriaTriggers;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.component.DataComponents;
7
import net.minecraft.core.particles.ParticleOptions;
8
import net.minecraft.core.particles.ParticleTypes;
9
import net.minecraft.network.syncher.EntityDataAccessor;
10
import net.minecraft.network.syncher.EntityDataSerializers;
11
import net.minecraft.network.syncher.SynchedEntityData;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.server.level.ServerPlayer;
14
import net.minecraft.tags.TagKey;
15
import net.minecraft.world.InteractionHand;
16
import net.minecraft.world.damagesource.DamageSource;
17
import net.minecraft.world.damagesource.DamageType;
18
import net.minecraft.world.entity.ai.goal.PanicGoal;
19
import net.minecraft.world.entity.animal.Animal;
20
import net.minecraft.world.entity.player.Player;
21
import net.minecraft.world.food.FoodProperties;
22
import net.minecraft.world.item.ItemStack;
23
import net.minecraft.world.level.Level;
24
import net.minecraft.world.level.block.LeavesBlock;
25
import net.minecraft.world.level.block.state.BlockState;
26
import net.minecraft.world.level.gamerules.GameRules;
27
import net.minecraft.world.level.pathfinder.PathType;
28
import net.minecraft.world.level.pathfinder.WalkNodeEvaluator;
29
import net.minecraft.world.level.storage.ValueInput;
30
import net.minecraft.world.level.storage.ValueOutput;
31
import net.minecraft.world.scores.PlayerTeam;
32
import org.jspecify.annotations.Nullable;
33
34
public abstract class TamableAnimal extends Animal implements OwnableEntity {
35
	public static final int TELEPORT_WHEN_DISTANCE_IS_SQ = 144;
36
	private static final int MIN_HORIZONTAL_DISTANCE_FROM_TARGET_AFTER_TELEPORTING = 2;
37
	private static final int MAX_HORIZONTAL_DISTANCE_FROM_TARGET_AFTER_TELEPORTING = 3;
38
	private static final int MAX_VERTICAL_DISTANCE_FROM_TARGET_AFTER_TELEPORTING = 1;
39
	private static final boolean DEFAULT_ORDERED_TO_SIT = false;
40
	protected static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(TamableAnimal.class, EntityDataSerializers.BYTE);
41
	protected static final EntityDataAccessor<Optional<EntityReference<LivingEntity>>> DATA_OWNERUUID_ID = SynchedEntityData.defineId(
42
		TamableAnimal.class, EntityDataSerializers.OPTIONAL_LIVING_ENTITY_REFERENCE
43
	);
44
	private boolean orderedToSit = false;
45
46
	protected TamableAnimal(final EntityType<? extends TamableAnimal> type, final Level level) {
47
		super(type, level);
48
	}
49
50
	@Override
51
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
52
		super.defineSynchedData(entityData);
53
		entityData.define(DATA_FLAGS_ID, (byte)0);
54
		entityData.define(DATA_OWNERUUID_ID, Optional.empty());
55
	}
56
57
	@Override
58
	protected void addAdditionalSaveData(final ValueOutput output) {
59
		super.addAdditionalSaveData(output);
60
		EntityReference<LivingEntity> owner = this.getOwnerReference();
61
		EntityReference.store(owner, output, "Owner");
62
		output.putBoolean("Sitting", this.orderedToSit);
63
	}
64
65
	@Override
66
	protected void readAdditionalSaveData(final ValueInput input) {
67
		super.readAdditionalSaveData(input);
68
		EntityReference<LivingEntity> owner = EntityReference.readWithOldOwnerConversion(input, "Owner", this.level());
69
		if (owner != null) {
70
			try {
71
				this.entityData.set(DATA_OWNERUUID_ID, Optional.of(owner));
72
				this.setTame(true, false);
73
			} catch (Throwable ignored) {
74
				this.setTame(false, true);
75
			}
76
		} else {
77
			this.entityData.set(DATA_OWNERUUID_ID, Optional.empty());
78
			this.setTame(false, true);
79
		}
80
81
		this.orderedToSit = input.getBooleanOr("Sitting", false);
82
		this.setInSittingPose(this.orderedToSit);
83
	}
84
85
	@Override
86
	public boolean canBeLeashed() {
87
		return true;
88
	}
89
90
	protected void spawnTamingParticles(final boolean success) {
91
		ParticleOptions particle = ParticleTypes.HEART;
92
		if (!success) {
93
			particle = ParticleTypes.SMOKE;
94
		}
95
96
		for (int i = 0; i < 7; i++) {
97
			double xa = this.random.nextGaussian() * 0.02;
98
			double ya = this.random.nextGaussian() * 0.02;
99
			double za = this.random.nextGaussian() * 0.02;
100
			this.level().addParticle(particle, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), xa, ya, za);
101
		}
102
	}
103
104
	@Override
105
	public void handleEntityEvent(final @EntityEvent.Value byte id) {
106
		if (id == 7) {
107
			this.spawnTamingParticles(true);
108
		} else if (id == 6) {
109
			this.spawnTamingParticles(false);
110
		} else {
111
			super.handleEntityEvent(id);
112
		}
113
	}
114
115
	public boolean isTame() {
116
		return (this.entityData.get(DATA_FLAGS_ID) & 4) != 0;
117
	}
118
119
	public void setTame(final boolean isTame, final boolean includeSideEffects) {
120
		byte current = this.entityData.get(DATA_FLAGS_ID);
121
		if (isTame) {
122
			this.entityData.set(DATA_FLAGS_ID, (byte)(current | 4));
123
		} else {
124
			this.entityData.set(DATA_FLAGS_ID, (byte)(current & -5));
125
		}
126
127
		if (includeSideEffects) {
128
			this.applyTamingSideEffects();
129
		}
130
	}
131
132
	protected void applyTamingSideEffects() {
133
	}
134
135
	protected void feed(final Player player, final InteractionHand hand, final ItemStack itemStack, final float healingFactor, final float defaultHeal) {
136
		FoodProperties foodProperties = itemStack.get(DataComponents.FOOD);
137
		this.usePlayerItem(player, hand, itemStack);
138
		this.heal(foodProperties != null ? healingFactor * foodProperties.nutrition() : defaultHeal);
139
		this.playEatingSound();
140
	}
141
142
	public boolean isInSittingPose() {
143
		return (this.entityData.get(DATA_FLAGS_ID) & 1) != 0;
144
	}
145
146
	public void setInSittingPose(final boolean value) {
147
		byte current = this.entityData.get(DATA_FLAGS_ID);
148
		if (value) {
149
			this.entityData.set(DATA_FLAGS_ID, (byte)(current | 1));
150
		} else {
151
			this.entityData.set(DATA_FLAGS_ID, (byte)(current & -2));
152
		}
153
	}
154
155
	@Override
156
	public @Nullable EntityReference<LivingEntity> getOwnerReference() {
157
		return this.entityData.get(DATA_OWNERUUID_ID).orElse(null);
158
	}
159
160
	public void setOwner(final @Nullable LivingEntity owner) {
161
		this.entityData.set(DATA_OWNERUUID_ID, Optional.ofNullable(owner).map(EntityReference::of));
162
	}
163
164
	public void setOwnerReference(final @Nullable EntityReference<LivingEntity> owner) {
165
		this.entityData.set(DATA_OWNERUUID_ID, Optional.ofNullable(owner));
166
	}
167
168
	public void tame(final Player player) {
169
		this.setTame(true, true);
170
		this.setOwner(player);
171
		if (player instanceof ServerPlayer serverPlayer) {
172
			CriteriaTriggers.TAME_ANIMAL.trigger(serverPlayer, this);
173
		}
174
	}
175
176
	@Override
177
	public boolean canAttack(final LivingEntity target) {
178
		return this.isOwnedBy(target) ? false : super.canAttack(target);
179
	}
180
181
	public boolean isOwnedBy(final LivingEntity entity) {
182
		return entity == this.getOwner();
183
	}
184
185
	public boolean wantsToAttack(final LivingEntity target, final LivingEntity owner) {
186
		return true;
187
	}
188
189
	@Override
190
	public @Nullable PlayerTeam getTeam() {
191
		PlayerTeam ownTeam = super.getTeam();
192
		if (ownTeam != null) {
193
			return ownTeam;
194
		}
195
196
		if (this.isTame()) {
197
			LivingEntity owner = this.getRootOwner();
198
			if (owner != null) {
199
				return owner.getTeam();
200
			}
201
		}
202
203
		return null;
204
	}
205
206
	@Override
207
	protected boolean considersEntityAsAlly(final Entity other) {
208
		if (this.isTame()) {
209
			LivingEntity owner = this.getRootOwner();
210
			if (other == owner) {
211
				return true;
212
			}
213
214
			if (owner != null) {
215
				return owner.considersEntityAsAlly(other);
216
			}
217
		}
218
219
		return super.considersEntityAsAlly(other);
220
	}
221
222
	@Override
223
	public void die(final DamageSource source) {
224
		if (this.level() instanceof ServerLevel serverLevel
225
			&& serverLevel.getGameRules().get(GameRules.SHOW_DEATH_MESSAGES)
226
			&& this.getOwner() instanceof ServerPlayer serverPlayer) {
227
			serverPlayer.sendSystemMessage(this.getCombatTracker().getDeathMessage());
228
		}
229
230
		super.die(source);
231
	}
232
233
	public boolean isOrderedToSit() {
234
		return this.orderedToSit;
235
	}
236
237
	public void setOrderedToSit(final boolean orderedToSit) {
238
		this.orderedToSit = orderedToSit;
239
	}
240
241
	public void tryToTeleportToOwner() {
242
		LivingEntity owner = this.getOwner();
243
		if (owner != null) {
244
			this.teleportToAroundBlockPos(owner.blockPosition());
245
		}
246
	}
247
248
	public boolean shouldTryTeleportToOwner() {
249
		LivingEntity owner = this.getOwner();
250
		return owner != null && this.distanceToSqr(this.getOwner()) >= 144.0;
251
	}
252
253
	private void teleportToAroundBlockPos(final BlockPos targetPos) {
254
		for (int attempt = 0; attempt < 10; attempt++) {
255
			int xd = this.random.nextIntBetweenInclusive(-3, 3);
256
			int zd = this.random.nextIntBetweenInclusive(-3, 3);
257
			if (Math.abs(xd) >= 2 || Math.abs(zd) >= 2) {
258
				int yd = this.random.nextIntBetweenInclusive(-1, 1);
259
				if (this.maybeTeleportTo(targetPos.getX() + xd, targetPos.getY() + yd, targetPos.getZ() + zd)) {
260
					return;
261
				}
262
			}
263
		}
264
	}
265
266
	private boolean maybeTeleportTo(final int x, final int y, final int z) {
267
		if (!this.canTeleportTo(new BlockPos(x, y, z))) {
268
			return false;
269
		}
270
271
		this.snapTo(x + 0.5, y, z + 0.5, this.getYRot(), this.getXRot());
272
		this.navigation.stop();
273
		return true;
274
	}
275
276
	private boolean canTeleportTo(final BlockPos pos) {
277
		PathType pathType = WalkNodeEvaluator.getPathTypeStatic(this, pos);
278
		if (pathType != PathType.WALKABLE) {
279
			return false;
280
		}
281
282
		BlockState blockStateBelow = this.level().getBlockState(pos.below());
283
		if (!this.canFlyToOwner() && blockStateBelow.getBlock() instanceof LeavesBlock) {
284
			return false;
285
		}
286
287
		BlockPos delta = pos.subtract(this.blockPosition());
288
		return this.level().noCollision(this, this.getBoundingBox().move(delta));
289
	}
290
291
	public final boolean unableToMoveToOwner() {
292
		return this.isOrderedToSit() || this.isPassenger() || this.mayBeLeashed() || this.getOwner() != null && this.getOwner().isSpectator();
293
	}
294
295
	protected boolean canFlyToOwner() {
296
		return false;
297
	}
298
299
	public class TamableAnimalPanicGoal extends PanicGoal {
300
		public TamableAnimalPanicGoal(final double speedModifier, final TagKey<DamageType> panicCausingDamageTypes) {
301
			super(TamableAnimal.this, speedModifier, panicCausingDamageTypes);
302
		}
303
304
		public TamableAnimalPanicGoal(final double speedModifier) {
305
			super(TamableAnimal.this, speedModifier);
306
		}
307
308
		@Override
309
		public void tick() {
310
			if (!TamableAnimal.this.unableToMoveToOwner() && TamableAnimal.this.shouldTryTeleportToOwner()) {
311
				TamableAnimal.this.tryToTeleportToOwner();
312
			}
313
314
			super.tick();
315
		}
316
	}
317
}
318

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