// Get the last entity on the list
final Entity entity = entities.get(entities.size() - 1).getBukkitEntity();
final LivingEntity finalController = controller != null ? controller.getLivingEntity() : null;
BukkitRunnable task = new BukkitRunnable() {
Location location = null;
Boolean flying = true;
public void run() {
if (freeflight) {
// If freeflight is on, and the flying entity
// is ridden by another entity, let it keep
// flying where the controller is looking
if (!entity.isEmpty() && finalController.isInsideVehicle()) {
location = finalController.getEyeLocation()
.add(finalController.getEyeLocation().getDirection()
.multiply(30));
}
else {
flying = false;
}
}
else {
// If freelight is not on, keep flying only as long
// as there are destinations left
if (destinations.size() > 0) {
location = destinations.get(0);
}
else {
flying = false;
}
}
if (flying && entity.isValid()) {
// To avoid excessive turbulence, only have the entity rotate
// when it really needs to
if (!Rotation.isFacingLocation(entity, location, rotationThreshold)) {
Rotation.faceLocation(entity, location);
}
Vector v1 = entity.getLocation().toVector();
Vector v2 = location.toVector();
Vector v3 = v2.clone().subtract(v1).normalize().multiply(speed);
entity.setVelocity(v3);
// If freeflight is off, check if the entity has reached its
// destination, and remove the destination if that happens
// to be the case
if (!freeflight) {
if (Math.abs(v2.getX() - v1.getX()) < 2 && Math.abs(v2.getY() - v1.getY()) < 2
&& Math.abs(v2.getZ() - v1.getZ()) < 2) {
destinations.remove(0);
}
}
}
else {
flying = false;
this.cancel();
}
}
};
task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 3);
}