// Path: src/main/java/myessentials/MyEssentialsCore.java
// @Mod(modid = "MyEssentials-Core", name = "MyEssentials-Core", version = "@VERSION@", dependencies = "required-after:Forge", acceptableRemoteVersions = "*")
// public class MyEssentialsCore {
//     @Instance("MyEssentials-Core")
//     public static MyEssentialsCore instance;
// 
//     public Local LOCAL;
//     public Logger LOG;
// 
//     @EventHandler
//     public void preInit(FMLPreInitializationEvent ev) {
// 
//         LOG = ev.getModLog();
//         Constants.CONFIG_FOLDER = ev.getModConfigurationDirectory().getPath() + "/MyEssentials-Core/";
//         Constants.DATABASE_FOLDER = ev.getModConfigurationDirectory().getParent() + "/databases/";
//         // Load Configs
//         Config.instance.init(Constants.CONFIG_FOLDER + "/Core.cfg", "MyEssentials-Core");
//         // REF: The localization can simply take the whole config instance to get the localization needed.
//         LOCAL = new Local(Constants.CONFIG_FOLDER + "/localization/", Config.instance.localization.get(), "/myessentials/localization/", MyEssentialsCore.class);
//         LocalManager.register(LOCAL, "myessentials");
// 
//         // Register handlers/trackers
//         FMLCommonHandler.instance().bus().register(PlayerTracker.instance);
//         MinecraftForge.EVENT_BUS.register(PlayerTracker.instance);
// 
//         FMLCommonHandler.instance().bus().register(ToolManager.instance);
//         MinecraftForge.EVENT_BUS.register(ToolManager.instance);
// 
//         FMLCommonHandler.instance().bus().register(SignManager.instance);
//         MinecraftForge.EVENT_BUS.register(SignManager.instance);
//     }
// }


import com.google.gson.Gson;
import myessentials.MyEssentialsCore;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.io.*;
import java.lang.reflect.Type;
import java.util.List;
package myessentials.json.api;



/**
 * An abstract class for all JSON configs.
 * Instantiating the gson and gsonType is needed.
 * Methods are usually overriden but called inside their overriden method.
 */
public abstract class JsonConfig<T, L extends List<T>> {

    /**
     * The path to the file used.
     */
    protected final String path, name;
    protected Gson gson;
    protected Type gsonType;

    public JsonConfig(String path, String name) {
        this.path = path;
        this.name = name;
    }

    protected abstract L newList();

    public void init() {
        init(newList());
    }

    /**
     * Initializes everything.
     */
    public void init(L items) {
        File file = new File(path);

        File parent = file.getParentFile();
        if (!parent.exists() && !parent.mkdirs()) {
            throw new IllegalStateException("Couldn't create dir: " + parent);
        }
        if (!file.exists() || file.isDirectory()) {
            create(items);
        } else {
            read();
        }
    }

    /**
     * Creates the file if it doesn't exist with the initial given items
     */
    public void create(L initialItems) {
        try {
            Writer writer = new FileWriter(path);
            gson.toJson(initialItems, gsonType, writer);
            writer.close();
// Path: src/main/java/myessentials/MyEssentialsCore.java
// @Mod(modid = "MyEssentials-Core", name = "MyEssentials-Core", version = "@VERSION@", dependencies = "required-after:Forge", acceptableRemoteVersions = "*")
// public class MyEssentialsCore {
//     @Instance("MyEssentials-Core")
//     public static MyEssentialsCore instance;
// 
//     public Local LOCAL;
//     public Logger LOG;
// 
//     @EventHandler
//     public void preInit(FMLPreInitializationEvent ev) {
// 
//         LOG = ev.getModLog();
//         Constants.CONFIG_FOLDER = ev.getModConfigurationDirectory().getPath() + "/MyEssentials-Core/";
//         Constants.DATABASE_FOLDER = ev.getModConfigurationDirectory().getParent() + "/databases/";
//         // Load Configs
//         Config.instance.init(Constants.CONFIG_FOLDER + "/Core.cfg", "MyEssentials-Core");
//         // REF: The localization can simply take the whole config instance to get the localization needed.
//         LOCAL = new Local(Constants.CONFIG_FOLDER + "/localization/", Config.instance.localization.get(), "/myessentials/localization/", MyEssentialsCore.class);
//         LocalManager.register(LOCAL, "myessentials");
// 
//         // Register handlers/trackers
//         FMLCommonHandler.instance().bus().register(PlayerTracker.instance);
//         MinecraftForge.EVENT_BUS.register(PlayerTracker.instance);
// 
//         FMLCommonHandler.instance().bus().register(ToolManager.instance);
//         MinecraftForge.EVENT_BUS.register(ToolManager.instance);
// 
//         FMLCommonHandler.instance().bus().register(SignManager.instance);
//         MinecraftForge.EVENT_BUS.register(SignManager.instance);
//     }
// }


// Path: src/main/java/myessentials/json/api/JsonConfig.java
import com.google.gson.Gson;
import myessentials.MyEssentialsCore;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.io.*;
import java.lang.reflect.Type;
import java.util.List;

package myessentials.json.api;



/**
 * An abstract class for all JSON configs.
 * Instantiating the gson and gsonType is needed.
 * Methods are usually overriden but called inside their overriden method.
 */
public abstract class JsonConfig<T, L extends List<T>> {

    /**
     * The path to the file used.
     */
    protected final String path, name;
    protected Gson gson;
    protected Type gsonType;

    public JsonConfig(String path, String name) {
        this.path = path;
        this.name = name;
    }

    protected abstract L newList();

    public void init() {
        init(newList());
    }

    /**
     * Initializes everything.
     */
    public void init(L items) {
        File file = new File(path);

        File parent = file.getParentFile();
        if (!parent.exists() && !parent.mkdirs()) {
            throw new IllegalStateException("Couldn't create dir: " + parent);
        }
        if (!file.exists() || file.isDirectory()) {
            create(items);
        } else {
            read();
        }
    }

    /**
     * Creates the file if it doesn't exist with the initial given items
     */
    public void create(L initialItems) {
        try {
            Writer writer = new FileWriter(path);
            gson.toJson(initialItems, gsonType, writer);
            writer.close();
            MyEssentialsCore.instance.LOG.info("Created new " + name + " file successfully!");