package net.voidteam.cicon;

import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Events.ShopDestroyedEvent;
import me.nighteyes604.ItemStay.FrozenItem;
import me.nighteyes604.ItemStay.ItemStayListener;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Sign;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Iterator;

/**
 * Created by Robby Duke on 1/9/15.
 * Copyright (c) 2015
 */
public class ChestIcon extends JavaPlugin implements Listener {
    private Plugin itemStay = null;

    @Override
    public void onEnable() {
        /**
         * Let's just check that the dependencies are loaded, even though the plugin.yml should do this!
         */
        itemStay = getServer().getPluginManager().getPlugin("ItemStay");
        Plugin chestShop = getServer().getPluginManager().getPlugin("ChestShop");

        if (!itemStay.equals(null) && !itemStay.equals(null)) {
            this.getServer().getPluginManager().registerEvents(this, this);
        } else {
            Bukkit.getLogger().severe("ItemStay/ChestShop not found! Disabling plugin...");
            this.setEnabled(false);
        }
    }

    @Override
    public void onDisable() {
        super.onDisable();
    }

    @EventHandler
    public void shopCreate(final ShopCreatedEvent event) {
        Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this.itemStay, new Runnable() {
            public void run() {
                Location displayLocation = event.getChest().getLocation().add(0, 1, 0);

                ItemStack displayStack = new ItemStack(Material.matchMaterial(event.getSignLine((short) 3)));
                Item displayItem = event.getPlayer().getWorld().dropItem(displayLocation, displayStack);

                Boolean allowed = true;

                for (int x = 0; x < ItemStayListener.plugin.frozenItems.size(); x++) {
                    FrozenItem frozenItem = ItemStayListener.plugin.frozenItems.get(x);
                    Iterator entitiesIterator = displayItem.getNearbyEntities(0.5D, 0.5D, 0.5D).iterator();

                    while (entitiesIterator.hasNext()) {
                        Entity entity = (Entity) entitiesIterator.next();
                        if (entity.getLocation().getBlock().equals(frozenItem.location.getBlock()) && entity instanceof Item && !entity.equals(displayItem)) {
                            allowed = false;
                            displayItem.remove();
                        }
                    }
                }

                if (allowed.booleanValue()) {
                    ItemStayListener.plugin.frozenItems.add(
                            new FrozenItem(event.getPlayer().getName().toLowerCase(),
                                    displayItem.getLocation(), displayItem.getType().name(), displayItem.getItemStack().getType(),
                                    displayItem.getItemStack().getDurability())
                    );

                    displayItem.remove();
                }
            }
        }, 20L);
    }

    @EventHandler
    public void shopDestroy(final ShopDestroyedEvent event) {
        for (int i = 0; i < ItemStayListener.plugin.frozenItems.size(); i++) {
            FrozenItem frozenItem = (FrozenItem) ItemStayListener.plugin.frozenItems.get(i);

            if (event.getSign().getType() == Material.SIGN_POST || event.getSign().getType() == Material.WALL_SIGN) {
                Sign s = (Sign) event.getSign().getBlock().getState().getData();
                Block attachedBlock = event.getSign().getBlock().getRelative(s.getAttachedFace());

                Location chestLocation = attachedBlock.getLocation().add(0.5, 1.9, 0.5);
                if (frozenItem.location.equals(chestLocation)) {
                    frozenItem.destroy();
                    ItemStayListener.plugin.frozenItems.remove(ItemStayListener.plugin.frozenItems.get(i));
                }
            }
        }
    }
}

@@ -18,24 +18,227 @@
 eam.cicon;%0A%0A
+import com.Acrobot.Breeze.Utils.MaterialUtil;%0Aimport com.Acrobot.Breeze.Utils.StringUtil;%0Aimport com.Acrobot.ChestShop.Configuration.Properties;%0Aimport com.Acrobot.ChestShop.Events.PreShopCreationEvent;%0A
 import com.A
@@ -331,24 +331,67 @@
 royedEvent;%0A
+import com.Acrobot.ChestShop.Utils.uBlock;%0A
 import me.ni
@@ -782,21 +782,152 @@
 kit.
-material.Sign
+inventory.meta.ItemMeta;%0Aimport org.bukkit.material.MaterialData;%0Aimport org.bukkit.material.Sign;%0Aimport org.bukkit.metadata.FixedMetadataValue
 ;%0Aim
@@ -2195,24 +2195,25 @@
 (0, 1, 0);%0A%0A
+%0A
             
@@ -2220,101 +2220,370 @@
     
-ItemStack displayStack = new ItemStack(Material.matchMaterial(event.getSignLine((short) 3)));
+String itemCode = event.getSignLine((byte) 3);%0A                String%5B%5D dataInput = String.valueOf(event.getSignLine((short) 3)).split(%22:%22);%0A%0A                ItemStack displayStack = MaterialUtil.getItem(itemCode);%0A%0A                /*if( dataInput.length == 2 ) %7B%0A                    displayStack.setDurability(Short.valueOf(dataInput%5B1%5D));%0A                %7D*/%0A%0A
 %0A   
@@ -2681,24 +2681,26 @@
 layStack);%0A%0A
+%0A%0A
             
@@ -3495,23 +3495,8 @@
 owed
-.booleanValue()
 ) %7B%0A

Add support for complex item names.