/*
 * Copyright (c) 2014 Wyatt Childers.
 *
 * All Rights Reserved
 */

package com.skelril.aurora.anticheat;

import com.sk89q.commandbook.CommandBook;
import com.skelril.aurora.util.ChatUtil;
import com.zachsthings.libcomponents.ComponentInformation;
import com.zachsthings.libcomponents.bukkit.BukkitComponent;
import com.zachsthings.libcomponents.config.ConfigurationBase;
import com.zachsthings.libcomponents.config.Setting;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerTeleportEvent;

import java.util.logging.Logger;

@ComponentInformation(friendlyName = "Anti Jump", desc = "Stop the jump hackers")
public class AntiJumpComponent extends BukkitComponent implements Listener {

    private final CommandBook inst = CommandBook.inst();
    private final Logger log = inst.getLogger();
    private final Server server = CommandBook.server();

    private LocalConfiguration config;

    @Override
    public void enable() {

        config = configure(new LocalConfiguration());
        //noinspection AccessStaticViaInstance
        inst.registerEvents(this);
    }

    @Override
    public void reload() {

        super.reload();
        configure(config);
    }

    private static class LocalConfiguration extends ConfigurationBase {

        @Setting("upwards-velocity")
        public double upwardsVelocity = .1;
        @Setting("leap-distance")
        public double leapDistance = 1.2;
        @Setting("radius")
        public double radius = 2;
    }

    @EventHandler(priority = EventPriority.MONITOR)
    public void onBlockPlace(BlockPlaceEvent event) {

        if (event.isCancelled()) {

            final Player player = event.getPlayer();
            final Location playerLoc = player.getLocation();

            final double x = playerLoc.getX();
            final double y = playerLoc.getY();
            final double z = playerLoc.getZ();

            final Location blockLoc = event.getBlock().getLocation();
            final int blockY = blockLoc.getBlockY();

            if (Math.abs(player.getVelocity().getY()) > config.upwardsVelocity && y > blockY) {
                server.getScheduler().runTaskLater(inst, new Runnable() {

                    @Override
                    public void run() {

                        if (player.getLocation().getY() >= (blockY + config.leapDistance)) {

                            Location basePlayerLoc = playerLoc.clone();
                            basePlayerLoc.setY(0);
                            Location baseBlockLoc = blockLoc.clone();
                            baseBlockLoc.setY(0);

                            if (basePlayerLoc.distanceSquared(baseBlockLoc) > config.radius * config.radius) {
                                return;
                            }

                            ChatUtil.sendWarning(player, "Hack jumping detected.");

                            Location playerLoc = player.getLocation();
                            playerLoc.setX(x);
                            playerLoc.setY(blockY);
                            playerLoc.setZ(z);

                            player.teleport(playerLoc, PlayerTeleportEvent.TeleportCause.UNKNOWN);
                        }
                    }
                }, 4);
            }
        }
    }
}

@@ -2382,98 +2382,16 @@
 st, 
-new Runnable() %7B%0A%0A                    @Override%0A                    public void run()
+() -%3E
  %7B%0A
-%0A 
     
@@ -2402,27 +2402,24 @@
             
-   
 if (player.g
@@ -2492,36 +2492,32 @@
                 
-    
 Location basePla
@@ -2536,36 +2536,32 @@
 yerLoc.clone();%0A
-    
                 
@@ -2607,36 +2607,32 @@
                 
-    
 Location baseBlo
@@ -2677,28 +2677,24 @@
             
-    
 baseBlockLoc
@@ -2724,28 +2724,24 @@
             
-   
- 
 if (basePlay
@@ -2839,20 +2839,16 @@
         
-    
 return;%0A
@@ -2871,20 +2871,16 @@
         
-    
 %7D%0A%0A     
@@ -2898,20 +2898,16 @@
         
-  
-  
 ChatUtil
@@ -2971,36 +2971,32 @@
                 
-    
 Location playerL
@@ -2989,32 +2989,33 @@
 cation playerLoc
+1
  = player.getLoc
@@ -3039,36 +3039,32 @@
                 
-   
- 
 playerLoc.setX(x
@@ -3052,24 +3052,25 @@
    playerLoc
+1
 .setX(x);%0A  
@@ -3067,20 +3067,16 @@
 etX(x);%0A
-    
         
@@ -3096,24 +3096,25 @@
    playerLoc
+1
 .setY(blockY
@@ -3116,20 +3116,16 @@
 lockY);%0A
-    
         
@@ -3145,24 +3145,25 @@
    playerLoc
+1
 .setZ(z);%0A%0A 
@@ -3157,28 +3157,24 @@
 1.setZ(z);%0A%0A
-    
             
@@ -3210,16 +3210,17 @@
 layerLoc
+1
 , Player
@@ -3261,34 +3261,8 @@
 N);%0A
-                        %7D%0A
     

Converted a runnable to a Lambda