AbstractCubeMob .java

net.minecraft.world.entity.monster.cubemob.AbstractCubeMob (class, 519 lines): members: CubeMobRandomDirectionGoal, CubeMobKeepOnJumpingGoal, targetSquish.

net.minecraft.world.entity.monster.cubemobclass519 linescommonextends AgeableMob15.7 KB

API Surface (46)

void registerGoals()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void setSize(final int size, final boolean updateHealth)
void setCubeMobHealth(final int actualSize)
int getSize()
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
boolean isTiny()
void tick()
void decreaseSquish()
int getJumpDelay()
void refreshDimensions()
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
EntityType<? extends AbstractCubeMob> getType()
void remove(final Entity.RemovalReason reason)
void setUpSplitCube(final AbstractCubeMob cubeMob, final int halfSize, final float xd, final float zd)
int getSplitCount()
void push(final Entity entity)
void playerTouch(final Player player)
void dealDamage(final LivingEntity target)
Vec3 getPassengerAttachmentPoint(final Entity passenger, final EntityDimensions dimensions, final float scale)
boolean canDealDamage()
float getAttackDamage()
EntityDimensions getDefaultDimensions(final Pose pose)
void jumpFromGround()
SoundSource getSoundSource()
float getSoundVolume()
int getMaxHeadXRot()
boolean doPlayJumpSound()
float getSoundPitch()
void setSpawnSize(final ServerLevelAccessor level, final DifficultyInstance difficulty)
boolean canUse()
void start()
boolean canContinueToUse()
boolean requiresUpdateEveryTick()
void tick()
boolean canUse()
boolean requiresUpdateEveryTick()
void tick()
boolean canUse()
void tick()
void setDirection(final float yRot, final boolean isAggressive)
void setWantedMovement(final double speedModifier)
void tick()
boolean canUse()
void tick()

Fields & Constants (10)

ID_SIZE
MIN_SIZE
MAX_SIZE
MAX_NATURAL_SIZE
DEFAULT_WAS_ON_GROUND
targetSquish
squish
oSquish
wasOnGround
spawnReason

String literals (2)

"Size"
"wasOnGround"

Full Source

AbstractCubeMob.java519 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster.cubemob;
2
3
import com.google.common.annotations.VisibleForTesting;
4
import java.util.EnumSet;
5
import net.minecraft.core.particles.ParticleOptions;
6
import net.minecraft.network.syncher.EntityDataAccessor;
7
import net.minecraft.network.syncher.EntityDataSerializers;
8
import net.minecraft.network.syncher.SynchedEntityData;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.sounds.SoundEvent;
11
import net.minecraft.sounds.SoundEvents;
12
import net.minecraft.sounds.SoundSource;
13
import net.minecraft.util.Mth;
14
import net.minecraft.util.RandomSource;
15
import net.minecraft.world.DifficultyInstance;
16
import net.minecraft.world.damagesource.DamageSource;
17
import net.minecraft.world.effect.MobEffects;
18
import net.minecraft.world.entity.AgeableMob;
19
import net.minecraft.world.entity.ConversionParams;
20
import net.minecraft.world.entity.ConversionType;
21
import net.minecraft.world.entity.Entity;
22
import net.minecraft.world.entity.EntityDimensions;
23
import net.minecraft.world.entity.EntitySpawnReason;
24
import net.minecraft.world.entity.EntityType;
25
import net.minecraft.world.entity.LivingEntity;
26
import net.minecraft.world.entity.Pose;
27
import net.minecraft.world.entity.SpawnGroupData;
28
import net.minecraft.world.entity.ai.attributes.Attributes;
29
import net.minecraft.world.entity.ai.control.MoveControl;
30
import net.minecraft.world.entity.ai.goal.Goal;
31
import net.minecraft.world.entity.animal.golem.IronGolem;
32
import net.minecraft.world.entity.player.Player;
33
import net.minecraft.world.item.enchantment.EnchantmentHelper;
34
import net.minecraft.world.level.Level;
35
import net.minecraft.world.level.ServerLevelAccessor;
36
import net.minecraft.world.level.storage.ValueInput;
37
import net.minecraft.world.level.storage.ValueOutput;
38
import net.minecraft.world.phys.Vec3;
39
import net.minecraft.world.scores.PlayerTeam;
40
import org.jspecify.annotations.Nullable;
41
42
public abstract class AbstractCubeMob extends AgeableMob {
43
	protected static final EntityDataAccessor<Integer> ID_SIZE = SynchedEntityData.defineId(AbstractCubeMob.class, EntityDataSerializers.INT);
44
	public static final int MIN_SIZE = 1;
45
	public static final int MAX_SIZE = 127;
46
	public static final int MAX_NATURAL_SIZE = 4;
47
	private static final boolean DEFAULT_WAS_ON_GROUND = false;
48
	public float targetSquish;
49
	public float squish;
50
	public float oSquish;
51
	private boolean wasOnGround = false;
52
53
	protected AbstractCubeMob(final EntityType<? extends AbstractCubeMob> type, final Level level) {
54
		super(type, level);
55
		this.fixupDimensions();
56
		this.moveControl = new AbstractCubeMob.CubeMobMoveControl<>(this);
57
	}
58
59
	@Override
60
	protected void registerGoals() {
61
		this.goalSelector.addGoal(1, new AbstractCubeMob.CubeMobFloatGoal(this));
62
		this.goalSelector.addGoal(4, new AbstractCubeMob.CubeMobRandomDirectionGoal(this));
63
		this.goalSelector.addGoal(5, new AbstractCubeMob.CubeMobKeepOnJumpingGoal(this));
64
		this.addBehaviourGoals();
65
		this.addTargetingGoals();
66
	}
67
68
	protected abstract void addBehaviourGoals();
69
70
	protected abstract void addTargetingGoals();
71
72
	@Override
73
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
74
		super.defineSynchedData(entityData);
75
		entityData.define(ID_SIZE, 1);
76
	}
77
78
	@VisibleForTesting
79
	public void setSize(final int size, final boolean updateHealth) {
80
		int actualSize = Mth.clamp(size, 1, 127);
81
		this.entityData.set(ID_SIZE, actualSize);
82
		this.reapplyPosition();
83
		this.refreshDimensions();
84
		this.setCubeMobHealth(actualSize);
85
		this.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(0.2F + 0.1F * actualSize);
86
		if (updateHealth) {
87
			this.setHealth(this.getMaxHealth());
88
		}
89
	}
90
91
	protected void setCubeMobHealth(final int actualSize) {
92
		this.getAttribute(Attributes.MAX_HEALTH).setBaseValue(actualSize * actualSize);
93
	}
94
95
	public int getSize() {
96
		return this.entityData.get(ID_SIZE);
97
	}
98
99
	@Override
100
	protected void addAdditionalSaveData(final ValueOutput output) {
101
		super.addAdditionalSaveData(output);
102
		output.putInt("Size", this.getSize() - 1);
103
		output.putBoolean("wasOnGround", this.wasOnGround);
104
	}
105
106
	@Override
107
	protected void readAdditionalSaveData(final ValueInput input) {
108
		this.setSize(input.getIntOr("Size", 0) + 1, false);
109
		super.readAdditionalSaveData(input);
110
		this.wasOnGround = input.getBooleanOr("wasOnGround", false);
111
	}
112
113
	public boolean isTiny() {
114
		return this.getSize() <= 1;
115
	}
116
117
	protected abstract @Nullable ParticleOptions getParticleType();
118
119
	@Override
120
	public void tick() {
121
		this.oSquish = this.squish;
122
		this.squish = this.squish + (this.targetSquish - this.squish) * 0.5F;
123
		super.tick();
124
		if (this.onGround() && !this.wasOnGround) {
125
			float size = this.getDimensions(this.getPose()).width() * 2.0F;
126
			float radius = size / 2.0F;
127
128
			for (int i = 0; i < size * 16.0F; i++) {
129
				float dir = this.random.nextFloat() * (float) (Math.PI * 2);
130
				float d = this.random.nextFloat() * 0.5F + 0.5F;
131
				float xd = Mth.sin(dir) * radius * d;
132
				float zd = Mth.cos(dir) * radius * d;
133
				ParticleOptions particleType = this.getParticleType();
134
				if (particleType != null) {
135
					this.level().addParticle(particleType, this.getX() + xd, this.getY(), this.getZ() + zd, 0.0, 0.0, 0.0);
136
				}
137
			}
138
139
			this.playSound(this.getSquishSound(), this.getSoundVolume(), ((this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F) / 0.8F);
140
			this.targetSquish = -0.5F;
141
		} else if (!this.onGround() && this.wasOnGround) {
142
			this.targetSquish = 1.0F;
143
		}
144
145
		this.wasOnGround = this.onGround();
146
		this.decreaseSquish();
147
	}
148
149
	protected void decreaseSquish() {
150
		this.targetSquish *= 0.6F;
151
	}
152
153
	protected int getJumpDelay() {
154
		return this.random.nextInt(20) + 10;
155
	}
156
157
	@Override
158
	public void refreshDimensions() {
159
		double oldX = this.getX();
160
		double oldY = this.getY();
161
		double oldZ = this.getZ();
162
		super.refreshDimensions();
163
		this.setPos(oldX, oldY, oldZ);
164
	}
165
166
	@Override
167
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
168
		if (ID_SIZE.equals(accessor)) {
169
			this.refreshDimensions();
170
			this.setYRot(this.yHeadRot);
171
			this.yBodyRot = this.yHeadRot;
172
			if (this.isInWater() && this.random.nextInt(20) == 0) {
173
				this.doWaterSplashEffect();
174
			}
175
		}
176
177
		super.onSyncedDataUpdated(accessor);
178
	}
179
180
	@Override
181
	public EntityType<? extends AbstractCubeMob> getType() {
182
		return (EntityType<? extends AbstractCubeMob>)super.getType();
183
	}
184
185
	@Override
186
	public void remove(final Entity.RemovalReason reason) {
187
		int size = this.getSize();
188
		if (!this.level().isClientSide() && size > 1 && this.isDeadOrDying()) {
189
			float width = this.getDimensions(this.getPose()).width();
190
			float xzCubeSpawnOffset = width / 2.0F;
191
			int halfSize = size / 2;
192
			int count = this.getSplitCount();
193
			PlayerTeam team = this.getTeam();
194
195
			for (int i = 0; i < count; i++) {
196
				float xd = (i % 2 - 0.5F) * xzCubeSpawnOffset;
197
				float zd = (i / 2 - 0.5F) * xzCubeSpawnOffset;
198
				this.convertTo(
199
					this.getType(),
200
					new ConversionParams(ConversionType.SPLIT_ON_DEATH, false, false, team),
201
					EntitySpawnReason.TRIGGERED,
202
					cubeMob -> this.setUpSplitCube(cubeMob, halfSize, xd, zd)
203
				);
204
			}
205
		}
206
207
		super.remove(reason);
208
	}
209
210
	protected void setUpSplitCube(final AbstractCubeMob cubeMob, final int halfSize, final float xd, final float zd) {
211
		cubeMob.setSize(halfSize, true);
212
		cubeMob.snapTo(this.getX() + xd, this.getY() + 0.5, this.getZ() + zd, this.random.nextFloat() * 360.0F, 0.0F);
213
	}
214
215
	protected int getSplitCount() {
216
		return 2 + this.random.nextInt(3);
217
	}
218
219
	@Override
220
	public void push(final Entity entity) {
221
		super.push(entity);
222
		if (entity instanceof IronGolem && this.canDealDamage()) {
223
			this.dealDamage((LivingEntity)entity);
224
		}
225
	}
226
227
	@Override
228
	public void playerTouch(final Player player) {
229
		if (this.canDealDamage()) {
230
			this.dealDamage(player);
231
		}
232
	}
233
234
	protected void dealDamage(final LivingEntity target) {
235
		if (this.level() instanceof ServerLevel level
236
			&& this.isAlive()
237
			&& this.doTeamsAllowDamage(target)
238
			&& this.isWithinMeleeAttackRange(target)
239
			&& this.hasLineOfSight(target)) {
240
			DamageSource damageSource = this.damageSources().mobAttack(this);
241
			if (target.hurtServer(level, damageSource, this.getAttackDamage())) {
242
				this.playSound(SoundEvents.SLIME_ATTACK, 1.0F, (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F);
243
				EnchantmentHelper.doPostAttackEffects(level, target, damageSource);
244
			}
245
		}
246
	}
247
248
	@Override
249
	protected Vec3 getPassengerAttachmentPoint(final Entity passenger, final EntityDimensions dimensions, final float scale) {
250
		return new Vec3(0.0, dimensions.height() - 0.015625 * this.getSize() * scale, 0.0);
251
	}
252
253
	protected boolean canDealDamage() {
254
		return !this.isTiny() && this.isEffectiveAi();
255
	}
256
257
	protected float getAttackDamage() {
258
		return (float)this.getAttributeValue(Attributes.ATTACK_DAMAGE);
259
	}
260
261
	@Override
262
	public EntityDimensions getDefaultDimensions(final Pose pose) {
263
		return this.getType().getDimensions().scale(this.getSize());
264
	}
265
266
	@Override
267
	public void jumpFromGround() {
268
		Vec3 movement = this.getDeltaMovement();
269
		this.setDeltaMovement(movement.x, this.getJumpPower(), movement.z);
270
		this.needsSync = true;
271
	}
272
273
	@Override
274
	public SoundSource getSoundSource() {
275
		return SoundSource.HOSTILE;
276
	}
277
278
	@Override
279
	protected float getSoundVolume() {
280
		return 0.4F * this.getSize();
281
	}
282
283
	@Override
284
	public int getMaxHeadXRot() {
285
		return 0;
286
	}
287
288
	protected boolean doPlayJumpSound() {
289
		return this.getSize() > 0;
290
	}
291
292
	public float getSoundPitch() {
293
		float pitchAdjuster = this.isTiny() ? 1.4F : 0.8F;
294
		return ((this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F) * pitchAdjuster;
295
	}
296
297
	protected abstract SoundEvent getJumpSound();
298
299
	@Override
300
	protected abstract SoundEvent getHurtSound(final DamageSource source);
301
302
	@Override
303
	protected abstract SoundEvent getDeathSound();
304
305
	protected abstract SoundEvent getSquishSound();
306
307
	public @Nullable AbstractCubeMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
308
		return null;
309
	}
310
311
	@Override
312
	public @Nullable SpawnGroupData finalizeSpawn(
313
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
314
	) {
315
		SpawnGroupData data = super.finalizeSpawn(level, difficulty, spawnReason, groupData);
316
		this.setSpawnSize(level, difficulty);
317
		return data;
318
	}
319
320
	protected void setSpawnSize(final ServerLevelAccessor level, final DifficultyInstance difficulty) {
321
		RandomSource random = level.getRandom();
322
		int sizeScale = random.nextInt(3);
323
		if (sizeScale < 2 && random.nextFloat() < 0.5F * difficulty.getSpecialMultiplier()) {
324
			sizeScale++;
325
		}
326
327
		int size = 1 << sizeScale;
328
		this.setSize(size, true);
329
	}
330
331
	protected static class CubeMobAttackGoal extends Goal {
332
		private final AbstractCubeMob cubeMob;
333
		private int growTiredTimer;
334
335
		public CubeMobAttackGoal(final AbstractCubeMob cubeMob) {
336
			this.cubeMob = cubeMob;
337
			this.setFlags(EnumSet.of(Goal.Flag.LOOK));
338
		}
339
340
		@Override
341
		public boolean canUse() {
342
			LivingEntity target = this.cubeMob.getTarget();
343
			if (target == null) {
344
				return false;
345
			} else {
346
				return !this.cubeMob.canAttack(target) ? false : this.cubeMob.getMoveControl() instanceof AbstractCubeMob.CubeMobMoveControl;
347
			}
348
		}
349
350
		@Override
351
		public void start() {
352
			this.growTiredTimer = reducedTickDelay(300);
353
			super.start();
354
		}
355
356
		@Override
357
		public boolean canContinueToUse() {
358
			LivingEntity target = this.cubeMob.getTarget();
359
			if (target == null) {
360
				return false;
361
			} else {
362
				return !this.cubeMob.canAttack(target) ? false : --this.growTiredTimer > 0;
363
			}
364
		}
365
366
		@Override
367
		public boolean requiresUpdateEveryTick() {
368
			return true;
369
		}
370
371
		@Override
372
		public void tick() {
373
			LivingEntity target = this.cubeMob.getTarget();
374
			if (target != null) {
375
				this.cubeMob.lookAt(target, 10.0F, 10.0F);
376
			}
377
378
			if (this.cubeMob.getMoveControl() instanceof AbstractCubeMob.CubeMobMoveControl cubeMobMoveControl) {
379
				cubeMobMoveControl.setDirection(this.cubeMob.getYRot(), this.cubeMob.canDealDamage());
380
			}
381
		}
382
	}
383
384
	private static class CubeMobFloatGoal extends Goal {
385
		private final AbstractCubeMob cubeMob;
386
387
		public CubeMobFloatGoal(final AbstractCubeMob mob) {
388
			this.cubeMob = mob;
389
			this.setFlags(EnumSet.of(Goal.Flag.JUMP, Goal.Flag.MOVE));
390
			mob.getNavigation().setCanFloat(true);
391
		}
392
393
		@Override
394
		public boolean canUse() {
395
			return this.cubeMob.isInLiquid() && this.cubeMob.getMoveControl() instanceof AbstractCubeMob.CubeMobMoveControl;
396
		}
397
398
		@Override
399
		public boolean requiresUpdateEveryTick() {
400
			return true;
401
		}
402
403
		@Override
404
		public void tick() {
405
			if (this.cubeMob.getRandom().nextFloat() < 0.8F) {
406
				this.cubeMob.getJumpControl().jump();
407
			}
408
409
			if (this.cubeMob.getMoveControl() instanceof AbstractCubeMob.CubeMobMoveControl cubeMobMoveControl) {
410
				cubeMobMoveControl.setWantedMovement(1.2);
411
			}
412
		}
413
	}
414
415
	private static class CubeMobKeepOnJumpingGoal extends Goal {
416
		private final AbstractCubeMob cubeMob;
417
418
		public CubeMobKeepOnJumpingGoal(final AbstractCubeMob mob) {
419
			this.cubeMob = mob;
420
			this.setFlags(EnumSet.of(Goal.Flag.JUMP, Goal.Flag.MOVE));
421
		}
422
423
		@Override
424
		public boolean canUse() {
425
			return !this.cubeMob.isPassenger();
426
		}
427
428
		@Override
429
		public void tick() {
430
			if (this.cubeMob.getMoveControl() instanceof AbstractCubeMob.CubeMobMoveControl cubeMobMoveControl) {
431
				cubeMobMoveControl.setWantedMovement(1.0);
432
			}
433
		}
434
	}
435
436
	protected static class CubeMobMoveControl<T extends AbstractCubeMob> extends MoveControl<T> {
437
		private float yRot;
438
		private int jumpDelay;
439
		private boolean isAggressive;
440
441
		public CubeMobMoveControl(final T cubeMob) {
442
			super(cubeMob);
443
			this.yRot = 180.0F * cubeMob.getYRot() / (float) Math.PI;
444
		}
445
446
		public void setDirection(final float yRot, final boolean isAggressive) {
447
			this.yRot = yRot;
448
			this.isAggressive = isAggressive;
449
		}
450
451
		public void setWantedMovement(final double speedModifier) {
452
			this.speedModifier = speedModifier;
453
			this.operation = MoveControl.Operation.MOVE_TO;
454
		}
455
456
		@Override
457
		public void tick() {
458
			this.mob.setYRot(this.rotlerp(this.mob.getYRot(), this.yRot, 90.0F));
459
			this.mob.yHeadRot = this.mob.getYRot();
460
			this.mob.yBodyRot = this.mob.getYRot();
461
			if (this.operation != MoveControl.Operation.MOVE_TO) {
462
				this.mob.setZza(0.0F);
463
			} else {
464
				this.operation = MoveControl.Operation.WAIT;
465
				if (this.mob.onGround()) {
466
					this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
467
					if (this.jumpDelay-- <= 0) {
468
						this.jumpDelay = this.mob.getJumpDelay();
469
						if (this.isAggressive) {
470
							this.jumpDelay /= 3;
471
						}
472
473
						this.mob.getJumpControl().jump();
474
						if (this.mob.doPlayJumpSound()) {
475
							this.mob.playSound(this.mob.getJumpSound(), this.mob.getSoundVolume(), this.mob.getSoundPitch());
476
						}
477
					} else {
478
						this.mob.xxa = 0.0F;
479
						this.mob.zza = 0.0F;
480
						this.mob.setSpeed(0.0F);
481
					}
482
				} else {
483
					this.mob.setSpeed((float)(this.speedModifier * this.mob.getAttributeValue(Attributes.MOVEMENT_SPEED)));
484
				}
485
			}
486
		}
487
	}
488
489
	private static class CubeMobRandomDirectionGoal extends Goal {
490
		private final AbstractCubeMob cubeMob;
491
		private float chosenDegrees;
492
		private int nextRandomizeTime;
493
494
		public CubeMobRandomDirectionGoal(final AbstractCubeMob cubeMob) {
495
			this.cubeMob = cubeMob;
496
			this.setFlags(EnumSet.of(Goal.Flag.LOOK));
497
		}
498
499
		@Override
500
		public boolean canUse() {
501
			return this.cubeMob.getTarget() == null
502
				&& (this.cubeMob.onGround() || this.cubeMob.isInLiquid() || this.cubeMob.hasEffect(MobEffects.LEVITATION))
503
				&& this.cubeMob.getMoveControl() instanceof AbstractCubeMob.CubeMobMoveControl;
504
		}
505
506
		@Override
507
		public void tick() {
508
			if (--this.nextRandomizeTime <= 0) {
509
				this.nextRandomizeTime = this.adjustedTickDelay(40 + this.cubeMob.getRandom().nextInt(60));
510
				this.chosenDegrees = this.cubeMob.getRandom().nextInt(360);
511
			}
512
513
			if (this.cubeMob.getMoveControl() instanceof AbstractCubeMob.CubeMobMoveControl cubeMobMoveControl) {
514
				cubeMobMoveControl.setDirection(this.chosenDegrees, false);
515
			}
516
		}
517
	}
518
}
519

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