package com.simplyian.easydb;

import com.simplyian.easydb.command.DBReloadCommand;
import java.util.logging.Level;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * EasyDBPlugin Main class.
 */
public class EasyDBPlugin extends JavaPlugin {
    private Database db;

    @Override
    public void onEnable() {
        // Make sure the config has been made
        saveDefaultConfig();

        EasyDB.setInstance(this);
        reloadDb();

        getCommand("dbreload").setExecutor(new DBReloadCommand(this));

        if (!db.isValid()) {
            getLogger().log(Level.WARNING, "Database credentials are invalid. Please check your credentials and run /dbreload.");
        } else {
            getLogger().log(Level.INFO, "Connected to database at " + db.getSource().getServerName() + ":" + db.getSource().getPort() + " successfully.");
        }
        getLogger().log(Level.INFO, "Plugin loaded.");
    }

    @Override
    public void onDisable() {
        EasyDB.setInstance(null);
        getLogger().log(Level.INFO, "Plugin unloaded.");
    }

    /**
     * Loads the database.
     */
    public void reloadDb() {
        ConfigurationSection s = getConfig().getConfigurationSection("db");
        if (s == null) {
            s = getConfig().createSection("db");
        }

        String dbUser = s.getString("user");
        String dbPass = s.getString("pass", "");
        String dbHost = s.getString("host");
        int dbPort = s.getInt("port");
        String dbName = s.getString("name");
        db = new Database(this, dbUser, dbPass, dbHost, dbPort, dbName);
    }

    /**
     * Gets the database.
     *
     * @return
     */
    public Database getDb() {
        return db;
    }
}

@@ -1356,20 +1356,20 @@
 tring db
-User
+Host
  = s.get
@@ -1380,12 +1380,12 @@
 ng(%22
-user
+host
 %22);%0A
@@ -1396,29 +1396,26 @@
     
-Str
 in
-g
+t
  dbP
-ass
+ort
  = s.get
 Stri
@@ -1414,24 +1414,17 @@
 .get
-String(%22pass%22, %22
+Int(%22port
 %22);%0A
@@ -1432,36 +1432,36 @@
        String db
-Host
+User
  = s.getString(%22
@@ -1452,36 +1452,36 @@
  = s.getString(%22
-host
+user
 %22);%0A        int 
@@ -1476,34 +1476,37 @@
         
+Str
 in
-t
+g
  dbP
-ort
+ass
  = s.get
 Int(%22por
@@ -1493,33 +1493,40 @@
 Pass = s.get
-Int(%22port
+String(%22pass%22, %22
 %22);%0A        

Reorder keys to match the config file.