package com.pg85.otg.customobjects;

import com.pg85.otg.customobjects.bo2.BO2Loader;
import com.pg85.otg.customobjects.bo3.BO3Loader;
import com.pg85.otg.customobjects.bo4.BO4Loader;
import com.pg85.otg.util.minecraft.defaults.TreeType;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * This class is the registry for the custom object types. It also stores
 * the global objects. World objects are stored in the WorldConfig class.
 * <p />
 * 
 * Open Terrain Generator supports multiple types of custom objects. By default, it
 * supports BO2s, BO3s and a number of special "objects" like trees and
 * UseWorld.
 * <p />
 * 
 * All those implement CustomObject. Plugin developers can register their
 * own custom object types. If you have a number of CustomObjects that you
 * want to register, just add your object to the global objects in the
 * onStart event using registerGlobalObject. If you have your own file
 * format, just use registerCustomObjectLoader(extension, loader).
 * <p />
 * 
 * Even trees are custom objects. If you want to add your own tree type, add
 * your tree to the global objects and make sure that it's canSpawnAsObject
 * returns false.
 * <p />
 * 
 * If your object implements StructuredCustomObject instead of CustomObject,
 * it will be able to have other objects attached to it, forming a
 * structure. As long as each individual object fits in a chunk, Terrain
 * Control will make sure that the structure gets spawned correctly, chunk
 * for chunk.
 */
public class CustomObjectManager
{
    private final Map<String, CustomObjectLoader> loaders;
    
    private final CustomObjectCollection globalCustomObjects;

    public CustomObjectManager()
    {
        // These are the actual lists, not just a copy.
        this.loaders = new HashMap<String, CustomObjectLoader>();

        // Register loaders
        registerCustomObjectLoader("bo2", new BO2Loader());
        registerCustomObjectLoader("bo3", new BO3Loader());
        registerCustomObjectLoader("bo4", new BO4Loader());
        registerCustomObjectLoader("bo4data", new BO4Loader());

        this.globalCustomObjects = new CustomObjectCollection();
        
        // Put some default CustomObjects
        for (TreeType type : TreeType.values())
        {
            registerGlobalObject(new TreeObject(type));
        }
    }
    
    public void reloadCustomObjectFiles()
    {
    	this.globalCustomObjects.reloadCustomObjectFiles();
    }

    /**
     * Registers a custom object loader. Register before the config files are
     * getting loaded, please!
     *
     * @param extension The extension of the file. This loader will be responsible for
     *                  all files with this extension.
     * @param loader    The loader.
     */
    private void registerCustomObjectLoader(String extension, CustomObjectLoader loader)
    {
        loaders.put(extension.toLowerCase(), loader);
    }

    /**
     * Register a global object.
     *
     * @param object The object to register.
     */
    public void registerGlobalObject(CustomObject object)
    {
    	globalCustomObjects.addLoadedGlobalObject(object);
    }
        
    /**
     * Gets all global objects.
     * @return The global objects.
     */
    public CustomObjectCollection getGlobalObjects()
    {
        return globalCustomObjects;
    }

    /**
     * Gets an unmodifiable view of all object loaders, indexed by the
     * lowercase extension without the dot (for example "bo3").
     * @return The loaders.
     */
    public Map<String, CustomObjectLoader> getObjectLoaders()
    {
        return Collections.unmodifiableMap(loaders);
    }    
    
    /**
     * Calls the {@link CustomObjectLoader#onShutdown()} method of each
     * loader, then unloads them.
     */
    public void shutdown()
    {
        for (CustomObjectLoader loader : loaders.values())
        {
            loader.onShutdown();
        }
        loaders.clear();
    }  
}

@@ -30,16 +30,41 @@
 jects;%0A%0A
+import com.pg85.otg.OTG;%0A
 import c
@@ -226,43 +226,48 @@
 otg.
-util.minecraft.defaults.TreeTyp
+logging.LogMarker;%0Aimport java.io.Fil
 e;%0A
-%0A
 impo
@@ -2219,24 +2219,31 @@
 ();%0A        
+       
 %0A        // 
@@ -2243,153 +2243,563 @@
     
-// Put some default CustomObjects%0A        for (TreeType type : TreeType.valu
+if(!OTG.getPluginConfig().developerMode)%0A        %7B%0A%09        new Thread() %7B %0A%09    %09%09public void run()%0A%09    %09%09%7B%0A%09    %09%09%09globalCustomObjects.indexGlobalObjectsFolder();%0A%09    %09%09%09%0A%09    %09%09    for(File file : OTG.getEngine().getWorldsDirectory().listFil
 es())%0A
+%09
     
+%09%09
     %7B%0A
+%09
     
+%09%09
     
-    registerGlobalObject(new TreeObject(type)
+%09if(file.isDirectory())%0A%09    %09%09    %09%7B%0A%09    %09%09    %09%09String worldName = file.getName();%0A%09    %09%09    %09%09globalCustomObjects.indexWorldObjectsFolder(worldName);%0A%09    %09%09    %09%7D%0A%09    %09%09    %7D%0A%09    %09%09    OTG.log(LogMarker.INFO, %22All CustomObject files indexed.%22);%0A%09    %09%09%7D%0A%09        %7D.start(
 );%0A 

Index GlobalObjects/WorldObjects at app start 2