package ljfa.tntutils.proxy;

import ljfa.tntutils.Config;
import ljfa.tntutils.TNTUtils;
import ljfa.tntutils.blocks.BlockReplacedTNT;
import ljfa.tntutils.handlers.EntityJoinHandler;
import ljfa.tntutils.handlers.ExplosionHandler;
import ljfa.tntutils.util.LogHelper;
import ljfa.tntutils.util.ReflectionHelper;
import ljfa.tntutils.util.Utils;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.util.ObjectIntIdentityMap;
import net.minecraft.util.RegistryNamespaced;
import net.minecraft.util.RegistrySimple;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;

import com.google.common.collect.BiMap;

public class CommonProxy {
    public void preInit(FMLPreInitializationEvent event) {
        Config.loadConfig(event.getSuggestedConfigurationFile());
        
        if(Config.preventChainExpl) {
            TNTUtils.replaced_tnt = new BlockReplacedTNT().setHardness(0.0F).setStepSound(Block.soundTypeGrass).setUnlocalizedName("tnt");
            replaceVanillaTNT();
        }
    }
    
    public void init(FMLInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(new ExplosionHandler());
        if(Config.disableTNT || Config.disableTNTMinecart)
            MinecraftForge.EVENT_BUS.register(new EntityJoinHandler());
    }
    
    public void postInit(FMLPostInitializationEvent event) {
        
    }
    
    private void replaceVanillaTNT() {
        try {
            LogHelper.info("About to replace Vanilla TNT");
            
            //Get TNT object to replace
            Block oldTNT = (Block)Block.blockRegistry.getObject("tnt");
            int tntID = Block.blockRegistry.getIDForObject(oldTNT);

            //Replace it in the "underlyingIntegerMap"
            String fieldName = Utils.deobfuscatedEnv ? "underlyingIntegerMap" : "field_148759_a";
            ObjectIntIdentityMap intMap = (ObjectIntIdentityMap)ReflectionHelper.getField(RegistryNamespaced.class, fieldName, Block.blockRegistry);
            intMap.put(TNTUtils.replaced_tnt, tntID);
            
            //Replace it in the "registryObjects"
            fieldName = Utils.deobfuscatedEnv ? "registryObjects" : "field_82596_a";
            BiMap regMap = (BiMap)ReflectionHelper.getField(RegistrySimple.class, fieldName, Block.blockRegistry);
            regMap.forcePut(new ResourceLocation("minecraft:tnt"), TNTUtils.replaced_tnt);
            
            //Replace it in the associated ItemBlock
            ItemBlock tntItem = (ItemBlock)Item.itemRegistry.getObjectById(tntID);
            fieldName = Utils.deobfuscatedEnv ? "block" : "field_150939_a";
            ReflectionHelper.setFinalField(ItemBlock.class, fieldName, tntItem, TNTUtils.replaced_tnt);
            
            //Replace it in the Blocks class
            fieldName = Utils.deobfuscatedEnv ? "tnt" : "field_150335_W";
            ReflectionHelper.setFinalField(Blocks.class, fieldName, null, TNTUtils.replaced_tnt);
            LogHelper.info("Replaced Vanilla TNT");
        } catch(Exception ex) {
            throw new RuntimeException("Failed to replace Vanilla TNT!", ex);
        }
    }
}

@@ -1687,24 +1687,59 @@
 %0A    %7D%0A    %0A
+    @SuppressWarnings(%22unchecked%22)%0A
     private 
@@ -2103,221 +2103,8 @@
     
- String fieldName = Utils.deobfuscatedEnv ? %22underlyingIntegerMap%22 : %22field_148759_a%22;%0A            ObjectIntIdentityMap intMap = (ObjectIntIdentityMap)ReflectionHelper.getField(RegistryNamespaced.class, fieldName,
  Blo
@@ -2119,34 +2119,34 @@
 Registry
-);%0A            int
+.underlyingInteger
 Map.put(
@@ -2255,194 +2255,90 @@
     
-fieldName = Utils.deobfuscatedEnv ? %22registryObjects%22 : %22field_82596_a%22;%0A            BiMap regMap = (BiMap)ReflectionHelper.getField(RegistrySimple.class, fieldName, Block.blockRegistry)
+BiMap%3CResourceLocation, Block%3E regMap = (BiMap)Block.blockRegistry.registryObjects
 ;%0A  
@@ -2591,151 +2591,23 @@
     
-fieldName = Utils.deobfuscatedEnv ? %22block%22 : %22field_150939_a%22;%0A            ReflectionHelper.setFinalField(
+tnt
 Item
-B
+.b
 lock
-.class, fieldName, tntItem,
+ =
  TNT
@@ -2616,33 +2616,32 @@
 ils.replaced_tnt
-)
 ;%0A            %0A 
@@ -2700,143 +2700,20 @@
     
-fieldName = Utils.deobfuscatedEnv ? %22tnt%22 : %22field_150335_W%22;%0A            ReflectionHelper.setFinalField(Blocks.class, fieldName, null,
+Blocks.tnt =
  TNT
@@ -2722,34 +2722,46 @@
 ils.replaced_tnt
-)
 ;
+%0A            
 %0A            Log

Remove unneeded reflection due to Access Transformer