Update code style

This commit is contained in:
nossr50
2019-04-24 22:52:53 -07:00
parent dc758a6dfc
commit 02a69cbb05
334 changed files with 4660 additions and 5158 deletions

View File

@ -43,13 +43,13 @@ public interface DatabaseManager {
boolean saveUser(PlayerProfile profile);
/**
* Retrieve leaderboard info.
*
* @param skill The skill to retrieve info on
* @param pageNumber Which page in the leaderboards to retrieve
* @param statsPerPage The number of stats per page
* @return the requested leaderboard information
*/
* Retrieve leaderboard info.
*
* @param skill The skill to retrieve info on
* @param pageNumber Which page in the leaderboards to retrieve
* @param statsPerPage The number of stats per page
* @return the requested leaderboard information
*/
List<PlayerStat> readLeaderboard(PrimarySkillType skill, int pageNumber, int statsPerPage);
/**
@ -67,20 +67,19 @@ public interface DatabaseManager {
* Add a new user to the database.
*
* @param playerName The name of the player to be added to the database
* @param uuid The uuid of the player to be added to the database
* @param uuid The uuid of the player to be added to the database
*/
void newUser(String playerName, UUID uuid);
/**
* Load a player from the database.
*
* @deprecated replaced by {@link #loadPlayerProfile(String playerName, UUID uuid, boolean createNew)}
*
* @param playerName The name of the player to load from the database
* @param createNew Whether to create a new record if the player is not
* found
* @param createNew Whether to create a new record if the player is not
* found
* @return The player's data, or an unloaded PlayerProfile if not found
* and createNew is false
* and createNew is false
* @deprecated replaced by {@link #loadPlayerProfile(String playerName, UUID uuid, boolean createNew)}
*/
@Deprecated
PlayerProfile loadPlayerProfile(String playerName, boolean createNew);
@ -97,11 +96,11 @@ public interface DatabaseManager {
* Load a player from the database. Attempt to use uuid, fall back on playername
*
* @param playerName The name of the player to load from the database
* @param uuid The uuid of the player to load from the database
* @param createNew Whether to create a new record if the player is not
* found
* @param uuid The uuid of the player to load from the database
* @param createNew Whether to create a new record if the player is not
* found
* @return The player's data, or an unloaded PlayerProfile if not found
* and createNew is false
* and createNew is false
*/
PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean createNew);

View File

@ -10,12 +10,10 @@ public class DatabaseManagerFactory {
if (customManager != null) {
try {
return createDefaultCustomDatabaseManager();
}
catch (Exception e) {
} catch (Exception e) {
mcMMO.p.debug("Could not create custom database manager");
e.printStackTrace();
}
catch (Throwable e) {
} catch (Throwable e) {
mcMMO.p.debug("Failed to create custom database manager");
e.printStackTrace();
}
@ -25,6 +23,10 @@ public class DatabaseManagerFactory {
return mcMMO.getMySQLConfigSettings().isMySQLEnabled() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
}
public static Class<? extends DatabaseManager> getCustomDatabaseManagerClass() {
return customManager;
}
/**
* Sets the custom DatabaseManager class for mcMMO to use. This should be
* called prior to mcMMO enabling.
@ -37,7 +39,6 @@ public class DatabaseManagerFactory {
* versions.
*
* @param clazz the DatabaseManager class to use
*
* @throws IllegalArgumentException if the provided class does not have
* an empty constructor
*/
@ -45,16 +46,11 @@ public class DatabaseManagerFactory {
try {
clazz.getConstructor();
customManager = clazz;
}
catch (Throwable e) {
} catch (Throwable e) {
throw new IllegalArgumentException("Provided database manager class must have an empty constructor", e);
}
}
public static Class<? extends DatabaseManager> getCustomDatabaseManagerClass() {
return customManager;
}
public static DatabaseManager createDatabaseManager(DatabaseType type) {
switch (type) {
case FLATFILE:
@ -66,8 +62,7 @@ public class DatabaseManagerFactory {
case CUSTOM:
try {
return createDefaultCustomDatabaseManager();
}
catch (Throwable e) {
} catch (Throwable e) {
e.printStackTrace();
}

View File

@ -16,13 +16,52 @@ import java.io.*;
import java.util.*;
public final class FlatfileDatabaseManager implements DatabaseManager {
private static final Object fileWritingLock = new Object();
public static int USERNAME = 0;
public static int SKILLS_MINING = 1;
public static int EXP_MINING = 4;
public static int SKILLS_WOODCUTTING = 5;
public static int EXP_WOODCUTTING = 6;
public static int SKILLS_REPAIR = 7;
public static int SKILLS_UNARMED = 8;
public static int SKILLS_HERBALISM = 9;
public static int SKILLS_EXCAVATION = 10;
public static int SKILLS_ARCHERY = 11;
public static int SKILLS_SWORDS = 12;
public static int SKILLS_AXES = 13;
public static int SKILLS_ACROBATICS = 14;
public static int EXP_REPAIR = 15;
public static int EXP_UNARMED = 16;
public static int EXP_HERBALISM = 17;
public static int EXP_EXCAVATION = 18;
public static int EXP_ARCHERY = 19;
public static int EXP_SWORDS = 20;
public static int EXP_AXES = 21;
public static int EXP_ACROBATICS = 22;
public static int SKILLS_TAMING = 24;
public static int EXP_TAMING = 25;
public static int COOLDOWN_BERSERK = 26;
public static int COOLDOWN_GIGA_DRILL_BREAKER = 27;
public static int COOLDOWN_TREE_FELLER = 28;
public static int COOLDOWN_GREEN_TERRA = 29;
public static int COOLDOWN_SERRATED_STRIKES = 30;
public static int COOLDOWN_SKULL_SPLITTER = 31;
public static int COOLDOWN_SUPER_BREAKER = 32;
public static int SKILLS_FISHING = 34;
public static int EXP_FISHING = 35;
public static int COOLDOWN_BLAST_MINING = 36;
public static int LAST_LOGIN = 37;
public static int HEALTHBAR = 38;
public static int SKILLS_ALCHEMY = 39;
public static int EXP_ALCHEMY = 40;
public static int UUID_INDEX = 41;
public static int SCOREBOARD_TIPS = 42;
public static int COOLDOWN_CHIMAERA_WING = 43;
private final HashMap<PrimarySkillType, List<PlayerStat>> playerStatHash = new HashMap<>();
private final List<PlayerStat> powerLevels = new ArrayList<>();
private final File usersFile;
private long lastUpdate = 0;
private long updateWaitTime;
private final File usersFile;
private static final Object fileWritingLock = new Object();
protected FlatfileDatabaseManager() {
updateWaitTime = mcMMO.getConfigManager().getConfigDatabase().getConfigDatabaseFlatFile().getLeaderboardUpdateIntervalMinutes() * (1000 * 60);
usersFile = new File(mcMMO.getUsersFilePath());
@ -65,8 +104,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// If they're still around, rewrite them to the file.
if (!powerless) {
writer.append(line).append("\r\n");
}
else {
} else {
purgedUsers++;
}
}
@ -74,24 +112,20 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Write the new file
out = new FileWriter(usersFilePath);
out.write(writer.toString());
}
catch (IOException e) {
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -125,8 +159,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
boolean rewrite = false;
try {
lastPlayed = Long.parseLong(character[37]) * Misc.TIME_CONVERSION_FACTOR;
}
catch (NumberFormatException e) {
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (lastPlayed == 0) {
@ -137,15 +170,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
if (currentTime - lastPlayed > PURGE_TIME) {
removedPlayers++;
}
else {
} else {
if (rewrite) {
// Rewrite their data with a valid time
character[37] = Long.toString(lastPlayed);
String newLine = org.apache.commons.lang.StringUtils.join(character, ":");
writer.append(newLine).append("\r\n");
}
else {
} else {
writer.append(line).append("\r\n");
}
}
@ -154,24 +185,20 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Write the new file
out = new FileWriter(usersFilePath);
out.write(writer.toString());
}
catch (IOException e) {
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -207,24 +234,20 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -258,8 +281,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
String[] character = line.split(":");
if (!(uuid != null && character[UUID_INDEX].equalsIgnoreCase(uuid.toString())) && !character[USERNAME].equalsIgnoreCase(playerName)) {
writer.append(line).append("\r\n");
}
else {
} else {
// Otherwise write the new player information
writeUserToLine(profile, playerName, uuid, writer);
wroteUser = true;
@ -269,8 +291,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
/*
* If we couldn't find the user in the DB we need to add him
*/
if(!wroteUser)
{
if (!wroteUser) {
writeUserToLine(profile, playerName, uuid, writer);
}
@ -278,25 +299,21 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath);
out.write(writer.toString());
return true;
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
return false;
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -431,16 +448,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Add more in the same format as the line above
out.newLine();
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
finally {
} finally {
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -502,18 +516,15 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
newUser(playerName, uuid);
return new PlayerProfile(playerName, uuid, true);
}
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
finally {
} finally {
// I have no idea why it's necessary to inline tryClose() here, but it removes
// a resource leak warning, and I'm trusting the compiler on this one.
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -545,23 +556,19 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
try {
destination.saveUser(loadFromLine(character));
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
convertedUsers++;
Misc.printProgress(convertedUsers, progressInterval, startMillis);
}
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -602,25 +609,21 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
} finally {
mcMMO.p.getLogger().info(i + " entries written while saving UUID for " + userName);
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -661,25 +664,21 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
}
catch (Exception e) {
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
} finally {
mcMMO.p.getLogger().info(i + " entries written while saving UUID batch");
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -704,16 +703,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
String[] character = line.split(":");
users.add(character[USERNAME]);
}
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -781,16 +777,13 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
putStat(powerLevels, playerName, powerLevel);
}
}
catch (Exception e) {
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString());
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -899,8 +892,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
continue;
}
//Level Cap
if(mcMMO.getPlayerLevelingSettings().isLevelCapEnabled(skill))
{
if (mcMMO.getPlayerLevelingSettings().isLevelCapEnabled(skill)) {
int cap = mcMMO.getPlayerLevelingSettings().getLevelCap(skill);
if (Integer.valueOf(character[index]) > cap) {
mcMMO.p.getLogger().warning("Truncating " + skill.getName() + " to configured max level for player " + character[USERNAME]);
@ -1005,11 +997,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
corrupted = true;
if (i == 37) {
character[i] = String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
}
else if (i == 38) {
} else if (i == 38) {
character[i] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
}
else {
} else {
character[i] = "0";
}
}
@ -1056,24 +1046,20 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Write the new file
out = new FileWriter(usersFilePath);
out.write(writer.toString());
}
catch (IOException e) {
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
@ -1095,8 +1081,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
try {
mcMMO.p.debug("Creating mcmmo.users file...");
new File(mcMMO.getUsersFilePath()).createNewFile();
}
catch (IOException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
@ -1124,16 +1109,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return statValue;
}
private class SkillComparator implements Comparator<PlayerStat> {
@Override
public int compare(PlayerStat o1, PlayerStat o2) {
return (o2.statVal - o1.statVal);
}
}
private PlayerProfile loadFromLine(String[] character) {
Map<PrimarySkillType, Integer> skills = getSkillMapFromLine(character); // Skill levels
Map<PrimarySkillType, Float> skillsXp = new EnumMap<>(PrimarySkillType.class); // Skill & XP
Map<PrimarySkillType, Integer> skills = getSkillMapFromLine(character); // Skill levels
Map<PrimarySkillType, Float> skillsXp = new EnumMap<>(PrimarySkillType.class); // Skill & XP
Map<SuperAbilityType, Integer> skillsDATS = new EnumMap<>(SuperAbilityType.class); // Ability & Cooldown
Map<UniqueDataType, Integer> uniquePlayerDataMap = new EnumMap<>(UniqueDataType.class);
MobHealthbarType mobHealthbarType;
@ -1170,30 +1148,26 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
try {
mobHealthbarType = MobHealthbarType.valueOf(character[HEALTHBAR]);
}
catch (Exception e) {
} catch (Exception e) {
mobHealthbarType = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType();
}
UUID uuid;
try {
uuid = UUID.fromString(character[UUID_INDEX]);
}
catch (Exception e) {
} catch (Exception e) {
uuid = null;
}
try {
scoreboardTipsShown = Integer.valueOf(character[SCOREBOARD_TIPS]);
}
catch (Exception e) {
} catch (Exception e) {
scoreboardTipsShown = 0;
}
try {
uniquePlayerDataMap.put(UniqueDataType.CHIMAERA_WING_DATS, Integer.valueOf(character[COOLDOWN_CHIMAERA_WING]));
}
catch (Exception e) {
} catch (Exception e) {
uniquePlayerDataMap.put(UniqueDataType.CHIMAERA_WING_DATS, 0);
}
@ -1225,7 +1199,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
}
@Override
public void onDisable() { }
public void onDisable() {
}
private int getSkillIndex(PrimarySkillType skill) {
switch (skill) {
@ -1257,50 +1232,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return SKILLS_WOODCUTTING;
default:
throw new RuntimeException("Primary Skills only");
}
}
public static int USERNAME = 0;
public static int SKILLS_MINING = 1;
public static int EXP_MINING = 4;
public static int SKILLS_WOODCUTTING = 5;
public static int EXP_WOODCUTTING = 6;
public static int SKILLS_REPAIR = 7;
public static int SKILLS_UNARMED = 8;
public static int SKILLS_HERBALISM = 9;
public static int SKILLS_EXCAVATION = 10;
public static int SKILLS_ARCHERY = 11;
public static int SKILLS_SWORDS = 12;
public static int SKILLS_AXES = 13;
public static int SKILLS_ACROBATICS = 14;
public static int EXP_REPAIR = 15;
public static int EXP_UNARMED = 16;
public static int EXP_HERBALISM = 17;
public static int EXP_EXCAVATION = 18;
public static int EXP_ARCHERY = 19;
public static int EXP_SWORDS = 20;
public static int EXP_AXES = 21;
public static int EXP_ACROBATICS = 22;
public static int SKILLS_TAMING = 24;
public static int EXP_TAMING = 25;
public static int COOLDOWN_BERSERK = 26;
public static int COOLDOWN_GIGA_DRILL_BREAKER = 27;
public static int COOLDOWN_TREE_FELLER = 28;
public static int COOLDOWN_GREEN_TERRA = 29;
public static int COOLDOWN_SERRATED_STRIKES = 30;
public static int COOLDOWN_SKULL_SPLITTER = 31;
public static int COOLDOWN_SUPER_BREAKER = 32;
public static int SKILLS_FISHING = 34;
public static int EXP_FISHING = 35;
public static int COOLDOWN_BLAST_MINING = 36;
public static int LAST_LOGIN = 37;
public static int HEALTHBAR = 38;
public static int SKILLS_ALCHEMY = 39;
public static int EXP_ALCHEMY = 40;
public static int UUID_INDEX = 41;
public static int SCOREBOARD_TIPS = 42;
public static int COOLDOWN_CHIMAERA_WING = 43;
public void resetMobHealthSettings() {
BufferedReader in = null;
@ -1319,9 +1253,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
continue;
}
String[] character = line.split(":");
character[HEALTHBAR] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
line = org.apache.commons.lang.StringUtils.join(character, ":") + ":";
writer.append(line).append("\r\n");
@ -1330,28 +1264,31 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Write the new file
out = new FileWriter(usersFilePath);
out.write(writer.toString());
}
catch (IOException e) {
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
}
finally {
} finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
if (out != null) {
try {
out.close();
}
catch (IOException e) {
} catch (IOException e) {
// Ignore
}
}
}
}
}
private class SkillComparator implements Comparator<PlayerStat> {
@Override
public int compare(PlayerStat o1, PlayerStat o2) {
return (o2.statVal - o1.statVal);
}
}
}

View File

@ -19,12 +19,10 @@ import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
public final class SQLDatabaseManager implements DatabaseManager {
private static final String ALL_QUERY_VERSION = "total";
public static final String COM_MYSQL_JDBC_DRIVER = "com.mysql.jdbc.Driver";
private String tablePrefix = mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getTablePrefix();
private static final String ALL_QUERY_VERSION = "total";
private final Map<UUID, Integer> cachedUserIDs = new HashMap<>();
private String tablePrefix = mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getTablePrefix();
private DataSource miscPool;
private DataSource loadPool;
private DataSource savePool;
@ -35,20 +33,19 @@ public final class SQLDatabaseManager implements DatabaseManager {
String connectionString = "jdbc:mysql://" + mcMMO.getMySQLConfigSettings().getUserConfigSectionServer().getServerAddress()
+ ":" + mcMMO.getMySQLConfigSettings().getUserConfigSectionServer().getServerPort() + "/" + mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName();
if(mcMMO.getMySQLConfigSettings().getUserConfigSectionServer().isUseSSL())
if (mcMMO.getMySQLConfigSettings().getUserConfigSectionServer().isUseSSL())
connectionString +=
"?verifyServerCertificate=false"+
"&useSSL=true"+
"&requireSSL=true";
"?verifyServerCertificate=false" +
"&useSSL=true" +
"&requireSSL=true";
else
connectionString+=
connectionString +=
"?useSSL=false";
try {
// Force driver to load if not yet loaded
Class.forName(COM_MYSQL_JDBC_DRIVER);
}
catch (ClassNotFoundException e) {
} catch (ClassNotFoundException e) {
e.printStackTrace();
return;
//throw e; // aborts onEnable() Riking if you want to do this, fully implement it.
@ -62,10 +59,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
/**
* Set up our pools
*
* @param connectionString the MySQL connection string
*/
private void setupPools(String connectionString)
{
private void setupPools(String connectionString) {
miscPool = new DataSource(setupPool(PoolIdentifier.MISC, connectionString));
loadPool = new DataSource(setupPool(PoolIdentifier.LOAD, connectionString));
savePool = new DataSource(setupPool(PoolIdentifier.SAVE, connectionString));
@ -73,12 +70,12 @@ public final class SQLDatabaseManager implements DatabaseManager {
/**
* Sets up our pool using settings from the users config
* @param poolIdentifier the target pool
*
* @param poolIdentifier the target pool
* @param connectionString the MySQL connection string
* @return the pool properties ready for conversion
*/
private PoolProperties setupPool(PoolIdentifier poolIdentifier, String connectionString)
{
private PoolProperties setupPool(PoolIdentifier poolIdentifier, String connectionString) {
PoolProperties poolProperties = new PoolProperties();
poolProperties.setDriverClassName(COM_MYSQL_JDBC_DRIVER);
poolProperties.setUrl(connectionString);
@ -139,11 +136,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.executeUpdate("DELETE FROM `" + tablePrefix + "huds` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "skills` `s` WHERE `" + tablePrefix + "huds`.`user_id` = `s`.`user_id`)");
statement.executeUpdate("DELETE FROM `" + tablePrefix + "cooldowns` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "skills` `s` WHERE `" + tablePrefix + "cooldowns`.`user_id` = `s`.`user_id`)");
statement.executeUpdate("DELETE FROM `" + tablePrefix + "users` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "skills` `s` WHERE `" + tablePrefix + "users`.`id` = `s`.`user_id`)");
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(statement);
tryClose(connection);
massUpdateLock.unlock();
@ -170,11 +165,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
"JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
"JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
"WHERE ((UNIX_TIMESTAMP() - lastlogin) > " + PURGE_TIME + ")");
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(statement);
tryClose(connection);
massUpdateLock.unlock();
@ -201,11 +194,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setString(1, playerName);
success = statement.executeUpdate() != 0;
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(statement);
tryClose(connection);
}
@ -331,11 +322,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().severe("Failed to update hud settings for " + profile.getPlayerName());
return false;
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(statement);
tryClose(connection);
}
@ -367,11 +356,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
stats.add(new PlayerStat(column.get(1), Integer.valueOf(column.get(0))));
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
tryClose(connection);
@ -463,11 +450,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
resultSet.close();
statement.close();
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
tryClose(connection);
@ -482,11 +467,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
connection = getConnection(PoolIdentifier.MISC);
newUser(connection, playerName, uuid);
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(connection);
}
}
@ -518,11 +501,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
writeMissingRows(connection, resultSet.getInt(1));
return resultSet.getInt(1);
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
}
@ -610,17 +591,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
return profile;
}
catch (SQLException e) {
} catch (SQLException e) {
printErrors(e);
}
}
resultSet.close();
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
tryClose(connection);
@ -666,19 +644,16 @@ public final class SQLDatabaseManager implements DatabaseManager {
resultSet.next();
destination.saveUser(loadFromResult(playerName, resultSet));
resultSet.close();
}
catch (SQLException e) {
} catch (SQLException e) {
printErrors(e);
// Ignore
}
convertedUsers++;
Misc.printProgress(convertedUsers, progressInterval, startMillis);
}
}
catch (SQLException e) {
} catch (SQLException e) {
printErrors(e);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
tryClose(connection);
@ -699,12 +674,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setString(2, userName);
statement.execute();
return true;
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
return false;
}
finally {
} finally {
tryClose(statement);
tryClose(connection);
}
@ -739,12 +712,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
return true;
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
return false;
}
finally {
} finally {
tryClose(statement);
tryClose(connection);
}
@ -764,11 +735,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
while (resultSet.next()) {
users.add(resultSet.getString("user"));
}
}
catch (SQLException e) {
} catch (SQLException e) {
printErrors(e);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
tryClose(connection);
@ -799,13 +768,13 @@ public final class SQLDatabaseManager implements DatabaseManager {
if (!resultSet.next()) {
createStatement = connection.createStatement();
createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` ("
+ "`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
+ "`user` varchar(40) NOT NULL,"
+ "`uuid` varchar(36) NULL DEFAULT NULL,"
+ "`lastlogin` int(32) unsigned NOT NULL,"
+ "PRIMARY KEY (`id`),"
+ "INDEX(`user`(20) ASC),"
+ "UNIQUE KEY `uuid` (`uuid`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
+ "`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
+ "`user` varchar(40) NOT NULL,"
+ "`uuid` varchar(36) NULL DEFAULT NULL,"
+ "`lastlogin` int(32) unsigned NOT NULL,"
+ "PRIMARY KEY (`id`),"
+ "INDEX(`user`(20) ASC),"
+ "UNIQUE KEY `uuid` (`uuid`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
tryClose(createStatement);
}
tryClose(resultSet);
@ -860,20 +829,20 @@ public final class SQLDatabaseManager implements DatabaseManager {
createStatement = connection.createStatement();
createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` ("
+ "`user_id` int(10) unsigned NOT NULL,"
+ "`taming` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`mining` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`repair` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`unarmed` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`herbalism` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`excavation` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`archery` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`swords` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`axes` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`fishing` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`alchemy` int(10) unsigned NOT NULL DEFAULT "+startingLevel+","
+ "`total` int(10) unsigned NOT NULL DEFAULT "+totalLevel+","
+ "`taming` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`mining` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`woodcutting` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`repair` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`unarmed` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`herbalism` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`excavation` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`archery` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`swords` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`axes` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`acrobatics` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`fishing` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`alchemy` int(10) unsigned NOT NULL DEFAULT " + startingLevel + ","
+ "`total` int(10) unsigned NOT NULL DEFAULT " + totalLevel + ","
+ "PRIMARY KEY (`user_id`)) "
+ "DEFAULT CHARSET=latin1;");
tryClose(createStatement);
@ -914,14 +883,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
//Level Cap Stuff
if (mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
if(!mcMMO.getPlayerLevelingSettings().isLevelCapEnabled(skill))
if (!mcMMO.getPlayerLevelingSettings().isLevelCapEnabled(skill))
continue;
//Shrink skills above the cap
int cap = mcMMO.getPlayerLevelingSettings().getLevelCap(skill);
statement = connection.prepareStatement("UPDATE `" + tablePrefix + "skills` SET `" + skill.name().toLowerCase() + "` = " + cap + " WHERE `" + skill.name().toLowerCase() + "` > " + cap);
statement.executeUpdate();
tryClose(statement);
statement = connection.prepareStatement("UPDATE `" + tablePrefix + "skills` SET `" + skill.name().toLowerCase() + "` = " + cap + " WHERE `" + skill.name().toLowerCase() + "` > " + cap);
statement.executeUpdate();
tryClose(statement);
}
}
@ -931,11 +900,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "huds` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "huds`.`user_id` = `u`.`id`)");
createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "cooldowns` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "cooldowns`.`user_id` = `u`.`id`)");
createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "skills` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "skills`.`user_id` = `u`.`id`)");
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
tryClose(createStatement);
@ -1033,11 +1000,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
//mcMMO.getUpgradeManager().setUpgradeCompleted(upgrade);
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(statement);
}
}
@ -1067,11 +1032,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.setInt(3, 0);
statement.execute();
statement.close();
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(statement);
}
}
@ -1136,22 +1099,19 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
mobHealthbarType = MobHealthbarType.valueOf(result.getString(OFFSET_OTHER + 1));
}
catch (Exception e) {
} catch (Exception e) {
mobHealthbarType = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType();
}
try {
scoreboardTipsShown = result.getInt(OFFSET_OTHER + 2);
}
catch (Exception e) {
} catch (Exception e) {
scoreboardTipsShown = 0;
}
try {
uuid = UUID.fromString(result.getString(OFFSET_OTHER + 3));
}
catch (Exception e) {
} catch (Exception e) {
uuid = null;
}
@ -1182,7 +1142,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
resultSet.close();
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables to drop name uniqueness...");
statement.execute("ALTER TABLE `" + tablePrefix + "users` "
statement.execute("ALTER TABLE `" + tablePrefix + "users` "
+ "DROP INDEX `user`,"
+ "ADD INDEX `user` (`user`(20) ASC)");
} catch (SQLException ex) {
@ -1195,8 +1155,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void checkUpgradeAddAlchemy(final Statement statement) throws SQLException {
try {
statement.executeQuery("SELECT `alchemy` FROM `" + tablePrefix + "skills` LIMIT 1");
}
catch (SQLException ex) {
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Alchemy...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD `alchemy` int(10) NOT NULL DEFAULT '0'");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "experience` ADD `alchemy` int(10) NOT NULL DEFAULT '0'");
@ -1206,8 +1165,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void checkUpgradeAddBlastMiningCooldown(final Statement statement) throws SQLException {
try {
statement.executeQuery("SELECT `blast_mining` FROM `" + tablePrefix + "cooldowns` LIMIT 1");
}
catch (SQLException ex) {
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0'");
}
@ -1216,8 +1174,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void checkUpgradeAddUniqueChimaeraWing(final Statement statement) throws SQLException {
try {
statement.executeQuery("SELECT `chimaera_wing` FROM `" + tablePrefix + "cooldowns` LIMIT 1");
}
catch (SQLException ex) {
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Chimaera Wing...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "cooldowns` ADD `chimaera_wing` int(32) NOT NULL DEFAULT '0'");
}
@ -1226,8 +1183,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void checkUpgradeAddFishing(final Statement statement) throws SQLException {
try {
statement.executeQuery("SELECT `fishing` FROM `" + tablePrefix + "skills` LIMIT 1");
}
catch (SQLException ex) {
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0'");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0'");
@ -1237,8 +1193,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void checkUpgradeAddMobHealthbars(final Statement statement) throws SQLException {
try {
statement.executeQuery("SELECT `mobhealthbar` FROM `" + tablePrefix + "huds` LIMIT 1");
}
catch (SQLException ex) {
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for mob healthbars...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "huds` ADD `mobhealthbar` varchar(50) NOT NULL DEFAULT '" + mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType() + "'");
}
@ -1247,8 +1202,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void checkUpgradeAddScoreboardTips(final Statement statement) throws SQLException {
try {
statement.executeQuery("SELECT `scoreboardtips` FROM `" + tablePrefix + "huds` LIMIT 1");
}
catch (SQLException ex) {
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for scoreboard tips...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "huds` ADD `scoreboardtips` int(10) NOT NULL DEFAULT '0' ;");
}
@ -1269,17 +1223,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_" + skill_name + "` (`" + skill_name + "`) USING BTREE");
}
catch (SQLException ex) {
} catch (SQLException ex) {
// Ignore
}
}
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
}
}
@ -1305,11 +1256,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "users` ADD `uuid` varchar(36) NULL DEFAULT NULL");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "users` ADD UNIQUE INDEX `uuid` (`uuid`) USING BTREE");
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
}
@ -1369,11 +1318,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().info("Removing party name from users table...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "users` DROP COLUMN `party`");
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
}
}
@ -1404,11 +1351,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_total` (`total`) USING BTREE");
connection.commit();
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
connection.setAutoCommit(true);
tryClose(resultSet);
tryClose(statement);
@ -1435,11 +1380,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
mcMMO.p.getLogger().info("Removing Spout HUD type from huds table...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "huds` DROP COLUMN `hudtype`");
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
}
}
@ -1467,11 +1410,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
return id;
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
}
@ -1493,24 +1434,21 @@ public final class SQLDatabaseManager implements DatabaseManager {
return id;
}
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(resultSet);
tryClose(statement);
}
return -1;
}
private void tryClose(AutoCloseable closeable) {
if (closeable != null) {
try {
closeable.close();
}
catch (Exception e) {
} catch (Exception e) {
// Ignore
}
}
@ -1533,11 +1471,9 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET mobhealthbar = ?");
statement.setString(1, mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString());
statement.executeUpdate();
}
catch (SQLException ex) {
} catch (SQLException ex) {
printErrors(ex);
}
finally {
} finally {
tryClose(statement);
tryClose(connection);
}