Correctly fire VehicleExitEvent. Fixes BUKKIT-3761

This change makes it so that EntityHuman#setPassengerOf(Entity) invokes
its parent method when leaving vehicles so that VehicleExitEvent is fired
for players leaving vehicles.

This change also fixes BUKKIT-2110, making it so VehicleExitEvent
correctly handles cancellation. The implementation of VehicleExitEvent
completely ignored the cancellation state of the event, making it so that
cancelling the event had no effect.  Cancelling a VehicleExitEvent now
causes the entity to remain inside of the vehicle, with no visual stutter.
This commit is contained in:
Nate Mortensen
2013-07-12 16:45:42 -06:00
parent 67f15266da
commit cebc247b78
4 changed files with 52 additions and 6 deletions

View File

@@ -1391,6 +1391,8 @@ public abstract class Entity {
// b(null) doesn't really fly for overloaded methods,
// so this method is needed
Entity originalVehicle = this.vehicle;
Entity originalPassenger = this.vehicle == null ? null : this.vehicle.passenger;
PluginManager pluginManager = Bukkit.getPluginManager();
this.getBukkitEntity(); // make sure bukkitEntity is initialised
// CraftBukkit end
@@ -1402,6 +1404,10 @@ public abstract class Entity {
if ((this.bukkitEntity instanceof LivingEntity) && (this.vehicle.getBukkitEntity() instanceof Vehicle)) {
VehicleExitEvent event = new VehicleExitEvent((Vehicle) this.vehicle.getBukkitEntity(), (LivingEntity) this.bukkitEntity);
pluginManager.callEvent(event);
if (event.isCancelled() || this.vehicle != originalVehicle) {
return;
}
}
// CraftBukkit end
@@ -1413,10 +1419,28 @@ public abstract class Entity {
} else {
// CraftBukkit start
if ((this.bukkitEntity instanceof LivingEntity) && (entity.getBukkitEntity() instanceof Vehicle) && entity.world.isChunkLoaded((int) entity.locX >> 4, (int) entity.locZ >> 4)) {
// It's possible to move from one vehicle to another. We need to check if they're already in a vehicle, and fire an exit event if they are.
VehicleExitEvent exitEvent = null;
if (this.vehicle != null) {
exitEvent = new VehicleExitEvent((Vehicle) this.vehicle.getBukkitEntity(), (LivingEntity) this.bukkitEntity);
pluginManager.callEvent(exitEvent);
if (exitEvent.isCancelled() || this.vehicle != originalVehicle || (this.vehicle != null && this.vehicle.passenger != originalPassenger)) {
return;
}
}
VehicleEnterEvent event = new VehicleEnterEvent((Vehicle) entity.getBukkitEntity(), this.bukkitEntity);
pluginManager.callEvent(event);
if (event.isCancelled()) {
// If a plugin messes with the vehicle or the vehicle's passenger
if (event.isCancelled() || this.vehicle != originalVehicle || (this.vehicle != null && this.vehicle.passenger != originalPassenger)) {
// If we only cancelled the enterevent then we need to put the player in a decent position.
if (exitEvent != null && this.vehicle == originalVehicle && this.vehicle != null && this.vehicle.passenger == originalPassenger) {
this.setPositionRotation(this.vehicle.locX, this.vehicle.boundingBox.b + (double) this.vehicle.length, this.vehicle.locZ, this.yaw, this.pitch);
this.vehicle.passenger = null;
this.vehicle = null;
}
return;
}
}