ShulkerBullet .java

net.minecraft.world.entity.projectile.ShulkerBullet (class): members: currentMoveDirection, targetDeltaX, targetDeltaY, targetDeltaZ, finalTarget, SPEED.

net.minecraft.world.entity.projectileclass342 linescommonextends Projectile11.6 KB

API Surface (20)

SoundSource getSoundSource()
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void defineSynchedData(final SynchedEntityData.Builder entityData)
void checkDespawn()
double getDefaultGravity()
void tick()
boolean isAffectedByBlocks()
boolean canHitEntity(final Entity entity)
boolean isOnFire()
boolean shouldRenderAtSqrDistance(final double distance)
float getLightLevelDependentMagicValue()
void onHitEntity(final EntityHitResult hitResult)
void onHitBlock(final BlockHitResult hitResult)
void onRedirectProjectile(final Projectile hitProjectile)
void onHit(final HitResult hitResult)
boolean isPickable()
boolean hurtClient(final DamageSource source)
boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage)
void recreateFromPacket(final ClientboundAddEntityPacket packet)

Fields & Constants (7)

SPEED
finalTarget
currentMoveDirection
flightSteps
targetDeltaX
targetDeltaY
targetDeltaZ

String literals (2)

"Target"
"Steps"

Full Source

ShulkerBullet.java342 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.projectile;
2
3
import com.google.common.base.MoreObjects;
4
import com.google.common.collect.Lists;
5
import java.util.List;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.core.UUIDUtil;
9
import net.minecraft.core.particles.ParticleTypes;
10
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
11
import net.minecraft.network.syncher.SynchedEntityData;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.sounds.SoundEvents;
14
import net.minecraft.sounds.SoundSource;
15
import net.minecraft.util.Mth;
16
import net.minecraft.world.Difficulty;
17
import net.minecraft.world.damagesource.DamageSource;
18
import net.minecraft.world.effect.MobEffectInstance;
19
import net.minecraft.world.effect.MobEffects;
20
import net.minecraft.world.entity.Entity;
21
import net.minecraft.world.entity.EntityReference;
22
import net.minecraft.world.entity.EntityType;
23
import net.minecraft.world.entity.EntityTypes;
24
import net.minecraft.world.entity.LivingEntity;
25
import net.minecraft.world.entity.player.Player;
26
import net.minecraft.world.item.enchantment.EnchantmentHelper;
27
import net.minecraft.world.level.Level;
28
import net.minecraft.world.level.gameevent.GameEvent;
29
import net.minecraft.world.level.storage.ValueInput;
30
import net.minecraft.world.level.storage.ValueOutput;
31
import net.minecraft.world.phys.BlockHitResult;
32
import net.minecraft.world.phys.EntityHitResult;
33
import net.minecraft.world.phys.HitResult;
34
import net.minecraft.world.phys.Vec3;
35
import org.jspecify.annotations.Nullable;
36
37
public class ShulkerBullet extends Projectile {
38
	private static final double SPEED = 0.15;
39
	private @Nullable EntityReference<Entity> finalTarget;
40
	private @Nullable Direction currentMoveDirection;
41
	private int flightSteps;
42
	private double targetDeltaX;
43
	private double targetDeltaY;
44
	private double targetDeltaZ;
45
46
	public ShulkerBullet(final EntityType<? extends ShulkerBullet> type, final Level level) {
47
		super(type, level);
48
		this.noPhysics = true;
49
	}
50
51
	public ShulkerBullet(final Level level, final LivingEntity owner, final Entity target, final Direction.Axis invalidStartAxis) {
52
		this(EntityTypes.SHULKER_BULLET, level);
53
		this.setOwner(owner);
54
		Vec3 position = owner.getBoundingBox().getCenter();
55
		this.snapTo(position.x, position.y, position.z, this.getYRot(), this.getXRot());
56
		this.finalTarget = EntityReference.of(target);
57
		this.currentMoveDirection = Direction.UP;
58
		this.selectNextMoveDirection(invalidStartAxis, target);
59
	}
60
61
	@Override
62
	public SoundSource getSoundSource() {
63
		return SoundSource.HOSTILE;
64
	}
65
66
	@Override
67
	protected void addAdditionalSaveData(final ValueOutput output) {
68
		super.addAdditionalSaveData(output);
69
		if (this.finalTarget != null) {
70
			output.store("Target", UUIDUtil.CODEC, this.finalTarget.getUUID());
71
		}
72
73
		output.storeNullable("Dir", Direction.LEGACY_ID_CODEC, this.currentMoveDirection);
74
		output.putInt("Steps", this.flightSteps);
75
		output.putDouble("TXD", this.targetDeltaX);
76
		output.putDouble("TYD", this.targetDeltaY);
77
		output.putDouble("TZD", this.targetDeltaZ);
78
	}
79
80
	@Override
81
	protected void readAdditionalSaveData(final ValueInput input) {
82
		super.readAdditionalSaveData(input);
83
		this.flightSteps = input.getIntOr("Steps", 0);
84
		this.targetDeltaX = input.getDoubleOr("TXD", 0.0);
85
		this.targetDeltaY = input.getDoubleOr("TYD", 0.0);
86
		this.targetDeltaZ = input.getDoubleOr("TZD", 0.0);
87
		this.currentMoveDirection = input.read("Dir", Direction.LEGACY_ID_CODEC).orElse(null);
88
		this.finalTarget = EntityReference.read(input, "Target");
89
	}
90
91
	@Override
92
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
93
	}
94
95
	private @Nullable Direction getMoveDirection() {
96
		return this.currentMoveDirection;
97
	}
98
99
	private void setMoveDirection(final @Nullable Direction direction) {
100
		this.currentMoveDirection = direction;
101
	}
102
103
	private void selectNextMoveDirection(final Direction.@Nullable Axis avoidAxis, final @Nullable Entity target) {
104
		double yOffset = 0.5;
105
		BlockPos targetPos;
106
		if (target == null) {
107
			targetPos = this.blockPosition().below();
108
		} else {
109
			yOffset = target.getBbHeight() * 0.5;
110
			targetPos = BlockPos.containing(target.getX(), target.getY() + yOffset, target.getZ());
111
		}
112
113
		double targetX = targetPos.getX() + 0.5;
114
		double targetY = targetPos.getY() + yOffset;
115
		double targetZ = targetPos.getZ() + 0.5;
116
		Direction selection = null;
117
		if (!targetPos.closerToCenterThan(this.position(), 2.0)) {
118
			BlockPos current = this.blockPosition();
119
			List<Direction> options = Lists.newArrayList();
120
			if (avoidAxis != Direction.Axis.X) {
121
				if (current.getX() < targetPos.getX() && this.level().isEmptyBlock(current.east())) {
122
					options.add(Direction.EAST);
123
				} else if (current.getX() > targetPos.getX() && this.level().isEmptyBlock(current.west())) {
124
					options.add(Direction.WEST);
125
				}
126
			}
127
128
			if (avoidAxis != Direction.Axis.Y) {
129
				if (current.getY() < targetPos.getY() && this.level().isEmptyBlock(current.above())) {
130
					options.add(Direction.UP);
131
				} else if (current.getY() > targetPos.getY() && this.level().isEmptyBlock(current.below())) {
132
					options.add(Direction.DOWN);
133
				}
134
			}
135
136
			if (avoidAxis != Direction.Axis.Z) {
137
				if (current.getZ() < targetPos.getZ() && this.level().isEmptyBlock(current.south())) {
138
					options.add(Direction.SOUTH);
139
				} else if (current.getZ() > targetPos.getZ() && this.level().isEmptyBlock(current.north())) {
140
					options.add(Direction.NORTH);
141
				}
142
			}
143
144
			selection = Direction.getRandom(this.random);
145
			if (options.isEmpty()) {
146
				for (int attempts = 5; !this.level().isEmptyBlock(current.relative(selection)) && attempts > 0; attempts--) {
147
					selection = Direction.getRandom(this.random);
148
				}
149
			} else {
150
				selection = options.get(this.random.nextInt(options.size()));
151
			}
152
153
			targetX = this.getX() + selection.getStepX();
154
			targetY = this.getY() + selection.getStepY();
155
			targetZ = this.getZ() + selection.getStepZ();
156
		}
157
158
		this.setMoveDirection(selection);
159
		double xa = targetX - this.getX();
160
		double ya = targetY - this.getY();
161
		double za = targetZ - this.getZ();
162
		double distance = Math.sqrt(xa * xa + ya * ya + za * za);
163
		if (distance == 0.0) {
164
			this.targetDeltaX = 0.0;
165
			this.targetDeltaY = 0.0;
166
			this.targetDeltaZ = 0.0;
167
		} else {
168
			this.targetDeltaX = xa / distance * 0.15;
169
			this.targetDeltaY = ya / distance * 0.15;
170
			this.targetDeltaZ = za / distance * 0.15;
171
		}
172
173
		this.needsSync = true;
174
		this.flightSteps = 10 + this.random.nextInt(5) * 10;
175
	}
176
177
	@Override
178
	public void checkDespawn() {
179
		if (this.level().getDifficulty() == Difficulty.PEACEFUL) {
180
			this.discard();
181
		}
182
	}
183
184
	@Override
185
	protected double getDefaultGravity() {
186
		return 0.04;
187
	}
188
189
	@Override
190
	public void tick() {
191
		super.tick();
192
		Entity finalTarget = !this.level().isClientSide() ? EntityReference.getEntity(this.finalTarget, this.level()) : null;
193
		HitResult hitResult = null;
194
		if (!this.level().isClientSide()) {
195
			if (finalTarget == null) {
196
				this.finalTarget = null;
197
			}
198
199
			if (finalTarget == null || !finalTarget.isAlive() || finalTarget instanceof Player && finalTarget.isSpectator()) {
200
				this.applyGravity();
201
			} else {
202
				this.targetDeltaX = Mth.clamp(this.targetDeltaX * 1.025, -1.0, 1.0);
203
				this.targetDeltaY = Mth.clamp(this.targetDeltaY * 1.025, -1.0, 1.0);
204
				this.targetDeltaZ = Mth.clamp(this.targetDeltaZ * 1.025, -1.0, 1.0);
205
				Vec3 movement = this.getDeltaMovement();
206
				this.setDeltaMovement(movement.add((this.targetDeltaX - movement.x) * 0.2, (this.targetDeltaY - movement.y) * 0.2, (this.targetDeltaZ - movement.z) * 0.2));
207
			}
208
209
			hitResult = ProjectileUtil.getHitResultOnMoveVector(this, this::canHitEntity);
210
		}
211
212
		Vec3 movement = this.getDeltaMovement();
213
		this.setPos(this.position().add(movement));
214
		this.applyEffectsFromBlocks();
215
		if (this.portalProcess != null && this.portalProcess.isInsidePortalThisTick()) {
216
			this.handlePortal();
217
		}
218
219
		if (hitResult != null && this.isAlive() && hitResult.getType() != HitResult.Type.MISS) {
220
			this.hitTargetOrDeflectSelf(hitResult);
221
		}
222
223
		ProjectileUtil.rotateTowardsMovement(this, 0.5F);
224
		if (this.level().isClientSide()) {
225
			this.level().addParticle(ParticleTypes.END_ROD, this.getX() - movement.x, this.getY() - movement.y + 0.15, this.getZ() - movement.z, 0.0, 0.0, 0.0);
226
		} else if (finalTarget != null) {
227
			if (this.flightSteps > 0) {
228
				this.flightSteps--;
229
				if (this.flightSteps == 0) {
230
					this.selectNextMoveDirection(this.currentMoveDirection == null ? null : this.currentMoveDirection.getAxis(), finalTarget);
231
				}
232
			}
233
234
			if (this.currentMoveDirection != null) {
235
				BlockPos current = this.blockPosition();
236
				Direction.Axis axis = this.currentMoveDirection.getAxis();
237
				if (this.level().loadedAndEntityCanStandOn(current.relative(this.currentMoveDirection), this)) {
238
					this.selectNextMoveDirection(axis, finalTarget);
239
				} else {
240
					BlockPos targetPos = finalTarget.blockPosition();
241
					if (axis == Direction.Axis.X && current.getX() == targetPos.getX()
242
						|| axis == Direction.Axis.Z && current.getZ() == targetPos.getZ()
243
						|| axis == Direction.Axis.Y && current.getY() == targetPos.getY()) {
244
						this.selectNextMoveDirection(axis, finalTarget);
245
					}
246
				}
247
			}
248
		}
249
	}
250
251
	@Override
252
	protected boolean isAffectedByBlocks() {
253
		return !this.isRemoved();
254
	}
255
256
	@Override
257
	protected boolean canHitEntity(final Entity entity) {
258
		return super.canHitEntity(entity) && !entity.noPhysics;
259
	}
260
261
	@Override
262
	public boolean isOnFire() {
263
		return false;
264
	}
265
266
	@Override
267
	public boolean shouldRenderAtSqrDistance(final double distance) {
268
		return distance < 16384.0;
269
	}
270
271
	@Override
272
	public float getLightLevelDependentMagicValue() {
273
		return 1.0F;
274
	}
275
276
	@Override
277
	protected void onHitEntity(final EntityHitResult hitResult) {
278
		super.onHitEntity(hitResult);
279
		Entity target = hitResult.getEntity();
280
		Entity owner = this.getOwner();
281
		LivingEntity livingOwner = owner instanceof LivingEntity livingEntity ? livingEntity : null;
282
		DamageSource damageSource = this.damageSources().mobProjectile(this, livingOwner);
283
		boolean wasHurt = target.hurtOrSimulate(damageSource, 4.0F);
284
		if (wasHurt) {
285
			if (this.level() instanceof ServerLevel serverLevel) {
286
				EnchantmentHelper.doPostAttackEffects(serverLevel, target, damageSource);
287
			}
288
289
			if (target instanceof LivingEntity livingTarget) {
290
				livingTarget.addEffect(new MobEffectInstance(MobEffects.LEVITATION, 200), MoreObjects.firstNonNull(owner, this));
291
			}
292
		}
293
	}
294
295
	@Override
296
	protected void onHitBlock(final BlockHitResult hitResult) {
297
		super.onHitBlock(hitResult);
298
		((ServerLevel)this.level()).sendParticles(ParticleTypes.EXPLOSION, this.getX(), this.getY(), this.getZ(), 2, 0.2, 0.2, 0.2, 0.0);
299
		this.playSound(SoundEvents.SHULKER_BULLET_HIT, 1.0F, 1.0F);
300
	}
301
302
	@Override
303
	protected void onRedirectProjectile(final Projectile hitProjectile) {
304
		hitProjectile.deflect(ProjectileDeflection.MOMENTUM_DEFLECT, this, this.owner, true, 1.0);
305
	}
306
307
	private void destroy() {
308
		this.discard();
309
		this.level().gameEvent(GameEvent.ENTITY_DAMAGE, this.position(), GameEvent.Context.of(this));
310
	}
311
312
	@Override
313
	protected void onHit(final HitResult hitResult) {
314
		super.onHit(hitResult);
315
		this.destroy();
316
	}
317
318
	@Override
319
	public boolean isPickable() {
320
		return true;
321
	}
322
323
	@Override
324
	public boolean hurtClient(final DamageSource source) {
325
		return true;
326
	}
327
328
	@Override
329
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
330
		this.playSound(SoundEvents.SHULKER_BULLET_HURT, 1.0F, 1.0F);
331
		level.sendParticles(ParticleTypes.CRIT, this.getX(), this.getY(), this.getZ(), 15, 0.2, 0.2, 0.2, 0.0);
332
		this.destroy();
333
		return true;
334
	}
335
336
	@Override
337
	public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
338
		super.recreateFromPacket(packet);
339
		this.setDeltaMovement(packet.getMovement());
340
	}
341
}
342

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