FireworkRocketEntity .java

net.minecraft.world.entity.projectile.FireworkRocketEntity (class): members: DATA_ATTACHED_TO_TARGET, DATA_ID_FIREWORKS_ITEM, DEFAULT_SHOT_AT_ANGLE, life.

net.minecraft.world.entity.projectileclass322 linescommonextends Projectile11.7 KB

Implements

API Surface (13)

void defineSynchedData(final SynchedEntityData.Builder entityData)
boolean shouldRenderAtSqrDistance(final double distance)
boolean shouldRender(final double camX, final double camY, final double camZ)
void tick()
void onHitEntity(final EntityHitResult hitResult)
void onHitBlock(final BlockHitResult hitResult)
boolean isShotAtAngle()
void handleEntityEvent(final @EntityEvent.Value byte id)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
ItemStack getItem()
boolean isAttackable()
DoubleDoubleImmutablePair calculateHorizontalHurtKnockbackDirection(final LivingEntity hurtEntity, final DamageSource damageSource)

Fields & Constants (10)

DATA_ID_FIREWORKS_ITEM
DATA_ATTACHED_TO_TARGET
DATA_SHOT_AT_ANGLE
DEFAULT_LIFE
DEFAULT_LIFE_TIME
DEFAULT_SHOT_AT_ANGLE
life
lifetime
attachedToEntity
z

String literals (4)

"Life"
"LifeTime"
"FireworksItem"
"ShotAtAngle"

Full Source

FireworkRocketEntity.java322 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.projectile;
2
3
import it.unimi.dsi.fastutil.doubles.DoubleDoubleImmutablePair;
4
import java.util.List;
5
import java.util.OptionalInt;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.component.DataComponents;
8
import net.minecraft.core.particles.ParticleTypes;
9
import net.minecraft.network.syncher.EntityDataAccessor;
10
import net.minecraft.network.syncher.EntityDataSerializers;
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.world.damagesource.DamageSource;
16
import net.minecraft.world.entity.Entity;
17
import net.minecraft.world.entity.EntityEvent;
18
import net.minecraft.world.entity.EntityType;
19
import net.minecraft.world.entity.EntityTypes;
20
import net.minecraft.world.entity.InsideBlockEffectApplier;
21
import net.minecraft.world.entity.LivingEntity;
22
import net.minecraft.world.entity.MoverType;
23
import net.minecraft.world.item.ItemStack;
24
import net.minecraft.world.item.Items;
25
import net.minecraft.world.item.component.FireworkExplosion;
26
import net.minecraft.world.item.component.Fireworks;
27
import net.minecraft.world.level.ClipContext;
28
import net.minecraft.world.level.Level;
29
import net.minecraft.world.level.gameevent.GameEvent;
30
import net.minecraft.world.level.storage.ValueInput;
31
import net.minecraft.world.level.storage.ValueOutput;
32
import net.minecraft.world.phys.BlockHitResult;
33
import net.minecraft.world.phys.EntityHitResult;
34
import net.minecraft.world.phys.HitResult;
35
import net.minecraft.world.phys.Vec3;
36
import org.jspecify.annotations.Nullable;
37
38
public class FireworkRocketEntity extends Projectile implements ItemSupplier {
39
	private static final EntityDataAccessor<ItemStack> DATA_ID_FIREWORKS_ITEM = SynchedEntityData.defineId(
40
		FireworkRocketEntity.class, EntityDataSerializers.ITEM_STACK
41
	);
42
	private static final EntityDataAccessor<OptionalInt> DATA_ATTACHED_TO_TARGET = SynchedEntityData.defineId(
43
		FireworkRocketEntity.class, EntityDataSerializers.OPTIONAL_UNSIGNED_INT
44
	);
45
	private static final EntityDataAccessor<Boolean> DATA_SHOT_AT_ANGLE = SynchedEntityData.defineId(FireworkRocketEntity.class, EntityDataSerializers.BOOLEAN);
46
	private static final int DEFAULT_LIFE = 0;
47
	private static final int DEFAULT_LIFE_TIME = 0;
48
	private static final boolean DEFAULT_SHOT_AT_ANGLE = false;
49
	private int life = 0;
50
	private int lifetime = 0;
51
	private @Nullable LivingEntity attachedToEntity;
52
53
	public FireworkRocketEntity(final EntityType<? extends FireworkRocketEntity> type, final Level level) {
54
		super(type, level);
55
	}
56
57
	public FireworkRocketEntity(final Level level, final double x, final double y, final double z, final ItemStack sourceItemStack) {
58
		super(EntityTypes.FIREWORK_ROCKET, level);
59
		this.life = 0;
60
		this.setPos(x, y, z);
61
		this.entityData.set(DATA_ID_FIREWORKS_ITEM, sourceItemStack.copy());
62
		int flightCount = 1;
63
		Fireworks fireworks = sourceItemStack.get(DataComponents.FIREWORKS);
64
		if (fireworks != null) {
65
			flightCount += fireworks.flightDuration();
66
		}
67
68
		this.setDeltaMovement(this.random.triangle(0.0, 0.002297), 0.05, this.random.triangle(0.0, 0.002297));
69
		this.lifetime = 10 * flightCount + this.random.nextInt(6) + this.random.nextInt(7);
70
	}
71
72
	public FireworkRocketEntity(final Level level, final @Nullable Entity owner, final double x, final double y, final double z, final ItemStack sourceItemStack) {
73
		this(level, x, y, z, sourceItemStack);
74
		this.setOwner(owner);
75
	}
76
77
	public FireworkRocketEntity(final Level level, final ItemStack sourceItemStack, final LivingEntity stuckTo) {
78
		this(level, stuckTo, stuckTo.getX(), stuckTo.getY(), stuckTo.getZ(), sourceItemStack);
79
		this.entityData.set(DATA_ATTACHED_TO_TARGET, OptionalInt.of(stuckTo.getId()));
80
		this.attachedToEntity = stuckTo;
81
	}
82
83
	public FireworkRocketEntity(final Level level, final ItemStack sourceItemStack, final double x, final double y, final double z, final boolean shotAtAngle) {
84
		this(level, x, y, z, sourceItemStack);
85
		this.entityData.set(DATA_SHOT_AT_ANGLE, shotAtAngle);
86
	}
87
88
	public FireworkRocketEntity(
89
		final Level level, final ItemStack sourceItemStack, final Entity owner, final double x, final double y, final double z, final boolean shotAtAngle
90
	) {
91
		this(level, sourceItemStack, x, y, z, shotAtAngle);
92
		this.setOwner(owner);
93
	}
94
95
	@Override
96
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
97
		entityData.define(DATA_ID_FIREWORKS_ITEM, getDefaultItem());
98
		entityData.define(DATA_ATTACHED_TO_TARGET, OptionalInt.empty());
99
		entityData.define(DATA_SHOT_AT_ANGLE, false);
100
	}
101
102
	@Override
103
	public boolean shouldRenderAtSqrDistance(final double distance) {
104
		return distance < 4096.0 && !this.isAttachedToEntity();
105
	}
106
107
	@Override
108
	public boolean shouldRender(final double camX, final double camY, final double camZ) {
109
		return super.shouldRender(camX, camY, camZ) && !this.isAttachedToEntity();
110
	}
111
112
	@Override
113
	public void tick() {
114
		super.tick();
115
		HitResult hitResult;
116
		if (this.isAttachedToEntity()) {
117
			if (this.attachedToEntity == null) {
118
				this.entityData.get(DATA_ATTACHED_TO_TARGET).ifPresent(id -> {
119
					if (this.level().getEntity(id) instanceof LivingEntity livingEntity) {
120
						this.attachedToEntity = livingEntity;
121
					}
122
				});
123
			}
124
125
			if (this.attachedToEntity != null) {
126
				Vec3 handAngle;
127
				if (this.attachedToEntity.isFallFlying()) {
128
					Vec3 lookAngle = this.attachedToEntity.getLookAngle();
129
					double power = 1.5;
130
					double powerAdd = 0.1;
131
					Vec3 movement = this.attachedToEntity.getDeltaMovement();
132
					this.attachedToEntity
133
						.setDeltaMovement(
134
							movement.add(
135
								lookAngle.x * 0.1 + (lookAngle.x * 1.5 - movement.x) * 0.5,
136
								lookAngle.y * 0.1 + (lookAngle.y * 1.5 - movement.y) * 0.5,
137
								lookAngle.z * 0.1 + (lookAngle.z * 1.5 - movement.z) * 0.5
138
							)
139
						);
140
					handAngle = this.attachedToEntity.getHandHoldingItemAngle(Items.FIREWORK_ROCKET);
141
				} else {
142
					handAngle = Vec3.ZERO;
143
				}
144
145
				this.setPos(this.attachedToEntity.getX() + handAngle.x, this.attachedToEntity.getY() + handAngle.y, this.attachedToEntity.getZ() + handAngle.z);
146
				this.setDeltaMovement(this.attachedToEntity.getDeltaMovement());
147
			}
148
149
			hitResult = ProjectileUtil.getHitResultOnMoveVector(this, this::canHitEntity);
150
		} else {
151
			if (!this.isShotAtAngle()) {
152
				double horizontalAcceleration = this.horizontalCollision ? 1.0 : 1.15;
153
				this.setDeltaMovement(this.getDeltaMovement().multiply(horizontalAcceleration, 1.0, horizontalAcceleration).add(0.0, 0.04, 0.0));
154
			}
155
156
			Vec3 movement = this.getDeltaMovement();
157
			hitResult = ProjectileUtil.getHitResultOnMoveVector(this, this::canHitEntity);
158
			this.move(MoverType.SELF, movement);
159
			this.applyEffectsFromBlocks();
160
			this.setDeltaMovement(movement);
161
		}
162
163
		if (!this.noPhysics && this.isAlive() && hitResult.getType() != HitResult.Type.MISS) {
164
			this.hitTargetOrDeflectSelf(hitResult);
165
			this.needsSync = true;
166
		}
167
168
		this.updateRotation();
169
		if (this.life == 0 && !this.isSilent()) {
170
			this.level().playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.FIREWORK_ROCKET_LAUNCH, SoundSource.AMBIENT, 3.0F, 1.0F);
171
		}
172
173
		this.life++;
174
		if (this.level().isClientSide() && this.life % 2 < 2) {
175
			this.level()
176
				.addParticle(
177
					ParticleTypes.FIREWORK,
178
					this.getX(),
179
					this.getY(),
180
					this.getZ(),
181
					this.random.nextGaussian() * 0.05,
182
					-this.getDeltaMovement().y * 0.5,
183
					this.random.nextGaussian() * 0.05
184
				);
185
		}
186
187
		if (this.life > this.lifetime && this.level() instanceof ServerLevel level) {
188
			this.explode(level);
189
		}
190
	}
191
192
	private void explode(final ServerLevel level) {
193
		level.broadcastEntityEvent(this, (byte)17);
194
		this.gameEvent(GameEvent.EXPLODE, this.getOwner());
195
		this.dealExplosionDamage(level);
196
		this.discard();
197
	}
198
199
	@Override
200
	protected void onHitEntity(final EntityHitResult hitResult) {
201
		super.onHitEntity(hitResult);
202
		if (this.level() instanceof ServerLevel level) {
203
			this.explode(level);
204
		}
205
	}
206
207
	@Override
208
	protected void onHitBlock(final BlockHitResult hitResult) {
209
		BlockPos pos = hitResult.getBlockPos().immutable();
210
		this.level().getBlockState(pos).entityInside(this.level(), pos, this, InsideBlockEffectApplier.NOOP, true);
211
		if (this.level() instanceof ServerLevel level && this.hasExplosion()) {
212
			this.explode(level);
213
		}
214
215
		super.onHitBlock(hitResult);
216
	}
217
218
	private boolean hasExplosion() {
219
		return !this.getExplosions().isEmpty();
220
	}
221
222
	private void dealExplosionDamage(final ServerLevel level) {
223
		float damageAmount = 0.0F;
224
		List<FireworkExplosion> explosions = this.getExplosions();
225
		if (!explosions.isEmpty()) {
226
			damageAmount = 5.0F + explosions.size() * 2;
227
		}
228
229
		if (damageAmount > 0.0F) {
230
			if (this.attachedToEntity != null) {
231
				this.attachedToEntity.hurtServer(level, this.damageSources().fireworks(this, this.getOwner()), 5.0F + explosions.size() * 2);
232
			}
233
234
			double radius = 5.0;
235
			Vec3 rocketPos = this.position();
236
237
			for (LivingEntity target : this.level().getEntitiesOfClass(LivingEntity.class, this.getBoundingBox().inflate(5.0))) {
238
				if (target != this.attachedToEntity && !(this.distanceToSqr(target) > 25.0)) {
239
					boolean canSee = false;
240
241
					for (int testStep = 0; testStep < 2; testStep++) {
242
						Vec3 to = new Vec3(target.getX(), target.getY(0.5 * testStep), target.getZ());
243
						HitResult clip = this.level().clip(new ClipContext(rocketPos, to, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this));
244
						if (clip.getType() == HitResult.Type.MISS) {
245
							canSee = true;
246
							break;
247
						}
248
					}
249
250
					if (canSee) {
251
						float damage = damageAmount * (float)Math.sqrt((5.0 - this.distanceTo(target)) / 5.0);
252
						target.hurtServer(level, this.damageSources().fireworks(this, this.getOwner()), damage);
253
					}
254
				}
255
			}
256
		}
257
	}
258
259
	private boolean isAttachedToEntity() {
260
		return this.entityData.get(DATA_ATTACHED_TO_TARGET).isPresent();
261
	}
262
263
	public boolean isShotAtAngle() {
264
		return this.entityData.get(DATA_SHOT_AT_ANGLE);
265
	}
266
267
	@Override
268
	public void handleEntityEvent(final @EntityEvent.Value byte id) {
269
		if (id == 17 && this.level().isClientSide()) {
270
			Vec3 movement = this.getDeltaMovement();
271
			this.level().createFireworks(this.getX(), this.getY(), this.getZ(), movement.x, movement.y, movement.z, this.getExplosions(), !this.isSilent());
272
		}
273
274
		super.handleEntityEvent(id);
275
	}
276
277
	@Override
278
	protected void addAdditionalSaveData(final ValueOutput output) {
279
		super.addAdditionalSaveData(output);
280
		output.putInt("Life", this.life);
281
		output.putInt("LifeTime", this.lifetime);
282
		output.store("FireworksItem", ItemStack.CODEC, this.getItem());
283
		output.putBoolean("ShotAtAngle", this.entityData.get(DATA_SHOT_AT_ANGLE));
284
	}
285
286
	@Override
287
	protected void readAdditionalSaveData(final ValueInput input) {
288
		super.readAdditionalSaveData(input);
289
		this.life = input.getIntOr("Life", 0);
290
		this.lifetime = input.getIntOr("LifeTime", 0);
291
		this.entityData.set(DATA_ID_FIREWORKS_ITEM, input.read("FireworksItem", ItemStack.CODEC).orElse(getDefaultItem()));
292
		this.entityData.set(DATA_SHOT_AT_ANGLE, input.getBooleanOr("ShotAtAngle", false));
293
	}
294
295
	private List<FireworkExplosion> getExplosions() {
296
		ItemStack sourceItemStack = this.entityData.get(DATA_ID_FIREWORKS_ITEM);
297
		Fireworks fireworks = sourceItemStack.get(DataComponents.FIREWORKS);
298
		return fireworks != null ? fireworks.explosions() : List.of();
299
	}
300
301
	@Override
302
	public ItemStack getItem() {
303
		return this.entityData.get(DATA_ID_FIREWORKS_ITEM);
304
	}
305
306
	@Override
307
	public boolean isAttackable() {
308
		return false;
309
	}
310
311
	private static ItemStack getDefaultItem() {
312
		return new ItemStack(Items.FIREWORK_ROCKET);
313
	}
314
315
	@Override
316
	public DoubleDoubleImmutablePair calculateHorizontalHurtKnockbackDirection(final LivingEntity hurtEntity, final DamageSource damageSource) {
317
		double dx = hurtEntity.position().x - this.position().x;
318
		double dz = hurtEntity.position().z - this.position().z;
319
		return DoubleDoubleImmutablePair.of(dx, dz);
320
	}
321
}
322

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