ThrownEnderpearl .java

net.minecraft.world.entity.projectile.throwableitemprojectile.ThrownEnderpearl (class): members: ticketTimer, canTeleport, onInsideBlock, onRemoval, onHit.

net.minecraft.world.entity.projectile.throwableitemprojectileclass231 linescommonextends ThrowableItemProjectile7.7 KB

API Surface (10)

Item getDefaultItem()
void setOwner(final @Nullable EntityReference<Entity> owner)
void onHitEntity(final EntityHitResult hitResult)
void onHit(final HitResult hitResult)
void tick()
boolean canTeleport(final Level from, final Level to)
void onInsideBlock(final BlockState state)
void onRemoval(final Entity.RemovalReason reason)
void onAboveBubbleColumn(final boolean dragDown, final BlockPos pos)
void onInsideBubbleColumn(final boolean dragDown)

Fields & Constants (1)

ticketTimer

Full Source

ThrownEnderpearl.java231 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.projectile.throwableitemprojectile;
2
3
import java.util.UUID;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.SectionPos;
6
import net.minecraft.core.particles.ParticleTypes;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.server.level.ServerPlayer;
9
import net.minecraft.sounds.SoundEvents;
10
import net.minecraft.sounds.SoundSource;
11
import net.minecraft.world.Difficulty;
12
import net.minecraft.world.entity.Entity;
13
import net.minecraft.world.entity.EntityReference;
14
import net.minecraft.world.entity.EntitySpawnReason;
15
import net.minecraft.world.entity.EntityType;
16
import net.minecraft.world.entity.EntityTypes;
17
import net.minecraft.world.entity.LivingEntity;
18
import net.minecraft.world.entity.Relative;
19
import net.minecraft.world.entity.monster.Endermite;
20
import net.minecraft.world.item.Item;
21
import net.minecraft.world.item.ItemStack;
22
import net.minecraft.world.item.Items;
23
import net.minecraft.world.level.Level;
24
import net.minecraft.world.level.block.Blocks;
25
import net.minecraft.world.level.block.state.BlockState;
26
import net.minecraft.world.level.gamerules.GameRules;
27
import net.minecraft.world.level.portal.TeleportTransition;
28
import net.minecraft.world.phys.EntityHitResult;
29
import net.minecraft.world.phys.HitResult;
30
import net.minecraft.world.phys.Vec3;
31
import org.jspecify.annotations.Nullable;
32
33
public class ThrownEnderpearl extends ThrowableItemProjectile {
34
	private long ticketTimer = 0L;
35
36
	public ThrownEnderpearl(final EntityType<? extends ThrownEnderpearl> type, final Level level) {
37
		super(type, level);
38
	}
39
40
	public ThrownEnderpearl(final Level level, final LivingEntity mob, final ItemStack itemStack) {
41
		super(EntityTypes.ENDER_PEARL, mob, level, itemStack);
42
	}
43
44
	@Override
45
	protected Item getDefaultItem() {
46
		return Items.ENDER_PEARL;
47
	}
48
49
	@Override
50
	protected void setOwner(final @Nullable EntityReference<Entity> owner) {
51
		this.deregisterFromCurrentOwner();
52
		super.setOwner(owner);
53
		this.registerToCurrentOwner();
54
	}
55
56
	private void deregisterFromCurrentOwner() {
57
		if (this.getOwner() instanceof ServerPlayer serverPlayer) {
58
			serverPlayer.deregisterEnderPearl(this);
59
		}
60
	}
61
62
	private void registerToCurrentOwner() {
63
		if (this.getOwner() instanceof ServerPlayer serverPlayer) {
64
			serverPlayer.registerEnderPearl(this);
65
		}
66
	}
67
68
	@Override
69
	public @Nullable Entity getOwner() {
70
		return this.owner != null && this.level() instanceof ServerLevel serverLevel ? this.owner.getEntity(serverLevel, Entity.class) : super.getOwner();
71
	}
72
73
	private static @Nullable Entity findOwnerIncludingDeadPlayer(final ServerLevel serverLevel, final UUID uuid) {
74
		Entity owner = serverLevel.getEntityInAnyDimension(uuid);
75
		return owner != null ? owner : serverLevel.getServer().getPlayerList().getPlayer(uuid);
76
	}
77
78
	@Override
79
	protected void onHitEntity(final EntityHitResult hitResult) {
80
		super.onHitEntity(hitResult);
81
		hitResult.getEntity().hurt(this.damageSources().thrown(this, this.getOwner()), 0.0F);
82
	}
83
84
	@Override
85
	protected void onHit(final HitResult hitResult) {
86
		super.onHit(hitResult);
87
88
		for (int i = 0; i < 32; i++) {
89
			this.level()
90
				.addParticle(
91
					ParticleTypes.PORTAL, this.getX(), this.getY() + this.random.nextDouble() * 2.0, this.getZ(), this.random.nextGaussian(), 0.0, this.random.nextGaussian()
92
				);
93
		}
94
95
		if (this.level() instanceof ServerLevel level && !this.isRemoved()) {
96
			Entity owner = this.getOwner();
97
			if (owner != null && isAllowedToTeleportOwner(owner, level)) {
98
				Vec3 teleportPos = this.oldPosition();
99
				if (owner instanceof ServerPlayer player) {
100
					if (player.connection.isAcceptingMessages()) {
101
						if (this.random.nextFloat() < 0.05F && level.isSpawningMonsters() && level.getLevelData().getDifficulty() != Difficulty.PEACEFUL) {
102
							Endermite endermite = EntityTypes.ENDERMITE.create(level, EntitySpawnReason.TRIGGERED);
103
							if (endermite != null) {
104
								endermite.snapTo(owner.getX(), owner.getY(), owner.getZ(), owner.getYRot(), owner.getXRot());
105
								level.addFreshEntity(endermite);
106
							}
107
						}
108
109
						if (this.isOnPortalCooldown()) {
110
							owner.setPortalCooldown();
111
						}
112
113
						ServerPlayer newOwner = player.teleport(
114
							new TeleportTransition(level, teleportPos, Vec3.ZERO, 0.0F, 0.0F, Relative.ROTATION, TeleportTransition.DO_NOTHING)
115
						);
116
						if (newOwner != null) {
117
							newOwner.resetFallDistance();
118
							newOwner.resetCurrentImpulseContext();
119
							newOwner.hurtServer(player.level(), this.damageSources().enderPearl(), 5.0F);
120
						}
121
122
						this.playSound(level, teleportPos);
123
					}
124
				} else {
125
					Entity newOwner = owner.teleport(new TeleportTransition(level, teleportPos, Vec3.ZERO, owner.getYRot(), owner.getXRot(), TeleportTransition.DO_NOTHING));
126
					if (newOwner != null) {
127
						newOwner.resetFallDistance();
128
					}
129
130
					if (newOwner instanceof LivingEntity livingEntity) {
131
						livingEntity.resetCurrentImpulseContext();
132
					}
133
134
					this.playSound(level, teleportPos);
135
				}
136
137
				this.discard();
138
			} else {
139
				this.discard();
140
			}
141
		}
142
	}
143
144
	private static boolean isAllowedToTeleportOwner(final Entity owner, final Level newLevel) {
145
		if (owner.level().dimension() == newLevel.dimension()) {
146
			return !(owner instanceof LivingEntity livingOwner) ? owner.isAlive() : livingOwner.isAlive() && !livingOwner.isSleeping();
147
		} else {
148
			return owner.canUsePortal(true);
149
		}
150
	}
151
152
	@Override
153
	public void tick() {
154
		if (this.level() instanceof ServerLevel serverLevel) {
155
			int var7 = SectionPos.blockToSectionCoord(this.position().x());
156
			int previousChunkZ = SectionPos.blockToSectionCoord(this.position().z());
157
			Entity owner = this.owner != null ? findOwnerIncludingDeadPlayer(serverLevel, this.owner.getUUID()) : null;
158
			if (owner instanceof ServerPlayer serverPlayer
159
				&& !owner.isAlive()
160
				&& !serverPlayer.wonGame
161
				&& serverPlayer.level().getGameRules().get(GameRules.ENDER_PEARLS_VANISH_ON_DEATH)) {
162
				this.discard();
163
			} else {
164
				super.tick();
165
			}
166
167
			if (this.isAlive()) {
168
				BlockPos currentPos = BlockPos.containing(this.position());
169
				if ((
170
						--this.ticketTimer <= 0L
171
							|| var7 != SectionPos.blockToSectionCoord(currentPos.getX())
172
							|| previousChunkZ != SectionPos.blockToSectionCoord(currentPos.getZ())
173
					)
174
					&& owner instanceof ServerPlayer serverPlayer) {
175
					this.ticketTimer = serverPlayer.registerAndUpdateEnderPearlTicket(this);
176
				}
177
			}
178
		} else {
179
			super.tick();
180
		}
181
	}
182
183
	private void playSound(final Level level, final Vec3 position) {
184
		level.playSound(null, position.x, position.y, position.z, SoundEvents.PLAYER_TELEPORT, SoundSource.PLAYERS);
185
	}
186
187
	@Override
188
	public @Nullable Entity teleport(final TeleportTransition transition) {
189
		Entity newEntity = super.teleport(transition);
190
		if (newEntity != null) {
191
			newEntity.placePortalTicket(BlockPos.containing(newEntity.position()));
192
		}
193
194
		return newEntity;
195
	}
196
197
	@Override
198
	public boolean canTeleport(final Level from, final Level to) {
199
		return from.dimension() == Level.END && to.dimension() == Level.OVERWORLD && this.getOwner() instanceof ServerPlayer player
200
			? super.canTeleport(from, to) && player.seenCredits
201
			: super.canTeleport(from, to);
202
	}
203
204
	@Override
205
	protected void onInsideBlock(final BlockState state) {
206
		super.onInsideBlock(state);
207
		if (state.is(Blocks.END_GATEWAY) && this.getOwner() instanceof ServerPlayer player) {
208
			player.onInsideBlock(state);
209
		}
210
	}
211
212
	@Override
213
	public void onRemoval(final Entity.RemovalReason reason) {
214
		if (reason != Entity.RemovalReason.UNLOADED_WITH_PLAYER) {
215
			this.deregisterFromCurrentOwner();
216
		}
217
218
		super.onRemoval(reason);
219
	}
220
221
	@Override
222
	public void onAboveBubbleColumn(final boolean dragDown, final BlockPos pos) {
223
		Entity.handleOnAboveBubbleColumn(this, dragDown, pos);
224
	}
225
226
	@Override
227
	public void onInsideBubbleColumn(final boolean dragDown) {
228
		Entity.handleOnInsideBubbleColumn(this, dragDown);
229
	}
230
}
231

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