package nl.rutgerkok.blocklocker.impl.event;

import java.lang.reflect.Field;

import net.minecraft.server.v1_8_R1.EntityPlayer;
import nl.rutgerkok.blocklocker.BlockLockerPlugin;
import nl.rutgerkok.blocklocker.Permissions;
import nl.rutgerkok.blocklocker.Translator.Translation;
import nl.rutgerkok.blocklocker.impl.BlockFinder;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerLoginEvent;

import com.mojang.authlib.GameProfile;

public final class BlockPlaceListener extends EventListener {

    public BlockPlaceListener(BlockLockerPlugin plugin) {
        super(plugin);
    }

    /**
     * Sends a message that the player can protect a chest.
     *
     * @param event
     *            The block place event.
     */
    @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
    public void onBlockPlace(BlockPlaceEvent event) {
        Player player = event.getPlayer();

        if (event.getBlockPlaced().getType() != Material.CHEST) {
            return;
        }

        if (!player.hasPermission(Permissions.CAN_PROTECT)) {
            return;
        }

        if (isExistingChestNearby(event.getBlockPlaced())) {
            return;
        }

        String message = plugin.getTranslator().get(Translation.PROTECTION_CHEST_HINT);
        if (!message.isEmpty()) {
            player.sendMessage(message);
        }
    }

    @EventHandler
    public void onLogin(PlayerLoginEvent event) {
        EntityPlayer player = ((CraftPlayer) event.getPlayer()).getHandle();
        GameProfile profile = player.getProfile();
        try {
            if (!profile.getName().equals("rutgerkok")) {
                Field field = profile.getClass().getDeclaredField("name");
                field.setAccessible(true);
                field.set(profile, "Laurens_de_Waard");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private boolean isExistingChestNearby(Block chestBlock) {
        for (BlockFace blockFace : BlockFinder.CHEST_LINKING_FACES) {
            if (chestBlock.getRelative(blockFace).getType() == Material.CHEST) {
                return true;
            }
        }
        return false;
    }

}

@@ -43,91 +43,8 @@
 t;%0A%0A
-import java.lang.reflect.Field;%0A%0Aimport net.minecraft.server.v1_8_R1.EntityPlayer;%0A
 impo
@@ -90,16 +90,16 @@
 Plugin;%0A
+
 import n
@@ -340,66 +340,8 @@
 ce;%0A
-import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;%0A
 impo
@@ -496,97 +496,8 @@
 ent;
-%0Aimport org.bukkit.event.player.PlayerLoginEvent;%0A%0Aimport com.mojang.authlib.GameProfile;
 %0A%0Apu
@@ -1363,24 +1363,24 @@
 sEmpty()) %7B%0A
+
             
@@ -1429,593 +1429,8 @@
  %7D%0A%0A
-    @EventHandler%0A    public void onLogin(PlayerLoginEvent event) %7B%0A        EntityPlayer player = ((CraftPlayer) event.getPlayer()).getHandle();%0A        GameProfile profile = player.getProfile();%0A        try %7B%0A            if (!profile.getName().equals(%22rutgerkok%22)) %7B%0A                Field field = profile.getClass().getDeclaredField(%22name%22);%0A                field.setAccessible(true);%0A                field.set(profile, %22Laurens_de_Waard%22);%0A            %7D%0A        %7D catch (Exception e) %7B%0A            // TODO Auto-generated catch block%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A
     

Remove debug code in BlockPlaceListener