package com.dumptruckman.supersimplespawners;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * EGG PLACE SPAWNER.  SPAWNER DROP EGG.
 */
public class SuperSimpleSpawners extends JavaPlugin implements Listener {

    /**
     * Spawn Egg item id.
     */
    private static final int SPAWN_EGG = 383;
    /**
     * Player's reach in blocks.
     */
    private static final int PLAYER_REACH = 4;
    /**
     * Permission to be able to place spawn eggs as spawners.
     */
    private static final Permission CAN_PLACE = new Permission(
            "supersimplespawners.place",
            "Spawn eggs place spawners.",
            PermissionDefault.FALSE);
    /**
     * Permission to have spawners drop as spawn eggs.
     */
    private static final Permission CAN_DROP = new Permission(
            "supersimplespawners.drop",
            "Spawners drop spawn eggs",
            PermissionDefault.FALSE);

    @Override
    public final void onDisable() {
        // Display disable message/version info
        this.getLogger().info("disabled.");
    }

    @Override
    public final void onEnable() {
        Bukkit.getPluginManager().registerEvents(this, this);

        this.getLogger().info("enabled.");
    }

    /**
     * Called when a player left or right clicks.
     *
     * @param event The event for said left or right clicks.
     */
    @EventHandler(priority = EventPriority.HIGH)
    public final void playerInteract(final PlayerInteractEvent event) {
        Player player = event.getPlayer();
        if (event.isCancelled()
                || !event.hasItem()
                || event.getItem().getTypeId() != SPAWN_EGG
                || !player.hasPermission(CAN_PLACE)
                || !event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
            return;
        }
        event.setCancelled(true);
        event.setUseItemInHand(Event.Result.DENY);
        Block targetBlock = player.getTargetBlock(null, PLAYER_REACH);
        if (targetBlock.getType().equals(Material.AIR)
                || !event.getBlockFace().equals(BlockFace.UP)) {
            return;
        }
        Location playerLocation = player.getLocation();
        if (playerLocation.getWorld().getBlockAt(
                playerLocation.getBlockX(),
                playerLocation.getBlockY() - 1,
                playerLocation.getBlockZ()).equals(targetBlock)) {
            return;
        }
        Block placedBlock = targetBlock.getWorld().getBlockAt(
                targetBlock.getX(),
                targetBlock.getY() + 1,
                targetBlock.getZ());
        placedBlock.setType(Material.MOB_SPAWNER);
        CreatureSpawner spawner = (CreatureSpawner) placedBlock.getState();
        ItemStack itemInHand = player.getItemInHand();
        spawner.setCreatureType(EntityType.valueOf(
                itemInHand.getDurability()).getType());
        itemInHand.setTypeId(0);
    }

    /**
     * Called when a player breaks a block, successful or not.
     *
     * @param event The event for said breakage.
     */
    @EventHandler(priority = EventPriority.HIGH)
    public final void blockBreak(final BlockBreakEvent event) {
        Block block = event.getBlock();
        if (event.isCancelled()
                || !block.getType().equals(Material.MOB_SPAWNER)
                || !event.getPlayer().hasPermission(CAN_DROP)) {
            return;
        }
        CreatureSpawner spawner = (CreatureSpawner) block.getState();
        ItemStack spawnEgg = new ItemStack(SPAWN_EGG, 1,
                EntityType.valueOf(spawner.getCreatureType()).getId());
        block.getWorld().dropItemNaturally(block.getLocation(),
                spawnEgg);
        block.setTypeId(0, true);
    }
}

@@ -44,34 +44,8 @@
 s;%0A%0A
-import org.bukkit.Bukkit;%0A
 impo
@@ -64,24 +64,24 @@
 t.Location;%0A
+
 import org.b
@@ -645,16 +645,56 @@
 efault;%0A
+import org.bukkit.plugin.PluginManager;%0A
 import o
@@ -1037,9 +1037,9 @@
 H = 
-4
+5
 ;%0A  
@@ -1762,14 +1762,43 @@
     
-Bukkit
+PluginManager pm = this.getServer()
 .get
@@ -1812,16 +1812,28 @@
 anager()
+;%0A        pm
 .registe
@@ -1853,16 +1853,88 @@
  this);%0A
+        pm.addPermission(CAN_PLACE);%0A        pm.addPermission(CAN_DROP);
 %0A       
@@ -2550,32 +2550,187 @@
 turn;%0A        %7D%0A
+        ItemStack itemInHand = player.getItemInHand();%0A        if (EntityType.valueOf(itemInHand.getDurability()) == null) %7B%0A            return;%0A        %7D%0A
         event.se
@@ -3621,63 +3621,8 @@
 ();%0A
-        ItemStack itemInHand = player.getItemInHand();%0A
     
@@ -4327,126 +4327,332 @@
     
-ItemStack spawnEgg = new ItemStack(SPAWN_EGG, 1,%0A                EntityType.valueOf(spawner.getC
+Short entityId = EntityType.valueOf(spawner.getCreatureType()).getId();%0A        if (entityId == null) %7B%0A            this.getLogger().warning(%22Unsupported spawner type, %22 +%0A                    %22nag dumptruckman to update this!%22);%0A            
 re
-a
 tur
-eType()).getId()
+n;%0A        %7D%0A        ItemStack spawnEgg = new ItemStack(SPAWN_EGG, 1, entityId
 );%0A 

Range is 5 not 4.