1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648 |
- 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.EnumMap;
- 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 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() {
- String 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");
- }
- catch (ClassNotFoundException e) {
- e.printStackTrace();
- return;
- //throw e; // aborts onEnable() Riking if you want to do this, fully implement it.
- }
- Properties connectionProperties = new Properties();
- connectionProperties.put("user", Config.getInstance().getMySQLUserName());
- connectionProperties.put("password", Config.getInstance().getMySQLUserPassword());
- connectionProperties.put("autoReconnect", "false");
- connectionProperties.put("cachePrepStmts", "true");
- connectionProperties.put("prepStmtCacheSize", "64");
- connectionProperties.put("prepStmtCacheSqlLimit", "2048");
- connectionProperties.put("useServerPrepStmts", "true");
- connectionPool = new ConnectionPool("mcMMO-Pool",
- 1 /*Minimum of one*/,
- Config.getInstance().getMySQLMaxPoolSize() /*max pool size */,
- Config.getInstance().getMySQLMaxConnections() /*max num connections*/,
- 0 /* idle timeout of connections */,
- connectionString,
- connectionProperties);
- connectionPool.init(); // Init first connection
- connectionPool.registerShutdownHook(); // Auto release on jvm exit just in case
- 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 EnumMap<SkillType, Integer>(SkillType.class);
- 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 EnumMap<SkillType, Integer>(SkillType.class); // Skill & Level
- Map<SkillType, Float> skillsXp = new EnumMap<SkillType, Float>(SkillType.class); // Skill & XP
- Map<AbilityType, Integer> skillsDATS = new EnumMap<AbilityType, Integer>(AbilityType.class); // 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) {
- StackTraceElement element = ex.getStackTrace()[0];
- mcMMO.p.getLogger().severe("Location: " + 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());
- }
- 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();
- }
- }
|