SQLDatabaseManager.java 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194
  1. package com.gmail.nossr50.database;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.Collection;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. import java.util.Properties;
  14. import java.util.logging.Level;
  15. import com.gmail.nossr50.mcMMO;
  16. import com.gmail.nossr50.config.Config;
  17. import com.gmail.nossr50.datatypes.MobHealthbarType;
  18. import com.gmail.nossr50.datatypes.database.DatabaseUpdateType;
  19. import com.gmail.nossr50.datatypes.database.PlayerStat;
  20. import com.gmail.nossr50.datatypes.player.PlayerProfile;
  21. import com.gmail.nossr50.datatypes.skills.AbilityType;
  22. import com.gmail.nossr50.datatypes.skills.SkillType;
  23. import com.gmail.nossr50.datatypes.spout.huds.HudType;
  24. import com.gmail.nossr50.runnables.database.SQLReconnectTask;
  25. import com.gmail.nossr50.util.Misc;
  26. public final class SQLDatabaseManager implements DatabaseManager {
  27. private String connectionString;
  28. private String tablePrefix = Config.getInstance().getMySQLTablePrefix();
  29. private Connection connection = null;
  30. // Scale waiting time by this much per failed attempt
  31. private final double SCALING_FACTOR = 40.0;
  32. // Minimum wait in nanoseconds (default 500ms)
  33. private final long MIN_WAIT = 500L * 1000000L;
  34. // Maximum time to wait between reconnects (default 5 minutes)
  35. private final long MAX_WAIT = 5L * 60L * 1000L * 1000000L;
  36. // How long to wait when checking if connection is valid (default 3 seconds)
  37. private final int VALID_TIMEOUT = 3;
  38. // When next to try connecting to Database in nanoseconds
  39. private long nextReconnectTimestamp = 0L;
  40. // How many connection attempts have failed
  41. private int reconnectAttempt = 0;
  42. protected SQLDatabaseManager() {
  43. checkConnected();
  44. checkStructure();
  45. }
  46. public void purgePowerlessUsers() {
  47. checkConnected();
  48. mcMMO.p.getLogger().info("Purging powerless users...");
  49. Collection<ArrayList<String>> usernames = read("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").values();
  50. write("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
  51. "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
  52. "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
  53. "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
  54. "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
  55. "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");
  56. processPurge(usernames);
  57. mcMMO.p.getLogger().info("Purged " + usernames.size() + " users from the database.");
  58. }
  59. public void purgeOldUsers() {
  60. checkConnected();
  61. long currentTime = System.currentTimeMillis();
  62. mcMMO.p.getLogger().info("Purging old users...");
  63. Collection<ArrayList<String>> usernames = read("SELECT user FROM " + tablePrefix + "users WHERE ((" + currentTime + " - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")").values();
  64. write("DELETE FROM u, e, h, s, c USING " + tablePrefix + "users u " +
  65. "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
  66. "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
  67. "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
  68. "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
  69. "WHERE ((" + currentTime + " - lastlogin * " + Misc.TIME_CONVERSION_FACTOR + ") > " + PURGE_TIME + ")");
  70. processPurge(usernames);
  71. mcMMO.p.getLogger().info("Purged " + usernames.size() + " users from the database.");;
  72. }
  73. public boolean removeUser(String playerName) {
  74. checkConnected();
  75. boolean success = update("DELETE FROM u, e, h, s, c " +
  76. "USING " + tablePrefix + "users u " +
  77. "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) " +
  78. "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) " +
  79. "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) " +
  80. "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) " +
  81. "WHERE u.user = '" + playerName + "'") != 0;
  82. Misc.profileCleanup(playerName);
  83. return success;
  84. }
  85. public void saveUser(PlayerProfile profile) {
  86. checkConnected();
  87. int userId = readId(profile.getPlayerName());
  88. if (userId == -1) {
  89. newUser(profile.getPlayerName());
  90. userId = readId(profile.getPlayerName());
  91. if (userId == -1) {
  92. mcMMO.p.getLogger().log(Level.WARNING, "Failed to save user " + profile.getPlayerName());
  93. return;
  94. }
  95. }
  96. MobHealthbarType mobHealthbarType = profile.getMobHealthbarType();
  97. HudType hudType = profile.getHudType();
  98. saveLogin(userId, ((int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR)));
  99. saveHuds(userId, (hudType == null ? "STANDARD" : hudType.toString()), (mobHealthbarType == null ? Config.getInstance().getMobHealthbarDefault().toString() : mobHealthbarType.toString()));
  100. saveLongs(
  101. "UPDATE " + tablePrefix + "cooldowns SET "
  102. + " mining = ?, woodcutting = ?, unarmed = ?"
  103. + ", herbalism = ?, excavation = ?, swords = ?"
  104. + ", axes = ?, blast_mining = ? WHERE user_id = ?",
  105. userId,
  106. profile.getSkillDATS(AbilityType.SUPER_BREAKER),
  107. profile.getSkillDATS(AbilityType.TREE_FELLER),
  108. profile.getSkillDATS(AbilityType.BERSERK),
  109. profile.getSkillDATS(AbilityType.GREEN_TERRA),
  110. profile.getSkillDATS(AbilityType.GIGA_DRILL_BREAKER),
  111. profile.getSkillDATS(AbilityType.SERRATED_STRIKES),
  112. profile.getSkillDATS(AbilityType.SKULL_SPLITTER),
  113. profile.getSkillDATS(AbilityType.BLAST_MINING));
  114. saveIntegers(
  115. "UPDATE " + tablePrefix + "skills SET "
  116. + " taming = ?, mining = ?, repair = ?, woodcutting = ?"
  117. + ", unarmed = ?, herbalism = ?, excavation = ?"
  118. + ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
  119. + ", fishing = ? WHERE user_id = ?",
  120. profile.getSkillLevel(SkillType.TAMING),
  121. profile.getSkillLevel(SkillType.MINING),
  122. profile.getSkillLevel(SkillType.REPAIR),
  123. profile.getSkillLevel(SkillType.WOODCUTTING),
  124. profile.getSkillLevel(SkillType.UNARMED),
  125. profile.getSkillLevel(SkillType.HERBALISM),
  126. profile.getSkillLevel(SkillType.EXCAVATION),
  127. profile.getSkillLevel(SkillType.ARCHERY),
  128. profile.getSkillLevel(SkillType.SWORDS),
  129. profile.getSkillLevel(SkillType.AXES),
  130. profile.getSkillLevel(SkillType.ACROBATICS),
  131. profile.getSkillLevel(SkillType.FISHING),
  132. userId);
  133. saveIntegers(
  134. "UPDATE " + tablePrefix + "experience SET "
  135. + " taming = ?, mining = ?, repair = ?, woodcutting = ?"
  136. + ", unarmed = ?, herbalism = ?, excavation = ?"
  137. + ", archery = ?, swords = ?, axes = ?, acrobatics = ?"
  138. + ", fishing = ? WHERE user_id = ?",
  139. profile.getSkillXpLevel(SkillType.TAMING),
  140. profile.getSkillXpLevel(SkillType.MINING),
  141. profile.getSkillXpLevel(SkillType.REPAIR),
  142. profile.getSkillXpLevel(SkillType.WOODCUTTING),
  143. profile.getSkillXpLevel(SkillType.UNARMED),
  144. profile.getSkillXpLevel(SkillType.HERBALISM),
  145. profile.getSkillXpLevel(SkillType.EXCAVATION),
  146. profile.getSkillXpLevel(SkillType.ARCHERY),
  147. profile.getSkillXpLevel(SkillType.SWORDS),
  148. profile.getSkillXpLevel(SkillType.AXES),
  149. profile.getSkillXpLevel(SkillType.ACROBATICS),
  150. profile.getSkillXpLevel(SkillType.FISHING),
  151. userId);
  152. }
  153. public List<PlayerStat> readLeaderboard(String skillName, int pageNumber, int statsPerPage) {
  154. List<PlayerStat> stats = new ArrayList<PlayerStat>();
  155. if (checkConnected()) {
  156. String query = skillName.equalsIgnoreCase("ALL") ? "taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing" : skillName;
  157. ResultSet resultSet = null;
  158. PreparedStatement statement = null;
  159. try {
  160. 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 ?, ?");
  161. statement.setInt(1, (pageNumber * statsPerPage) - statsPerPage);
  162. statement.setInt(2, statsPerPage);
  163. resultSet = statement.executeQuery();
  164. while (resultSet.next()) {
  165. ArrayList<String> column = new ArrayList<String>();
  166. for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
  167. column.add(resultSet.getString(i));
  168. }
  169. stats.add(new PlayerStat(column.get(1), Integer.valueOf(column.get(0))));
  170. }
  171. }
  172. catch (SQLException ex) {
  173. printErrors(ex);
  174. }
  175. finally {
  176. if (statement != null) {
  177. try {
  178. statement.close();
  179. }
  180. catch (SQLException e) {
  181. // Ignore
  182. }
  183. }
  184. }
  185. }
  186. return stats;
  187. }
  188. public Map<String, Integer> readRank(String playerName) {
  189. Map<String, Integer> skills = new HashMap<String, Integer>();
  190. if (checkConnected()) {
  191. ResultSet resultSet;
  192. try {
  193. for (SkillType skillType : SkillType.nonChildSkills()) {
  194. String skillName = skillType.name().toLowerCase();
  195. String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
  196. "AND " + skillName + " > (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  197. "WHERE user = ?)";
  198. PreparedStatement statement = connection.prepareStatement(sql);
  199. statement.setString(1, playerName);
  200. resultSet = statement.executeQuery();
  201. resultSet.next();
  202. int rank = resultSet.getInt("rank");
  203. sql = "SELECT user, " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE " + skillName + " > 0 " +
  204. "AND " + skillName + " = (SELECT " + skillName + " FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  205. "WHERE user = '" + playerName + "') ORDER BY user";
  206. statement.close();
  207. statement = connection.prepareStatement(sql);
  208. resultSet = statement.executeQuery();
  209. while (resultSet.next()) {
  210. if (resultSet.getString("user").equalsIgnoreCase(playerName)) {
  211. skills.put(skillType.name(), rank + resultSet.getRow());
  212. break;
  213. }
  214. }
  215. statement.close();
  216. }
  217. String sql = "SELECT COUNT(*) AS rank FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  218. "WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 " +
  219. "AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > " +
  220. "(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
  221. "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?)";
  222. PreparedStatement statement = connection.prepareStatement(sql);
  223. statement.setString(1, playerName);
  224. resultSet = statement.executeQuery();
  225. resultSet.next();
  226. int rank = resultSet.getInt("rank");
  227. statement.close();
  228. sql = "SELECT user, taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
  229. "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id " +
  230. "WHERE taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing > 0 " +
  231. "AND taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing = " +
  232. "(SELECT taming+mining+woodcutting+repair+unarmed+herbalism+excavation+archery+swords+axes+acrobatics+fishing " +
  233. "FROM " + tablePrefix + "users JOIN " + tablePrefix + "skills ON user_id = id WHERE user = ?) ORDER BY user";
  234. statement = connection.prepareStatement(sql);
  235. statement.setString(1, playerName);
  236. resultSet = statement.executeQuery();
  237. while (resultSet.next()) {
  238. if (resultSet.getString("user").equalsIgnoreCase(playerName)) {
  239. skills.put("ALL", rank + resultSet.getRow());
  240. break;
  241. }
  242. }
  243. statement.close();
  244. }
  245. catch (SQLException ex) {
  246. printErrors(ex);
  247. }
  248. }
  249. return skills;
  250. }
  251. public void newUser(String playerName) {
  252. checkConnected();
  253. PreparedStatement statement = null;
  254. try {
  255. statement = connection.prepareStatement("INSERT INTO " + tablePrefix + "users (user, lastlogin) VALUES (?, ?)", Statement.RETURN_GENERATED_KEYS);
  256. statement.setString(1, playerName);
  257. statement.setLong(2, System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
  258. statement.execute();
  259. int id = readId(playerName);
  260. writeMissingRows(id);
  261. }
  262. catch (SQLException ex) {
  263. printErrors(ex);
  264. }
  265. finally {
  266. if (statement != null) {
  267. try {
  268. statement.close();
  269. }
  270. catch (SQLException e) {
  271. // Ignore
  272. }
  273. }
  274. }
  275. }
  276. public PlayerProfile loadPlayerProfile(String playerName, boolean create) {
  277. checkConnected();
  278. PreparedStatement statement = null;
  279. try {
  280. statement = connection.prepareStatement(
  281. "SELECT "
  282. + "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, "
  283. + "e.taming, e.mining, e.repair, e.woodcutting, e.unarmed, e.herbalism, e.excavation, e.archery, e.swords, e.axes, e.acrobatics, e.fishing, "
  284. + "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, "
  285. + "h.hudtype, h.mobhealthbar "
  286. + "FROM " + tablePrefix + "users u "
  287. + "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) "
  288. + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) "
  289. + "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
  290. + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
  291. + "WHERE u.user = ?");
  292. statement.setString(1, playerName);
  293. ResultSet result = statement.executeQuery();
  294. if (result.next()) {
  295. try {
  296. PlayerProfile ret = loadFromResult(playerName, result);
  297. result.close();
  298. return ret;
  299. }
  300. catch (SQLException e) {}
  301. }
  302. result.close();
  303. }
  304. catch (SQLException ex) {
  305. printErrors(ex);
  306. }
  307. finally {
  308. if (statement != null) {
  309. try {
  310. statement.close();
  311. }
  312. catch (SQLException e) {
  313. // Ignore
  314. }
  315. }
  316. }
  317. // Problem, nothing was returned
  318. // First, read User Id - this is to check for orphans
  319. int id = readId(playerName);
  320. if (id == -1) {
  321. // There is no such user
  322. if (create) {
  323. newUser(playerName);
  324. }
  325. return new PlayerProfile(playerName, create);
  326. }
  327. // There is such a user
  328. writeMissingRows(id);
  329. // Retry, and abort on re-failure
  330. return loadPlayerProfile(playerName, false);
  331. }
  332. public void convertUsers(DatabaseManager destination) {
  333. checkConnected();
  334. PreparedStatement statement = null;
  335. try {
  336. statement = connection.prepareStatement(
  337. "SELECT "
  338. + "s.taming, s.mining, s.repair, s.woodcutting, s.unarmed, s.herbalism, s.excavation, s.archery, s.swords, s.axes, s.acrobatics, s.fishing, "
  339. + "e.taming, e.mining, e.repair, e.woodcutting, e.unarmed, e.herbalism, e.excavation, e.archery, e.swords, e.axes, e.acrobatics, e.fishing, "
  340. + "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, "
  341. + "h.hudtype, h.mobhealthbar "
  342. + "FROM " + tablePrefix + "users u "
  343. + "JOIN " + tablePrefix + "skills s ON (u.id = s.user_id) "
  344. + "JOIN " + tablePrefix + "experience e ON (u.id = e.user_id) "
  345. + "JOIN " + tablePrefix + "cooldowns c ON (u.id = c.user_id) "
  346. + "JOIN " + tablePrefix + "huds h ON (u.id = h.user_id) "
  347. + "WHERE u.user = ?");
  348. List<String> usernames = getStoredUsers();
  349. ResultSet result = null;
  350. for (String playerName : usernames) {
  351. statement.setString(1, playerName);
  352. try {
  353. result = statement.executeQuery();
  354. result.next();
  355. destination.saveUser(loadFromResult(playerName, result));
  356. result.close();
  357. }
  358. catch (SQLException e) {
  359. // Ignore
  360. }
  361. }
  362. }
  363. catch (SQLException e) {
  364. printErrors(e);
  365. }
  366. finally {
  367. if (statement != null) {
  368. try {
  369. statement.close();
  370. } catch (SQLException e) {
  371. // Ignore
  372. }
  373. }
  374. }
  375. }
  376. /**
  377. * Check connection status and re-establish if dead or stale.
  378. *
  379. * If the very first immediate attempt fails, further attempts
  380. * will be made in progressively larger intervals up to MAX_WAIT
  381. * intervals.
  382. *
  383. * This allows for MySQL to time out idle connections as needed by
  384. * server operator, without affecting McMMO, while still providing
  385. * protection against a database outage taking down Bukkit's tick
  386. * processing loop due to attempting a database connection each
  387. * time McMMO needs the database.
  388. *
  389. * @return the boolean value for whether or not we are connected
  390. */
  391. public boolean checkConnected() {
  392. boolean isClosed = true;
  393. boolean isValid = false;
  394. boolean exists = (connection != null);
  395. // If we're waiting for server to recover then leave early
  396. if (nextReconnectTimestamp > 0 && nextReconnectTimestamp > System.nanoTime()) {
  397. return false;
  398. }
  399. if (exists) {
  400. try {
  401. isClosed = connection.isClosed();
  402. }
  403. catch (SQLException e) {
  404. isClosed = true;
  405. e.printStackTrace();
  406. printErrors(e);
  407. }
  408. if (!isClosed) {
  409. try {
  410. isValid = connection.isValid(VALID_TIMEOUT);
  411. }
  412. catch (SQLException e) {
  413. // Don't print stack trace because it's valid to lose idle connections to the server and have to restart them.
  414. isValid = false;
  415. }
  416. }
  417. }
  418. // Leave if all ok
  419. if (exists && !isClosed && isValid) {
  420. // Housekeeping
  421. nextReconnectTimestamp = 0;
  422. reconnectAttempt = 0;
  423. return true;
  424. }
  425. // Cleanup after ourselves for GC and MySQL's sake
  426. if (exists && !isClosed) {
  427. try {
  428. connection.close();
  429. }
  430. catch (SQLException ex) {
  431. // This is a housekeeping exercise, ignore errors
  432. }
  433. }
  434. // Try to connect again
  435. connect();
  436. // Leave if connection is good
  437. try {
  438. if (connection != null && !connection.isClosed()) {
  439. // Schedule a database save if we really had an outage
  440. if (reconnectAttempt > 1) {
  441. new SQLReconnectTask().runTaskLater(mcMMO.p, 5);
  442. }
  443. nextReconnectTimestamp = 0;
  444. reconnectAttempt = 0;
  445. return true;
  446. }
  447. }
  448. catch (SQLException e) {
  449. // Failed to check isClosed, so presume connection is bad and attempt later
  450. e.printStackTrace();
  451. printErrors(e);
  452. }
  453. reconnectAttempt++;
  454. nextReconnectTimestamp = (long) (System.nanoTime() + Math.min(MAX_WAIT, (reconnectAttempt * SCALING_FACTOR * MIN_WAIT)));
  455. return false;
  456. }
  457. public List<String> getStoredUsers() {
  458. checkConnected();
  459. ArrayList<String> users = new ArrayList<String>();
  460. Statement stmt = null;
  461. try {
  462. stmt = connection.createStatement();
  463. ResultSet result = stmt.executeQuery("SELECT user FROM " + tablePrefix + "users");
  464. while (result.next()) {
  465. users.add(result.getString("user"));
  466. }
  467. result.close();
  468. }
  469. catch (SQLException e) {
  470. printErrors(e);
  471. }
  472. finally {
  473. if (stmt != null) {
  474. try {
  475. stmt.close();
  476. } catch (SQLException e) {
  477. // Ignore
  478. }
  479. }
  480. }
  481. return users;
  482. }
  483. /**
  484. * Attempt to connect to the mySQL database.
  485. */
  486. private void connect() {
  487. connectionString = "jdbc:mysql://" + Config.getInstance().getMySQLServerName() + ":" + Config.getInstance().getMySQLServerPort() + "/" + Config.getInstance().getMySQLDatabaseName();
  488. try {
  489. mcMMO.p.getLogger().info("Attempting connection to MySQL...");
  490. // Force driver to load if not yet loaded
  491. Class.forName("com.mysql.jdbc.Driver");
  492. Properties connectionProperties = new Properties();
  493. connectionProperties.put("user", Config.getInstance().getMySQLUserName());
  494. connectionProperties.put("password", Config.getInstance().getMySQLUserPassword());
  495. connectionProperties.put("autoReconnect", "false");
  496. connectionProperties.put("maxReconnects", "0");
  497. connection = DriverManager.getConnection(connectionString, connectionProperties);
  498. mcMMO.p.getLogger().info("Connection to MySQL was a success!");
  499. }
  500. catch (SQLException ex) {
  501. connection = null;
  502. if (reconnectAttempt == 0 || reconnectAttempt >= 11) {
  503. mcMMO.p.getLogger().severe("Connection to MySQL failed!");
  504. printErrors(ex);
  505. }
  506. }
  507. catch (ClassNotFoundException ex) {
  508. connection = null;
  509. if (reconnectAttempt == 0 || reconnectAttempt >= 11) {
  510. mcMMO.p.getLogger().severe("MySQL database driver not found!");
  511. }
  512. }
  513. }
  514. /**
  515. * Checks that the database structure is present and correct
  516. */
  517. private void checkStructure() {
  518. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "users` ("
  519. + "`id` int(10) unsigned NOT NULL AUTO_INCREMENT,"
  520. + "`user` varchar(40) NOT NULL,"
  521. + "`lastlogin` int(32) unsigned NOT NULL,"
  522. + "PRIMARY KEY (`id`),"
  523. + "UNIQUE KEY `user` (`user`)) DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;");
  524. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "huds` ("
  525. + "`user_id` int(10) unsigned NOT NULL,"
  526. + "`hudtype` varchar(50) NOT NULL DEFAULT 'STANDARD',"
  527. + "`mobhealthbar` varchar(50) NOT NULL DEFAULT '" + Config.getInstance().getMobHealthbarDefault() + "',"
  528. + "PRIMARY KEY (`user_id`)) "
  529. + "DEFAULT CHARSET=latin1;");
  530. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "cooldowns` ("
  531. + "`user_id` int(10) unsigned NOT NULL,"
  532. + "`taming` int(32) unsigned NOT NULL DEFAULT '0',"
  533. + "`mining` int(32) unsigned NOT NULL DEFAULT '0',"
  534. + "`woodcutting` int(32) unsigned NOT NULL DEFAULT '0',"
  535. + "`repair` int(32) unsigned NOT NULL DEFAULT '0',"
  536. + "`unarmed` int(32) unsigned NOT NULL DEFAULT '0',"
  537. + "`herbalism` int(32) unsigned NOT NULL DEFAULT '0',"
  538. + "`excavation` int(32) unsigned NOT NULL DEFAULT '0',"
  539. + "`archery` int(32) unsigned NOT NULL DEFAULT '0',"
  540. + "`swords` int(32) unsigned NOT NULL DEFAULT '0',"
  541. + "`axes` int(32) unsigned NOT NULL DEFAULT '0',"
  542. + "`acrobatics` int(32) unsigned NOT NULL DEFAULT '0',"
  543. + "`blast_mining` int(32) unsigned NOT NULL DEFAULT '0',"
  544. + "PRIMARY KEY (`user_id`)) "
  545. + "DEFAULT CHARSET=latin1;");
  546. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "skills` ("
  547. + "`user_id` int(10) unsigned NOT NULL,"
  548. + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
  549. + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
  550. + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
  551. + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
  552. + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
  553. + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
  554. + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
  555. + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
  556. + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
  557. + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
  558. + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
  559. + "`fishing` int(10) unsigned NOT NULL DEFAULT '0',"
  560. + "PRIMARY KEY (`user_id`)) "
  561. + "DEFAULT CHARSET=latin1;");
  562. write("CREATE TABLE IF NOT EXISTS `" + tablePrefix + "experience` ("
  563. + "`user_id` int(10) unsigned NOT NULL,"
  564. + "`taming` int(10) unsigned NOT NULL DEFAULT '0',"
  565. + "`mining` int(10) unsigned NOT NULL DEFAULT '0',"
  566. + "`woodcutting` int(10) unsigned NOT NULL DEFAULT '0',"
  567. + "`repair` int(10) unsigned NOT NULL DEFAULT '0',"
  568. + "`unarmed` int(10) unsigned NOT NULL DEFAULT '0',"
  569. + "`herbalism` int(10) unsigned NOT NULL DEFAULT '0',"
  570. + "`excavation` int(10) unsigned NOT NULL DEFAULT '0',"
  571. + "`archery` int(10) unsigned NOT NULL DEFAULT '0',"
  572. + "`swords` int(10) unsigned NOT NULL DEFAULT '0',"
  573. + "`axes` int(10) unsigned NOT NULL DEFAULT '0',"
  574. + "`acrobatics` int(10) unsigned NOT NULL DEFAULT '0',"
  575. + "`fishing` int(10) unsigned NOT NULL DEFAULT '0',"
  576. + "PRIMARY KEY (`user_id`)) "
  577. + "DEFAULT CHARSET=latin1;");
  578. for (DatabaseUpdateType updateType : DatabaseUpdateType.values()) {
  579. checkDatabaseStructure(updateType);
  580. }
  581. }
  582. /**
  583. * Check database structure for missing values.
  584. *
  585. * @param update Type of data to check updates for
  586. */
  587. private void checkDatabaseStructure(DatabaseUpdateType update) {
  588. String sql = "";
  589. switch (update) {
  590. case BLAST_MINING:
  591. sql = "SELECT * FROM `" + tablePrefix + "cooldowns` ORDER BY `" + tablePrefix + "cooldowns`.`blast_mining` ASC LIMIT 0 , 30";
  592. break;
  593. case FISHING:
  594. sql = "SELECT * FROM `" + tablePrefix + "experience` ORDER BY `" + tablePrefix + "experience`.`fishing` ASC LIMIT 0 , 30";
  595. break;
  596. case INDEX:
  597. if (read("SHOW INDEX FROM " + tablePrefix + "skills").size() != 13 && checkConnected()) {
  598. mcMMO.p.getLogger().info("Indexing tables, this may take a while on larger databases");
  599. write("ALTER TABLE `" + tablePrefix + "skills` ADD INDEX `idx_taming` (`taming`) USING BTREE, "
  600. + "ADD INDEX `idx_mining` (`mining`) USING BTREE, "
  601. + "ADD INDEX `idx_woodcutting` (`woodcutting`) USING BTREE, "
  602. + "ADD INDEX `idx_repair` (`repair`) USING BTREE, "
  603. + "ADD INDEX `idx_unarmed` (`unarmed`) USING BTREE, "
  604. + "ADD INDEX `idx_herbalism` (`herbalism`) USING BTREE, "
  605. + "ADD INDEX `idx_excavation` (`excavation`) USING BTREE, "
  606. + "ADD INDEX `idx_archery` (`archery`) USING BTREE, "
  607. + "ADD INDEX `idx_swords` (`swords`) USING BTREE, "
  608. + "ADD INDEX `idx_axes` (`axes`) USING BTREE, "
  609. + "ADD INDEX `idx_acrobatics` (`acrobatics`) USING BTREE, "
  610. + "ADD INDEX `idx_fishing` (`fishing`) USING BTREE;");
  611. }
  612. return;
  613. case MOB_HEALTHBARS:
  614. sql = "SELECT * FROM `" + tablePrefix + "huds` ORDER BY `" + tablePrefix + "huds`.`mobhealthbar` ASC LIMIT 0 , 30";
  615. break;
  616. case PARTY_NAMES:
  617. write("ALTER TABLE `" + tablePrefix + "users` DROP COLUMN `party` ;");
  618. return;
  619. case KILL_ORPHANS:
  620. mcMMO.p.getLogger().info("Killing orphans");
  621. write(
  622. "DELETE FROM " + tablePrefix + "experience " +
  623. "WHERE NOT EXISTS (SELECT * FROM " +
  624. tablePrefix + "users u WHERE " +
  625. tablePrefix + "experience.user_id = u.id);"
  626. );
  627. write(
  628. "DELETE FROM " + tablePrefix + "huds " +
  629. "WHERE NOT EXISTS (SELECT * FROM " +
  630. tablePrefix + "users u WHERE " +
  631. tablePrefix + "huds.user_id = u.id);"
  632. );
  633. write(
  634. "DELETE FROM " + tablePrefix + "cooldowns " +
  635. "WHERE NOT EXISTS (SELECT * FROM " +
  636. tablePrefix + "users u WHERE " +
  637. tablePrefix + "cooldowns.user_id = u.id);"
  638. );
  639. write(
  640. "DELETE FROM " + tablePrefix + "skills " +
  641. "WHERE NOT EXISTS (SELECT * FROM " +
  642. tablePrefix + "users u WHERE " +
  643. tablePrefix + "skills.user_id = u.id);"
  644. );
  645. return;
  646. default:
  647. break;
  648. }
  649. ResultSet resultSet = null;
  650. HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
  651. PreparedStatement statement = null;
  652. try {
  653. if (!checkConnected()) {
  654. return;
  655. }
  656. statement = connection.prepareStatement(sql);
  657. resultSet = statement.executeQuery();
  658. while (resultSet.next()) {
  659. ArrayList<String> column = new ArrayList<String>();
  660. for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
  661. column.add(resultSet.getString(i));
  662. }
  663. rows.put(resultSet.getRow(), column);
  664. }
  665. }
  666. catch (SQLException ex) {
  667. switch (update) {
  668. case BLAST_MINING:
  669. mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Blast Mining...");
  670. write("ALTER TABLE `"+tablePrefix + "cooldowns` ADD `blast_mining` int(32) NOT NULL DEFAULT '0' ;");
  671. break;
  672. case FISHING:
  673. mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for Fishing...");
  674. write("ALTER TABLE `"+tablePrefix + "skills` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  675. write("ALTER TABLE `"+tablePrefix + "experience` ADD `fishing` int(10) NOT NULL DEFAULT '0' ;");
  676. break;
  677. case MOB_HEALTHBARS:
  678. mcMMO.p.getLogger().info("Updating mcMMO MySQL tables for mob healthbars...");
  679. write("ALTER TABLE `" + tablePrefix + "huds` ADD `mobhealthbar` varchar(50) NOT NULL DEFAULT '" + Config.getInstance().getMobHealthbarDefault() + "' ;");
  680. break;
  681. default:
  682. break;
  683. }
  684. }
  685. finally {
  686. if (statement != null) {
  687. try {
  688. statement.close();
  689. }
  690. catch (SQLException e) {
  691. // Ignore the error, we're leaving
  692. }
  693. }
  694. }
  695. }
  696. /**
  697. * Attempt to write the SQL query.
  698. *
  699. * @param sql Query to write.
  700. * @return true if the query was successfully written, false otherwise.
  701. */
  702. private boolean write(String sql) {
  703. if (!checkConnected()) {
  704. return false;
  705. }
  706. PreparedStatement statement = null;
  707. try {
  708. statement = connection.prepareStatement(sql);
  709. statement.executeUpdate();
  710. return true;
  711. }
  712. catch (SQLException ex) {
  713. if (!sql.equalsIgnoreCase("ALTER TABLE `" + tablePrefix + "users` DROP COLUMN `party` ;")) {
  714. printErrors(ex);
  715. }
  716. return false;
  717. }
  718. finally {
  719. if (statement != null) {
  720. try {
  721. statement.close();
  722. }
  723. catch (SQLException e) {
  724. // Ignore
  725. }
  726. }
  727. }
  728. }
  729. /**
  730. * Returns the number of rows affected by either a DELETE or UPDATE query
  731. *
  732. * @param sql SQL query to execute
  733. * @return the number of rows affected
  734. */
  735. private int update(String sql) {
  736. int rows = 0;
  737. if (checkConnected()) {
  738. PreparedStatement statement = null;
  739. try {
  740. statement = connection.prepareStatement(sql);
  741. rows = statement.executeUpdate();
  742. }
  743. catch (SQLException ex) {
  744. printErrors(ex);
  745. }
  746. finally {
  747. if (statement != null) {
  748. try {
  749. statement.close();
  750. }
  751. catch (SQLException e) {
  752. // Ignore
  753. }
  754. }
  755. }
  756. }
  757. return rows;
  758. }
  759. /**
  760. * Read SQL query.
  761. *
  762. * @param sql SQL query to read
  763. * @return the rows in this SQL query
  764. */
  765. private HashMap<Integer, ArrayList<String>> read(String sql) {
  766. HashMap<Integer, ArrayList<String>> rows = new HashMap<Integer, ArrayList<String>>();
  767. if (checkConnected()) {
  768. PreparedStatement statement = null;
  769. ResultSet resultSet;
  770. try {
  771. statement = connection.prepareStatement(sql);
  772. resultSet = statement.executeQuery();
  773. while (resultSet.next()) {
  774. ArrayList<String> column = new ArrayList<String>();
  775. for (int i = 1; i <= resultSet.getMetaData().getColumnCount(); i++) {
  776. column.add(resultSet.getString(i));
  777. }
  778. rows.put(resultSet.getRow(), column);
  779. }
  780. }
  781. catch (SQLException ex) {
  782. printErrors(ex);
  783. }
  784. finally {
  785. if (statement != null) {
  786. try {
  787. statement.close();
  788. }
  789. catch (SQLException e) {
  790. // Ignore
  791. }
  792. }
  793. }
  794. }
  795. return rows;
  796. }
  797. /**
  798. * Get the Integer. Only return first row / first field.
  799. *
  800. * @param sql SQL query to execute
  801. * @return the value in the first row / first field
  802. */
  803. private int readInt(PreparedStatement statement) {
  804. int result = -1;
  805. if (checkConnected()) {
  806. ResultSet resultSet = null;
  807. try {
  808. resultSet = statement.executeQuery();
  809. if (resultSet.next()) {
  810. result = resultSet.getInt(1);
  811. }
  812. }
  813. catch (SQLException ex) {
  814. printErrors(ex);
  815. }
  816. finally {
  817. if (statement != null) {
  818. try {
  819. statement.close();
  820. }
  821. catch (SQLException e) {
  822. // Ignore
  823. }
  824. }
  825. }
  826. }
  827. return result;
  828. }
  829. private void writeMissingRows(int id) {
  830. PreparedStatement statement = null;
  831. try {
  832. statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "experience (user_id) VALUES (?)");
  833. statement.setInt(1, id);
  834. statement.execute();
  835. statement.close();
  836. statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "skills (user_id) VALUES (?)");
  837. statement.setInt(1, id);
  838. statement.execute();
  839. statement.close();
  840. statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "cooldowns (user_id) VALUES (?)");
  841. statement.setInt(1, id);
  842. statement.execute();
  843. statement.close();
  844. statement = connection.prepareStatement("INSERT IGNORE INTO " + tablePrefix + "huds (user_id, mobhealthbar) VALUES (? ,'" + Config.getInstance().getMobHealthbarDefault().name() + "')");
  845. statement.setInt(1, id);
  846. statement.execute();
  847. statement.close();
  848. }
  849. catch (SQLException ex) {
  850. printErrors(ex);
  851. }
  852. finally {
  853. if (statement != null) {
  854. try {
  855. statement.close();
  856. }
  857. catch (SQLException e) {
  858. // Ignore
  859. }
  860. }
  861. }
  862. }
  863. private void processPurge(Collection<ArrayList<String>> usernames) {
  864. for (ArrayList<String> user : usernames) {
  865. Misc.profileCleanup(user.get(0));
  866. }
  867. }
  868. private void saveIntegers(String sql, int... args) {
  869. PreparedStatement statement = null;
  870. try {
  871. statement = connection.prepareStatement(sql);
  872. int i = 1;
  873. for (int arg : args) {
  874. statement.setInt(i++, arg);
  875. }
  876. statement.execute();
  877. }
  878. catch (SQLException ex) {
  879. printErrors(ex);
  880. }
  881. finally {
  882. if (statement != null) {
  883. try {
  884. statement.close();
  885. }
  886. catch (SQLException e) {
  887. // Ignore
  888. }
  889. }
  890. }
  891. }
  892. private void saveLongs(String sql, int id, long... args) {
  893. PreparedStatement statement = null;
  894. try {
  895. statement = connection.prepareStatement(sql);
  896. int i = 1;
  897. for (long arg : args) {
  898. statement.setLong(i++, arg);
  899. }
  900. statement.setInt(i++, id);
  901. statement.execute();
  902. }
  903. catch (SQLException ex) {
  904. printErrors(ex);
  905. }
  906. finally {
  907. if (statement != null) {
  908. try {
  909. statement.close();
  910. }
  911. catch (SQLException e) {
  912. // Ignore
  913. }
  914. }
  915. }
  916. }
  917. /**
  918. * Retrieve the database id for a player
  919. *
  920. * @param playerName The name of the user to retrieve the id for
  921. * @return the requested id or -1 if not found
  922. */
  923. private int readId(String playerName) {
  924. int id = -1;
  925. try {
  926. PreparedStatement statement = connection.prepareStatement("SELECT id FROM " + tablePrefix + "users WHERE user = ?");
  927. statement.setString(1, playerName);
  928. id = readInt(statement);
  929. }
  930. catch (SQLException ex) {
  931. printErrors(ex);
  932. }
  933. return id;
  934. }
  935. private void saveLogin(int id, long login) {
  936. PreparedStatement statement = null;
  937. try {
  938. statement = connection.prepareStatement("UPDATE " + tablePrefix + "users SET lastlogin = ? WHERE id = ?");
  939. statement.setLong(1, login);
  940. statement.setInt(2, id);
  941. statement.execute();
  942. }
  943. catch (SQLException ex) {
  944. printErrors(ex);
  945. }
  946. finally {
  947. if (statement != null) {
  948. try {
  949. statement.close();
  950. }
  951. catch (SQLException e) {
  952. // Ignore
  953. }
  954. }
  955. }
  956. }
  957. private void saveHuds(int userId, String hudType, String mobHealthBar) {
  958. PreparedStatement statement = null;
  959. try {
  960. statement = connection.prepareStatement("UPDATE " + tablePrefix + "huds SET hudtype = ?, mobhealthbar = ? WHERE user_id = ?");
  961. statement.setString(1, hudType);
  962. statement.setString(2, mobHealthBar);
  963. statement.setInt(3, userId);
  964. statement.execute();
  965. }
  966. catch (SQLException ex) {
  967. printErrors(ex);
  968. }
  969. finally {
  970. if (statement != null) {
  971. try {
  972. statement.close();
  973. }
  974. catch (SQLException e) {
  975. // Ignore
  976. }
  977. }
  978. }
  979. }
  980. private PlayerProfile loadFromResult(String playerName, ResultSet result) throws SQLException {
  981. Map<SkillType, Integer> skills = new HashMap<SkillType, Integer>(); // Skill & Level
  982. Map<SkillType, Float> skillsXp = new HashMap<SkillType, Float>(); // Skill & XP
  983. Map<AbilityType, Integer> skillsDATS = new HashMap<AbilityType, Integer>(); // Ability & Cooldown
  984. HudType hudType;
  985. MobHealthbarType mobHealthbarType;
  986. final int OFFSET_SKILLS = 0; // TODO update these numbers when the query changes (a new skill is added)
  987. final int OFFSET_XP = 12;
  988. final int OFFSET_DATS = 24;
  989. final int OFFSET_OTHER = 36;
  990. skills.put(SkillType.TAMING, result.getInt(OFFSET_SKILLS + 1));
  991. skills.put(SkillType.MINING, result.getInt(OFFSET_SKILLS + 2));
  992. skills.put(SkillType.REPAIR, result.getInt(OFFSET_SKILLS + 3));
  993. skills.put(SkillType.WOODCUTTING, result.getInt(OFFSET_SKILLS + 4));
  994. skills.put(SkillType.UNARMED, result.getInt(OFFSET_SKILLS + 5));
  995. skills.put(SkillType.HERBALISM, result.getInt(OFFSET_SKILLS + 6));
  996. skills.put(SkillType.EXCAVATION, result.getInt(OFFSET_SKILLS + 7));
  997. skills.put(SkillType.ARCHERY, result.getInt(OFFSET_SKILLS + 8));
  998. skills.put(SkillType.SWORDS, result.getInt(OFFSET_SKILLS + 9));
  999. skills.put(SkillType.AXES, result.getInt(OFFSET_SKILLS + 10));
  1000. skills.put(SkillType.ACROBATICS, result.getInt(OFFSET_SKILLS + 11));
  1001. skills.put(SkillType.FISHING, result.getInt(OFFSET_SKILLS + 12));
  1002. skillsXp.put(SkillType.TAMING, result.getFloat(OFFSET_XP + 1));
  1003. skillsXp.put(SkillType.MINING, result.getFloat(OFFSET_XP + 2));
  1004. skillsXp.put(SkillType.REPAIR, result.getFloat(OFFSET_XP + 3));
  1005. skillsXp.put(SkillType.WOODCUTTING, result.getFloat(OFFSET_XP + 4));
  1006. skillsXp.put(SkillType.UNARMED, result.getFloat(OFFSET_XP + 5));
  1007. skillsXp.put(SkillType.HERBALISM, result.getFloat(OFFSET_XP + 6));
  1008. skillsXp.put(SkillType.EXCAVATION, result.getFloat(OFFSET_XP + 7));
  1009. skillsXp.put(SkillType.ARCHERY, result.getFloat(OFFSET_XP + 8));
  1010. skillsXp.put(SkillType.SWORDS, result.getFloat(OFFSET_XP + 9));
  1011. skillsXp.put(SkillType.AXES, result.getFloat(OFFSET_XP + 10));
  1012. skillsXp.put(SkillType.ACROBATICS, result.getFloat(OFFSET_XP + 11));
  1013. skillsXp.put(SkillType.FISHING, result.getFloat(OFFSET_XP + 12));
  1014. // Taming - Unused - result.getInt(OFFSET_DATS + 1)
  1015. skillsDATS.put(AbilityType.SUPER_BREAKER, result.getInt(OFFSET_DATS + 2));
  1016. // Repair - Unused - result.getInt(OFFSET_DATS + 3)
  1017. skillsDATS.put(AbilityType.TREE_FELLER, result.getInt(OFFSET_DATS + 4));
  1018. skillsDATS.put(AbilityType.BERSERK, result.getInt(OFFSET_DATS + 5));
  1019. skillsDATS.put(AbilityType.GREEN_TERRA, result.getInt(OFFSET_DATS + 6));
  1020. skillsDATS.put(AbilityType.GIGA_DRILL_BREAKER, result.getInt(OFFSET_DATS + 7));
  1021. // Archery - Unused - result.getInt(OFFSET_DATS + 8)
  1022. skillsDATS.put(AbilityType.SERRATED_STRIKES, result.getInt(OFFSET_DATS + 9));
  1023. skillsDATS.put(AbilityType.SKULL_SPLITTER, result.getInt(OFFSET_DATS + 10));
  1024. // Acrobatics - Unused - result.getInt(OFFSET_DATS + 11)
  1025. skillsDATS.put(AbilityType.BLAST_MINING, result.getInt(OFFSET_DATS + 12));
  1026. try {
  1027. hudType = HudType.valueOf(result.getString(OFFSET_OTHER + 1));
  1028. }
  1029. catch (Exception e) {
  1030. hudType = HudType.STANDARD; // Shouldn't happen unless database is being tampered with
  1031. }
  1032. try {
  1033. mobHealthbarType = MobHealthbarType.valueOf(result.getString(OFFSET_OTHER + 2));
  1034. }
  1035. catch (Exception e) {
  1036. mobHealthbarType = Config.getInstance().getMobHealthbarDefault();
  1037. }
  1038. return new PlayerProfile(playerName, skills, skillsXp, skillsDATS, hudType, mobHealthbarType);
  1039. }
  1040. private void printErrors(SQLException ex) {
  1041. mcMMO.p.getLogger().severe("SQLException: " + ex.getMessage());
  1042. mcMMO.p.getLogger().severe("SQLState: " + ex.getSQLState());
  1043. mcMMO.p.getLogger().severe("VendorError: " + ex.getErrorCode());
  1044. }
  1045. }