Search in sources :

Example 1 with VehicleMoveEvent

use of org.bukkit.event.vehicle.VehicleMoveEvent in project Glowstone by GlowstoneMC.

the class VehicleMoveHandler method handle.

@Override
public void handle(GlowSession session, VehicleMoveMessage message) {
    GlowPlayer player = session.getPlayer();
    if (!player.isInsideVehicle() || !(player.getVehicle() instanceof Vehicle)) {
        return;
    }
    GlowEntity vehicle = (GlowEntity) player.getVehicle();
    Location oldLocation = vehicle.getLocation();
    Location newLocation = oldLocation.clone();
    message.update(newLocation);
    // don't let players reach an illegal position
    if (Math.abs(newLocation.getBlockX()) > 32000000 || Math.abs(newLocation.getBlockZ()) > 32000000) {
        session.getPlayer().kickPlayer("Illegal position");
        return;
    }
    /*
          don't let players move more than 100 blocks in a single packet
          if they move greater than 10 blocks, but less than 100, just warn
          this is NOT robust hack prevention - only to prevent client
          confusion about where its actual location is (e.g. during login)
        */
    if (Position.hasMoved(oldLocation, newLocation) && !player.isDead() && !vehicle.isDead()) {
        double distance = newLocation.distanceSquared(oldLocation);
        if (distance > 100 * 100) {
            session.getPlayer().kickPlayer("You moved too quickly :( (Hacking?)");
            return;
        } else if (distance > 100) {
            GlowServer.logger.warning(session.getPlayer().getName() + " moved too quickly!");
        }
    }
    if (!isValidMovement(vehicle, oldLocation, newLocation)) {
        vehicle.teleport(oldLocation);
        return;
    }
    // call move event if movement actually occurred and there are handlers registered
    if (!oldLocation.equals(newLocation) && VehicleMoveEvent.getHandlerList().getRegisteredListeners().length > 0) {
        EventFactory.getInstance().callEvent(new VehicleMoveEvent((Vehicle) vehicle, oldLocation, newLocation.clone()));
    }
    // simply update location
    vehicle.setRawLocation(newLocation);
    player.setRawLocation(vehicle.getMountLocation());
    // track movement stats
    Vector delta = newLocation.clone().subtract(oldLocation).toVector();
    delta.setX(Math.abs(delta.getX()));
    delta.setY(Math.abs(delta.getY()));
    delta.setZ(Math.abs(delta.getZ()));
    int flatDistance = (int) Math.round(Math.hypot(delta.getX(), delta.getZ()) * 100.0);
    if (vehicle instanceof Boat) {
        player.incrementStatistic(Statistic.BOAT_ONE_CM, flatDistance);
    } else if (vehicle instanceof Minecart) {
        player.incrementStatistic(Statistic.MINECART_ONE_CM, flatDistance);
    }
}
Also used : Vehicle(org.bukkit.entity.Vehicle) GlowPlayer(net.glowstone.entity.GlowPlayer) GlowEntity(net.glowstone.entity.GlowEntity) VehicleMoveEvent(org.bukkit.event.vehicle.VehicleMoveEvent) Minecart(org.bukkit.entity.Minecart) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) Boat(org.bukkit.entity.Boat)

Aggregations

GlowEntity (net.glowstone.entity.GlowEntity)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 Location (org.bukkit.Location)1 Boat (org.bukkit.entity.Boat)1 Minecart (org.bukkit.entity.Minecart)1 Vehicle (org.bukkit.entity.Vehicle)1 VehicleMoveEvent (org.bukkit.event.vehicle.VehicleMoveEvent)1 Vector (org.bukkit.util.Vector)1