Search in sources :

Example 1 with MoveType

use of com.sk89q.worldguard.session.MoveType in project WorldGuard by EngineHub.

the class PlayerMoveListener method onPlayerMove.

@EventHandler(priority = EventPriority.HIGH)
public void onPlayerMove(PlayerMoveEvent event) {
    Location from = event.getFrom();
    Location to = event.getTo();
    if (from.getBlockX() == to.getBlockX() && from.getBlockY() == to.getBlockY() && from.getBlockZ() == to.getBlockZ()) {
        return;
    }
    final Player player = event.getPlayer();
    LocalPlayer localPlayer = getPlugin().wrapPlayer(player);
    Session session = WorldGuard.getInstance().getPlatform().getSessionManager().get(localPlayer);
    MoveType moveType = MoveType.MOVE;
    if (event.getPlayer().isGliding()) {
        moveType = MoveType.GLIDE;
    } else if (event.getPlayer().isSwimming()) {
        moveType = MoveType.SWIM;
    } else if (event.getPlayer().getVehicle() != null && event.getPlayer().getVehicle() instanceof Horse) {
        moveType = MoveType.RIDE;
    }
    com.sk89q.worldedit.util.Location weLocation = session.testMoveTo(localPlayer, BukkitAdapter.adapt(to), moveType);
    if (weLocation != null) {
        final Location override = BukkitAdapter.adapt(weLocation);
        override.setX(override.getBlockX() + 0.5);
        override.setY(override.getBlockY());
        override.setZ(override.getBlockZ() + 0.5);
        override.setPitch(to.getPitch());
        override.setYaw(to.getYaw());
        event.setTo(override.clone());
        Entity vehicle = player.getVehicle();
        if (vehicle != null) {
            vehicle.eject();
            Entity current = vehicle;
            while (current != null) {
                current.eject();
                vehicle.setVelocity(new Vector());
                if (vehicle instanceof LivingEntity) {
                    vehicle.teleport(override.clone());
                } else {
                    vehicle.teleport(override.clone().add(0, 1, 0));
                }
                current = current.getVehicle();
            }
            player.teleport(override.clone().add(0, 1, 0));
            Bukkit.getScheduler().runTaskLater(getPlugin(), () -> player.teleport(override.clone().add(0, 1, 0)), 1);
        }
    }
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) LocalPlayer(com.sk89q.worldguard.LocalPlayer) Player(org.bukkit.entity.Player) LocalPlayer(com.sk89q.worldguard.LocalPlayer) Horse(org.bukkit.entity.Horse) MoveType(com.sk89q.worldguard.session.MoveType) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location) Session(com.sk89q.worldguard.session.Session) EventHandler(org.bukkit.event.EventHandler)

Aggregations

LocalPlayer (com.sk89q.worldguard.LocalPlayer)1 MoveType (com.sk89q.worldguard.session.MoveType)1 Session (com.sk89q.worldguard.session.Session)1 Location (org.bukkit.Location)1 Entity (org.bukkit.entity.Entity)1 Horse (org.bukkit.entity.Horse)1 LivingEntity (org.bukkit.entity.LivingEntity)1 Player (org.bukkit.entity.Player)1 EventHandler (org.bukkit.event.EventHandler)1 Vector (org.bukkit.util.Vector)1