package ljfa.tntutils.proxy;

import ljfa.tntutils.Config;
import ljfa.tntutils.blocks.ModBlocks;
import ljfa.tntutils.util.LogHelper;
import ljfa.tntutils.util.ReflectionHelper;
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 org.apache.logging.log4j.Level;

import com.google.common.collect.BiMap;

import cpw.mods.fml.common.ObfuscationReflectionHelper;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

public class CommonProxy {
    public void preInit(FMLPreInitializationEvent event) {
        Config.loadConfig(event.getSuggestedConfigurationFile());
        ModBlocks.preInit();
        
        if(Config.replaceTNT)
            replaceVanillaTNT();
    }
    
    public void init(FMLInitializationEvent event) {
        
    }
    
    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"
            ObjectIntIdentityMap intMap = ObfuscationReflectionHelper.getPrivateValue(RegistryNamespaced.class, Block.blockRegistry, "underlyingIntegerMap", "field_148759_a");
            intMap.func_148746_a(ModBlocks.replaced_tnt, tntID);
            
            //Replace it in the "registryObjects"
            BiMap regMap = ObfuscationReflectionHelper.getPrivateValue(RegistrySimple.class, Block.blockRegistry, "registryObjects", "field_82596_a");
            regMap.forcePut("minecraft:tnt", ModBlocks.replaced_tnt);
            
            //Replace it in the associated ItemBlock
            ItemBlock tntItem = (ItemBlock)Item.itemRegistry.getObjectById(tntID);
            ReflectionHelper.setFinalField(ItemBlock.class, "field_150939_a", tntItem, ModBlocks.replaced_tnt);
            
            //Replace it in the Blocks class
            ReflectionHelper.setFinalField(Blocks.class, "tnt", null, ModBlocks.replaced_tnt);
            LogHelper.info("Replaced Vanilla TNT");
        } catch(Exception ex) {
            LogHelper.log(Level.ERROR, ex, "Failed to replace Vanilla TNT!");
        }
    }
}

@@ -531,64 +531,8 @@
 p;%0A%0A
-import cpw.mods.fml.common.ObfuscationReflectionHelper;%0A
 impo
@@ -1536,35 +1536,46 @@
 ap intMap = 
+(
 Ob
-fuscation
+jectIntIdentityMap)
 ReflectionHe
@@ -1578,36 +1578,29 @@
 onHelper.get
-PrivateValue
+Field
 (RegistryNam
@@ -1613,37 +1613,16 @@
 d.class,
- Block.blockRegistry,
  %22underl
@@ -1642,24 +1642,27 @@
 p%22, 
-%22field_148759_a%22
+Block.blockRegistry
 );%0A 
@@ -1819,19 +1819,15 @@
 p = 
-Obfuscation
+(BiMap)
 Refl
@@ -1846,20 +1846,13 @@
 .get
-PrivateValue
+Field
 (Reg
@@ -1870,16 +1870,35 @@
 .class, 
+%22registryObjects%22, 
 Block.bl
@@ -1912,44 +1912,8 @@
 stry
-, %22registryObjects%22, %22field_82596_a%22
 );%0A 

Revert "Use ObfuscationReflectionHelper"