package net.nunnerycode.bukkit.mythicdrops.crafting;

import net.nunnerycode.bukkit.mythicdrops.api.MythicDrops;
import net.nunnerycode.bukkit.mythicdrops.socketting.SocketGem;
import net.nunnerycode.bukkit.mythicdrops.socketting.SocketItem;
import net.nunnerycode.bukkit.mythicdrops.utils.SocketGemUtil;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public final class CraftingListener implements Listener {

  private MythicDrops plugin;

  public CraftingListener(MythicDrops plugin) {
    this.plugin = plugin;
  }

  @EventHandler
  public void onItemCraftEvent(CraftItemEvent event) {
    String replaceString = plugin.getSockettingSettings().getSocketGemName().replace('&',
                                                                                          '\u00A7')
        .replace("\u00A7\u00A7", "&").replaceAll("%(?s)(.*?)%", "").replaceAll("\\s+", " ");
    String[] splitString = ChatColor.stripColor(replaceString).split(" ");
    for (ItemStack is : event.getInventory().getMatrix()) {
      if (is.getType() == Material.AIR) {
        continue;
      }
      if (!is.hasItemMeta()) {
        continue;
      }
      ItemMeta im = is.getItemMeta();
      if (!im.hasDisplayName()) {
        continue;
      }
      String displayName = im.getDisplayName();
      String colorlessName = ChatColor.stripColor(displayName);

      for (String s : splitString) {
        if (colorlessName.contains(s)) {
          colorlessName = colorlessName.replace(s, "");
        }
      }

      colorlessName = colorlessName.replaceAll("\\s+", " ").trim();

      SocketGem socketGem = SocketGemUtil.getSocketGemFromName(colorlessName);
      if (socketGem == null) {
        continue;
      }
      if (is.isSimilar(new SocketItem(is.getData(), socketGem))) {
        event.setCancelled(true);
        return;
      }
    }
  }

}

@@ -47,16 +47,77 @@
 fting;%0A%0A
+import net.nunnerycode.bukkit.mythicdrops.MythicDropsPlugin;%0A
 import n
@@ -235,73 +235,8 @@
 em;%0A
-import net.nunnerycode.bukkit.mythicdrops.socketting.SocketItem;%0A
 impo
@@ -293,16 +293,76 @@
 GemUtil;
+%0Aimport net.nunnerycode.bukkit.mythicdrops.utils.StringUtil;
 %0A%0Aimport
@@ -1032,21 +1032,16 @@
         
-     
 '%5Cu00A7'
@@ -1266,24 +1266,74 @@
 Matrix()) %7B%0A
+      if (is == null) %7B%0A        continue;%0A      %7D%0A
       if (is
@@ -2024,64 +2024,687 @@
     
-if (is.isSimilar(new SocketItem(is.getData(), socketGem)
+String otherName = ChatColor.GOLD + StringUtil.replaceArgs(MythicDropsPlugin%0A                                                    .getInstance()%0A                                                    .getSockettingSettings()%0A                                                    .getSocketGemName(),%0A                                                new String%5B%5D%5B%5D%7B%0A                                                    %7B%22%25socketgem%25%22,%0A                                                     socketGem%0A                                                         .getName()%7D%7D).replace('&', '%5Cu00A7')%0A          .replace(%22%5Cu00A7%5Cu00A7%22, %22&%22) + ChatColor.GOLD;%0A      if (displayName.equals(otherName
 )) %7B

fix for crafting