/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.skelril.skree.system.dropclear.DropClearSystem;
import com.skelril.skree.system.modifier.ModifierSystem;
import com.skelril.skree.system.playerstate.PlayerStateSystem;
import com.skelril.skree.system.registry.block.CustomBlockSystem;
import com.skelril.skree.system.registry.item.CustomItemSystem;
import com.skelril.skree.system.shutdown.ShutdownSystem;
import com.skelril.skree.system.weather.WeatherCommandSystem;
import com.skelril.skree.system.world.WorldSystem;
import org.spongepowered.api.Game;
import org.spongepowered.api.event.Subscribe;
import org.spongepowered.api.event.state.PreInitializationEvent;
import org.spongepowered.api.event.state.ServerStartedEvent;
import org.spongepowered.api.plugin.Plugin;

import java.util.logging.Logger;

@Singleton
@Plugin(id = "Skree", name = "Skree", version = "1.0")
public class SkreePlugin {

    @Inject
    private Game game;

    @Inject
    private Logger logger;

    public static CustomItemSystem customItemSystem;
    public static CustomBlockSystem customBlockSystem;

    @Subscribe
    public void onPreInit(PreInitializationEvent event) {
        customItemSystem = new CustomItemSystem(this, game);
        customItemSystem.preInit();

        customBlockSystem = new CustomBlockSystem(this, game);
        customBlockSystem.preInit();
    }

    @Subscribe
    public void onServerStart(ServerStartedEvent event) {
        registerPrimaryHybridSystems();
        System.out.println(game.getPlatform().getExecutionType());
        switch (game.getPlatform().getExecutionType()) {
            case CLIENT:
                registerPrimaryClientSystems();
                break;
            case SERVER:
                registerPrimaryServerSystems();
                break;
        }

        logger.info("Skree Started! Kaw!");
    }

    private void registerPrimaryHybridSystems() {

    }

    private void registerPrimaryClientSystems() {

    }

    private void registerPrimaryServerSystems() {
        try {
            new DropClearSystem(this, game);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            new ModifierSystem(this, game);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            new ShutdownSystem(this, game);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            new WorldSystem(this, game);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            new WeatherCommandSystem(this, game);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        try {
            new PlayerStateSystem(this, game);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

@@ -231,16 +231,64 @@
 skree;%0A%0A
+import com.google.common.collect.ImmutableList;%0A
 import c
@@ -312,16 +312,16 @@
 Inject;%0A
-
 import c
@@ -1079,16 +1079,54 @@
 lugin;%0A%0A
+import java.lang.reflect.Constructor;%0A
 import j
@@ -2377,125 +2377,99 @@
     
-try %7B%0A            new DropClearSystem(this, game);%0A        %7D catch (Exception ex) %7B%0A            ex.printStackTrace();
+ImmutableList%3CClass%3E initialized = ImmutableList.of(%0A                DropClearSystem.class,
 %0A   
@@ -2469,34 +2469,32 @@
 ss,%0A        
-%7D%0A
         
 try %7B%0A      
@@ -2485,259 +2485,143 @@
     
-try %7B%0A            new ModifierSystem(this, game);%0A        %7D catch (Exception ex) %7B%0A            ex.printStackTrace();%0A        %7D%0A        try %7B%0A            new ShutdownSystem(this, game);%0A        %7D catch (Exception ex) %7B%0A            ex.printStackTrace();
+ModifierSystem.class,%0A                PlayerStateSystem.class,%0A                ShutdownSystem.class,%0A                WorldSystem.class,
 %0A   
@@ -2621,34 +2621,32 @@
 ss,%0A        
-%7D%0A
         
 try %7B%0A      
@@ -2637,132 +2637,96 @@
     
-try %7B%0A            new WorldSystem(this, game);%0A        %7D catch (Exception ex) %7B%0A            ex.printStackTrace();%0A    
+WeatherCommandSystem.class%0A        );%0A%0A        for (Class%3C?%3E entry : initialized) %7B%0A
     
-%7D%0A
     
@@ -2751,110 +2751,91 @@
     
-new WeatherCommandSystem(this, game);%0A        %7D catch (Exception ex) %7B%0A            ex.printStackTrace(
+    Constructor%3C?%3E constructor = entry.getConstructor(SkreePlugin.class, Game.class
 );%0A 
@@ -2845,18 +2845,16 @@
     
-%7D%0A
         
 try 
@@ -2853,47 +2853,31 @@
     
-try %7B%0A            new PlayerStateSystem
+constructor.newInstance
 (thi
@@ -2894,16 +2894,20 @@
         
+  
+  
 %7D catch 
@@ -2935,16 +2935,20 @@
         
+    
 ex.print
@@ -2957,24 +2957,38 @@
 ackTrace();%0A
+            %7D%0A
         %7D%0A  

Initialize via reflection