Fixes issues #259 and #335

This commit is contained in:
TfT_02 2012-12-28 11:40:15 +01:00
parent e5cc06e822
commit 6f1ee88b78
2 changed files with 39 additions and 30 deletions

View File

@ -40,20 +40,29 @@ public class Config extends ConfigLoader {
/* mySQL */ /* mySQL */
public boolean getUseMySQL() { return config.getBoolean("MySQL.Enabled", false); } public boolean getUseMySQL() { return config.getBoolean("MySQL.Enabled", false); }
public String getMySQLTablePrefix() { return config.getString("MySQL.Database.TablePrefix", "mcmmo_"); } public String getMySQLTablePrefix() { return config.getString("MySQL.Database.TablePrefix", "mcmmo_"); }
public String getMySQLDatabaseName() { return config.getString("MySQL.Database.Name", "DatabaseName"); } public String getMySQLDatabaseName() { return getStringIncludingInts(config, "MySQL.Database.Name"); }
public String getMySQLUserName() { return config.getString("MySQL.Database.User_Name", "UserName"); } //Really should be labeled under MySQL.User_Name instead... public String getMySQLUserName() { return getStringIncludingInts(config, "MySQL.Database.User_Name"); }
public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); } public int getMySQLServerPort() { return config.getInt("MySQL.Server.Port", 3306); }
public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); } public String getMySQLServerName() { return config.getString("MySQL.Server.Address", "localhost"); }
public String getMySQLUserPassword() { public String getMySQLUserPassword() {
if (config.getString("MySQL.Database.User_Password", null) != null) { if (getStringIncludingInts(config, "MySQL.Database.User_Password") != null) {
return config.getString("MySQL.Database.User_Password", null); return getStringIncludingInts(config, "MySQL.Database.User_Password");
} }
else { else {
return ""; return "";
} }
} }
private static String getStringIncludingInts(ConfigurationSection cfg, String key) {
String str = cfg.getString(key);
if (str == null)
str = String.valueOf(cfg.getInt(key));
if (str == null)
str = "No value set for '" + key + "'";
return str;
}
/* Hardcore Mode */ /* Hardcore Mode */
public boolean getHardcoreEnabled() { return config.getBoolean("Hardcore.Enabled", false); } public boolean getHardcoreEnabled() { return config.getBoolean("Hardcore.Enabled", false); }
public double getHardcoreDeathStatPenaltyPercentage() { return config.getDouble("Hardcore.Death_Stat_Loss_Penalty_Percentage", 75); } public double getHardcoreDeathStatPenaltyPercentage() { return config.getDouble("Hardcore.Death_Stat_Loss_Penalty_Percentage", 75); }

View File

@ -17,20 +17,20 @@ import com.gmail.nossr50.runnables.SQLReconnect;
public class Database { public class Database {
private static Config configInstance = Config.getInstance(); private static Config configInstance = Config.getInstance();
private static String connectionString;
private static String connectionString = "jdbc:mysql://" + configInstance.getMySQLServerName() + ":" + configInstance.getMySQLServerPort() + "/" + configInstance.getMySQLDatabaseName() + "?user=" + configInstance.getMySQLUserName() + "&password=" + configInstance.getMySQLUserPassword();
private static String tablePrefix = configInstance.getMySQLTablePrefix(); private static String tablePrefix = configInstance.getMySQLTablePrefix();
private static Connection connection = null; private static Connection connection = null;
private static mcMMO plugin = null; private static mcMMO plugin = null;
// Scale waiting time by this much per failed attempt // Scale waiting time by this much per failed attempt
private static final double SCALING_FACTOR = 5; private static final double SCALING_FACTOR = 10;
// Minimum wait in nanoseconds (default 500ms) // Minimum wait in nanoseconds (default 500ms)
private static final long MIN_WAIT = 500*100000L; private static final long MIN_WAIT = 500L*1000000L;
// Maximum time to wait between reconnects (default 5 minutes) // Maximum time to wait between reconnects (default 5 minutes)
private static final long MAX_WAIT = 5*60000000000L; private static final long MAX_WAIT = 5L * 60L * 1000L * 1000000L;
// How long to wait when checking if connection is valid (default 3 seconds) // How long to wait when checking if connection is valid (default 3 seconds)
private static final int VALID_TIMEOUT = 3; private static final int VALID_TIMEOUT = 3;
@ -50,28 +50,27 @@ public class Database {
* Attempt to connect to the mySQL database. * Attempt to connect to the mySQL database.
*/ */
public static void connect() { public static void connect() {
try { connectionString = "jdbc:mysql://" + configInstance.getMySQLServerName() + ":" + configInstance.getMySQLServerPort() + "/" + configInstance.getMySQLDatabaseName();
System.out.println("[mcMMO] Attempting connection to MySQL..."); try {
mcMMO.p.getLogger().info("Attempting connection to MySQL...");
// Force driver to load if not yet loaded // Force driver to load if not yet loaded
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
Properties connectionProperties = new Properties(); Properties connectionProperties = new Properties();
connectionProperties.put("autoReconnect", "false"); connectionProperties.put("user", configInstance.getMySQLUserName());
connectionProperties.put("maxReconnects", "0"); connectionProperties.put("password", configInstance.getMySQLUserPassword());
connection = DriverManager.getConnection(connectionString, connectionProperties); connectionProperties.put("autoReconnect", "false");
connectionProperties.put("maxReconnects", "0");
connection = DriverManager.getConnection(connectionString, connectionProperties);
System.out.println("[mcMMO] Connection to MySQL was a success!"); mcMMO.p.getLogger().info("Connection to MySQL was a success!");
} } catch (SQLException ex) {
catch (SQLException ex) { connection = null;
connection = null; if(reconnectAttempt == 0 || reconnectAttempt >= 11) mcMMO.p.getLogger().info("Connection to MySQL failed!");
System.out.println("[mcMMO] Connection to MySQL failed!"); } catch (ClassNotFoundException ex) {
ex.printStackTrace(); connection = null;
printErrors(ex); if(reconnectAttempt == 0 || reconnectAttempt >= 11) mcMMO.p.getLogger().info("MySQL database driver not found!");
} catch (ClassNotFoundException ex) { }
connection = null;
System.out.println("[mcMMO] MySQL database driver not found!");
ex.printStackTrace();
}
} }
/** /**
@ -156,6 +155,7 @@ public class Database {
PreparedStatement statement = null; PreparedStatement statement = null;
try { try {
if(!checkConnected()) return;
statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql);
resultSet = statement.executeQuery(); resultSet = statement.executeQuery();
@ -172,12 +172,12 @@ public class Database {
catch (SQLException ex) { catch (SQLException ex) {
switch (update) { switch (update) {
case BLAST_MINING: case BLAST_MINING:
System.out.println("Updating mcMMO MySQL tables for Blast Mining..."); mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;"); write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;");
break; break;
case FISHING: case FISHING:
System.out.println("Updating mcMMO MySQL tables for Fishing..."); mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing...");
write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;"); write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;"); write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
break; break;