AgeableMob .java

net.minecraft.world.entity.AgeableMob (class, 314 lines): members: AGE_LOCK_DOWNWARDS_MOVING_PARTICLE_Y_OFFSET, getSpeedUpSecondsWhenFeeding, getForcedAge.

net.minecraft.world.entityclass314 linescommonextends PathfinderMob9.4 KB

API Surface (29)

InteractionResult mobInteract(final Player player, final InteractionHand hand)
boolean canUseGoldenDandelion(final ItemStack itemInHand, final boolean isBaby, final int cooldown, final Mob mob)
void setAgeLocked(final Mob mob, final Supplier<Boolean> isAgedLocked, final Player player, final ItemStack itemInHand, final Consumer<Mob> setAgeLockData)
void defineSynchedData(final SynchedEntityData.Builder entityData)
boolean canBreed()
int getAge()
void ageUp(final int seconds, final boolean forced)
void ageUp(final int seconds)
void setAge(final int newAge)
void setAgeLocked(final boolean locked)
void addAdditionalSaveData(final ValueOutput output)
boolean canBeABaby()
void readAdditionalSaveData(final ValueInput input)
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
void aiStep()
boolean canAgeUp()
int makeAgeLockedParticle(final Level level, final Mob mob, int ageLockParticleTimer, final boolean isAgeLocked)
void ageBoundaryReached()
int getBabyStartAge()
boolean isBaby()
void setBaby(final boolean baby)
int getSpeedUpSecondsWhenFeeding(final int ticksUntilAdult)
int getForcedAge()
int getForcedAgeTimer()
boolean isAgeLocked()
int getGroupSize()
void increaseGroupSizeByOne()
boolean isShouldSpawnBaby()
float getBabySpawnChance()

Fields & Constants (14)

DATA_BABY_ID
AGE_LOCKED
BABY_START_AGE
AGE_LOCK_COOLDOWN_TICKS
AGE_LOCK_DOWNWARDS_MOVING_PARTICLE_Y_OFFSET
FORCED_AGE_PARTICLE_TICKS
DEFAULT_AGE
DEFAULT_FORCED_AGE
age
forcedAge
forcedAgeTimer
ageLockParticleTimer
spawnReason
itemInHand

String literals (2)

"ForcedAge"
"AgeLocked"

Full Source

AgeableMob.java314 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import java.util.function.Consumer;
5
import java.util.function.Supplier;
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.SoundEvents;
12
import net.minecraft.sounds.SoundSource;
13
import net.minecraft.tags.EntityTypeTags;
14
import net.minecraft.world.DifficultyInstance;
15
import net.minecraft.world.InteractionHand;
16
import net.minecraft.world.InteractionResult;
17
import net.minecraft.world.entity.player.Player;
18
import net.minecraft.world.entity.vehicle.boat.AbstractBoat;
19
import net.minecraft.world.item.ItemStack;
20
import net.minecraft.world.item.Items;
21
import net.minecraft.world.level.Level;
22
import net.minecraft.world.level.ServerLevelAccessor;
23
import net.minecraft.world.level.storage.ValueInput;
24
import net.minecraft.world.level.storage.ValueOutput;
25
import net.minecraft.world.phys.Vec3;
26
import org.jspecify.annotations.Nullable;
27
28
public abstract class AgeableMob extends PathfinderMob {
29
	private static final EntityDataAccessor<Boolean> DATA_BABY_ID = SynchedEntityData.defineId(AgeableMob.class, EntityDataSerializers.BOOLEAN);
30
	private static final EntityDataAccessor<Boolean> AGE_LOCKED = SynchedEntityData.defineId(AgeableMob.class, EntityDataSerializers.BOOLEAN);
31
	public static final int BABY_START_AGE = -24000;
32
	public static final int AGE_LOCK_COOLDOWN_TICKS = 40;
33
	public static final float AGE_LOCK_DOWNWARDS_MOVING_PARTICLE_Y_OFFSET = 0.2F;
34
	private static final int FORCED_AGE_PARTICLE_TICKS = 40;
35
	protected static final int DEFAULT_AGE = 0;
36
	protected static final int DEFAULT_FORCED_AGE = 0;
37
	protected int age = 0;
38
	protected int forcedAge = 0;
39
	protected int forcedAgeTimer;
40
	protected int ageLockParticleTimer = 0;
41
42
	protected AgeableMob(final EntityType<? extends AgeableMob> type, final Level level) {
43
		super(type, level);
44
	}
45
46
	@Override
47
	public @Nullable SpawnGroupData finalizeSpawn(
48
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, @Nullable SpawnGroupData groupData
49
	) {
50
		if (groupData == null) {
51
			groupData = new AgeableMob.AgeableMobGroupData(true);
52
		}
53
54
		AgeableMob.AgeableMobGroupData ageableMobGroupData = (AgeableMob.AgeableMobGroupData)groupData;
55
		if (ageableMobGroupData.isShouldSpawnBaby()
56
			&& ageableMobGroupData.getGroupSize() > 0
57
			&& level.getRandom().nextFloat() <= ageableMobGroupData.getBabySpawnChance()) {
58
			this.setAge(this.getBabyStartAge());
59
		}
60
61
		ageableMobGroupData.increaseGroupSizeByOne();
62
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
63
	}
64
65
	public abstract @Nullable AgeableMob getBreedOffspring(final ServerLevel level, AgeableMob partner);
66
67
	@Override
68
	protected InteractionResult mobInteract(final Player player, final InteractionHand hand) {
69
		ItemStack itemInHand = player.getItemInHand(hand);
70
		if (canUseGoldenDandelion(itemInHand, this.isBaby(), this.ageLockParticleTimer, this)) {
71
			setAgeLocked(this, this::isAgeLocked, player, itemInHand, mob -> this.setAgeLockedData());
72
			return InteractionResult.SUCCESS;
73
		} else {
74
			return super.mobInteract(player, hand);
75
		}
76
	}
77
78
	public static boolean canUseGoldenDandelion(final ItemStack itemInHand, final boolean isBaby, final int cooldown, final Mob mob) {
79
		return itemInHand.getItem() == Items.GOLDEN_DANDELION && isBaby && cooldown == 0 && !mob.is(EntityTypeTags.CANNOT_BE_AGE_LOCKED);
80
	}
81
82
	private void setAgeLockedData() {
83
		this.setAgeLocked(!this.isAgeLocked());
84
		this.setAge(this.getBabyStartAge());
85
		this.ageLockParticleTimer = 40;
86
	}
87
88
	public static void setAgeLocked(
89
		final Mob mob, final Supplier<Boolean> isAgedLocked, final Player player, final ItemStack itemInHand, final Consumer<Mob> setAgeLockData
90
	) {
91
		setAgeLockData.accept(mob);
92
		itemInHand.consume(1, player);
93
		boolean isAgeLocked = isAgedLocked.get();
94
		if (isAgeLocked) {
95
			mob.setPersistenceRequired();
96
		}
97
98
		mob.level()
99
			.playSound(null, mob.blockPosition(), isAgeLocked ? SoundEvents.GOLDEN_DANDELION_USE : SoundEvents.GOLDEN_DANDELION_UNUSE, SoundSource.PLAYERS, 1.0F, 1.0F);
100
	}
101
102
	@Override
103
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
104
		super.defineSynchedData(entityData);
105
		entityData.define(DATA_BABY_ID, false);
106
		entityData.define(AGE_LOCKED, false);
107
	}
108
109
	public boolean canBreed() {
110
		return false;
111
	}
112
113
	public int getAge() {
114
		if (this.level().isClientSide()) {
115
			return this.entityData.get(DATA_BABY_ID) ? -1 : 1;
116
		} else {
117
			return this.age;
118
		}
119
	}
120
121
	public void ageUp(final int seconds, final boolean forced) {
122
		int age = this.getAge();
123
		int oldAge = age;
124
		age += seconds * 20;
125
		if (age > 0) {
126
			age = 0;
127
		}
128
129
		int delta = age - oldAge;
130
		this.setAge(age);
131
		if (forced) {
132
			this.forcedAge += delta;
133
			if (this.forcedAgeTimer == 0) {
134
				this.forcedAgeTimer = 40;
135
			}
136
		}
137
138
		if (this.getAge() == 0) {
139
			this.setAge(this.forcedAge);
140
		}
141
	}
142
143
	public void ageUp(final int seconds) {
144
		this.ageUp(seconds, false);
145
	}
146
147
	public void setAge(final int newAge) {
148
		int oldAge = this.getAge();
149
		this.age = newAge;
150
		if (oldAge < 0 && newAge >= 0 || oldAge >= 0 && newAge < 0) {
151
			this.entityData.set(DATA_BABY_ID, newAge < 0);
152
			this.ageBoundaryReached();
153
		}
154
	}
155
156
	protected void setAgeLocked(final boolean locked) {
157
		this.entityData.set(AGE_LOCKED, locked);
158
	}
159
160
	@Override
161
	protected void addAdditionalSaveData(final ValueOutput output) {
162
		super.addAdditionalSaveData(output);
163
		if (this.canBeABaby()) {
164
			output.putInt("Age", this.getAge());
165
			output.putInt("ForcedAge", this.forcedAge);
166
			output.putBoolean("AgeLocked", this.isAgeLocked());
167
		}
168
	}
169
170
	protected boolean canBeABaby() {
171
		return true;
172
	}
173
174
	@Override
175
	protected void readAdditionalSaveData(final ValueInput input) {
176
		super.readAdditionalSaveData(input);
177
		if (this.canBeABaby()) {
178
			this.setAge(input.getIntOr("Age", 0));
179
			this.forcedAge = input.getIntOr("ForcedAge", 0);
180
			this.setAgeLocked(input.getBooleanOr("AgeLocked", false));
181
		}
182
	}
183
184
	@Override
185
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
186
		if (DATA_BABY_ID.equals(accessor)) {
187
			this.refreshDimensions();
188
		}
189
190
		super.onSyncedDataUpdated(accessor);
191
	}
192
193
	@Override
194
	public void aiStep() {
195
		super.aiStep();
196
		Level level = this.level();
197
		if (level instanceof ServerLevel serverLevel) {
198
			if (this.forcedAgeTimer > 0) {
199
				if (this.forcedAgeTimer % 4 == 0) {
200
					serverLevel.sendParticles(ParticleTypes.HAPPY_VILLAGER, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), 1, 0.0, 0.0, 0.0, 0.0);
201
				}
202
203
				this.forcedAgeTimer--;
204
			}
205
206
			if (this.isAlive()) {
207
				int age = this.getAge();
208
				if (this.canAgeUp()) {
209
					this.setAge(++age);
210
				} else if (age > 0) {
211
					this.setAge(--age);
212
				}
213
			}
214
		}
215
216
		this.ageLockParticleTimer = makeAgeLockedParticle(level, this, this.ageLockParticleTimer, this.isAgeLocked());
217
	}
218
219
	public boolean canAgeUp() {
220
		return this.isBaby() && !this.isAgeLocked();
221
	}
222
223
	public static int makeAgeLockedParticle(final Level level, final Mob mob, int ageLockParticleTimer, final boolean isAgeLocked) {
224
		if (ageLockParticleTimer > 0) {
225
			if (ageLockParticleTimer % 2 == 0 && level instanceof ServerLevel serverLevel) {
226
				float yParticleOffset = isAgeLocked ? 0.2F : 0.0F;
227
				Vec3 spawnPosition = new Vec3(mob.getRandomX(1.0), mob.getRandomY(0.2) + mob.getBbHeight() + yParticleOffset, mob.getRandomZ(1.0));
228
				serverLevel.sendParticles(
229
					isAgeLocked ? ParticleTypes.PAUSE_MOB_GROWTH : ParticleTypes.RESET_MOB_GROWTH, spawnPosition.x, spawnPosition.y, spawnPosition.z, 1, 0.0, 0.0, 0.0, 0.0
230
				);
231
			}
232
233
			ageLockParticleTimer--;
234
		}
235
236
		return ageLockParticleTimer;
237
	}
238
239
	protected void ageBoundaryReached() {
240
		if (!this.isBaby() && this.isPassenger() && this.getVehicle() instanceof AbstractBoat boat && !boat.hasEnoughSpaceFor(this)) {
241
			this.stopRiding();
242
		}
243
	}
244
245
	protected int getBabyStartAge() {
246
		return -24000;
247
	}
248
249
	@Override
250
	public final boolean isBaby() {
251
		return this.canBeABaby() && this.getAge() < 0;
252
	}
253
254
	@Override
255
	public final void setBaby(final boolean baby) {
256
		if (this.canBeABaby()) {
257
			this.setAge(baby ? this.getBabyStartAge() : 0);
258
		}
259
	}
260
261
	public static int getSpeedUpSecondsWhenFeeding(final int ticksUntilAdult) {
262
		return (int)(ticksUntilAdult / 20 * 0.1F);
263
	}
264
265
	@VisibleForTesting
266
	public int getForcedAge() {
267
		return this.forcedAge;
268
	}
269
270
	@VisibleForTesting
271
	public int getForcedAgeTimer() {
272
		return this.forcedAgeTimer;
273
	}
274
275
	public boolean isAgeLocked() {
276
		return this.entityData.get(AGE_LOCKED);
277
	}
278
279
	public static class AgeableMobGroupData implements SpawnGroupData {
280
		private int groupSize;
281
		private final boolean shouldSpawnBaby;
282
		private final float babySpawnChance;
283
284
		public AgeableMobGroupData(final boolean shouldSpawnBaby, final float babySpawnChance) {
285
			this.shouldSpawnBaby = shouldSpawnBaby;
286
			this.babySpawnChance = babySpawnChance;
287
		}
288
289
		public AgeableMobGroupData(final boolean shouldSpawnBaby) {
290
			this(shouldSpawnBaby, 0.05F);
291
		}
292
293
		public AgeableMobGroupData(final float babySpawnChance) {
294
			this(true, babySpawnChance);
295
		}
296
297
		public int getGroupSize() {
298
			return this.groupSize;
299
		}
300
301
		public void increaseGroupSizeByOne() {
302
			this.groupSize++;
303
		}
304
305
		public boolean isShouldSpawnBaby() {
306
			return this.shouldSpawnBaby;
307
		}
308
309
		public float getBabySpawnChance() {
310
			return this.babySpawnChance;
311
		}
312
	}
313
}
314

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