Part 2 of removing bad design patterns

This commit is contained in:
nossr50
2019-06-27 03:04:34 -07:00
parent 5ee862effd
commit 87af2419a3
234 changed files with 2303 additions and 2746 deletions

View File

@ -4,7 +4,6 @@ import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.datatypes.database.PlayerStat;
import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.mcMMO;
import java.util.List;
import java.util.Map;
@ -12,7 +11,7 @@ import java.util.UUID;
public interface DatabaseManager {
// One month in milliseconds
long PURGE_TIME = 2630000000L * mcMMO.getDatabaseCleaningSettings().getOldUserCutoffMonths();
long PURGE_TIME = 2630000000L * pluginRef.getDatabaseCleaningSettings().getOldUserCutoffMonths();
// During convertUsers, how often to output a status
int progressInterval = 200;

View File

@ -1,7 +1,6 @@
package com.gmail.nossr50.database;
import com.gmail.nossr50.datatypes.database.DatabaseType;
import com.gmail.nossr50.mcMMO;
public class DatabaseManagerFactory {
private static Class<? extends DatabaseManager> customManager = null;
@ -11,16 +10,16 @@ public class DatabaseManagerFactory {
try {
return createDefaultCustomDatabaseManager();
} catch (Exception e) {
mcMMO.p.debug("Could not create custom database manager");
pluginRef.debug("Could not create custom database manager");
e.printStackTrace();
} catch (Throwable e) {
mcMMO.p.debug("Failed to create custom database manager");
pluginRef.debug("Failed to create custom database manager");
e.printStackTrace();
}
mcMMO.p.debug("Falling back on " + (mcMMO.getMySQLConfigSettings().isMySQLEnabled() ? "SQL" : "Flatfile") + " database");
pluginRef.debug("Falling back on " + (pluginRef.getMySQLConfigSettings().isMySQLEnabled() ? "SQL" : "Flatfile") + " database");
}
return mcMMO.getMySQLConfigSettings().isMySQLEnabled() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
return pluginRef.getMySQLConfigSettings().isMySQLEnabled() ? new SQLDatabaseManager() : new FlatfileDatabaseManager();
}
public static Class<? extends DatabaseManager> getCustomDatabaseManagerClass() {

View File

@ -7,7 +7,6 @@ import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.player.UniqueDataType;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.StringUtils;
import org.bukkit.OfflinePlayer;
@ -64,8 +63,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
private long updateWaitTime;
protected FlatfileDatabaseManager() {
updateWaitTime = mcMMO.getConfigManager().getConfigDatabase().getConfigDatabaseFlatFile().getLeaderboardUpdateIntervalMinutes() * (1000 * 60);
usersFile = new File(mcMMO.getUsersFilePath());
updateWaitTime = pluginRef.getConfigManager().getConfigDatabase().getConfigDatabaseFlatFile().getLeaderboardUpdateIntervalMinutes() * (1000 * 60);
usersFile = new File(pluginRef.getUsersFilePath());
checkStructure();
updateLeaderboards();
@ -77,11 +76,11 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public void purgePowerlessUsers() {
int purgedUsers = 0;
mcMMO.p.getLogger().info("Purging powerless users...");
pluginRef.getLogger().info("Purging powerless users...");
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
// This code is O(n) instead of O(n²)
synchronized (fileWritingLock) {
@ -96,7 +95,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
boolean powerless = true;
for (int skill : skills.values()) {
if (skill > mcMMO.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel()) {
if (skill > pluginRef.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel()) {
powerless = false;
break;
}
@ -114,7 +113,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath);
out.write(writer.toString());
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} finally {
if (in != null) {
try {
@ -133,18 +132,18 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
}
}
mcMMO.p.getLogger().info("Purged " + purgedUsers + " users from the database.");
pluginRef.getLogger().info("Purged " + purgedUsers + " users from the database.");
}
public void purgeOldUsers() {
int removedPlayers = 0;
long currentTime = System.currentTimeMillis();
mcMMO.p.getLogger().info("Purging old users...");
pluginRef.getLogger().info("Purging old users...");
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
// This code is O(n) instead of O(n²)
synchronized (fileWritingLock) {
@ -164,7 +163,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
e.printStackTrace();
}
if (lastPlayed == 0) {
OfflinePlayer player = mcMMO.p.getServer().getOfflinePlayer(name);
OfflinePlayer player = pluginRef.getServer().getOfflinePlayer(name);
lastPlayed = player.getLastPlayed();
rewrite = true;
}
@ -187,7 +186,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath);
out.write(writer.toString());
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} finally {
if (in != null) {
try {
@ -206,7 +205,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
}
}
mcMMO.p.getLogger().info("Purged " + removedPlayers + " users from the database.");
pluginRef.getLogger().info("Purged " + removedPlayers + " users from the database.");
}
public boolean removeUser(String playerName, UUID uuid) {
@ -215,7 +214,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
synchronized (fileWritingLock) {
try {
@ -226,7 +225,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
while ((line = in.readLine()) != null) {
// Write out the same file but when we get to the player we want to remove, we skip his line.
if (!worked && line.split(":")[USERNAME].equalsIgnoreCase(playerName)) {
mcMMO.p.getLogger().info("User found, removing...");
pluginRef.getLogger().info("User found, removing...");
worked = true;
continue; // Skip the player
}
@ -237,7 +236,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} finally {
if (in != null) {
try {
@ -272,7 +271,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
synchronized (fileWritingLock) {
try {
@ -368,7 +367,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
writer.append((int) profile.getAbilityDATS(SuperAbilityType.BLAST_MINING)).append(":");
writer.append(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR).append(":");
MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
writer.append(mobHealthbarType == null ? mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString() : mobHealthbarType.toString()).append(":");
writer.append(mobHealthbarType == null ? pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString() : mobHealthbarType.toString()).append(":");
writer.append(profile.getSkillLevel(PrimarySkillType.ALCHEMY)).append(":");
writer.append(profile.getSkillXpLevel(PrimarySkillType.ALCHEMY)).append(":");
writer.append(uuid != null ? uuid.toString() : "NULL").append(":");
@ -404,9 +403,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
synchronized (fileWritingLock) {
try {
// Open the file to write the player
out = new BufferedWriter(new FileWriter(mcMMO.getUsersFilePath(), true));
out = new BufferedWriter(new FileWriter(pluginRef.getUsersFilePath(), true));
String startingLevel = mcMMO.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel() + ":";
String startingLevel = pluginRef.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel() + ":";
// Add the player to the end
out.append(playerName).append(":");
@ -447,7 +446,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out.append("0:"); // FishingXp
out.append("0:"); // Blast Mining
out.append(String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)).append(":"); // LastLogin
out.append(mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString()).append(":"); // Mob Healthbar HUD
out.append(pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString()).append(":"); // Mob Healthbar HUD
out.append(startingLevel); // Alchemy
out.append("0:"); // AlchemyXp
out.append(uuid != null ? uuid.toString() : "NULL").append(":"); // UUID
@ -480,7 +479,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) {
BufferedReader in = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
synchronized (fileWritingLock) {
try {
@ -506,7 +505,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Update playerName in database after name change
if (!character[USERNAME].equalsIgnoreCase(playerName)) {
mcMMO.p.debug("Name change detected: " + character[USERNAME] + " => " + playerName);
pluginRef.debug("Name change detected: " + character[USERNAME] + " => " + playerName);
character[USERNAME] = playerName;
}
@ -548,7 +547,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public void convertUsers(DatabaseManager destination) {
BufferedReader in = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
int convertedUsers = 0;
long startMillis = System.currentTimeMillis();
@ -589,7 +588,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
int i = 0;
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
synchronized (fileWritingLock) {
try {
@ -601,8 +600,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
String[] character = line.split(":");
if (!worked && character[USERNAME].equalsIgnoreCase(userName)) {
if (character.length < 42) {
mcMMO.p.getLogger().severe("Could not update UUID for " + userName + "!");
mcMMO.p.getLogger().severe("Database entry is invalid.");
pluginRef.getLogger().severe("Could not update UUID for " + userName + "!");
pluginRef.getLogger().severe("Database entry is invalid.");
continue;
}
@ -617,9 +616,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} finally {
mcMMO.p.getLogger().info(i + " entries written while saving UUID for " + userName);
pluginRef.getLogger().info(i + " entries written while saving UUID for " + userName);
if (in != null) {
try {
in.close();
@ -643,7 +642,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs) {
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
int i = 0;
synchronized (fileWritingLock) {
@ -656,8 +655,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
String[] character = line.split(":");
if (!fetchedUUIDs.isEmpty() && fetchedUUIDs.containsKey(character[USERNAME])) {
if (character.length < 42) {
mcMMO.p.getLogger().severe("Could not update UUID for " + character[USERNAME] + "!");
mcMMO.p.getLogger().severe("Database entry is invalid.");
pluginRef.getLogger().severe("Could not update UUID for " + character[USERNAME] + "!");
pluginRef.getLogger().severe("Database entry is invalid.");
continue;
}
@ -672,9 +671,9 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath); // Write out the new file
out.write(writer.toString());
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} finally {
mcMMO.p.getLogger().info(i + " entries written while saving UUID batch");
pluginRef.getLogger().info(i + " entries written while saving UUID batch");
if (in != null) {
try {
in.close();
@ -698,7 +697,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public List<String> getStoredUsers() {
ArrayList<String> users = new ArrayList<>();
BufferedReader in = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
synchronized (fileWritingLock) {
try {
@ -734,7 +733,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
return;
}
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
lastUpdate = System.currentTimeMillis(); // Log when the last update was run
powerLevels.clear(); // Clear old values from the power levels
@ -785,7 +784,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
putStat(powerLevels, playerName, powerLevel);
}
} catch (Exception e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " during user " + playerName + " (Are you sure you formatted it correctly?) " + e.toString());
} finally {
if (in != null) {
try {
@ -836,7 +835,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
if (usersFile.exists()) {
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
synchronized (fileWritingLock) {
try {
@ -875,7 +874,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
if (character.length < 33) {
// Before Version 1.0 - Drop
mcMMO.p.getLogger().warning("Dropping malformed or before version 1.0 line from database - " + line);
pluginRef.getLogger().warning("Dropping malformed or before version 1.0 line from database - " + line);
continue;
}
@ -892,17 +891,17 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
updated = true;
}
if (mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
if (pluginRef.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
int index = getSkillIndex(skill);
if (index >= character.length) {
continue;
}
//Level Cap
if (mcMMO.getPlayerLevelingSettings().isSkillLevelCapEnabled(skill)) {
int cap = mcMMO.getPlayerLevelingSettings().getSkillLevelCap(skill);
if (pluginRef.getPlayerLevelingSettings().isSkillLevelCapEnabled(skill)) {
int cap = pluginRef.getPlayerLevelingSettings().getSkillLevelCap(skill);
if (Integer.valueOf(character[index]) > cap) {
mcMMO.p.getLogger().warning("Truncating " + skill.getName() + " to configured max level for player " + character[USERNAME]);
pluginRef.getLogger().warning("Truncating " + skill.getName() + " to configured max level for player " + character[USERNAME]);
character[index] = cap + "";
updated = true;
}
@ -962,7 +961,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
// Version 1.4.06
// commit da29185b7dc7e0d992754bba555576d48fa08aa6
character = Arrays.copyOf(character, character.length + 1);
character[character.length - 1] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
character[character.length - 1] = pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
if (oldVersion == null) {
oldVersion = "1.4.06";
}
@ -1005,7 +1004,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
if (i == 37) {
character[i] = String.valueOf(System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
} else if (i == 38) {
character[i] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
character[i] = pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
} else {
character[i] = "0";
}
@ -1013,7 +1012,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
if (StringUtils.isInt(character[i]) && i == 38) {
corrupted = true;
character[i] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
character[i] = pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
}
if (!StringUtils.isInt(character[i]) && !(i == 0 || i == 2 || i == 3 || i == 23 || i == 33 || i == 38 || i == 41)) {
@ -1023,17 +1022,17 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
}
if (corrupted) {
mcMMO.p.debug("Updating corrupted database line for player " + character[USERNAME]);
pluginRef.debug("Updating corrupted database line for player " + character[USERNAME]);
}
if (oldVersion != null) {
mcMMO.p.debug("Updating database line from before version " + oldVersion + " for player " + character[USERNAME]);
pluginRef.debug("Updating database line from before version " + oldVersion + " for player " + character[USERNAME]);
}
updated |= corrupted;
updated |= oldVersion != null;
if (mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
if (pluginRef.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
Map<PrimarySkillType, Integer> skills = getSkillMapFromLine(character);
for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
int cap = Integer.MAX_VALUE;
@ -1054,7 +1053,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath);
out.write(writer.toString());
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} finally {
if (in != null) {
try {
@ -1086,8 +1085,8 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
usersFile.getParentFile().mkdir();
try {
mcMMO.p.debug("Creating mcmmo.users file...");
new File(mcMMO.getUsersFilePath()).createNewFile();
pluginRef.debug("Creating mcmmo.users file...");
new File(pluginRef.getUsersFilePath()).createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
@ -1156,7 +1155,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
try {
mobHealthbarType = MobHealthbarType.valueOf(character[HEALTHBAR]);
} catch (Exception e) {
mobHealthbarType = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType();
mobHealthbarType = pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType();
}
UUID uuid;
@ -1246,7 +1245,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
public void resetMobHealthSettings() {
BufferedReader in = null;
FileWriter out = null;
String usersFilePath = mcMMO.getUsersFilePath();
String usersFilePath = pluginRef.getUsersFilePath();
synchronized (fileWritingLock) {
try {
@ -1261,7 +1260,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
}
String[] character = line.split(":");
character[HEALTHBAR] = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
character[HEALTHBAR] = pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString();
line = org.apache.commons.lang.StringUtils.join(character, ":") + ":";
@ -1272,7 +1271,7 @@ public final class FlatfileDatabaseManager implements DatabaseManager {
out = new FileWriter(usersFilePath);
out.write(writer.toString());
} catch (IOException e) {
mcMMO.p.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
pluginRef.getLogger().severe("Exception while reading " + usersFilePath + " (Are you sure you formatted it correctly?)" + e.toString());
} finally {
if (in != null) {
try {

View File

@ -9,7 +9,6 @@ import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.player.UniqueDataType;
import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.Misc;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
@ -22,7 +21,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
public static final String COM_MYSQL_JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String ALL_QUERY_VERSION = "total";
private final Map<UUID, Integer> cachedUserIDs = new HashMap<>();
private String tablePrefix = mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getTablePrefix();
private String tablePrefix = pluginRef.getMySQLConfigSettings().getConfigSectionDatabase().getTablePrefix();
private DataSource miscPool;
private DataSource loadPool;
private DataSource savePool;
@ -30,10 +29,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
private ReentrantLock massUpdateLock = new ReentrantLock();
protected SQLDatabaseManager() {
String connectionString = "jdbc:mysql://" + mcMMO.getMySQLConfigSettings().getUserConfigSectionServer().getServerAddress()
+ ":" + mcMMO.getMySQLConfigSettings().getUserConfigSectionServer().getServerPort() + "/" + mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName();
String connectionString = "jdbc:mysql://" + pluginRef.getMySQLConfigSettings().getUserConfigSectionServer().getServerAddress()
+ ":" + pluginRef.getMySQLConfigSettings().getUserConfigSectionServer().getServerPort() + "/" + pluginRef.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName();
if (mcMMO.getMySQLConfigSettings().getUserConfigSectionServer().isUseSSL())
if (pluginRef.getMySQLConfigSettings().getUserConfigSectionServer().isUseSSL())
connectionString +=
"?verifyServerCertificate=false" +
"&useSSL=true" +
@ -81,17 +80,17 @@ public final class SQLDatabaseManager implements DatabaseManager {
poolProperties.setUrl(connectionString);
//MySQL User Name
poolProperties.setUsername(mcMMO.getMySQLConfigSettings().getConfigSectionUser().getUsername());
poolProperties.setUsername(pluginRef.getMySQLConfigSettings().getConfigSectionUser().getUsername());
//MySQL User Password
poolProperties.setPassword(mcMMO.getMySQLConfigSettings().getConfigSectionUser().getPassword());
poolProperties.setPassword(pluginRef.getMySQLConfigSettings().getConfigSectionUser().getPassword());
//Initial Size
poolProperties.setInitialSize(0);
//Max Pool Size for Misc
poolProperties.setMaxIdle(mcMMO.getMySQLConfigSettings().getMaxPoolSize(poolIdentifier));
poolProperties.setMaxIdle(pluginRef.getMySQLConfigSettings().getMaxPoolSize(poolIdentifier));
//Max Connections for Misc
poolProperties.setMaxActive(mcMMO.getMySQLConfigSettings().getMaxConnections(poolIdentifier));
poolProperties.setMaxActive(pluginRef.getMySQLConfigSettings().getMaxConnections(poolIdentifier));
poolProperties.setMaxWait(-1);
poolProperties.setRemoveAbandoned(true);
@ -105,7 +104,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
public void purgePowerlessUsers() {
massUpdateLock.lock();
mcMMO.p.getLogger().info("Purging powerless users...");
pluginRef.getLogger().info("Purging powerless users...");
Connection connection = null;
Statement statement = null;
@ -115,7 +114,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
connection = getConnection(PoolIdentifier.MISC);
statement = connection.createStatement();
String startingLevel = String.valueOf(mcMMO.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel());
String startingLevel = String.valueOf(pluginRef.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel());
//Purge users who have not leveled from the default level
purged = statement.executeUpdate("DELETE FROM " + tablePrefix + "skills WHERE "
@ -144,12 +143,12 @@ public final class SQLDatabaseManager implements DatabaseManager {
massUpdateLock.unlock();
}
mcMMO.p.getLogger().info("Purged " + purged + " users from the database.");
pluginRef.getLogger().info("Purged " + purged + " users from the database.");
}
public void purgeOldUsers() {
massUpdateLock.lock();
mcMMO.p.getLogger().info("Purging inactive users older than " + (PURGE_TIME / 2630000000L) + " months...");
pluginRef.getLogger().info("Purging inactive users older than " + (PURGE_TIME / 2630000000L) + " months...");
Connection connection = null;
Statement statement = null;
@ -173,7 +172,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
massUpdateLock.unlock();
}
mcMMO.p.getLogger().info("Purged " + purged + " users from the database.");
pluginRef.getLogger().info("Purged " + purged + " users from the database.");
}
public boolean removeUser(String playerName, UUID uuid) {
@ -229,7 +228,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
if (id == -1) {
id = newUser(connection, profile.getPlayerName(), profile.getUniqueId());
if (id == -1) {
mcMMO.p.getLogger().severe("Failed to create new account for " + profile.getPlayerName());
pluginRef.getLogger().severe("Failed to create new account for " + profile.getPlayerName());
return false;
}
}
@ -239,7 +238,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
success &= (statement.executeUpdate() != 0);
statement.close();
if (!success) {
mcMMO.p.getLogger().severe("Failed to update last login for " + profile.getPlayerName());
pluginRef.getLogger().severe("Failed to update last login for " + profile.getPlayerName());
return false;
}
@ -269,7 +268,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
success &= (statement.executeUpdate() != 0);
statement.close();
if (!success) {
mcMMO.p.getLogger().severe("Failed to update skills for " + profile.getPlayerName());
pluginRef.getLogger().severe("Failed to update skills for " + profile.getPlayerName());
return false;
}
@ -295,7 +294,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
success &= (statement.executeUpdate() != 0);
statement.close();
if (!success) {
mcMMO.p.getLogger().severe("Failed to update experience for " + profile.getPlayerName());
pluginRef.getLogger().severe("Failed to update experience for " + profile.getPlayerName());
return false;
}
@ -316,18 +315,18 @@ public final class SQLDatabaseManager implements DatabaseManager {
success = (statement.executeUpdate() != 0);
statement.close();
if (!success) {
mcMMO.p.getLogger().severe("Failed to update cooldowns for " + profile.getPlayerName());
pluginRef.getLogger().severe("Failed to update cooldowns for " + profile.getPlayerName());
return false;
}
statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET mobhealthbar = ?, scoreboardtips = ? WHERE user_id = ?");
statement.setString(1, profile.getMobHealthbarType() == null ? mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().name() : profile.getMobHealthbarType().name());
statement.setString(1, profile.getMobHealthbarType() == null ? pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().name() : profile.getMobHealthbarType().name());
statement.setInt(2, profile.getScoreboardTipsShown());
statement.setInt(3, id);
success = (statement.executeUpdate() != 0);
statement.close();
if (!success) {
mcMMO.p.getLogger().severe("Failed to update hud settings for " + profile.getPlayerName());
pluginRef.getLogger().severe("Failed to update hud settings for " + profile.getPlayerName());
return false;
}
} catch (SQLException ex) {
@ -503,7 +502,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
resultSet = statement.getGeneratedKeys();
if (!resultSet.next()) {
mcMMO.p.getLogger().severe("Unable to create new user account in DB");
pluginRef.getLogger().severe("Unable to create new user account in DB");
return -1;
}
@ -770,7 +769,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
+ " WHERE table_schema = ?"
+ " AND table_name = ?");
//Database name
statement.setString(1, mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(1, pluginRef.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(2, tablePrefix + "users");
resultSet = statement.executeQuery();
if (!resultSet.next()) {
@ -787,14 +786,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
tryClose(resultSet);
//Database name
statement.setString(1, mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(1, pluginRef.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(2, tablePrefix + "huds");
resultSet = statement.executeQuery();
if (!resultSet.next()) {
createStatement = connection.createStatement();
createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "huds` ("
+ "`user_id` int(10) unsigned NOT NULL,"
+ "`mobhealthbar` varchar(50) NOT NULL DEFAULT '" + mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType() + "',"
+ "`mobhealthbar` varchar(50) NOT NULL DEFAULT '" + pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType() + "',"
+ "`scoreboardtips` int(10) NOT NULL DEFAULT '0',"
+ "PRIMARY KEY (`user_id`)) "
+ "DEFAULT CHARSET=latin1;");
@ -802,7 +801,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
tryClose(resultSet);
//Database name
statement.setString(1, mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(1, pluginRef.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(2, tablePrefix + "cooldowns");
resultSet = statement.executeQuery();
if (!resultSet.next()) {
@ -828,12 +827,12 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
tryClose(resultSet);
//Database name
statement.setString(1, mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(1, pluginRef.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(2, tablePrefix + "skills");
resultSet = statement.executeQuery();
if (!resultSet.next()) {
String startingLevel = "'" + mcMMO.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel() + "'";
String totalLevel = "'" + (mcMMO.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel() * (PrimarySkillType.values().length - PrimarySkillType.CHILD_SKILLS.size())) + "'";
String startingLevel = "'" + pluginRef.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel() + "'";
String totalLevel = "'" + (pluginRef.getPlayerLevelingSettings().getConfigSectionLevelingGeneral().getStartingLevel() * (PrimarySkillType.values().length - PrimarySkillType.CHILD_SKILLS.size())) + "'";
createStatement = connection.createStatement();
createStatement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` ("
+ "`user_id` int(10) unsigned NOT NULL,"
@ -857,7 +856,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
tryClose(resultSet);
//Database name
statement.setString(1, mcMMO.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(1, pluginRef.getMySQLConfigSettings().getConfigSectionDatabase().getDatabaseName());
statement.setString(2, tablePrefix + "experience");
resultSet = statement.executeQuery();
if (!resultSet.next()) {
@ -889,20 +888,20 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
//Level Cap Stuff
if (mcMMO.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
if (pluginRef.getPlayerLevelingSettings().getConfigSectionLevelCaps().getReducePlayerSkillsAboveCap()) {
for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
if (!mcMMO.getPlayerLevelingSettings().isSkillLevelCapEnabled(skill))
if (!pluginRef.getPlayerLevelingSettings().isSkillLevelCapEnabled(skill))
continue;
//Shrink skills above the cap
int cap = mcMMO.getPlayerLevelingSettings().getSkillLevelCap(skill);
int cap = pluginRef.getPlayerLevelingSettings().getSkillLevelCap(skill);
statement = connection.prepareStatement("UPDATE `" + tablePrefix + "skills` SET `" + skill.name().toLowerCase() + "` = " + cap + " WHERE `" + skill.name().toLowerCase() + "` > " + cap);
statement.executeUpdate();
tryClose(statement);
}
}
mcMMO.p.getLogger().info("Killing orphans");
pluginRef.getLogger().info("Killing orphans");
createStatement = connection.createStatement();
createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "experience` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "experience`.`user_id` = `u`.`id`)");
createStatement.executeUpdate("DELETE FROM `" + tablePrefix + "huds` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "huds`.`user_id` = `u`.`id`)");
@ -1036,7 +1035,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "huds (user_id, mobhealthbar, scoreboardtips) VALUES (?, ?, ?)");
statement.setInt(1, id);
statement.setString(2, mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().name());
statement.setString(2, pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().name());
statement.setInt(3, 0);
statement.execute();
statement.close();
@ -1108,7 +1107,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
mobHealthbarType = MobHealthbarType.valueOf(result.getString(OFFSET_OTHER + 1));
} catch (Exception e) {
mobHealthbarType = mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType();
mobHealthbarType = pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType();
}
try {
@ -1128,10 +1127,10 @@ public final class SQLDatabaseManager implements DatabaseManager {
private void printErrors(SQLException ex) {
StackTraceElement element = ex.getStackTrace()[0];
mcMMO.p.getLogger().severe("Location: " + element.getClassName() + " " + element.getMethodName() + " " + element.getLineNumber());
mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());
mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());
pluginRef.getLogger().severe("Location: " + element.getClassName() + " " + element.getMethodName() + " " + element.getLineNumber());
pluginRef.getLogger().severe("SQLException: " + ex.getMessage());
pluginRef.getLogger().severe("SQLState: " + ex.getSQLState());
pluginRef.getLogger().severe("VendorError: " + ex.getErrorCode());
}
public DatabaseType getDatabaseType() {
@ -1149,7 +1148,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
return;
}
resultSet.close();
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables to drop name uniqueness...");
pluginRef.getLogger().info("Updating mcMMO MySQL tables to drop name uniqueness...");
statement.execute("ALTER TABLE `" + tablePrefix + "users` "
+ "DROP INDEX `user`,"
+ "ADD INDEX `user` (`user`(20) ASC)");
@ -1164,7 +1163,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
statement.executeQuery("SELECT `alchemy` FROM `" + tablePrefix + "skills` LIMIT 1");
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Alchemy...");
pluginRef.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'");
}
@ -1174,7 +1173,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
statement.executeQuery("SELECT `blast_mining` FROM `" + tablePrefix + "cooldowns` LIMIT 1");
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
pluginRef.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0'");
}
}
@ -1183,7 +1182,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
statement.executeQuery("SELECT `chimaera_wing` FROM `" + tablePrefix + "cooldowns` LIMIT 1");
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Chimaera Wing...");
pluginRef.getLogger().info("Updating mcMMO MySQL tables for Chimaera Wing...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "cooldowns` ADD `chimaera_wing` int(32) NOT NULL DEFAULT '0'");
}
}
@ -1192,7 +1191,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
statement.executeQuery("SELECT `fishing` FROM `" + tablePrefix + "skills` LIMIT 1");
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing...");
pluginRef.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'");
}
@ -1202,8 +1201,8 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
statement.executeQuery("SELECT `mobhealthbar` FROM `" + tablePrefix + "huds` LIMIT 1");
} 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() + "'");
pluginRef.getLogger().info("Updating mcMMO MySQL tables for mob healthbars...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "huds` ADD `mobhealthbar` varchar(50) NOT NULL DEFAULT '" + pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType() + "'");
}
}
@ -1211,7 +1210,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
statement.executeQuery("SELECT `scoreboardtips` FROM `" + tablePrefix + "huds` LIMIT 1");
} catch (SQLException ex) {
mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for scoreboard tips...");
pluginRef.getLogger().info("Updating mcMMO MySQL tables for scoreboard tips...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "huds` ADD `scoreboardtips` int(10) NOT NULL DEFAULT '0' ;");
}
}
@ -1224,7 +1223,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
resultSet.last();
if (resultSet.getRow() != PrimarySkillType.NON_CHILD_SKILLS.size()) {
mcMMO.p.getLogger().info("Indexing tables, this may take a while on larger databases");
pluginRef.getLogger().info("Indexing tables, this may take a while on larger databases");
for (PrimarySkillType skill : PrimarySkillType.NON_CHILD_SKILLS) {
String skill_name = skill.name().toLowerCase();
@ -1260,7 +1259,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
if (!column_exists) {
mcMMO.p.getLogger().info("Adding UUIDs to mcMMO MySQL user table...");
pluginRef.getLogger().info("Adding UUIDs to mcMMO MySQL user table...");
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");
}
@ -1323,7 +1322,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
if (column_exists) {
mcMMO.p.getLogger().info("Removing party name from users table...");
pluginRef.getLogger().info("Removing party name from users table...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "users` DROP COLUMN `party`");
}
} catch (SQLException ex) {
@ -1353,7 +1352,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
if (!column_exists) {
mcMMO.p.getLogger().info("Adding skill total column to skills table...");
pluginRef.getLogger().info("Adding skill total column to skills table...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD COLUMN `total` int NOT NULL DEFAULT '0'");
statement.executeUpdate("UPDATE `" + tablePrefix + "skills` SET `total` = (taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy)");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_total` (`total`) USING BTREE");
@ -1385,7 +1384,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
}
if (column_exists) {
mcMMO.p.getLogger().info("Removing Spout HUD type from huds table...");
pluginRef.getLogger().info("Removing Spout HUD type from huds table...");
statement.executeUpdate("ALTER TABLE `" + tablePrefix + "huds` DROP COLUMN `hudtype`");
}
} catch (SQLException ex) {
@ -1464,7 +1463,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
@Override
public void onDisable() {
mcMMO.p.debug("Releasing connection pool resource...");
pluginRef.debug("Releasing connection pool resource...");
miscPool.close();
loadPool.close();
savePool.close();
@ -1477,7 +1476,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
try {
connection = getConnection(PoolIdentifier.MISC);
statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET mobhealthbar = ?");
statement.setString(1, mcMMO.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString());
statement.setString(1, pluginRef.getConfigManager().getConfigMobs().getCombat().getHealthBars().getDisplayBarType().toString());
statement.executeUpdate();
} catch (SQLException ex) {
printErrors(ex);