/*
 * Caveworld
 *
 * Copyright (c) 2014 kegare
 * https://github.com/kegare
 *
 * This mod is distributed under the terms of the Minecraft Mod Public License 1.0, or MMPL.
 * Please check the contents of the license located in http://www.mod-buildcraft.com/MMPL-1.0.txt
 */

package com.kegare.caveworld.core;

import static com.kegare.caveworld.core.Caveworld.*;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.MinecraftForge;

import com.kegare.caveworld.block.CaveBlock;
import com.kegare.caveworld.handler.CaveConnectionHandler;
import com.kegare.caveworld.handler.CaveEventHooks;
import com.kegare.caveworld.handler.CaveFuelHandler;
import com.kegare.caveworld.handler.CavePacketHandler;
import com.kegare.caveworld.handler.CavePlayerTracker;
import com.kegare.caveworld.plugin.CaveModPluginManager;
import com.kegare.caveworld.plugin.mceconomy.MCEconomyPlugin;
import com.kegare.caveworld.plugin.thaumcraft.ThaumcraftPlugin;
import com.kegare.caveworld.proxy.CommonProxy;
import com.kegare.caveworld.util.Version;
import com.kegare.caveworld.world.WorldProviderCaveworld;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.Mod.Metadata;
import cpw.mods.fml.common.ModMetadata;
import cpw.mods.fml.common.SidedProxy;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod
(
	modid = MODID,
	acceptedMinecraftVersions = "[1.6.4,)"
)
@NetworkMod
(
	clientSideRequired = true,
	serverSideRequired = false,
	channels = {"cw:config.sync", "cw:dim.sync", "cw:biome.sync", "cw:ore.sync", "cw:mining.sync", "cw:cavemusic"},
	packetHandler = CavePacketHandler.class,
	connectionHandler = CaveConnectionHandler.class
)
public class Caveworld
{
	public static final String
	MODID = "kegare.caveworld",
	PACKAGE_NAME = "com.kegare.caveworld";

	@Metadata(MODID)
	public static ModMetadata metadata;

	@SidedProxy(modId = MODID, clientSide = PACKAGE_NAME + ".proxy.ClientProxy", serverSide = PACKAGE_NAME + ".proxy.CommonProxy")
	public static CommonProxy proxy;

	@EventHandler
	public void preInit(FMLPreInitializationEvent event)
	{
		Version.versionCheck();

		Config.buildConfig();

		CaveBlock.initialize();
		CaveBlock.register();

		CaveAchievementList.initialize();
		CaveAchievementList.register();

		GameRegistry.registerFuelHandler(new CaveFuelHandler());
		GameRegistry.registerPlayerTracker(new CavePlayerTracker());
	}

	@EventHandler
	public void init(FMLInitializationEvent event)
	{
		proxy.registerRenderers();
		proxy.registerRecipes();

		DimensionManager.registerProviderType(Config.dimensionCaveworld, WorldProviderCaveworld.class, true);
		DimensionManager.registerDimension(Config.dimensionCaveworld, Config.dimensionCaveworld);

		MinecraftForge.EVENT_BUS.register(new CaveEventHooks());

		CaveModPluginManager.registerPlugin(MCEconomyPlugin.class);
		CaveModPluginManager.registerPlugin(ThaumcraftPlugin.class);
		CaveModPluginManager.initPlugins();
	}

	@EventHandler
	public void postInit(FMLPostInitializationEvent event)
	{
		CaveBiomeManager.loadCaveBiomes();
		CaveOreManager.loadCaveOres();
	}

	@EventHandler
	public void serverStarting(FMLServerStartingEvent event)
	{
		event.registerServerCommand(new CommandCaveworld());

		if (event.getSide().isServer() && (Version.DEV_DEBUG || Config.versionNotify && Version.isOutdated()))
		{
			event.getServer().logInfo("A new Caveworld version is available : " + Version.getLatest());
		}
	}
}