Vex .java

net.minecraft.world.entity.monster.Vex (class): members: VexCopyOwnerTargetGoal, VexChargeAttackGoal, VexRandomMoveGoal, FLAG_IS_CHARGING, VexMoveControl.

net.minecraft.world.entity.monsterclass364 linescommonextends Monster11.9 KB

Implements

API Surface (31)

boolean isFlapping()
boolean isAffectedByBlocks()
void tick()
void registerGoals()
AttributeSupplier.Builder createAttributes()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void readAdditionalSaveData(final ValueInput input)
void restoreFrom(final Entity oldEntity)
void addAdditionalSaveData(final ValueOutput output)
void setBoundOrigin(final @Nullable BlockPos boundOrigin)
boolean isCharging()
void setIsCharging(final boolean value)
void setOwner(final Mob owner)
void setLimitedLife(final int lifeTicks)
SoundEvent getAmbientSound()
SoundEvent getDeathSound()
SoundEvent getHurtSound(final DamageSource source)
float getLightLevelDependentMagicValue()
void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty)
boolean canUse()
boolean canContinueToUse()
void start()
void stop()
boolean requiresUpdateEveryTick()
void tick()
boolean canUse()
void start()
void tick()
boolean canUse()
boolean canContinueToUse()
void tick()

Fields & Constants (9)

FLAP_DEGREES_PER_TICK
TICKS_PER_FLAP
DATA_FLAGS_ID
FLAG_IS_CHARGING
owner
boundOrigin
hasLimitedLife
limitedLifeTicks
spawnReason

String literals (3)

"bound_pos"
"life_ticks"
"owner"

Full Source

Vex.java364 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster;
2
3
import java.util.EnumSet;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.network.syncher.EntityDataAccessor;
6
import net.minecraft.network.syncher.EntityDataSerializers;
7
import net.minecraft.network.syncher.SynchedEntityData;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.sounds.SoundEvents;
10
import net.minecraft.util.Mth;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.world.DifficultyInstance;
13
import net.minecraft.world.damagesource.DamageSource;
14
import net.minecraft.world.entity.Entity;
15
import net.minecraft.world.entity.EntityReference;
16
import net.minecraft.world.entity.EntitySpawnReason;
17
import net.minecraft.world.entity.EntityType;
18
import net.minecraft.world.entity.EquipmentSlot;
19
import net.minecraft.world.entity.LivingEntity;
20
import net.minecraft.world.entity.Mob;
21
import net.minecraft.world.entity.OwnableEntity;
22
import net.minecraft.world.entity.PathfinderMob;
23
import net.minecraft.world.entity.SpawnGroupData;
24
import net.minecraft.world.entity.Targeting;
25
import net.minecraft.world.entity.TraceableEntity;
26
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
27
import net.minecraft.world.entity.ai.attributes.Attributes;
28
import net.minecraft.world.entity.ai.control.MoveControl;
29
import net.minecraft.world.entity.ai.goal.FloatGoal;
30
import net.minecraft.world.entity.ai.goal.Goal;
31
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
32
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
33
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
34
import net.minecraft.world.entity.ai.goal.target.TargetGoal;
35
import net.minecraft.world.entity.ai.targeting.TargetingConditions;
36
import net.minecraft.world.entity.player.Player;
37
import net.minecraft.world.entity.raid.Raider;
38
import net.minecraft.world.item.ItemStack;
39
import net.minecraft.world.item.Items;
40
import net.minecraft.world.level.Level;
41
import net.minecraft.world.level.ServerLevelAccessor;
42
import net.minecraft.world.level.storage.ValueInput;
43
import net.minecraft.world.level.storage.ValueOutput;
44
import net.minecraft.world.phys.Vec3;
45
import org.jspecify.annotations.Nullable;
46
47
public class Vex extends Monster implements TraceableEntity, OwnableEntity {
48
	public static final float FLAP_DEGREES_PER_TICK = 45.836624F;
49
	public static final int TICKS_PER_FLAP = Mth.ceil((float) (Math.PI * 5.0 / 4.0));
50
	protected static final EntityDataAccessor<Byte> DATA_FLAGS_ID = SynchedEntityData.defineId(Vex.class, EntityDataSerializers.BYTE);
51
	private static final int FLAG_IS_CHARGING = 1;
52
	private @Nullable EntityReference<LivingEntity> owner;
53
	private @Nullable BlockPos boundOrigin;
54
	private boolean hasLimitedLife;
55
	private int limitedLifeTicks;
56
57
	public Vex(final EntityType<? extends Vex> type, final Level level) {
58
		super(type, level);
59
		this.moveControl = new Vex.VexMoveControl<>(this);
60
		this.xpReward = 3;
61
	}
62
63
	@Override
64
	public boolean isFlapping() {
65
		return this.tickCount % TICKS_PER_FLAP == 0;
66
	}
67
68
	@Override
69
	protected boolean isAffectedByBlocks() {
70
		return !this.isRemoved();
71
	}
72
73
	@Override
74
	public void tick() {
75
		this.noPhysics = true;
76
		super.tick();
77
		this.noPhysics = false;
78
		this.setNoGravity(true);
79
		if (this.hasLimitedLife && --this.limitedLifeTicks <= 0) {
80
			this.limitedLifeTicks = 20;
81
			this.hurt(this.damageSources().starve(), 1.0F);
82
		}
83
	}
84
85
	@Override
86
	protected void registerGoals() {
87
		super.registerGoals();
88
		this.goalSelector.addGoal(0, new FloatGoal(this));
89
		this.goalSelector.addGoal(4, new Vex.VexChargeAttackGoal());
90
		this.goalSelector.addGoal(8, new Vex.VexRandomMoveGoal());
91
		this.goalSelector.addGoal(9, new LookAtPlayerGoal(this, Player.class, 3.0F, 1.0F));
92
		this.goalSelector.addGoal(10, new LookAtPlayerGoal(this, Mob.class, 8.0F));
93
		this.targetSelector.addGoal(1, new HurtByTargetGoal(this, Raider.class).setAlertOthers());
94
		this.targetSelector.addGoal(2, new Vex.VexCopyOwnerTargetGoal(this));
95
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Player.class, true));
96
	}
97
98
	public static AttributeSupplier.Builder createAttributes() {
99
		return Monster.createMonsterAttributes().add(Attributes.MAX_HEALTH, 14.0).add(Attributes.ATTACK_DAMAGE, 4.0);
100
	}
101
102
	@Override
103
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
104
		super.defineSynchedData(entityData);
105
		entityData.define(DATA_FLAGS_ID, (byte)0);
106
	}
107
108
	@Override
109
	protected void readAdditionalSaveData(final ValueInput input) {
110
		super.readAdditionalSaveData(input);
111
		this.boundOrigin = input.read("bound_pos", BlockPos.CODEC).orElse(null);
112
		input.getInt("life_ticks").ifPresentOrElse(this::setLimitedLife, () -> this.hasLimitedLife = false);
113
		this.owner = EntityReference.read(input, "owner");
114
	}
115
116
	@Override
117
	public void restoreFrom(final Entity oldEntity) {
118
		super.restoreFrom(oldEntity);
119
		if (oldEntity instanceof Vex vex) {
120
			this.owner = vex.owner;
121
		}
122
	}
123
124
	@Override
125
	protected void addAdditionalSaveData(final ValueOutput output) {
126
		super.addAdditionalSaveData(output);
127
		output.storeNullable("bound_pos", BlockPos.CODEC, this.boundOrigin);
128
		if (this.hasLimitedLife) {
129
			output.putInt("life_ticks", this.limitedLifeTicks);
130
		}
131
132
		EntityReference.store(this.owner, output, "owner");
133
	}
134
135
	@Override
136
	public @Nullable LivingEntity getOwner() {
137
		return OwnableEntity.super.getOwner();
138
	}
139
140
	@Override
141
	public @Nullable EntityReference<LivingEntity> getOwnerReference() {
142
		return this.owner;
143
	}
144
145
	public @Nullable BlockPos getBoundOrigin() {
146
		return this.boundOrigin;
147
	}
148
149
	public void setBoundOrigin(final @Nullable BlockPos boundOrigin) {
150
		this.boundOrigin = boundOrigin;
151
	}
152
153
	private boolean getVexFlag(final int flag) {
154
		int flags = this.entityData.get(DATA_FLAGS_ID);
155
		return (flags & flag) != 0;
156
	}
157
158
	private void setVexFlag(final int flag, final boolean value) {
159
		int flags = this.entityData.get(DATA_FLAGS_ID);
160
		if (value) {
161
			flags |= flag;
162
		} else {
163
			flags &= ~flag;
164
		}
165
166
		this.entityData.set(DATA_FLAGS_ID, (byte)(flags & 0xFF));
167
	}
168
169
	public boolean isCharging() {
170
		return this.getVexFlag(1);
171
	}
172
173
	public void setIsCharging(final boolean value) {
174
		this.setVexFlag(1, value);
175
	}
176
177
	public void setOwner(final Mob owner) {
178
		this.owner = EntityReference.of(owner);
179
	}
180
181
	public void setLimitedLife(final int lifeTicks) {
182
		this.hasLimitedLife = true;
183
		this.limitedLifeTicks = lifeTicks;
184
	}
185
186
	@Override
187
	protected SoundEvent getAmbientSound() {
188
		return SoundEvents.VEX_AMBIENT;
189
	}
190
191
	@Override
192
	protected SoundEvent getDeathSound() {
193
		return SoundEvents.VEX_DEATH;
194
	}
195
196
	@Override
197
	protected SoundEvent getHurtSound(final DamageSource source) {
198
		return SoundEvents.VEX_HURT;
199
	}
200
201
	@Override
202
	public float getLightLevelDependentMagicValue() {
203
		return 1.0F;
204
	}
205
206
	@Override
207
	public @Nullable SpawnGroupData finalizeSpawn(
208
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
209
	) {
210
		RandomSource random = level.getRandom();
211
		this.populateDefaultEquipmentSlots(random, difficulty);
212
		this.populateDefaultEquipmentEnchantments(level, random, difficulty);
213
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
214
	}
215
216
	@Override
217
	protected void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty) {
218
		this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.IRON_SWORD));
219
		this.setDropChance(EquipmentSlot.MAINHAND, 0.0F);
220
	}
221
222
	private class VexChargeAttackGoal extends Goal {
223
		public VexChargeAttackGoal() {
224
			this.setFlags(EnumSet.of(Goal.Flag.MOVE));
225
		}
226
227
		@Override
228
		public boolean canUse() {
229
			LivingEntity target = Vex.this.getTarget();
230
			return target != null && target.isAlive() && !Vex.this.getMoveControl().hasWanted() && Vex.this.random.nextInt(reducedTickDelay(7)) == 0
231
				? Vex.this.distanceToSqr(target) > 4.0
232
				: false;
233
		}
234
235
		@Override
236
		public boolean canContinueToUse() {
237
			return Vex.this.getMoveControl().hasWanted() && Vex.this.isCharging() && Vex.this.getTarget() != null && Vex.this.getTarget().isAlive();
238
		}
239
240
		@Override
241
		public void start() {
242
			LivingEntity attackTarget = Vex.this.getTarget();
243
			if (attackTarget != null) {
244
				Vec3 eyePosition = attackTarget.getEyePosition();
245
				Vex.this.moveControl.setWantedPosition(eyePosition.x, eyePosition.y, eyePosition.z, 1.0);
246
			}
247
248
			Vex.this.setIsCharging(true);
249
			Vex.this.playSound(SoundEvents.VEX_CHARGE, 1.0F, 1.0F);
250
		}
251
252
		@Override
253
		public void stop() {
254
			Vex.this.setIsCharging(false);
255
		}
256
257
		@Override
258
		public boolean requiresUpdateEveryTick() {
259
			return true;
260
		}
261
262
		@Override
263
		public void tick() {
264
			LivingEntity attackTarget = Vex.this.getTarget();
265
			if (attackTarget != null) {
266
				if (Vex.this.getBoundingBox().intersects(attackTarget.getBoundingBox())) {
267
					Vex.this.doHurtTarget(getServerLevel(Vex.this.level()), attackTarget);
268
					Vex.this.setIsCharging(false);
269
				} else {
270
					double distance = Vex.this.distanceToSqr(attackTarget);
271
					if (distance < 9.0) {
272
						Vec3 eyePosition = attackTarget.getEyePosition();
273
						Vex.this.moveControl.setWantedPosition(eyePosition.x, eyePosition.y, eyePosition.z, 1.0);
274
					}
275
				}
276
			}
277
		}
278
	}
279
280
	private class VexCopyOwnerTargetGoal extends TargetGoal {
281
		private final TargetingConditions copyOwnerTargeting = TargetingConditions.forNonCombat().ignoreLineOfSight().ignoreInvisibilityTesting();
282
283
		public VexCopyOwnerTargetGoal(final PathfinderMob mob) {
284
			super(mob, false);
285
		}
286
287
		@Override
288
		public boolean canUse() {
289
			return Vex.this.getOwner() instanceof Targeting owner && owner.getTarget() != null && this.canAttack(owner.getTarget(), this.copyOwnerTargeting);
290
		}
291
292
		@Override
293
		public void start() {
294
			Vex.this.setTarget(Vex.this.getOwner() instanceof Targeting owner ? owner.getTarget() : null);
295
			super.start();
296
		}
297
	}
298
299
	private class VexMoveControl<T extends Mob> extends MoveControl<T> {
300
		public VexMoveControl(final T vex) {
301
			super(vex);
302
		}
303
304
		@Override
305
		public void tick() {
306
			if (this.operation == MoveControl.Operation.MOVE_TO) {
307
				Vec3 delta = new Vec3(this.wantedX - Vex.this.getX(), this.wantedY - Vex.this.getY(), this.wantedZ - Vex.this.getZ());
308
				double deltaLength = delta.length();
309
				if (deltaLength < Vex.this.getBoundingBox().getSize()) {
310
					this.operation = MoveControl.Operation.WAIT;
311
					Vex.this.setDeltaMovement(Vex.this.getDeltaMovement().scale(0.5));
312
				} else {
313
					Vex.this.setDeltaMovement(Vex.this.getDeltaMovement().add(delta.scale(this.speedModifier * 0.05 / deltaLength)));
314
					if (Vex.this.getTarget() == null) {
315
						Vec3 movement = Vex.this.getDeltaMovement();
316
						Vex.this.setYRot(-((float)Mth.atan2(movement.x, movement.z)) * (180.0F / (float)Math.PI));
317
						Vex.this.yBodyRot = Vex.this.getYRot();
318
					} else {
319
						double tx = Vex.this.getTarget().getX() - Vex.this.getX();
320
						double tz = Vex.this.getTarget().getZ() - Vex.this.getZ();
321
						Vex.this.setYRot(-((float)Mth.atan2(tx, tz)) * (180.0F / (float)Math.PI));
322
						Vex.this.yBodyRot = Vex.this.getYRot();
323
					}
324
				}
325
			}
326
		}
327
	}
328
329
	private class VexRandomMoveGoal extends Goal {
330
		public VexRandomMoveGoal() {
331
			this.setFlags(EnumSet.of(Goal.Flag.MOVE));
332
		}
333
334
		@Override
335
		public boolean canUse() {
336
			return !Vex.this.getMoveControl().hasWanted() && Vex.this.random.nextInt(reducedTickDelay(7)) == 0;
337
		}
338
339
		@Override
340
		public boolean canContinueToUse() {
341
			return false;
342
		}
343
344
		@Override
345
		public void tick() {
346
			BlockPos boundOrigin = Vex.this.getBoundOrigin();
347
			if (boundOrigin == null) {
348
				boundOrigin = Vex.this.blockPosition();
349
			}
350
351
			for (int attempts = 0; attempts < 3; attempts++) {
352
				BlockPos testPos = boundOrigin.offset(Vex.this.random.nextInt(15) - 7, Vex.this.random.nextInt(11) - 5, Vex.this.random.nextInt(15) - 7);
353
				if (Vex.this.level().isEmptyBlock(testPos)) {
354
					Vex.this.moveControl.setWantedPosition(testPos.getX() + 0.5, testPos.getY() + 0.5, testPos.getZ() + 0.5, 0.25);
355
					if (Vex.this.getTarget() == null) {
356
						Vex.this.getLookControl().setLookAt(testPos.getX() + 0.5, testPos.getY() + 0.5, testPos.getZ() + 0.5, 180.0F, 20.0F);
357
					}
358
					break;
359
				}
360
			}
361
		}
362
	}
363
}
364

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