12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640 |
- package com.gmail.nossr50.database;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Properties;
- import java.util.UUID;
- import com.gmail.nossr50.mcMMO;
- import com.gmail.nossr50.config.Config;
- import com.gmail.nossr50.datatypes.MobHealthbarType;
- import com.gmail.nossr50.datatypes.database.DatabaseType;
- import com.gmail.nossr50.datatypes.database.PlayerStat;
- import com.gmail.nossr50.datatypes.database.UpgradeType;
- import com.gmail.nossr50.datatypes.player.PlayerProfile;
- import com.gmail.nossr50.datatypes.skills.AbilityType;
- import com.gmail.nossr50.datatypes.skills.SkillType;
- import com.gmail.nossr50.runnables.database.UUIDUpdateAsyncTask;
- import com.gmail.nossr50.util.Misc;
- import snaq.db.ConnectionPool;
- public final class SQLDatabaseManager implements DatabaseManager {
- private static final String ALL_QUERY_VERSION = "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing+alchemy";
- private String connectionString;
- private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
- private final int POOL_FETCH_TIMEOUT = 0; // How long a method will wait for a connection. Since none are on main thread, we can safely say wait for as long as you like.
- private final Map<UUID, Integer> cachedUserIDs = new HashMap<UUID, Integer>();
- private final Map<String, Integer> cachedUserIDsByName = new HashMap<String, Integer>();
- private ConnectionPool connectionPool;
- protected SQLDatabaseManager() {
- connectionString = "jdbc:mysql://" + Config.getInstance().getMySQLServerName() + ":" + Config.getInstance().getMySQLServerPort() + "/" + Config.getInstance().getMySQLDatabaseName();
- try {
- // Force driver to load if not yet loaded
- Class.forName("com.mysql.jdbc.Driver");
- Properties connectionProperties = new Properties();
- connectionProperties.put("user", Config.getInstance().getMySQLUserName());
- connectionProperties.put("password", Config.getInstance().getMySQLUserPassword());
- connectionProperties.put("autoReconnect", "false");
- connectionPool = new ConnectionPool("mcMMO-Pool",
- 1 /*Minimum of one*/,
- 10 /*max pool size */, // TODO Configurable?
- 10 /*max num connections*/, // TODO Configurable?
- 0 /* idle timeout of connections */,
- connectionString,
- connectionProperties);
- connectionPool.init(); // Init first connection
- connectionPool.registerShutdownHook(); // Auto release on jvm exit just in case
- }
- catch (ClassNotFoundException e) {
- // TODO tft do something here everything will blow up
- e.printStackTrace();
- }
- checkStructure();
- }
- public void purgePowerlessUsers() {
- mcMMO.p.getLogger().info("Purging powerless users...");
- Connection connection = null;
- Statement statement = null;
- ResultSet resultSet = null;
- List<String> usernames = new ArrayList<String>();
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.createStatement();
- resultSet = statement.executeQuery("SELECT u.user FROM " + tablePrefix + "skills AS s, " + tablePrefix + "users AS u WHERE s.user_id = u.id AND (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0");
- while (resultSet.next()) {
- usernames.add(resultSet.getString("user"));
- }
- resultSet.close();
- statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
- "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
- "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
- "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
- "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
- "WHERE (s.taming+s.mining+s.woodcutting+s.repair+s.unarmed+s.herbalism+s.excavation+s.archery+s.swords+s.axes+s.acrobatics+s.fishing) = 0");
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- if (!usernames.isEmpty()) {
- processPurge(usernames);
- }
- mcMMO.p.getLogger().info("Purged " + usernames.size() + " users from the database.");
- }
- public void purgeOldUsers() {
- mcMMO.p.getLogger().info("Purging old users...");
- Connection connection = null;
- Statement statement = null;
- ResultSet resultSet = null;
- List<String> usernames = new ArrayList<String>();
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.createStatement();
- resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users WHERE ((NOW() - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")");
- while (resultSet.next()) {
- usernames.add(resultSet.getString("user"));
- }
- resultSet.close();
- statement.executeUpdate("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
- "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
- "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
- "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
- "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
- "WHERE ((NOW() - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")");
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- if (!usernames.isEmpty()) {
- processPurge(usernames);
- }
- mcMMO.p.getLogger().info("Purged " + usernames.size() + " users from the database.");
- }
- public boolean removeUser(String playerName) {
- boolean success = false;
- Connection connection = null;
- PreparedStatement statement = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.prepareStatement("DELETE FROM u, e, h, s, c " +
- "USING " + tablePrefix + "users u " +
- "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
- "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
- "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
- "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
- "WHERE u.user = ?");
- statement.setString(1, playerName);
- success = statement.executeUpdate() != 0;
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- if (success) {
- Misc.profileCleanup(playerName);
- }
- return success;
- }
- public boolean saveUser(PlayerProfile profile) {
- boolean success = true;
- PreparedStatement statement = null;
- Connection connection = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- int id = getUserID(connection, profile.getUniqueId());
- if (id == -1) {
- newUser(profile.getPlayerName(), profile.getUniqueId().toString());
- id = getUserID(connection, profile.getUniqueId());
- if (id == -1) {
- return false;
- }
- }
- statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET lastlogin = UNIX_TIMESTAMP() WHERE uuid = ?");
- statement.setString(1, profile.getUniqueId().toString());
- success &= (statement.executeUpdate() != 0);
- statement.close();
- statement = connection.prepareStatement("UPDATE " + tablePrefix + "skills SET "
- + " taming = ?, mining = ?, repair = ?, woodcutting = ?"
- + ", unarmed = ?, herbalism = ?, excavation = ?"
- + ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
- + ", fishing = ?, alchemy = ? WHERE user_id = ?");
- statement.setInt(1, profile.getSkillLevel(SkillType.TAMING));
- statement.setInt(2, profile.getSkillLevel(SkillType.MINING));
- statement.setInt(3, profile.getSkillLevel(SkillType.REPAIR));
- statement.setInt(4, profile.getSkillLevel(SkillType.WOODCUTTING));
- statement.setInt(5, profile.getSkillLevel(SkillType.UNARMED));
- statement.setInt(6, profile.getSkillLevel(SkillType.HERBALISM));
- statement.setInt(7, profile.getSkillLevel(SkillType.EXCAVATION));
- statement.setInt(8, profile.getSkillLevel(SkillType.ARCHERY));
- statement.setInt(9, profile.getSkillLevel(SkillType.SWORDS));
- statement.setInt(10, profile.getSkillLevel(SkillType.AXES));
- statement.setInt(11, profile.getSkillLevel(SkillType.ACROBATICS));
- statement.setInt(12, profile.getSkillLevel(SkillType.FISHING));
- statement.setInt(13, profile.getSkillLevel(SkillType.ALCHEMY));
- statement.setInt(14, id);
- success &= (statement.executeUpdate() != 0);
- statement.close();
- statement = connection.prepareStatement("UPDATE " + tablePrefix + "experience SET "
- + " taming = ?, mining = ?, repair = ?, woodcutting = ?"
- + ", unarmed = ?, herbalism = ?, excavation = ?"
- + ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
- + ", fishing = ?, alchemy = ? WHERE user_id = ?");
- statement.setInt(1, profile.getSkillXpLevel(SkillType.TAMING));
- statement.setInt(2, profile.getSkillXpLevel(SkillType.MINING));
- statement.setInt(3, profile.getSkillXpLevel(SkillType.REPAIR));
- statement.setInt(4, profile.getSkillXpLevel(SkillType.WOODCUTTING));
- statement.setInt(5, profile.getSkillXpLevel(SkillType.UNARMED));
- statement.setInt(6, profile.getSkillXpLevel(SkillType.HERBALISM));
- statement.setInt(7, profile.getSkillXpLevel(SkillType.EXCAVATION));
- statement.setInt(8, profile.getSkillXpLevel(SkillType.ARCHERY));
- statement.setInt(9, profile.getSkillXpLevel(SkillType.SWORDS));
- statement.setInt(10, profile.getSkillXpLevel(SkillType.AXES));
- statement.setInt(11, profile.getSkillXpLevel(SkillType.ACROBATICS));
- statement.setInt(12, profile.getSkillXpLevel(SkillType.FISHING));
- statement.setInt(13, profile.getSkillXpLevel(SkillType.ALCHEMY));
- statement.setInt(14, id);
- success &= (statement.executeUpdate() != 0);
- statement.close();
- statement = connection.prepareStatement("UPDATE " + tablePrefix + "cooldowns SET "
- + " mining = ?, woodcutting = ?, unarmed = ?"
- + ", herbalism = ?, excavation = ?, swords = ?"
- + ", axes = ?, blast_mining = ? WHERE user_id = ?");
- statement.setLong(1, profile.getAbilityDATS(AbilityType.SUPER_BREAKER));
- statement.setLong(2, profile.getAbilityDATS(AbilityType.TREE_FELLER));
- statement.setLong(3, profile.getAbilityDATS(AbilityType.BERSERK));
- statement.setLong(4, profile.getAbilityDATS(AbilityType.GREEN_TERRA));
- statement.setLong(5, profile.getAbilityDATS(AbilityType.GIGA_DRILL_BREAKER));
- statement.setLong(6, profile.getAbilityDATS(AbilityType.SERRATED_STRIKES));
- statement.setLong(7, profile.getAbilityDATS(AbilityType.SKULL_SPLITTER));
- statement.setLong(8, profile.getAbilityDATS(AbilityType.BLAST_MINING));
- statement.setInt(9, id);
- success = (statement.executeUpdate() != 0);
- statement.close();
- statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET mobhealthbar = ? WHERE user_id = ?");
- statement.setString(1, profile.getMobHealthbarType() == null ? Config.getInstance().getMobHealthbarDefault().name() : profile.getMobHealthbarType().name());
- statement.setInt(2, id);
- success = (statement.executeUpdate() != 0);
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- return success;
- }
- public List<PlayerStat> readLeaderboard(SkillType skill, int pageNumber, int statsPerPage) {
- List<PlayerStat> stats = new ArrayList<PlayerStat>();
- String query = skill == null ? ALL_QUERY_VERSION : skill.name().toLowerCase();
- ResultSet resultSet = null;
- PreparedStatement statement = null;
- Connection connection = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.prepareStatement("SELECT " + query + ", user, NOW() FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON (user_id = id) WHERE " + query + " > 0 ORDER BY " + query + " DESC, user LIMIT ?, ?");
- statement.setInt(1, (pageNumber * statsPerPage) - statsPerPage);
- statement.setInt(2, statsPerPage);
- resultSet = statement.executeQuery();
- while (resultSet.next()) {
- ArrayList<String> column = new ArrayList<String>();
- for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
- column.add(resultSet.getString(i));
- }
- stats.add(new PlayerStat(column.get(1), Integer.valueOf(column.get(0))));
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- return stats;
- }
- public Map<SkillType, Integer> readRank(String playerName) {
- Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>();
- ResultSet resultSet = null;
- PreparedStatement statement = null;
- Connection connection = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- for (SkillType skillType : SkillType.NON_CHILD_SKILLS) {
- String skillName = skillType.name().toLowerCase();
- String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
- "AND " + skillName + " > (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
- "WHERE user = ?)";
- statement = connection.prepareStatement(sql);
- statement.setString(1, playerName);
- resultSet = statement.executeQuery();
- resultSet.next();
- int rank = resultSet.getInt("rank");
- sql = "SELECT user, " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
- "AND " + skillName + " = (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
- "WHERE user = '" + playerName + "') ORDER BY user";
- resultSet.close();
- statement.close();
- statement = connection.prepareStatement(sql);
- resultSet = statement.executeQuery();
- while (resultSet.next()) {
- if (resultSet.getString("user").equalsIgnoreCase(playerName)) {
- skills.put(skillType, rank + resultSet.getRow());
- break;
- }
- }
- resultSet.close();
- statement.close();
- }
- String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
- "WHERE " + ALL_QUERY_VERSION + " > 0 " +
- "AND " + ALL_QUERY_VERSION + " > " +
- "(SELECT " + ALL_QUERY_VERSION + " " +
- "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?)";
- statement = connection.prepareStatement(sql);
- statement.setString(1, playerName);
- resultSet = statement.executeQuery();
- resultSet.next();
- int rank = resultSet.getInt("rank");
- resultSet.close();
- statement.close();
- sql = "SELECT user, " + ALL_QUERY_VERSION + " " +
- "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
- "WHERE " + ALL_QUERY_VERSION + " > 0 " +
- "AND " + ALL_QUERY_VERSION + " = " +
- "(SELECT " + ALL_QUERY_VERSION + " " +
- "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?) ORDER BY user";
- statement = connection.prepareStatement(sql);
- statement.setString(1, playerName);
- resultSet = statement.executeQuery();
- while (resultSet.next()) {
- if (resultSet.getString("user").equalsIgnoreCase(playerName)) {
- skills.put(null, rank + resultSet.getRow());
- break;
- }
- }
- resultSet.close();
- statement.close();
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- return skills;
- }
- public void newUser(String playerName, String uuid) {
- Connection connection = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- newUser(connection, playerName, uuid);
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- private void newUser(Connection connection, String playerName, String uuid) {
- ResultSet resultSet = null;
- PreparedStatement statement = null;
- try {
- statement = connection.prepareStatement("INSERT INTO " + tablePrefix + "users (user, uuid, lastlogin) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
- statement.setString(1, playerName);
- statement.setString(2, uuid);
- statement.setLong(3, System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
- statement.executeUpdate();
- resultSet = statement.getGeneratedKeys();
- if (!resultSet.next()) {
- return;
- }
- writeMissingRows(connection, resultSet.getInt(1));
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- /**
- * This is a fallback method to provide the old way of getting a
- * PlayerProfile in case there is no UUID match found
- */
- private PlayerProfile loadPlayerNameProfile(String playerName, String uuid, boolean create, boolean retry) {
- PreparedStatement statement = null;
- Connection connection = null;
- ResultSet resultSet = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- int id = getUserID(connection, playerName);
- if (id == -1) {
- // There is no such user
- if (create) {
- newUser(playerName, uuid);
- return loadPlayerNameProfile(playerName, uuid, false, false);
- }
- // Return unloaded profile if can't create
- return new PlayerProfile(playerName, false);
- }
- // There is such a user
- writeMissingRows(connection, id);
- statement = connection.prepareStatement(
- "SELECT "
- + "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, s.alchemy, "
- + "e.taming, e.mining, e.repair, e.woodcutting, e.unarmed, e.herbalism, e.excavation, e.archery, e.swords, e.axes, e.acrobatics, e.fishing, e.alchemy, "
- + "c.taming, c.mining, c.repair, c.woodcutting, c.unarmed, c.herbalism, c.excavation, c.archery, c.swords, c.axes, c.acrobatics, c.blast_mining, "
- + "h.mobhealthbar, u.uuid "
- + "FROM " + tablePrefix + "users u "
- + "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) "
- + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) "
- + "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
- + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
- + "WHERE u.user = ?");
- statement.setString(1, playerName);
- resultSet = statement.executeQuery();
- if (resultSet.next()) {
- try {
- PlayerProfile ret = loadFromResult(playerName, resultSet);
- return ret;
- }
- catch (SQLException e) {
- }
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- // Problem, nothing was returned
- // Quit if this is second time around
- if (!retry) {
- return new PlayerProfile(playerName, false);
- }
- // Retry, and abort on re-failure
- return loadPlayerNameProfile(playerName, uuid, create, false);
- }
- @Deprecated
- public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
- return loadPlayerProfile(playerName, "", false, true);
- }
- public PlayerProfile loadPlayerProfile(UUID uuid) {
- return loadPlayerProfile("", uuid.toString(), false, true);
- }
- public PlayerProfile loadPlayerProfile(String playerName, UUID uuid, boolean create) {
- return loadPlayerProfile(playerName, uuid.toString(), create, true);
- }
- private PlayerProfile loadPlayerProfile(String playerName, String uuid, boolean create, boolean retry) {
- PreparedStatement statement = null;
- Connection connection = null;
- ResultSet resultSet = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- int id = getUserID(connection, playerName);
- if (id == -1) {
- // There is no such user
- if (create) {
- newUser(playerName, uuid);
- return loadPlayerNameProfile(playerName, uuid, false, false);
- }
- // Return unloaded profile if can't create
- return new PlayerProfile(playerName, false);
- }
- // There is such a user
- writeMissingRows(connection, id);
- statement = connection.prepareStatement(
- "SELECT "
- + "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, s.alchemy, "
- + "e.taming, e.mining, e.repair, e.woodcutting, e.unarmed, e.herbalism, e.excavation, e.archery, e.swords, e.axes, e.acrobatics, e.fishing, e.alchemy, "
- + "c.taming, c.mining, c.repair, c.woodcutting, c.unarmed, c.herbalism, c.excavation, c.archery, c.swords, c.axes, c.acrobatics, c.blast_mining, "
- + "h.mobhealthbar, u.uuid "
- + "FROM " + tablePrefix + "users u "
- + "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) "
- + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) "
- + "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
- + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
- + "WHERE u.UUID = ?");
- statement.setString(1, uuid);
- resultSet = statement.executeQuery();
- if (resultSet.next()) {
- try {
- PlayerProfile profile = loadFromResult(playerName, resultSet);
- resultSet.close();
- statement.close();
- if (!playerName.isEmpty() && !profile.getPlayerName().isEmpty()) {
- statement = connection.prepareStatement(
- "UPDATE `" + tablePrefix + "users` "
- + "SET user = ? "
- + "WHERE UUID = ?");
- statement.setString(1, playerName);
- statement.setString(2, uuid);
- statement.executeUpdate();
- statement.close();
- }
- return profile;
- }
- catch (SQLException e) {
- }
- }
- resultSet.close();
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- // Problem, nothing was returned
- // Retry the old fashioned way if this is second time around
- if (!retry) {
- return loadPlayerNameProfile(playerName, uuid, create, true);
- }
- // Retry, and abort on re-failure
- return loadPlayerProfile(playerName, uuid, create, false);
- }
- public void convertUsers(DatabaseManager destination) {
- PreparedStatement statement = null;
- Connection connection = null;
- ResultSet resultSet = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.prepareStatement(
- "SELECT "
- + "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, s.alchemy, "
- + "e.taming, e.mining, e.repair, e.woodcutting, e.unarmed, e.herbalism, e.excavation, e.archery, e.swords, e.axes, e.acrobatics, e.fishing, e.alchemy, "
- + "c.taming, c.mining, c.repair, c.woodcutting, c.unarmed, c.herbalism, c.excavation, c.archery, c.swords, c.axes, c.acrobatics, c.blast_mining, "
- + "h.mobhealthbar, u.uuid "
- + "FROM " + tablePrefix + "users u "
- + "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) "
- + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) "
- + "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
- + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
- + "WHERE u.user = ?");
- List<String> usernames = getStoredUsers();
- int convertedUsers = 0;
- long startMillis = System.currentTimeMillis();
- for (String playerName : usernames) {
- statement.setString(1, playerName);
- try {
- resultSet = statement.executeQuery();
- resultSet.next();
- destination.saveUser(loadFromResult(playerName, resultSet));
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- convertedUsers++;
- Misc.printProgress(convertedUsers, progressInterval, startMillis);
- }
- }
- catch (SQLException e) {
- printErrors(e);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- public boolean saveUserUUID(String userName, UUID uuid) {
- PreparedStatement statement = null;
- Connection connection = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.prepareStatement(
- "UPDATE `" + tablePrefix + "users` SET "
- + " uuid = ? WHERE user = ?");
- statement.setString(1, uuid.toString());
- statement.setString(2, userName);
- statement.execute();
- return true;
- }
- catch (SQLException ex) {
- printErrors(ex);
- return false;
- }
- finally {
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- public boolean saveUserUUIDs(Map<String, UUID> fetchedUUIDs) {
- PreparedStatement statement = null;
- int count = 0;
- Connection connection = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET uuid = ? WHERE user = ?");
- for (Map.Entry<String, UUID> entry : fetchedUUIDs.entrySet()) {
- statement.setString(1, entry.getValue().toString());
- statement.setString(2, entry.getKey());
- statement.addBatch();
- count++;
- if ((count % 500) == 0) {
- statement.executeBatch();
- count = 0;
- }
- }
- if (count != 0) {
- statement.executeBatch();
- }
- return true;
- }
- catch (SQLException ex) {
- printErrors(ex);
- return false;
- }
- finally {
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- public List<String> getStoredUsers() {
- ArrayList<String> users = new ArrayList<String>();
- Statement statement = null;
- Connection connection = null;
- ResultSet resultSet = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.createStatement();
- resultSet = statement.executeQuery("SELECT user FROM " + tablePrefix + "users");
- while (resultSet.next()) {
- users.add(resultSet.getString("user"));
- }
- }
- catch (SQLException e) {
- printErrors(e);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- return users;
- }
- /**
- * Checks that the database structure is present and correct
- */
- private void checkStructure() {
- Statement statement = null;
- Connection connection = null;
- try {
- connection = connectionPool.getConnection(POOL_FETCH_TIMEOUT);
- statement = connection.createStatement();
- statement.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`),"
- + "UNIQUE KEY `user` (`user`),"
- + "UNIQUE KEY `uuid` (`uuid`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
- statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "huds` ("
- + "`user_id` int(10) unsigned NOT NULL,"
- + "`mobhealthbar` varchar(50) NOT NULL DEFAULT '" + Config.getInstance().getMobHealthbarDefault() + "',"
- + "PRIMARY KEY (`user_id`)) "
- + "DEFAULT CHARSET=latin1;");
- statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "cooldowns` ("
- + "`user_id` int(10) unsigned NOT NULL,"
- + "`taming` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`mining` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`repair` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`unarmed` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`herbalism` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`excavation` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`archery` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`swords` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`axes` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0',"
- + "`blast_mining` int(32) unsigned NOT NULL DEFAULT '0',"
- + "PRIMARY KEY (`user_id`)) "
- + "DEFAULT CHARSET=latin1;");
- statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` ("
- + "`user_id` int(10) unsigned NOT NULL,"
- + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`fishing` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`alchemy` int(10) unsigned NOT NULL DEFAULT '0',"
- + "PRIMARY KEY (`user_id`)) "
- + "DEFAULT CHARSET=latin1;");
- statement.executeUpdate("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "experience` ("
- + "`user_id` int(10) unsigned NOT NULL,"
- + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`fishing` int(10) unsigned NOT NULL DEFAULT '0',"
- + "`alchemy` int(10) unsigned NOT NULL DEFAULT '0',"
- + "PRIMARY KEY (`user_id`)) "
- + "DEFAULT CHARSET=latin1;");
- for (UpgradeType updateType : UpgradeType.values()) {
- checkDatabaseStructure(connection, updateType);
- }
- mcMMO.p.getLogger().info("Killing orphans");
- statement.executeUpdate("DELETE FROM `" + tablePrefix + "experience` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "experience`.`user_id` = `u`.`id`)");
- statement.executeUpdate("DELETE FROM `" + tablePrefix + "huds` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "huds`.`user_id` = `u`.`id`)");
- statement.executeUpdate("DELETE FROM `" + tablePrefix + "cooldowns` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "cooldowns`.`user_id` = `u`.`id`)");
- statement.executeUpdate("DELETE FROM `" + tablePrefix + "skills` WHERE NOT EXISTS (SELECT * FROM `" + tablePrefix + "users` `u` WHERE `" + tablePrefix + "skills`.`user_id` = `u`.`id`)");
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (connection != null) {
- try {
- connection.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- /**
- * Check database structure for necessary upgrades.
- *
- * @param upgrade Upgrade to attempt to apply
- */
- private void checkDatabaseStructure(Connection connection, UpgradeType upgrade) {
- if (!mcMMO.getUpgradeManager().shouldUpgrade(upgrade)) {
- mcMMO.p.debug("Skipping " + upgrade.name() + " upgrade (unneeded)");
- return;
- }
- Statement statement = null;
- try {
- statement = connection.createStatement();
- switch (upgrade) {
- case ADD_FISHING:
- checkUpgradeAddFishing(statement);
- break;
- case ADD_BLAST_MINING_COOLDOWN:
- checkUpgradeAddBlastMiningCooldown(statement);
- break;
- case ADD_SQL_INDEXES:
- checkUpgradeAddSQLIndexes(statement);
- break;
- case ADD_MOB_HEALTHBARS:
- checkUpgradeAddMobHealthbars(statement);
- break;
- case DROP_SQL_PARTY_NAMES:
- checkUpgradeDropPartyNames(statement);
- break;
- case DROP_SPOUT:
- checkUpgradeDropSpout(statement);
- break;
- case ADD_ALCHEMY:
- checkUpgradeAddAlchemy(statement);
- break;
- case ADD_UUIDS:
- checkUpgradeAddUUIDs(statement);
- return;
- default:
- break;
- }
- mcMMO.getUpgradeManager().setUpgradeCompleted(upgrade);
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- private void writeMissingRows(Connection connection, int id) {
- PreparedStatement statement = null;
- try {
- statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "experience (user_id) VALUES (?)");
- statement.setInt(1, id);
- statement.execute();
- statement.close();
- statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "skills (user_id) VALUES (?)");
- statement.setInt(1, id);
- statement.execute();
- statement.close();
- statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "cooldowns (user_id) VALUES (?)");
- statement.setInt(1, id);
- statement.execute();
- statement.close();
- statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "huds (user_id, mobhealthbar) VALUES (?, ?)");
- statement.setInt(1, id);
- statement.setString(2, Config.getInstance().getMobHealthbarDefault().name());
- statement.execute();
- statement.close();
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- private void processPurge(Collection<String> usernames) {
- for (String user : usernames) {
- Misc.profileCleanup(user);
- }
- }
- private PlayerProfile loadFromResult(String playerName, ResultSet result) throws SQLException {
- Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); // Skill & Level
- Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
- Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
- MobHealthbarType mobHealthbarType;
- UUID uuid;
- final int OFFSET_SKILLS = 0; // TODO update these numbers when the query
- // changes (a new skill is added)
- final int OFFSET_XP = 13;
- final int OFFSET_DATS = 26;
- final int OFFSET_OTHER = 38;
- skills.put(SkillType.TAMING, result.getInt(OFFSET_SKILLS + 1));
- skills.put(SkillType.MINING, result.getInt(OFFSET_SKILLS + 2));
- skills.put(SkillType.REPAIR, result.getInt(OFFSET_SKILLS + 3));
- skills.put(SkillType.WOODCUTTING, result.getInt(OFFSET_SKILLS + 4));
- skills.put(SkillType.UNARMED, result.getInt(OFFSET_SKILLS + 5));
- skills.put(SkillType.HERBALISM, result.getInt(OFFSET_SKILLS + 6));
- skills.put(SkillType.EXCAVATION, result.getInt(OFFSET_SKILLS + 7));
- skills.put(SkillType.ARCHERY, result.getInt(OFFSET_SKILLS + 8));
- skills.put(SkillType.SWORDS, result.getInt(OFFSET_SKILLS + 9));
- skills.put(SkillType.AXES, result.getInt(OFFSET_SKILLS + 10));
- skills.put(SkillType.ACROBATICS, result.getInt(OFFSET_SKILLS + 11));
- skills.put(SkillType.FISHING, result.getInt(OFFSET_SKILLS + 12));
- skills.put(SkillType.ALCHEMY, result.getInt(OFFSET_SKILLS + 13));
- skillsXp.put(SkillType.TAMING, result.getFloat(OFFSET_XP + 1));
- skillsXp.put(SkillType.MINING, result.getFloat(OFFSET_XP + 2));
- skillsXp.put(SkillType.REPAIR, result.getFloat(OFFSET_XP + 3));
- skillsXp.put(SkillType.WOODCUTTING, result.getFloat(OFFSET_XP + 4));
- skillsXp.put(SkillType.UNARMED, result.getFloat(OFFSET_XP + 5));
- skillsXp.put(SkillType.HERBALISM, result.getFloat(OFFSET_XP + 6));
- skillsXp.put(SkillType.EXCAVATION, result.getFloat(OFFSET_XP + 7));
- skillsXp.put(SkillType.ARCHERY, result.getFloat(OFFSET_XP + 8));
- skillsXp.put(SkillType.SWORDS, result.getFloat(OFFSET_XP + 9));
- skillsXp.put(SkillType.AXES, result.getFloat(OFFSET_XP + 10));
- skillsXp.put(SkillType.ACROBATICS, result.getFloat(OFFSET_XP + 11));
- skillsXp.put(SkillType.FISHING, result.getFloat(OFFSET_XP + 12));
- skillsXp.put(SkillType.ALCHEMY, result.getFloat(OFFSET_XP + 13));
- // Taming - Unused - result.getInt(OFFSET_DATS + 1)
- skillsDATS.put(AbilityType.SUPER_BREAKER, result.getInt(OFFSET_DATS + 2));
- // Repair - Unused - result.getInt(OFFSET_DATS + 3)
- skillsDATS.put(AbilityType.TREE_FELLER, result.getInt(OFFSET_DATS + 4));
- skillsDATS.put(AbilityType.BERSERK, result.getInt(OFFSET_DATS + 5));
- skillsDATS.put(AbilityType.GREEN_TERRA, result.getInt(OFFSET_DATS + 6));
- skillsDATS.put(AbilityType.GIGA_DRILL_BREAKER, result.getInt(OFFSET_DATS + 7));
- // Archery - Unused - result.getInt(OFFSET_DATS + 8)
- skillsDATS.put(AbilityType.SERRATED_STRIKES, result.getInt(OFFSET_DATS + 9));
- skillsDATS.put(AbilityType.SKULL_SPLITTER, result.getInt(OFFSET_DATS + 10));
- // Acrobatics - Unused - result.getInt(OFFSET_DATS + 11)
- skillsDATS.put(AbilityType.BLAST_MINING, result.getInt(OFFSET_DATS + 12));
- try {
- mobHealthbarType = MobHealthbarType.valueOf(result.getString(OFFSET_OTHER + 2));
- }
- catch (Exception e) {
- mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
- }
- try {
- uuid = UUID.fromString(result.getString(OFFSET_OTHER + 3));
- }
- catch (Exception e) {
- uuid = null;
- }
- return new PlayerProfile(playerName, uuid, skills, skillsXp, skillsDATS, mobHealthbarType);
- }
- private void printErrors(SQLException ex) {
- mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
- mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());
- mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());
- }
- public DatabaseType getDatabaseType() {
- return DatabaseType.SQL;
- }
- private void checkUpgradeAddAlchemy(final Statement statement) throws SQLException {
- try {
- statement.executeQuery("SELECT `alchemy` FROM `" + tablePrefix + "skills` LIMIT 1");
- }
- 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'");
- }
- }
- private void checkUpgradeAddBlastMiningCooldown(final Statement statement) throws SQLException {
- 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...");
- statement.executeUpdate("ALTER TABLE `" + tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0'");
- }
- }
- private void checkUpgradeAddFishing(final Statement statement) throws SQLException {
- try {
- statement.executeQuery("SELECT `fishing` FROM `" + tablePrefix + "skills` LIMIT 1");
- }
- 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'");
- }
- }
- private void checkUpgradeAddMobHealthbars(final Statement statement) throws SQLException {
- 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 '" + Config.getInstance().getMobHealthbarDefault() + "'");
- }
- }
- private void checkUpgradeAddSQLIndexes(final Statement statement) throws SQLException {
- ResultSet resultSet = null;
- try {
- resultSet = statement.executeQuery("SHOW INDEX FROM `" + tablePrefix + "skills` WHERE `Key_name` LIKE 'idx\\_%'");
- resultSet.last();
- if (resultSet.getRow() != SkillType.NON_CHILD_SKILLS.size()) {
- mcMMO.p.getLogger().info("Indexing tables, this may take a while on larger databases");
- for (SkillType skill : SkillType.NON_CHILD_SKILLS) {
- String skill_name = skill.name().toLowerCase();
- try {
- statement.executeUpdate("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_" + skill_name + "` (`" + skill_name + "`) USING BTREE");
- }
- catch (SQLException ex) {
- // Ignore
- }
- }
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- private void checkUpgradeAddUUIDs(final Statement statement) {
- List<String> names = new ArrayList<String>();
- ResultSet resultSet = null;
- try {
- resultSet = statement.executeQuery("SELECT * FROM `" + tablePrefix + "users` LIMIT 1");
- ResultSetMetaData rsmeta = resultSet.getMetaData();
- boolean column_exists = false;
- for (int i = 1; i <= rsmeta.getColumnCount(); i++) {
- if (rsmeta.getColumnName(i).equalsIgnoreCase("uuid")) {
- column_exists = true;
- break;
- }
- }
- if (!column_exists) {
- mcMMO.p.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");
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- try {
- resultSet = statement.executeQuery("SELECT `user` FROM `" + tablePrefix + "users` WHERE `uuid` IS NULL");
- while (resultSet.next()) {
- names.add(resultSet.getString("user"));
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- if (!names.isEmpty()) {
- new UUIDUpdateAsyncTask(mcMMO.p, names).runTaskAsynchronously(mcMMO.p);
- }
- }
- private void checkUpgradeDropPartyNames(final Statement statement) {
- ResultSet resultSet = null;
- try {
- resultSet = statement.executeQuery("SELECT * FROM `" + tablePrefix + "users` LIMIT 1");
- ResultSetMetaData rsmeta = resultSet.getMetaData();
- boolean column_exists = false;
- for (int i = 1; i <= rsmeta.getColumnCount(); i++) {
- if (rsmeta.getColumnName(i).equalsIgnoreCase("party")) {
- column_exists = true;
- break;
- }
- }
- if (column_exists) {
- mcMMO.p.getLogger().info("Removing party name from users table...");
- statement.executeUpdate("ALTER TABLE `" + tablePrefix + "users` DROP COLUMN `party`");
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- private void checkUpgradeDropSpout(final Statement statement) {
- ResultSet resultSet = null;
- try {
- resultSet = statement.executeQuery("SELECT * FROM `" + tablePrefix + "huds` LIMIT 1");
- ResultSetMetaData rsmeta = resultSet.getMetaData();
- boolean column_exists = false;
- for (int i = 1; i <= rsmeta.getColumnCount(); i++) {
- if (rsmeta.getColumnName(i).equalsIgnoreCase("hudtype")) {
- column_exists = true;
- break;
- }
- }
- if (column_exists) {
- mcMMO.p.getLogger().info("Removing Spout HUD type from huds table...");
- statement.executeUpdate("ALTER TABLE `" + tablePrefix + "huds` DROP COLUMN `hudtype`");
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- }
- private int getUserID(final Connection connection, final String playerName) {
- Integer id = cachedUserIDsByName.get(playerName.toLowerCase());
- if (id != null) {
- return id;
- }
- ResultSet resultSet = null;
- PreparedStatement statement = null;
- try {
- statement = connection.prepareStatement("SELECT id, uuid FROM " + tablePrefix + "users WHERE user = ?");
- statement.setString(1, playerName);
- resultSet = statement.executeQuery();
- if (resultSet.next()) {
- id = resultSet.getInt("id");
- cachedUserIDsByName.put(playerName.toLowerCase(), id);
- try {
- cachedUserIDs.put(UUID.fromString(resultSet.getString("uuid")), id);
- }
- catch (Exception e) {
- }
- return id;
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- return -1;
- }
- private int getUserID(final Connection connection, final UUID uuid) {
- if (cachedUserIDs.containsKey(uuid)) {
- return cachedUserIDs.get(uuid);
- }
- ResultSet resultSet = null;
- PreparedStatement statement = null;
- try {
- statement = connection.prepareStatement("SELECT id, user FROM " + tablePrefix + "users WHERE uuid = ?");
- statement.setString(1, uuid.toString());
- resultSet = statement.executeQuery();
- if (resultSet.next()) {
- int id = resultSet.getInt("id");
- cachedUserIDs.put(uuid, id);
- cachedUserIDsByName.put(resultSet.getString("user").toLowerCase(), id);
- return id;
- }
- }
- catch (SQLException ex) {
- printErrors(ex);
- }
- finally {
- if (resultSet != null) {
- try {
- resultSet.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- if (statement != null) {
- try {
- statement.close();
- }
- catch (SQLException e) {
- // Ignore
- }
- }
- }
- return -1;
- }
- @Override
- public void onDisable() {
- connectionPool.release();
- }
- }
|