/*
 * DefaultConfigurator.java
 *
 * Copyright (c) 2014 Graham Howden <graham_howden1 at yahoo.co.uk>.
 *
 * This file is part of PluginFramework.
 *
 * PluginFramework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PluginFramework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PluginFramework.  If not, see <http ://www.gnu.org/licenses/>.
 */

package com.publicuhc.configuration;

import com.google.inject.Inject;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class DefaultConfigurator implements Configurator {

    private final Plugin m_plugin;
    private final Map<String, FileConfiguration> m_configs = new HashMap<String, FileConfiguration>();

    @Inject
    public DefaultConfigurator(Plugin plugin) {
        m_plugin = plugin;
    }

    @Override
    public FileConfiguration getConfig(String id) {
        FileConfiguration config = m_configs.get(id);
        if (config == null) {
            try {
                config = loadFromFile(id + ".yml", true);
                m_configs.put(id, config);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
        return config;
    }

    @Override
    public void saveConfig(String id) throws IOException {
        FileConfiguration configuration = m_configs.get(id);
        if (configuration != null) {
            configuration.save(id + ".yml");
        }
    }

    /**
     * Load the config from the data directory and save it
     *
     * @param path        the path to load
     * @param setDefaults whether to set default values or not
     * @return the saved config
     * @throws IllegalArgumentException - if saving from the JAR failed after failed load from the data dir
     * @throws java.io.IOException      - if failed to save the config after options
     */
    protected FileConfiguration loadFromFile(String path, boolean setDefaults) throws IOException {
        File configFile = new File(m_plugin.getDataFolder(), path);
        if (!configFile.exists()) {
            saveFromJar(path, path, false);
            configFile = new File(m_plugin.getDataFolder(), path);
        }
        FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
        if (setDefaults) {
            // Look for defaults in the jar
            InputStream defConfigStream = m_plugin.getResource(path);
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                config.setDefaults(defConfig);
            }
            config.save(path);
        }
        return config;
    }

    /**
     * Save the file from the jar to the data directory
     * The resource is saved into the plugin's data folder using the same hierarchy as the .jar file (subdirectories are preserved).
     *
     * @param replace  if true, the embedded resource will overwrite the contents of an existing file.
     * @param path     the file in the jar
     * @param filename the file name to save as
     * @throws IllegalArgumentException - if the resource path is null, empty, or points to a nonexistent resource.
     */
    protected void saveFromJar(String path, String filename, boolean replace) {
        m_plugin.saveResource(path, false);
    }
}

@@ -2732,32 +2732,88 @@
 older(), path);%0A
+        System.out.print(configFile.getAbsoluteFile());%0A
         if (!con
@@ -2861,22 +2861,16 @@
 ar(path,
- path,
  false);
@@ -3400,20 +3400,44 @@
 ig.save(
-path
+configFile.getAbsoluteFile()
 );%0A     
@@ -3825,56 +3825,8 @@
 jar%0A
-     * @param filename the file name to save as%0A
     
@@ -3992,25 +3992,8 @@
 ath,
- String filename,
  boo
@@ -4040,28 +4040,30 @@
 ource(path, 
-fals
+replac
 e);%0A    %7D%0A%7D%0A

remove redundant parameter save to full directory not just base