From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shane Freeder <theboyetronic@gmail.com>
Date: Thu, 15 Nov 2018 13:38:37 +0000
Subject: [PATCH] force entity dismount during teleportation

Entities must be dismounted before teleportation in order to avoid
multiple issues in the server with regards to teleportation, shamefully,
too many plugins rely on the events firing, which means that not firing
these events caues more issues than it solves;

In order to counteract this, Entity dismount/exit vehicle events have
been modified to supress cancellation (and has a method to allow plugins
to check if this has been set), noting that cancellation will be silently
surpressed given that plugins are not expecting this event to not be cancellable.

This is a far from ideal scenario, however: given the current state of this
event and other alternatives causing issues elsewhere, I believe that
this is going to be the best soultion all around.

Improvements/suggestions welcome!

diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
index 15d4ad53cca71272e491bea31d83e8c8e697dac0..c7fe091d13349348b7288c0aa8bdde046dc47fa4 100644
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
@@ -1366,11 +1366,13 @@ public class ServerPlayer extends Player {
         }
     }
 
-    @Override
-    public void stopRiding() {
+    // Paper start
+    @Override public void stopRiding() { stopRiding(false); }
+    @Override public void stopRiding(boolean suppressCancellation) {
+        // Paper end
         Entity entity = this.getVehicle();
 
-        super.stopRiding();
+        super.stopRiding(suppressCancellation); // Paper
         Entity entity1 = this.getVehicle();
 
         if (entity1 != entity && this.connection != null) {
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
index b8257959acd2b38243fd259f367564080aff951e..d664b8691a8248a035cbe1b12a353f633980ea7b 100644
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
@@ -2396,11 +2396,16 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
     }
 
     public void removeVehicle() {
+        // Paper start
+        stopRiding(false);
+    }
+    public void stopRiding(boolean suppressCancellation) {
+        // Paper end
         if (this.vehicle != null) {
             Entity entity = this.vehicle;
 
             this.vehicle = null;
-            if (!entity.removePassenger(this)) this.vehicle = entity; // CraftBukkit
+            if (!entity.removePassenger(this, suppressCancellation)) this.vehicle = entity; // CraftBukkit // Paper
         }
 
     }
@@ -2467,7 +2472,10 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
         return true; // CraftBukkit
     }
 
-    protected boolean removePassenger(Entity entity) { // CraftBukkit
+    // Paper start
+    protected boolean removePassenger(Entity entity) { return removePassenger(entity, false);}
+    protected boolean removePassenger(Entity entity, boolean suppressCancellation) { // CraftBukkit
+        // Paper end
         if (entity.getVehicle() == this) {
             throw new IllegalStateException("Use x.stopRiding(y), not y.removePassenger(x)");
         } else {
@@ -2477,7 +2485,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
             if (this.getBukkitEntity() instanceof Vehicle && entity.getBukkitEntity() instanceof LivingEntity) {
                 VehicleExitEvent event = new VehicleExitEvent(
                         (Vehicle) this.getBukkitEntity(),
-                        (LivingEntity) entity.getBukkitEntity()
+                        (LivingEntity) entity.getBukkitEntity(), !suppressCancellation // Paper
                 );
                 // Suppress during worldgen
                 if (this.valid) {
@@ -2491,7 +2499,7 @@ public abstract class Entity implements Nameable, EntityAccess, CommandSource {
             }
             // CraftBukkit end
             // Spigot start
-            org.spigotmc.event.entity.EntityDismountEvent event = new org.spigotmc.event.entity.EntityDismountEvent(entity.getBukkitEntity(), this.getBukkitEntity());
+            org.spigotmc.event.entity.EntityDismountEvent event = new org.spigotmc.event.entity.EntityDismountEvent(entity.getBukkitEntity(), this.getBukkitEntity(), !suppressCancellation); // Paper
             // Suppress during worldgen
             if (this.valid) {
                 Bukkit.getPluginManager().callEvent(event);
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index 2c804ef89d910f706c449b4ce540ed1be3ac242a..3ff18ddb1d4b36fcfd97c9b29f2ecdcb4958916e 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -3432,9 +3432,15 @@ public abstract class LivingEntity extends Entity {
 
     @Override
     public void stopRiding() {
+        // Paper start
+        stopRiding(false);
+    }
+    @Override
+    public void stopRiding(boolean suppressCancellation) {
+        // Paper end
         Entity entity = this.getVehicle();
 
-        super.stopRiding();
+        super.stopRiding(suppressCancellation); // Paper - suppress
         if (entity != null && entity != this.getVehicle() && !this.level.isClientSide) {
             this.dismountVehicle(entity);
         }
diff --git a/src/main/java/net/minecraft/world/entity/player/Player.java b/src/main/java/net/minecraft/world/entity/player/Player.java
index 12904581bcf666c31ca6b02e5e0be2f5fe17de34..ba8b6d3fbc10eb9f02d3409eb2038827c47ca14d 100644
--- a/src/main/java/net/minecraft/world/entity/player/Player.java
+++ b/src/main/java/net/minecraft/world/entity/player/Player.java
@@ -1142,7 +1142,13 @@ public abstract class Player extends LivingEntity {
 
     @Override
     public void removeVehicle() {
-        super.removeVehicle();
+        // Paper start
+        stopRiding(false);
+    }
+    @Override
+    public void stopRiding(boolean suppressCancellation) {
+        // Paper end
+        super.stopRiding(suppressCancellation); // Paper - suppress
         this.boardingCooldown = 0;
     }
 
