Llama .java

net.minecraft.world.entity.animal.equine.Llama (class): members: LlamaHurtByTargetGoal, LlamaAttackWolfGoal, DATA_STRENGTH_ID, LlamaGroupData, caravanHead.

net.minecraft.world.entity.animal.equineclass529 linescommonextends AbstractChestedHorse16.1 KB

Implements

API Surface (44)

boolean isTraderLlama()
int getStrength()
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void registerGoals()
AttributeSupplier.Builder createAttributes()
void defineSynchedData(final SynchedEntityData.Builder entityData)
Llama.Variant getVariant()
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)
boolean isFood(final ItemStack itemStack)
boolean handleEating(final Player player, final ItemStack itemStack)
boolean isImmobile()
boolean canPerformRearing()
SoundEvent getAngrySound()
SoundEvent getAmbientSound()
SoundEvent getHurtSound(final DamageSource source)
SoundEvent getDeathSound()
SoundEvent getEatingSound()
void playStepSound(final BlockPos pos, final BlockState blockState)
void playChestEquipsSound()
int getInventoryColumns()
boolean canUseSlot(final EquipmentSlot slot)
int getMaxTemper()
boolean canMate(final Animal partner)
boolean causeFallDamage(final double fallDistance, final float damageModifier, final DamageSource damageSource)
void leaveCaravan()
void joinCaravan(final Llama tail)
boolean hasCaravanTail()
boolean inCaravan()
double followLeashSpeed()
boolean supportQuadLeash()
void followMommy(final ServerLevel level)
boolean canEatGrass()
void performRangedAttack(final LivingEntity target, final float power)
float rangedAttackUncertainty(final Level level)
Vec3 getLeashOffset()
EntityDimensions getDefaultDimensions(final Pose pose)
Vec3 getPassengerAttachmentPoint(final Entity passenger, final EntityDimensions dimensions, final float scale)
double getFollowDistance()
boolean canContinueToUse()
int getId()
Llama.Variant byId(final int id)
String getSerializedName()

Fields & Constants (8)

MAX_STRENGTH
DATA_STRENGTH_ID
DATA_VARIANT_ID
BABY_DIMENSIONS
didSpit
caravanHead
caravanTail
spawnReason

String literals (6)

"Variant"
"Strength"
"creamy"
"white"
"brown"
"gray"

Code Graph: Dependencies (60)

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

Full Source

Llama.java529 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.equine;
2
3
import com.mojang.serialization.Codec;
4
import io.netty.buffer.ByteBuf;
5
import java.util.function.IntFunction;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.component.DataComponentGetter;
8
import net.minecraft.core.component.DataComponentType;
9
import net.minecraft.core.component.DataComponents;
10
import net.minecraft.core.particles.ParticleTypes;
11
import net.minecraft.network.codec.ByteBufCodecs;
12
import net.minecraft.network.codec.StreamCodec;
13
import net.minecraft.network.syncher.EntityDataAccessor;
14
import net.minecraft.network.syncher.EntityDataSerializers;
15
import net.minecraft.network.syncher.SynchedEntityData;
16
import net.minecraft.server.level.ServerLevel;
17
import net.minecraft.sounds.SoundEvent;
18
import net.minecraft.sounds.SoundEvents;
19
import net.minecraft.tags.ItemTags;
20
import net.minecraft.util.ByIdMap;
21
import net.minecraft.util.RandomSource;
22
import net.minecraft.util.StringRepresentable;
23
import net.minecraft.util.Util;
24
import net.minecraft.world.DifficultyInstance;
25
import net.minecraft.world.damagesource.DamageSource;
26
import net.minecraft.world.entity.AgeableMob;
27
import net.minecraft.world.entity.Entity;
28
import net.minecraft.world.entity.EntityAttachment;
29
import net.minecraft.world.entity.EntityAttachments;
30
import net.minecraft.world.entity.EntityDimensions;
31
import net.minecraft.world.entity.EntitySpawnReason;
32
import net.minecraft.world.entity.EntityType;
33
import net.minecraft.world.entity.EntityTypes;
34
import net.minecraft.world.entity.EquipmentSlot;
35
import net.minecraft.world.entity.LivingEntity;
36
import net.minecraft.world.entity.Pose;
37
import net.minecraft.world.entity.SpawnGroupData;
38
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
39
import net.minecraft.world.entity.ai.goal.BreedGoal;
40
import net.minecraft.world.entity.ai.goal.FloatGoal;
41
import net.minecraft.world.entity.ai.goal.FollowParentGoal;
42
import net.minecraft.world.entity.ai.goal.LlamaFollowCaravanGoal;
43
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
44
import net.minecraft.world.entity.ai.goal.PanicGoal;
45
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
46
import net.minecraft.world.entity.ai.goal.RangedAttackGoal;
47
import net.minecraft.world.entity.ai.goal.RunAroundLikeCrazyGoal;
48
import net.minecraft.world.entity.ai.goal.TemptGoal;
49
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
50
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
51
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
52
import net.minecraft.world.entity.animal.Animal;
53
import net.minecraft.world.entity.animal.wolf.Wolf;
54
import net.minecraft.world.entity.monster.RangedAttackMob;
55
import net.minecraft.world.entity.player.Player;
56
import net.minecraft.world.entity.projectile.LlamaSpit;
57
import net.minecraft.world.entity.projectile.Projectile;
58
import net.minecraft.world.item.ItemStack;
59
import net.minecraft.world.item.Items;
60
import net.minecraft.world.level.Level;
61
import net.minecraft.world.level.ServerLevelAccessor;
62
import net.minecraft.world.level.block.state.BlockState;
63
import net.minecraft.world.level.storage.ValueInput;
64
import net.minecraft.world.level.storage.ValueOutput;
65
import net.minecraft.world.phys.Vec3;
66
import org.jspecify.annotations.Nullable;
67
68
public class Llama extends AbstractChestedHorse implements RangedAttackMob {
69
	private static final int MAX_STRENGTH = 5;
70
	private static final EntityDataAccessor<Integer> DATA_STRENGTH_ID = SynchedEntityData.defineId(Llama.class, EntityDataSerializers.INT);
71
	private static final EntityDataAccessor<Integer> DATA_VARIANT_ID = SynchedEntityData.defineId(Llama.class, EntityDataSerializers.INT);
72
	private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.LLAMA
73
		.getDimensions()
74
		.withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, EntityTypes.LLAMA.getHeight() - 0.25F, -0.3F))
75
		.scale(0.5F);
76
	private boolean didSpit;
77
	private @Nullable Llama caravanHead;
78
	private @Nullable Llama caravanTail;
79
80
	public Llama(final EntityType<? extends Llama> type, final Level level) {
81
		super(type, level);
82
		this.getNavigation().setRequiredPathLength(40.0F);
83
	}
84
85
	public boolean isTraderLlama() {
86
		return false;
87
	}
88
89
	private void setStrength(final int strength) {
90
		this.entityData.set(DATA_STRENGTH_ID, Math.max(1, Math.min(5, strength)));
91
	}
92
93
	private void setRandomStrength(final RandomSource random) {
94
		int maxStrength = random.nextFloat() < 0.04F ? 5 : 3;
95
		this.setStrength(1 + random.nextInt(maxStrength));
96
	}
97
98
	public int getStrength() {
99
		return this.entityData.get(DATA_STRENGTH_ID);
100
	}
101
102
	@Override
103
	protected void addAdditionalSaveData(final ValueOutput output) {
104
		super.addAdditionalSaveData(output);
105
		output.store("Variant", Llama.Variant.LEGACY_CODEC, this.getVariant());
106
		output.putInt("Strength", this.getStrength());
107
	}
108
109
	@Override
110
	protected void readAdditionalSaveData(final ValueInput input) {
111
		this.setStrength(input.getIntOr("Strength", 0));
112
		super.readAdditionalSaveData(input);
113
		this.setVariant(input.read("Variant", Llama.Variant.LEGACY_CODEC).orElse(Llama.Variant.DEFAULT));
114
	}
115
116
	@Override
117
	protected void registerGoals() {
118
		this.goalSelector.addGoal(0, new FloatGoal(this));
119
		this.goalSelector.addGoal(1, new RunAroundLikeCrazyGoal(this, 1.2));
120
		this.goalSelector.addGoal(2, new LlamaFollowCaravanGoal(this, 2.1F));
121
		this.goalSelector.addGoal(3, new RangedAttackGoal(this, 1.25, 40, 20.0F));
122
		this.goalSelector.addGoal(3, new PanicGoal(this, 1.2));
123
		this.goalSelector.addGoal(4, new BreedGoal(this, 1.0));
124
		this.goalSelector.addGoal(5, new TemptGoal(this, 1.25, i -> i.is(ItemTags.LLAMA_TEMPT_ITEMS), false));
125
		this.goalSelector.addGoal(6, new FollowParentGoal(this, 1.0));
126
		this.goalSelector.addGoal(7, new WaterAvoidingRandomStrollGoal(this, 0.7));
127
		this.goalSelector.addGoal(8, new LookAtPlayerGoal(this, Player.class, 6.0F));
128
		this.goalSelector.addGoal(9, new RandomLookAroundGoal(this));
129
		this.targetSelector.addGoal(1, new Llama.LlamaHurtByTargetGoal(this));
130
		this.targetSelector.addGoal(2, new Llama.LlamaAttackWolfGoal(this));
131
	}
132
133
	public static AttributeSupplier.Builder createAttributes() {
134
		return createBaseChestedHorseAttributes();
135
	}
136
137
	@Override
138
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
139
		super.defineSynchedData(entityData);
140
		entityData.define(DATA_STRENGTH_ID, 0);
141
		entityData.define(DATA_VARIANT_ID, 0);
142
	}
143
144
	public Llama.Variant getVariant() {
145
		return Llama.Variant.byId(this.entityData.get(DATA_VARIANT_ID));
146
	}
147
148
	private void setVariant(final Llama.Variant variant) {
149
		this.entityData.set(DATA_VARIANT_ID, variant.id);
150
	}
151
152
	@Override
153
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
154
		return type == DataComponents.LLAMA_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
155
	}
156
157
	@Override
158
	protected void applyImplicitComponents(final DataComponentGetter components) {
159
		this.applyImplicitComponentIfPresent(components, DataComponents.LLAMA_VARIANT);
160
		super.applyImplicitComponents(components);
161
	}
162
163
	@Override
164
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
165
		if (type == DataComponents.LLAMA_VARIANT) {
166
			this.setVariant(castComponentValue(DataComponents.LLAMA_VARIANT, value));
167
			return true;
168
		} else {
169
			return super.applyImplicitComponent(type, value);
170
		}
171
	}
172
173
	@Override
174
	public boolean isFood(final ItemStack itemStack) {
175
		return itemStack.is(ItemTags.LLAMA_FOOD);
176
	}
177
178
	@Override
179
	protected boolean handleEating(final Player player, final ItemStack itemStack) {
180
		int ageUp = 0;
181
		int temper = 0;
182
		float heal = 0.0F;
183
		boolean itemUsed = false;
184
		if (itemStack.is(Items.WHEAT)) {
185
			ageUp = 10;
186
			temper = 3;
187
			heal = 2.0F;
188
		} else if (itemStack.is(Items.HAY_BLOCK)) {
189
			ageUp = 90;
190
			temper = 6;
191
			heal = 10.0F;
192
			if (this.isTamed() && this.getAge() == 0 && this.canFallInLove()) {
193
				itemUsed = true;
194
				this.setInLove(player);
195
			}
196
		}
197
198
		if (this.getHealth() < this.getMaxHealth() && heal > 0.0F) {
199
			this.heal(heal);
200
			itemUsed = true;
201
		}
202
203
		if (this.isBaby() && ageUp > 0 && !this.isAgeLocked()) {
204
			this.level().addParticle(ParticleTypes.HAPPY_VILLAGER, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), 0.0, 0.0, 0.0);
205
			if (!this.level().isClientSide()) {
206
				this.ageUp(ageUp);
207
				itemUsed = true;
208
			}
209
		}
210
211
		if (temper > 0 && (itemUsed || !this.isTamed()) && this.getTemper() < this.getMaxTemper() && !this.level().isClientSide()) {
212
			this.modifyTemper(temper);
213
			itemUsed = true;
214
		}
215
216
		if (itemUsed && !this.isSilent()) {
217
			SoundEvent eatingSound = this.getEatingSound();
218
			if (eatingSound != null) {
219
				this.level()
220
					.playSound(
221
						null,
222
						this.getX(),
223
						this.getY(),
224
						this.getZ(),
225
						this.getEatingSound(),
226
						this.getSoundSource(),
227
						1.0F,
228
						1.0F + (this.random.nextFloat() - this.random.nextFloat()) * 0.2F
229
					);
230
			}
231
		}
232
233
		return itemUsed;
234
	}
235
236
	@Override
237
	public boolean isImmobile() {
238
		return this.isDeadOrDying() || this.isEating();
239
	}
240
241
	@Override
242
	public @Nullable SpawnGroupData finalizeSpawn(
243
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
244
	) {
245
		RandomSource random = level.getRandom();
246
		this.setRandomStrength(random);
247
		Llama.Variant variant;
248
		if (groupData instanceof Llama.LlamaGroupData) {
249
			variant = ((Llama.LlamaGroupData)groupData).variant;
250
		} else {
251
			variant = Util.getRandom(Llama.Variant.values(), random);
252
			groupData = new Llama.LlamaGroupData(variant);
253
		}
254
255
		this.setVariant(variant);
256
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
257
	}
258
259
	@Override
260
	protected boolean canPerformRearing() {
261
		return false;
262
	}
263
264
	@Override
265
	protected SoundEvent getAngrySound() {
266
		return SoundEvents.LLAMA_ANGRY;
267
	}
268
269
	@Override
270
	protected SoundEvent getAmbientSound() {
271
		return SoundEvents.LLAMA_AMBIENT;
272
	}
273
274
	@Override
275
	protected SoundEvent getHurtSound(final DamageSource source) {
276
		return SoundEvents.LLAMA_HURT;
277
	}
278
279
	@Override
280
	protected SoundEvent getDeathSound() {
281
		return SoundEvents.LLAMA_DEATH;
282
	}
283
284
	@Override
285
	protected SoundEvent getEatingSound() {
286
		return SoundEvents.LLAMA_EAT;
287
	}
288
289
	@Override
290
	protected void playStepSound(final BlockPos pos, final BlockState blockState) {
291
		this.playSound(SoundEvents.LLAMA_STEP, 0.15F, 1.0F);
292
	}
293
294
	@Override
295
	protected void playChestEquipsSound() {
296
		this.playSound(SoundEvents.LLAMA_CHEST, 1.0F, (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F);
297
	}
298
299
	@Override
300
	public int getInventoryColumns() {
301
		return this.hasChest() ? this.getStrength() : 0;
302
	}
303
304
	@Override
305
	public boolean canUseSlot(final EquipmentSlot slot) {
306
		return true;
307
	}
308
309
	@Override
310
	public int getMaxTemper() {
311
		return 30;
312
	}
313
314
	@Override
315
	public boolean canMate(final Animal partner) {
316
		return partner != this && partner instanceof Llama llama && this.canParent() && llama.canParent();
317
	}
318
319
	public @Nullable Llama getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
320
		Llama baby = this.makeNewLlama();
321
		if (baby != null) {
322
			this.setOffspringAttributes(partner, baby);
323
			Llama otherLlama = (Llama)partner;
324
			int babyStrength = this.random.nextInt(Math.max(this.getStrength(), otherLlama.getStrength())) + 1;
325
			if (this.random.nextFloat() < 0.03F) {
326
				babyStrength++;
327
			}
328
329
			baby.setStrength(babyStrength);
330
			baby.setVariant(this.random.nextBoolean() ? this.getVariant() : otherLlama.getVariant());
331
		}
332
333
		return baby;
334
	}
335
336
	protected @Nullable Llama makeNewLlama() {
337
		return EntityTypes.LLAMA.create(this.level(), EntitySpawnReason.BREEDING);
338
	}
339
340
	private void spit(final LivingEntity target) {
341
		LlamaSpit spit = new LlamaSpit(this.level(), this);
342
		double xd = target.getX() - this.getX();
343
		double yd = target.getY(0.3333333333333333) - spit.getY();
344
		double zd = target.getZ() - this.getZ();
345
		double yo = Math.sqrt(xd * xd + zd * zd) * 0.2F;
346
		if (this.level() instanceof ServerLevel serverLevel) {
347
			Projectile.spawnProjectileUsingShoot(spit, serverLevel, ItemStack.EMPTY, xd, yd + yo, zd, 1.5F, this.rangedAttackUncertainty(serverLevel));
348
		}
349
350
		if (!this.isSilent()) {
351
			this.level()
352
				.playSound(
353
					null,
354
					this.getX(),
355
					this.getY(),
356
					this.getZ(),
357
					SoundEvents.LLAMA_SPIT,
358
					this.getSoundSource(),
359
					1.0F,
360
					1.0F + (this.random.nextFloat() - this.random.nextFloat()) * 0.2F
361
				);
362
		}
363
364
		this.didSpit = true;
365
	}
366
367
	private void setDidSpit(final boolean b) {
368
		this.didSpit = b;
369
	}
370
371
	@Override
372
	public boolean causeFallDamage(final double fallDistance, final float damageModifier, final DamageSource damageSource) {
373
		int dmg = this.calculateFallDamage(fallDistance, damageModifier);
374
		if (dmg <= 0) {
375
			return false;
376
		}
377
378
		if (fallDistance >= 6.0) {
379
			this.hurt(damageSource, dmg);
380
			this.propagateFallToPassengers(fallDistance, damageModifier, damageSource);
381
		}
382
383
		this.playBlockFallSound();
384
		return true;
385
	}
386
387
	public void leaveCaravan() {
388
		if (this.caravanHead != null) {
389
			this.caravanHead.caravanTail = null;
390
		}
391
392
		this.caravanHead = null;
393
	}
394
395
	public void joinCaravan(final Llama tail) {
396
		this.caravanHead = tail;
397
		this.caravanHead.caravanTail = this;
398
	}
399
400
	public boolean hasCaravanTail() {
401
		return this.caravanTail != null;
402
	}
403
404
	public boolean inCaravan() {
405
		return this.caravanHead != null;
406
	}
407
408
	public @Nullable Llama getCaravanHead() {
409
		return this.caravanHead;
410
	}
411
412
	@Override
413
	protected double followLeashSpeed() {
414
		return 2.0;
415
	}
416
417
	@Override
418
	public boolean supportQuadLeash() {
419
		return false;
420
	}
421
422
	@Override
423
	protected void followMommy(final ServerLevel level) {
424
		if (!this.inCaravan() && this.isBaby()) {
425
			super.followMommy(level);
426
		}
427
	}
428
429
	@Override
430
	public boolean canEatGrass() {
431
		return false;
432
	}
433
434
	@Override
435
	public void performRangedAttack(final LivingEntity target, final float power) {
436
		this.spit(target);
437
	}
438
439
	@Override
440
	public float rangedAttackUncertainty(final Level level) {
441
		return 10.0F;
442
	}
443
444
	@Override
445
	public Vec3 getLeashOffset() {
446
		return new Vec3(0.0, 0.75 * this.getEyeHeight(), this.getBbWidth() * 0.5);
447
	}
448
449
	@Override
450
	public EntityDimensions getDefaultDimensions(final Pose pose) {
451
		return this.isBaby() ? BABY_DIMENSIONS : super.getDefaultDimensions(pose);
452
	}
453
454
	@Override
455
	protected Vec3 getPassengerAttachmentPoint(final Entity passenger, final EntityDimensions dimensions, final float scale) {
456
		return getDefaultPassengerAttachmentPoint(this, passenger, dimensions.attachments());
457
	}
458
459
	private static class LlamaAttackWolfGoal extends NearestAttackableTargetGoal<Wolf> {
460
		public LlamaAttackWolfGoal(final Llama llama) {
461
			super(llama, Wolf.class, 16, false, true, (target, level) -> !((Wolf)target).isTame());
462
		}
463
464
		@Override
465
		protected double getFollowDistance() {
466
			return super.getFollowDistance() * 0.25;
467
		}
468
	}
469
470
	private static class LlamaGroupData extends AgeableMob.AgeableMobGroupData {
471
		public final Llama.Variant variant;
472
473
		private LlamaGroupData(final Llama.Variant variant) {
474
			super(true);
475
			this.variant = variant;
476
		}
477
	}
478
479
	private static class LlamaHurtByTargetGoal extends HurtByTargetGoal {
480
		public LlamaHurtByTargetGoal(final Llama llama) {
481
			super(llama);
482
		}
483
484
		@Override
485
		public boolean canContinueToUse() {
486
			if (this.mob instanceof Llama llama && llama.didSpit) {
487
				llama.setDidSpit(false);
488
				return false;
489
			} else {
490
				return super.canContinueToUse();
491
			}
492
		}
493
	}
494
495
	public enum Variant implements StringRepresentable {
496
		CREAMY(0, "creamy"),
497
		WHITE(1, "white"),
498
		BROWN(2, "brown"),
499
		GRAY(3, "gray");
500
501
		public static final Llama.Variant DEFAULT = CREAMY;
502
		private static final IntFunction<Llama.Variant> BY_ID = ByIdMap.continuous(Llama.Variant::getId, values(), ByIdMap.OutOfBoundsStrategy.CLAMP);
503
		public static final Codec<Llama.Variant> CODEC = StringRepresentable.fromEnum(Llama.Variant::values);
504
		@Deprecated
505
		public static final Codec<Llama.Variant> LEGACY_CODEC = Codec.INT.xmap(BY_ID::apply, Llama.Variant::getId);
506
		public static final StreamCodec<ByteBuf, Llama.Variant> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, Llama.Variant::getId);
507
		private final int id;
508
		private final String name;
509
510
		Variant(final int id, final String name) {
511
			this.id = id;
512
			this.name = name;
513
		}
514
515
		public int getId() {
516
			return this.id;
517
		}
518
519
		public static Llama.Variant byId(final int id) {
520
			return BY_ID.apply(id);
521
		}
522
523
		@Override
524
		public String getSerializedName() {
525
			return this.name;
526
		}
527
	}
528
}
529

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