package io.github.Cnly.Crafter.Crafter.framework.configs;

import io.github.Cnly.Crafter.Crafter.utils.ResourceUtils;

import java.io.File;
import java.io.IOException;

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

public abstract class AbstractConfigManager implements IConfigManager
{
    
    protected File file;
    protected AutoSaveTask autoSaveTask = null;
    protected JavaPlugin jp;
    
    /**
     * The constructor
     * 
     * @param file
     *            the config file
     * @param copyDefault
     *            whether to call this.copyDefaultConfig() automatically
     * @param jp
     *            the JavaPlugin used for resource files obtaining and task
     *            registering. If this is null, these functions will throw
     *            exceptions.
     */
    public AbstractConfigManager(File file, boolean copyDefault,
            JavaPlugin jp)
    {
        this.file = file;
        this.jp = jp;
        
        if (copyDefault && !file.exists())
            this.copyDefaultConfig();
        
    }
    
    @Override
    public AbstractConfigManager setJavaPlugin(JavaPlugin jp)
    {
        this.jp = jp;
        return this;
    }
    
    @Override
    public JavaPlugin getJavaPlugin()
    {
        return this.jp;
    }
    
    @Override
    public AbstractConfigManager copyDefaultConfig()
    {
        this.copyDefaultConfig("/" + this.file.getName());
        return this;
    }
    
    @Override
    public AbstractConfigManager copyDefaultConfig(String resourceLocation)
    {
        
        if (null == this.jp)
            throw new NullPointerException("JavaPlugin is null!");
        
        try
        {
            ResourceUtils.copyFromJar(jp, resourceLocation, this.file);
        }
        catch (IOException e)
        {
            throw new RuntimeException("Unable to copy default file", e);
        }
        return this;
    }
    
    @Override
    public boolean isAutoSaveSet()
    {
        return this.autoSaveTask != null;
    }
    
    @Override
    public void setAutoSaveInterval(int seconds)
    {
        
        if (null == this.jp)
            throw new NullPointerException("JavaPlugin is null!");
        
        if (seconds == 0)
        {// Turn it off!
        
            if (this.autoSaveTask != null)
            {
                this.autoSaveTask.cancel();
                this.autoSaveTask = null;
            }
            
        }
        else
        {
            
            this.autoSaveTask = new AutoSaveTask();
            this.autoSaveTask.runTaskTimer(jp, seconds * 20, seconds * 20);
            
        }
        
    }
    
    private class AutoSaveTask extends BukkitRunnable
    {
        
        @Override
        public void run()
        {
            AbstractConfigManager.this.save();
        }
        
    }
    
}

@@ -894,28 +894,16 @@
 Default,
-%0A           
  JavaPlu
@@ -902,32 +902,32 @@
  JavaPlugin jp)%0A
+
     %7B%0A        th
@@ -1016,16 +1016,16 @@
 ists())%0A
-
         
@@ -1066,32 +1066,517 @@
     %0A    %7D%0A    %0A
+    /**%0A     * Creates an empty file if there isn't one. Any directory that doesn't%0A     * exist will be created as well.%0A     */%0A    protected void initFile()%0A    %7B%0A        %0A        if (file.exists())%0A            return;%0A        %0A        file.getParentFile().mkdirs();%0A        try%0A        %7B%0A            file.createNewFile();%0A        %7D%0A        catch (IOException e)%0A        %7B%0A            throw new RuntimeException(%22Unable to create new empty file!%22, e);%0A        %7D%0A        %0A    %7D%0A    %0A
     @Override%0A  

Add method initFile() for AbstractConfigManager