Fixed MySQL Database initiation.

Issue was sent on Spigot Discussion.
Fixed typo that was causing it.
This commit is contained in:
Rsl1122 2017-01-21 09:19:25 +02:00
parent 048c201bd6
commit 45279da849
5 changed files with 71 additions and 53 deletions

View File

@ -63,21 +63,21 @@ public class Plan extends JavaPlugin {
databases.add(new MySQLDB(this)); databases.add(new MySQLDB(this));
databases.add(new SQLiteDB(this)); databases.add(new SQLiteDB(this));
for (Database database : databases) { // for (Database database : databases) {
String name = database.getConfigName(); // String name = database.getConfigName();
//
ConfigurationSection section = getConfig().getConfigurationSection(name); // ConfigurationSection section = getConfig().getConfigurationSection(name);
//
if (section == null) { // if (section == null) {
section = getConfig().createSection(name); // section = getConfig().createSection(name);
} // }
//
database.getConfigDefaults(section); // database.getConfigDefaults(section);
//
if (section.getKeys(false).isEmpty()) { // if (section.getKeys(false).isEmpty()) {
getConfig().set(name, null); // getConfig().set(name, null);
} // }
} // }
getConfig().options().copyDefaults(true); getConfig().options().copyDefaults(true);

View File

@ -6,6 +6,8 @@ import org.bukkit.configuration.ConfigurationSection;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import org.bukkit.configuration.file.FileConfiguration;
import static org.bukkit.plugin.java.JavaPlugin.getPlugin;
/** /**
* *
@ -29,54 +31,55 @@ public class MySQLDB extends SQLDB {
*/ */
@Override @Override
protected Connection getNewConnection() { protected Connection getNewConnection() {
ConfigurationSection config = getConfigSection(); FileConfiguration config = getPlugin(Plan.class).getConfig();
setUserName(config.getString("tables.users"));
setLocationName(config.getString("tables.locations"));
setNicknamesName(config.getString("tables.nicknames"));
setGamemodetimesName(config.getString("tables.gamemodes"));
setIpsName(config.getString("tables.ips"));
setCommanduseName(config.getString("tables.commandusages"));
setServerdataName(config.getString("tables.serverdata"));
setUserName(config.getString("mysql.tables.users"));
setLocationName(config.getString("mysql.tables.locations"));
setNicknamesName(config.getString("mysql.tables.nicknames"));
setGamemodetimesName(config.getString("mysql.tables.gamemodetimes"));
setIpsName(config.getString("mysql.tables.ips"));
setCommanduseName(config.getString("mysql.tables.commandusages"));
setServerdataName(config.getString("mysql.tables.serverdata"));
try { try {
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://" + config.getString("host") + ":" + config.getString("port") + "/" + config.getString("database"); String url = "jdbc:mysql://" + config.getString("mysql.host") + ":" + config.getString("mysql.port") + "/" + config.getString("mysql.database");
return DriverManager.getConnection(url, config.getString("user"), config.getString("password")); return DriverManager.getConnection(url, config.getString("mysql.user"), config.getString("mysql.password"));
} catch (ClassNotFoundException | SQLException e) { } catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return null; return null;
} }
} }
private ConfigurationSection getSection(ConfigurationSection parent, String childName) { // private ConfigurationSection getSection(ConfigurationSection parent, String childName) {
ConfigurationSection child = parent.getConfigurationSection(childName); // ConfigurationSection child = parent.getConfigurationSection(childName);
//
if (child == null) { // if (child == null) {
child = parent.createSection(childName); // child = parent.createSection(childName);
} // }
//
return child; // return child;
} // }
@Override @Override
public void getConfigDefaults(ConfigurationSection section) { public void getConfigDefaults(ConfigurationSection section) {
section.addDefault("host", "localhost"); // section.addDefault("host", "localhost");
section.addDefault("port", 3306); // section.addDefault("port", 3306);
section.addDefault("user", "root"); // section.addDefault("user", "root");
section.addDefault("password", "minecraft"); // section.addDefault("password", "minecraft");
section.addDefault("database", "Plan"); // section.addDefault("database", "Plan");
//
ConfigurationSection tables = getSection(section, "tables"); // ConfigurationSection tables = getSection(section, "tables");
//
tables.addDefault("users", "plan_users"); // tables.addDefault("users", "plan_users");
tables.addDefault("locations", "plan_locations"); // tables.addDefault("locations", "plan_locations");
tables.addDefault("nicknames", "plan_nicknames"); // tables.addDefault("nicknames", "plan_nicknames");
tables.addDefault("gamemodetimes", "plan_gamemodetimes"); // tables.addDefault("gamemodetimes", "plan_gamemodetimes");
tables.addDefault("ips", "plan_ips"); // tables.addDefault("ips", "plan_ips");
tables.addDefault("commandusages", "plan_commandusages"); // tables.addDefault("commandusages", "plan_commandusages");
tables.addDefault("serverdata", "plan_serverdata"); // tables.addDefault("serverdata", "plan_serverdata");
} }
@Override @Override

View File

@ -27,7 +27,7 @@ import static org.bukkit.Bukkit.getOfflinePlayer;
public abstract class SQLDB extends Database { public abstract class SQLDB extends Database {
private final Plan plugin; final Plan plugin;
private final boolean supportsModification; private final boolean supportsModification;

View File

@ -47,3 +47,18 @@ Customization:
database: database:
type: sqlite type: sqlite
mysql:
host: localhost
port: 3306
user: root
password: minecraft
database: Plan
tables:
users: plan_users
locations: plan_locations
nicknames: plan_nicknames
gamemodetimes: plan_gamemodetimes
ips: plan_ips
commandusages: plan_commandusages
serverdata: plan_serverdata

View File

@ -1,7 +1,7 @@
name: Plan name: Plan
author: Rsl1122 author: Rsl1122
main: com.djrapitops.plan.Plan main: com.djrapitops.plan.Plan
version: 2.1.2 version: 2.1.3
commands: commands:
plan: plan: