package com.hea3ven.tools.commonutils.mod;

import java.nio.file.Path;
import java.nio.file.Paths;

import net.minecraft.item.crafting.IRecipe;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.registry.GameRegistry;

import com.hea3ven.tools.commonutils.mod.config.ConfigManagerBuilder;

public abstract class ModInitializerCommon {

	public void onPreInitEvent(final ProxyModBase proxy, FMLPreInitializationEvent event) {
		registerConfig(proxy, event);
		proxy.registerEnchantments();
		registerBlocks(proxy);
		registerTileEntities(proxy);
		registerItems(proxy);
		registerCreativeTabs(proxy);
	}

	public void onInitEvent(ProxyModBase proxy) {
		registerNetworkPackets(proxy);
		registerRecipes(proxy);
		registerGuiHandlers(proxy);
		registerCommands(proxy);
	}

	public void onPostInitEvent(ProxyModBase proxy) {
	}

	private void registerConfig(ProxyModBase proxy, FMLPreInitializationEvent event) {
		proxy.registerConfig();
		ConfigManagerBuilder builder = proxy.cfgMgrBuilder;
		if (builder == null)
			return;
		Path configDir = Paths.get(event.getModConfigurationDirectory().toString());
		proxy.cfgMgr = builder.build(proxy.getModId(), configDir);
		if(proxy.cfgMgr != null) {
			MinecraftForge.EVENT_BUS.register(proxy.cfgMgr);
			proxy.cfgMgr.onConfigChanged(null);
		}
	}

	private void registerBlocks(ProxyModBase proxy) {
		proxy.registerBlocks();
		for (InfoBlock block : proxy.blocks) {
			GameRegistry.registerBlock(block.getBlock(), block.getItemCls(), block.getName(),
					(block.getItemArgs() != null) ? block.getItemArgs() : new Object[0]);
		}
	}

	private void registerTileEntities(ProxyModBase proxy) {
		proxy.registerTileEntities();
		for (InfoTileEntity tile : proxy.tiles) {
			GameRegistry.registerTileEntity(tile.getTileClass(), tile.getName());
		}
	}

	private void registerItems(ProxyModBase proxy) {
		proxy.registerItems();
		for (InfoItem item : proxy.items) {
			GameRegistry.registerItem(item.getItem(), item.getName());
		}
	}

	private void registerCreativeTabs(ProxyModBase proxy) {
		proxy.registerCreativeTabs();
	}

	private void registerNetworkPackets(ProxyModBase proxy) {
		proxy.registerNetworkPackets();
	}

	private void registerRecipes(ProxyModBase proxy) {
		proxy.registerRecipes();
		for (IRecipe recipe : proxy.recipes) {
			GameRegistry.addRecipe(recipe);
		}
	}

	private void registerGuiHandlers(ProxyModBase proxy) {
		proxy.registerGuis();
		NetworkRegistry.INSTANCE.registerGuiHandler(proxy.getModId(), proxy.getGuiHandler());
	}

	private void registerCommands(ProxyModBase proxy) {
		proxy.registerCommands();
	}
}

@@ -118,29 +118,159 @@
 aft.
-item.crafting.IRecipe
+enchantment.Enchantment;%0Aimport net.minecraft.init.Enchantments;%0Aimport net.minecraft.item.crafting.IRecipe;%0Aimport net.minecraft.util.ResourceLocation
 ;%0A%0Ai
@@ -750,22 +750,16 @@
 ent);%0A%09%09
-proxy.
 register
@@ -771,16 +771,21 @@
 ntments(
+proxy
 );%0A%09%09reg
@@ -1458,16 +1458,17 @@
 r);%0A%09%09if
+ 
 (proxy.c
@@ -1536,16 +1536,16 @@
 fgMgr);%0A
-
 %09%09%09proxy
@@ -1575,32 +1575,433 @@
 (null);%0A%09%09%7D%0A%09%7D%0A%0A
+%09// TODO: Fix when there is a proper way to register%0A%09private static int nextId = 100;%0A%09private void registerEnchantments(ProxyModBase proxy) %7B%0A%09%09proxy.registerEnchantments();%0A%09%09for (InfoEnchantment ench : proxy.enchantments) %7B%0A%09%09%09ResourceLocation key = new ResourceLocation(ench.getDomain(), ench.getName());%0A%09%09%09Enchantment.enchantmentRegistry.register(nextId++, key, ench.getEnchantment());%0A%09%09%7D%0A%09%7D%0A%0A
 %09private void re

Implement registering enchantments