SQLConversionTask.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package com.gmail.nossr50.runnables;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import com.gmail.nossr50.m;
  5. import com.gmail.nossr50.mcMMO;
  6. import com.gmail.nossr50.config.LoadProperties;
  7. public class SQLConversionTask implements Runnable {
  8. private final mcMMO plugin;
  9. public SQLConversionTask(mcMMO plugin) {
  10. this.plugin = plugin;
  11. }
  12. @Override
  13. public void run() {
  14. String location = mcMMO.usersFile;
  15. try {
  16. FileReader file = new FileReader(location);
  17. BufferedReader in = new BufferedReader(file);
  18. String line = "";
  19. String playerName = null;
  20. String party = null;
  21. String mining = null;
  22. String woodcutting = null;
  23. String repair = null;
  24. String unarmed = null;
  25. String herbalism = null;
  26. String excavation = null;
  27. String archery = null;
  28. String swords = null;
  29. String axes = null;
  30. String acrobatics = null;
  31. String taming = null;
  32. String fishing = null;
  33. String miningXP = null;
  34. String woodCuttingXP = null;
  35. String repairXP = null;
  36. String unarmedXP = null;
  37. String herbalismXP = null;
  38. String excavationXP = null;
  39. String archeryXP = null;
  40. String swordsXP = null;
  41. String axesXP = null;
  42. String acrobaticsXP = null;
  43. String tamingXP = null;
  44. String fishingXP = null;
  45. int id = 0;
  46. int theCount = 0;
  47. while ((line = in.readLine()) != null) {
  48. //Find if the line contains the player we want.
  49. String[] character = line.split(":");
  50. playerName = character[0];
  51. //Check for things we don't want put in the DB
  52. if (playerName == null || playerName.equals("null") || playerName.equals("#Storage place for user information")) {
  53. continue;
  54. }
  55. if (character.length > 1) {
  56. mining = character[1];
  57. }
  58. if (character.length > 3) {
  59. party = character[3];
  60. }
  61. if (character.length > 4) {
  62. miningXP = character[4];
  63. }
  64. if (character.length > 5) {
  65. woodcutting = character[5];
  66. }
  67. if (character.length > 6) {
  68. woodCuttingXP = character[6];
  69. }
  70. if (character.length > 7) {
  71. repair = character[7];
  72. }
  73. if (character.length > 8) {
  74. unarmed = character[8];
  75. }
  76. if (character.length > 9) {
  77. herbalism = character[9];
  78. }
  79. if (character.length > 10) {
  80. excavation = character[10];
  81. }
  82. if (character.length > 11) {
  83. archery = character[11];
  84. }
  85. if (character.length > 12) {
  86. swords = character[12];
  87. }
  88. if (character.length > 13) {
  89. axes = character[13];
  90. }
  91. if (character.length > 14) {
  92. acrobatics = character[14];
  93. }
  94. if (character.length > 15) {
  95. repairXP = character[15];
  96. }
  97. if (character.length > 16) {
  98. unarmedXP = character[16];
  99. }
  100. if (character.length > 17) {
  101. herbalismXP = character[17];
  102. }
  103. if (character.length > 18) {
  104. excavationXP = character[18];
  105. }
  106. if (character.length > 19) {
  107. archeryXP = character[19];
  108. }
  109. if (character.length > 20) {
  110. swordsXP = character[20];
  111. }
  112. if (character.length > 21) {
  113. axesXP = character[21];
  114. }
  115. if (character.length > 22) {
  116. acrobaticsXP = character[22];
  117. }
  118. if (character.length > 24) {
  119. taming = character[24];
  120. }
  121. if (character.length > 25) {
  122. tamingXP = character[25];
  123. }
  124. if (character.length > 34) {
  125. fishing = character[34];
  126. }
  127. if (character.length > 35) {
  128. fishingXP = character[35];
  129. }
  130. //Check to see if the user is in the DB
  131. id = mcMMO.database.getInt("SELECT id FROM "
  132. + LoadProperties.MySQLtablePrefix
  133. + "users WHERE user = '" + playerName + "'");
  134. if (id > 0) {
  135. theCount++;
  136. //Update the skill values
  137. mcMMO.database.write("UPDATE "
  138. + LoadProperties.MySQLtablePrefix
  139. + "users SET lastlogin = " + 0
  140. + " WHERE id = " + id);
  141. mcMMO.database.write("UPDATE "
  142. + LoadProperties.MySQLtablePrefix
  143. + "skills SET "
  144. + " taming = taming+" + m.getInt(taming)
  145. + ", mining = mining+" + m.getInt(mining)
  146. + ", repair = repair+" + m.getInt(repair)
  147. + ", woodcutting = woodcutting+" + m.getInt(woodcutting)
  148. + ", unarmed = unarmed+" + m.getInt(unarmed)
  149. + ", herbalism = herbalism+" + m.getInt(herbalism)
  150. + ", excavation = excavation+" + m.getInt(excavation)
  151. + ", archery = archery+" + m.getInt(archery)
  152. + ", swords = swords+" + m.getInt(swords)
  153. + ", axes = axes+" + m.getInt(axes)
  154. + ", acrobatics = acrobatics+" + m.getInt(acrobatics)
  155. + ", fishing = fishing+" + m.getInt(fishing)
  156. + " WHERE user_id = " + id);
  157. mcMMO.database.write("UPDATE "
  158. + LoadProperties.MySQLtablePrefix
  159. + "experience SET "
  160. + " taming = " + m.getInt(tamingXP)
  161. + ", mining = " + m.getInt(miningXP)
  162. + ", repair = " + m.getInt(repairXP)
  163. + ", woodcutting = " + m.getInt(woodCuttingXP)
  164. + ", unarmed = " + m.getInt(unarmedXP)
  165. + ", herbalism = " + m.getInt(herbalismXP)
  166. + ", excavation = " + m.getInt(excavationXP)
  167. + ", archery = " + m.getInt(archeryXP)
  168. + ", swords = " + m.getInt(swordsXP)
  169. + ", axes = " + m.getInt(axesXP)
  170. + ", acrobatics = " + m.getInt(acrobaticsXP)
  171. + ", fishing = " + m.getInt(fishingXP)
  172. + " WHERE user_id = " + id);
  173. }
  174. else {
  175. theCount++;
  176. //Create the user in the DB
  177. mcMMO.database.write("INSERT INTO "
  178. + LoadProperties.MySQLtablePrefix
  179. + "users (user, lastlogin) VALUES ('"
  180. + playerName + "',"
  181. + System.currentTimeMillis() / 1000 + ")");
  182. id = mcMMO.database.getInt("SELECT id FROM "
  183. + LoadProperties.MySQLtablePrefix
  184. + "users WHERE user = '"
  185. + playerName + "'");
  186. mcMMO.database.write("INSERT INTO "
  187. + LoadProperties.MySQLtablePrefix
  188. + "skills (user_id) VALUES (" + id + ")");
  189. mcMMO.database.write("INSERT INTO "
  190. + LoadProperties.MySQLtablePrefix
  191. + "experience (user_id) VALUES (" + id
  192. + ")");
  193. //Update the skill values
  194. mcMMO.database.write("UPDATE "
  195. + LoadProperties.MySQLtablePrefix
  196. + "users SET lastlogin = " + 0
  197. + " WHERE id = " + id);
  198. mcMMO.database.write("UPDATE "
  199. + LoadProperties.MySQLtablePrefix
  200. + "users SET party = '" + party
  201. + "' WHERE id = " + id);
  202. mcMMO.database.write("UPDATE "
  203. + LoadProperties.MySQLtablePrefix
  204. + "skills SET "
  205. + " taming = taming+" + m.getInt(taming)
  206. + ", mining = mining+" + m.getInt(mining)
  207. + ", repair = repair+" + m.getInt(repair)
  208. + ", woodcutting = woodcutting+" + m.getInt(woodcutting)
  209. + ", unarmed = unarmed+" + m.getInt(unarmed)
  210. + ", herbalism = herbalism+" + m.getInt(herbalism)
  211. + ", excavation = excavation+" + m.getInt(excavation)
  212. + ", archery = archery+" + m.getInt(archery)
  213. + ", swords = swords+" + m.getInt(swords)
  214. + ", axes = axes+" + m.getInt(axes)
  215. + ", acrobatics = acrobatics+" + m.getInt(acrobatics)
  216. + ", fishing = fishing+" + m.getInt(fishing)
  217. + " WHERE user_id = " + id);
  218. mcMMO.database.write("UPDATE "
  219. + LoadProperties.MySQLtablePrefix
  220. + "experience SET "
  221. + " taming = " + m.getInt(tamingXP)
  222. + ", mining = " + m.getInt(miningXP)
  223. + ", repair = " + m.getInt(repairXP)
  224. + ", woodcutting = " + m.getInt(woodCuttingXP)
  225. + ", unarmed = " + m.getInt(unarmedXP)
  226. + ", herbalism = " + m.getInt(herbalismXP)
  227. + ", excavation = " + m.getInt(excavationXP)
  228. + ", archery = " + m.getInt(archeryXP)
  229. + ", swords = " + m.getInt(swordsXP)
  230. + ", axes = " + m.getInt(axesXP)
  231. + ", acrobatics = " + m.getInt(acrobaticsXP)
  232. + ", fishing = " + m.getInt(fishingXP)
  233. + " WHERE user_id = " + id);
  234. }
  235. }
  236. System.out.println("[mcMMO] MySQL Updated from users file, " + theCount + " items added/updated to MySQL DB"); //TODO: Locale
  237. in.close();
  238. }
  239. catch (Exception e) {
  240. plugin.getLogger().severe("Exception while reading " + location + " (Are you sure you formatted it correctly?)" + e.toString());
  241. }
  242. }
  243. }