package com.lothrazar.cyclicmagic.registry;
import java.util.HashMap;
import java.util.Map;
import com.lothrazar.cyclicmagic.IHasRecipe;
import com.lothrazar.cyclicmagic.ModCyclic;
import com.lothrazar.cyclicmagic.block.BlockDimensionOre;
import com.lothrazar.cyclicmagic.block.base.IBlockHasTESR;
import com.lothrazar.cyclicmagic.block.base.IHasOreDict;
import com.lothrazar.cyclicmagic.component.cable.BlockCableBase;
import com.lothrazar.cyclicmagic.config.IHasConfig;
import com.lothrazar.cyclicmagic.data.Const;
import com.lothrazar.cyclicmagic.module.BlockUtilityModule;
import com.lothrazar.cyclicmagic.registry.GuideRegistry.GuideCategory;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.renderer.block.statemap.IStateMapper;
import net.minecraft.client.renderer.block.statemap.StateMapperBase;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.oredict.OreDictionary;

public class ItemRegistry {
  public static Map<String, Item> itemMap = new HashMap<String, Item>();
  public static void register(Item item, String key, GuideCategory cat) {
    item.setUnlocalizedName(key);
    item.setRegistryName(new ResourceLocation(Const.MODID, key));
    itemMap.put(key, item);
    item = ItemRegistry.itemMap.get(key);
    item.setCreativeTab(ModCyclic.TAB);
    if (item instanceof IHasConfig) {
      ConfigRegistry.register((IHasConfig) item);
    }
    IRecipe recipe = null;
    if (item instanceof IHasRecipe) {
      recipe = ((IHasRecipe) item).addRecipe();
    }
    if (cat != null) {
      GuideRegistry.register(cat, item, recipe, null);
    }
  }
  public static void register(Item item, String key) {
    register(item, key, GuideCategory.ITEM);//defaults to in guide book with its own standalone page
  }
  @SubscribeEvent
  public static void onRegistryEvent(RegistryEvent.Register<Item> event) {
    // event.getRegistry().registerAll(ItemRegistry.itemMap.values().toArray(new Item[0]));
    //new registries are crazy wacky. so ore dict DOES NOT WORK in block reg, stack becomes empty
    for (Item item : ItemRegistry.itemMap.values()) {
      event.getRegistry().register(item);
      Block blockItem = Block.getBlockFromItem(item);
      if (blockItem != null && blockItem != Blocks.AIR) {
        if (blockItem instanceof IHasOreDict) {
          String oreName = ((IHasOreDict) blockItem).getOre();
          OreDictionary.registerOre(oreName, blockItem);
          ModCyclic.logger.info("Registered ore dict entry " + oreName + " : " + blockItem);
        }
        //hacky-ish way to register smelting.. we do not have ability do to this inside block class anymore
        if (blockItem instanceof BlockDimensionOre) {
          BlockDimensionOre ore = (BlockDimensionOre) blockItem;
          GameRegistry.addSmelting(item, ore.getSmeltingOutput(), 1);
        }
        if (blockItem instanceof IHasRecipe) {
          //ModCyclic.logger.info("item to block recipe?? " + blockItem);
          ((IHasRecipe) blockItem).addRecipe();
        }
      }
    }
  }
  //  @SideOnly(Side.CLIENT)
  //  private static final ModelResourceLocation ITEM_MODEL = new ModelResourceLocation(
  //      new ResourceLocation(Const.MODID, "cable"), "inventory");
  @SideOnly(Side.CLIENT)
  private static final IStateMapper STATE_MAPPER = new StateMapperBase() {
    @Override
    protected ModelResourceLocation getModelResourceLocation(IBlockState state) {
      return new ModelResourceLocation(state.getBlock().getRegistryName(), "normal");
    }
  };
  @SideOnly(Side.CLIENT)
  @SubscribeEvent
  public static void registerModels(ModelRegistryEvent event) {
    // with help from
    // http://www.minecraftforge.net/forum/index.php?topic=32492.0
    // https://github.com/TheOnlySilverClaw/Birdmod/blob/master/src/main/java/silverclaw/birds/client/ClientProxyBirds.java
    // More info on proxy rendering
    // http://www.minecraftforge.net/forum/index.php?topic=27684.0
    // http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/modification-development/2272349-lessons-from-my-first-mc-1-8-mod
    //    ItemModelMesher mesher = Minecraft.getMinecraft().getRenderItem().getItemModelMesher();
    String name;
    Item item;
    for (String key : ItemRegistry.itemMap.keySet()) {
      item = ItemRegistry.itemMap.get(key);
      if (item instanceof ItemBlock
      // || Block.getBlockFromItem(item) instanceof BlockBaseCable
      ) {
        continue;
      }
      name = Const.MODRES + item.getUnlocalizedName().replaceAll("item.", "");
      ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(name, "inventory"));
    }
    for (Block b : BlockRegistry.blocks) {
      item = Item.getItemFromBlock(b);
      if (b instanceof BlockCableBase) {
        ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(
            new ResourceLocation(Const.MODID, b.getUnlocalizedName().replaceAll("tile.", "")), "inventory"));
        ModelLoader.setCustomStateMapper(b, STATE_MAPPER);
        //TODO: CABLE REGISTRY OR SOMETHING
        continue;
      }
      name = Const.MODRES + b.getUnlocalizedName().replaceAll("tile.", "");
      ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(name, "inventory"));
      ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(name));
      if (b instanceof IBlockHasTESR) {
        ((IBlockHasTESR) b).initModel();
      }
    }

  }
}

@@ -3663,20 +3663,16 @@
   %7D%0A  %7D%0A
-  //
   @SideO
@@ -3694,204 +3694,91 @@
 )%0A  
-//  private static final ModelResourceLocation ITEM_MODEL = new ModelResourceLocation(%0A  //      new ResourceLocation(Const.MODID, %22cable%22), %22inventory%22);%0A  @SideOnly(Side.CLIENT)%0A  private static
+@SubscribeEvent%0A  public static void registerModels(ModelRegistryEvent event) %7B%0A   
  fin
@@ -3836,16 +3836,18 @@
 ) %7B%0A    
+  
 @Overrid
@@ -3848,16 +3848,18 @@
 verride%0A
+  
     prot
@@ -3938,16 +3938,18 @@
 %7B%0A      
+  
 return n
@@ -4028,121 +4028,18 @@
     
-%7D%0A
   %7D
-;
 %0A  
-@SideOnly(Side.CLIENT)%0A  @SubscribeEvent%0A  public static void registerModels(ModelRegistryEvent event) %7B
+  %7D;
 %0A   
@@ -5871,15 +5871,14 @@
 %7D%0A    %7D%0A
-%0A
   %7D%0A%7D%0A

fix server not loaded