SQLConversionTask.java 11 KB

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