--- a/net/minecraft/world/entity/projectile/ThrownEnderpearl.java
+++ b/net/minecraft/world/entity/projectile/ThrownEnderpearl.java
@@ -126,11 +_,18 @@
                 Vec3 vec3 = this.oldPosition();
                 if (owner instanceof ServerPlayer serverPlayer) {
                     if (serverPlayer.connection.isAcceptingMessages()) {
+                        // CraftBukkit start
+                        ServerPlayer serverPlayer1 = serverPlayer.teleport(new TeleportTransition(serverLevel, vec3, Vec3.ZERO, 0.0F, 0.0F, Relative.union(Relative.ROTATION, Relative.DELTA), TeleportTransition.DO_NOTHING, org.bukkit.event.player.PlayerTeleportEvent.TeleportCause.ENDER_PEARL));
+                        if (serverPlayer1 == null) {
+                            this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.HIT);
+                            return;
+                        }
+                        // CraftBukkit end
                         if (this.random.nextFloat() < 0.05F && serverLevel.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING)) {
                             Endermite endermite = EntityType.ENDERMITE.create(serverLevel, EntitySpawnReason.TRIGGERED);
                             if (endermite != null) {
                                 endermite.moveTo(owner.getX(), owner.getY(), owner.getZ(), owner.getYRot(), owner.getXRot());
-                                serverLevel.addFreshEntity(endermite);
+                                serverLevel.addFreshEntity(endermite, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.ENDER_PEARL);
                             }
                         }
 
@@ -138,15 +_,17 @@
                             owner.setPortalCooldown();
                         }
 
-                        ServerPlayer serverPlayer1 = serverPlayer.teleport(
-                            new TeleportTransition(
-                                serverLevel, vec3, Vec3.ZERO, 0.0F, 0.0F, Relative.union(Relative.ROTATION, Relative.DELTA), TeleportTransition.DO_NOTHING
-                            )
-                        );
+                        // CraftBukkit start - moved up
+                        // ServerPlayer serverPlayer1 = serverPlayer.teleport(
+                        //     new TeleportTransition(
+                        //         serverLevel, vec3, Vec3.ZERO, 0.0F, 0.0F, Relative.union(Relative.ROTATION, Relative.DELTA), TeleportTransition.DO_NOTHING
+                        //     )
+                        // );
+                        // CraftBukkit end - moved up
                         if (serverPlayer1 != null) {
                             serverPlayer1.resetFallDistance();
                             serverPlayer1.resetCurrentImpulseContext();
-                            serverPlayer1.hurtServer(serverPlayer.serverLevel(), this.damageSources().enderPearl(), 5.0F);
+                            serverPlayer1.hurtServer(serverPlayer.serverLevel(), this.damageSources().enderPearl().eventEntityDamager(this), 5.0F); // CraftBukkit // Paper - fix DamageSource API
                         }
 
                         this.playSound(serverLevel, vec3);
@@ -162,9 +_,9 @@
                     this.playSound(serverLevel, vec3);
                 }
 
-                this.discard();
+                this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.HIT); // CraftBukkit - add Bukkit remove cause
             } else {
-                this.discard();
+                this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.HIT); // CraftBukkit - add Bukkit remove cause
             }
         }
     }
@@ -185,7 +_,7 @@
         if (owner instanceof ServerPlayer serverPlayer
             && !owner.isAlive()
             && serverPlayer.serverLevel().getGameRules().getBoolean(GameRules.RULE_ENDER_PEARLS_VANISH_ON_DEATH)) {
-            this.discard();
+            this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
         } else {
             super.tick();
         }
@@ -212,7 +_,7 @@
     public Entity teleport(TeleportTransition teleportTransition) {
         Entity entity = super.teleport(teleportTransition);
         if (entity != null) {
-            entity.placePortalTicket(BlockPos.containing(entity.position()));
+            if (!this.level().paperConfig().misc.legacyEnderPearlBehavior) entity.placePortalTicket(BlockPos.containing(entity.position())); // Paper - Allow using old ender pearl behavior
         }
 
         return entity;
@@ -220,7 +_,7 @@
 
     @Override
     public boolean canTeleport(Level fromLevel, Level toLevel) {
-        return fromLevel.dimension() == Level.END && toLevel.dimension() == Level.OVERWORLD && this.getOwner() instanceof ServerPlayer serverPlayer
+        return fromLevel.getTypeKey() == net.minecraft.world.level.dimension.LevelStem.END && toLevel.getTypeKey() == net.minecraft.world.level.dimension.LevelStem.OVERWORLD && this.getOwner() instanceof ServerPlayer serverPlayer // CraftBukkit
             ? super.canTeleport(fromLevel, toLevel) && serverPlayer.seenCredits
             : super.canTeleport(fromLevel, toLevel);
     }
