package de.mrmysteri0us.dbmanager.config;

import de.mrmysteri0us.dbmanager.DBManagerPlugin;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by robin on 01/11/2014
 */
public class Config {
    private DBManagerPlugin     plugin;
    private File                configFile;
    private Yaml                configYaml;
    private Map<String, String> configMap;

    public Config(File configFile) {
        plugin = DBManagerPlugin.getInstance();
        this.configFile = configFile;
        this.configYaml = new Yaml();

        if(!configFile.exists()) {
            plugin.getLog().info("Could not find config. Creating file.");

            try {
                // noinspection ResultOfMethodCallIgnored
                configFile.createNewFile();
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void load() throws IOException {
        FileReader reader = new FileReader(configFile);
        Object data = configYaml.load(reader);
        reader.close();

        if(data instanceof HashMap) {
            configMap = (HashMap<String, String>) data;
        } else {
            plugin.getLog().error("Could not load config as a HashMap!");
        }
    }

    public void save() throws IOException {
        FileWriter writer = new FileWriter(configFile);
        configYaml.dump(configMap, writer);
        writer.close();
    }

    public byte getByte(String key) {
        return Byte.parseByte(getValue(key));
    }

    public short getShort(String key) {
        return Short.parseShort(getValue(key));
    }

    public int getInt(String key) {
        return Integer.parseInt(getValue(key));
    }

    public long getLong(String key) {
        return Long.parseLong(getValue(key));
    }

    public float getFloat(String key) {
        return Float.parseFloat(getValue(key));
    }

    public double getDouble(String key) {
        return Double.parseDouble(getValue(key));
    }

    public boolean getBoolean(String key) {
        return Boolean.parseBoolean(getValue(key));
    }

    public char getChar(String key) {
        return getValue(key).charAt(0);
    }

    public String getValue(String key) {
        return configMap.get(key);
    }

    public void setValue(String key, String value) {
        if(configMap.containsKey(key)) {
            configMap.remove(key);
        }

        configMap.put(key, value);
    }

    public Map<String, String> getMap() {
        return configMap;
    }
}

@@ -1,8 +1,1094 @@
+/*%0AThe MIT License (MIT)%0A%0ACopyright (c) 2014 MrMysteri0us%0A%0APermission is hereby granted, free of charge, to any person obtaining a copy%0Aof this software and associated documentation files (the %22Software%22), to deal%0Ain the Software without restriction, including without limitation the rights%0Ato use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0Acopies of the Software, and to permit persons to whom the Software is%0Afurnished to do so, subject to the following conditions:%0A%0AThe above copyright notice and this permission notice shall be included in%0Aall copies or substantial portions of the Software.%0A%0ATHE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0AIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0AFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0AAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0ALIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0AOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN%0ATHE SOFTWARE.%0A*/%0A%0A
 package 

Add license