SQLDatabaseManager.java 47 KB

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