package com.forgeessentials.worldedit.compat;

import com.forgeessentials.core.ForgeEssentials;
import com.forgeessentials.core.commands.ForgeEssentialsCommandBase;
import com.forgeessentials.core.compat.Environment;
import com.forgeessentials.core.moduleLauncher.FEModule;
import com.forgeessentials.core.moduleLauncher.ModuleLauncher;
import com.forgeessentials.util.FunctionHelper;
import com.forgeessentials.util.OutputHandler;
import com.forgeessentials.util.PlayerInfo;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.event.platform.PlatformReadyEvent;
import com.sk89q.worldedit.forge.ForgeWorldEdit;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLServerStartedEvent;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
import cpw.mods.fml.common.event.FMLServerStoppingEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.common.MinecraftForge;

import java.io.File;

@FEModule(name = "WEIntegrationTools", parentMod = ForgeEssentials.class)
public class WEIntegration {

    protected static int syncInterval;
    private static boolean disable;
    private FEPlatform platform;

    @FEModule.ModuleDir
    public static File moduleDir;

    private boolean getDevOverride()
    {
        String prop = System.getProperty("forgeessentials.developermode.we");
        if (prop != null && prop.equals("true"))
        { // FOR DEVS ONLY! THAT IS WHY IT IS A PROPERTY!!!

            OutputHandler.felog.severe("Developer mode has been enabled, things may break.");
            return true;
        }
        else
        {
            return false;
        }
    }

    @SubscribeEvent
    public void load(FMLInitializationEvent e)
    {
        if (getDevOverride())
        {
            disable = true;
            return;
        }

        if (!Environment.hasWorldEdit)
        {
            OutputHandler.felog.severe("You cannot run the FE integration tools for WorldEdit without installing WorldEdit Forge.");
            ModuleLauncher.instance.unregister("WEIntegrationTools");
            return;
        }
        ForgeEssentials.worldEditCompatilityPresent = true;
        PlayerInfo.selectionProvider = new WESelectionHandler();
    }

    @SubscribeEvent
    public void postLoad(FMLPostInitializationEvent e)
    {
        if (disable)
        {
            OutputHandler.felog.severe("Requested to force-disable WorldEdit.");
            if (Loader.isModLoaded("WorldEdit"))
                MinecraftForge.EVENT_BUS.unregister(ForgeWorldEdit.inst); //forces worldedit forge NOT to load
            ModuleLauncher.instance.unregister("WEIntegrationTools");
        }

    }

    @SubscribeEvent
    public void serverStart(FMLServerStartingEvent e)
    {
        this.platform = new FEPlatform();
        WorldEdit.getInstance().getPlatformManager().register(platform);

    }

    @SubscribeEvent
    public void serverStarted(FMLServerStartedEvent e)
    {
        for (ForgeEssentialsCommandBase cmd : FEPlatform.commands)
        {
            FunctionHelper.registerServerCommand(cmd);
        }
        WorldEdit.getInstance().getEventBus().post(new PlatformReadyEvent());
    }

    @SubscribeEvent
    public void serverStopping(FMLServerStoppingEvent e)
    {
        WorldEdit.getInstance().getPlatformManager().unregister(platform);
    }

}

@@ -484,409 +484,529 @@
 com.
-sk89q.worldedit.WorldEdit;%0Aimport com.sk89q.worldedit.event.platform.PlatformReadyEvent;%0Aimport com.sk89q.worldedit.forge.ForgeWorldEdit;%0Aimport cpw.mods.fml.common.Loader;%0Aimport cpw.mods.fml.common.event.FMLInitializationEvent;%0Aimport cpw.mods.fml.common.event.FMLPostInitializationEvent;%0Aimport cpw.mods.fml.common.event.FMLServerStartedEvent;%0Aimport cpw.mods.fml.common.event.FMLServerStartingEven
+forgeessentials.util.events.FEModuleEvent.FEModuleInitEvent;%0Aimport com.forgeessentials.util.events.FEModuleEvent.FEModulePostInitEvent;%0Aimport com.forgeessentials.util.events.FEModuleEvent.FEModuleServerInitEvent;%0Aimport com.forgeessentials.util.events.FEModuleEvent.FEModuleServerPostInitEvent;%0Aimport com.forgeessentials.util.events.FEModuleEvent.FEModuleServerStopEvent;%0Aimport com.sk89q.worldedit.WorldEdit;%0Aimport com.sk89q.worldedit.event.platform.PlatformReadyEvent;%0Aimport com.sk89q.worldedit.forge.ForgeWorldEdi
 t;%0Ai
@@ -1035,36 +1035,14 @@
 mon.
-event.FMLServerStoppingEvent
+Loader
 ;%0Aim
@@ -1911,24 +1911,19 @@
 ad(F
-MLInitialization
+EModuleInit
 Even
@@ -2496,18 +2496,23 @@
 ad(F
-ML
+EModule
 PostInit
 iali
@@ -2511,18 +2511,8 @@
 Init
-ialization
 Even
@@ -2935,24 +2935,25 @@
 rt(F
-MLServerStarting
+EModuleServerInit
 Even
@@ -3141,23 +3141,29 @@
 ed(F
-MLServerStarted
+EModuleServerPostInit
 Even
@@ -3456,10 +3456,15 @@
 ng(F
-ML
+EModule
 Serv
@@ -3469,20 +3469,16 @@
 rverStop
-ping
 Event e)

Fix weintegration derp