/src/com/gmail/nossr50/Leaderboard.java

https://github.com/krinsdeath/mcMMO · Java · 245 lines · 210 code · 16 blank · 19 comment · 41 complexity · f1edde660b107529c0749ac93b87e35b MD5 · raw file

  1. package com.gmail.nossr50;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. import com.gmail.nossr50.config.LoadProperties;
  10. import com.gmail.nossr50.datatypes.PlayerStat;
  11. import com.gmail.nossr50.datatypes.SkillType;
  12. import com.gmail.nossr50.datatypes.Tree;
  13. public class Leaderboard
  14. {
  15. static String location = "plugins/mcMMO/FlatFileStuff/mcmmo.users"; //$NON-NLS-1$
  16. protected static final Logger log = Logger.getLogger("Minecraft"); //$NON-NLS-1$
  17. /*
  18. * Read from the file
  19. */
  20. public static void makeLeaderboards()
  21. {
  22. //Make Trees
  23. Tree Mining = new Tree();
  24. Tree WoodCutting = new Tree();
  25. Tree Herbalism = new Tree();
  26. Tree Excavation = new Tree();
  27. Tree Acrobatics = new Tree();
  28. Tree Repair = new Tree();
  29. Tree Swords = new Tree();
  30. Tree Axes = new Tree();
  31. Tree Archery = new Tree();
  32. Tree Unarmed = new Tree();
  33. Tree Taming = new Tree();
  34. Tree PowerLevel = new Tree();
  35. //Add Data To Trees
  36. try {
  37. //Open the user file
  38. FileReader file = new FileReader(location);
  39. BufferedReader in = new BufferedReader(file);
  40. String line = ""; //$NON-NLS-1$
  41. while((line = in.readLine()) != null)
  42. {
  43. String[] character = line.split(":"); //$NON-NLS-1$
  44. String p = character[0];
  45. int Plvl = 0;
  46. if(character.length > 1 && m.isInt(character[1]))
  47. {
  48. Mining.add(p, Integer.valueOf(character[1]));
  49. Plvl += Integer.valueOf(character[1]);
  50. }
  51. if(character.length > 5 && m.isInt(character[5])){
  52. WoodCutting.add(p, Integer.valueOf(character[5]));
  53. Plvl += Integer.valueOf(character[5]);
  54. }
  55. if(character.length > 7 && m.isInt(character[7])){
  56. Repair.add(p, Integer.valueOf(character[7]));
  57. Plvl += Integer.valueOf(character[7]);
  58. }
  59. if(character.length > 8 && m.isInt(character[8])){
  60. Unarmed.add(p, Integer.valueOf(character[8]));
  61. Plvl += Integer.valueOf(character[8]);
  62. }
  63. if(character.length > 9 && m.isInt(character[9])){
  64. Herbalism.add(p, Integer.valueOf(character[9]));
  65. Plvl += Integer.valueOf(character[9]);
  66. }
  67. if(character.length > 10 && m.isInt(character[10])){
  68. Excavation.add(p, Integer.valueOf(character[10]));
  69. Plvl += Integer.valueOf(character[10]);
  70. }
  71. if(character.length > 11 && m.isInt(character[11])){
  72. Archery.add(p, Integer.valueOf(character[11]));
  73. Plvl += Integer.valueOf(character[11]);
  74. }
  75. if(character.length > 12 && m.isInt(character[12])){
  76. Swords.add(p, Integer.valueOf(character[12]));
  77. Plvl += Integer.valueOf(character[12]);
  78. }
  79. if(character.length > 13 && m.isInt(character[13])){
  80. Axes.add(p, Integer.valueOf(character[13]));
  81. Plvl += Integer.valueOf(character[13]);
  82. }
  83. if(character.length > 14 && m.isInt(character[14])){
  84. Acrobatics.add(p, Integer.valueOf(character[14]));
  85. Plvl += Integer.valueOf(character[14]);
  86. }
  87. if(character.length > 24 && m.isInt(character[24])){
  88. Taming.add(p, Integer.valueOf(character[24]));
  89. Plvl += Integer.valueOf(character[24]);
  90. }
  91. PowerLevel.add(p, Plvl);
  92. }
  93. in.close();
  94. } catch (Exception e) {
  95. log.log(Level.SEVERE, "Exception while reading " //$NON-NLS-1$
  96. + location + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$
  97. }
  98. //Write the leader board files
  99. leaderWrite(Mining.inOrder(), SkillType.MINING); //$NON-NLS-1$
  100. leaderWrite(WoodCutting.inOrder(), SkillType.WOODCUTTING); //$NON-NLS-1$
  101. leaderWrite(Repair.inOrder(), SkillType.REPAIR); //$NON-NLS-1$
  102. leaderWrite(Unarmed.inOrder(), SkillType.UNARMED); //$NON-NLS-1$
  103. leaderWrite(Herbalism.inOrder(), SkillType.HERBALISM); //$NON-NLS-1$
  104. leaderWrite(Excavation.inOrder(), SkillType.EXCAVATION); //$NON-NLS-1$
  105. leaderWrite(Archery.inOrder(), SkillType.ARCHERY); //$NON-NLS-1$
  106. leaderWrite(Swords.inOrder(), SkillType.SWORDS); //$NON-NLS-1$
  107. leaderWrite(Axes.inOrder(), SkillType.AXES); //$NON-NLS-1$
  108. leaderWrite(Acrobatics.inOrder(), SkillType.ACROBATICS); //$NON-NLS-1$
  109. leaderWrite(Taming.inOrder(), SkillType.TAMING); //$NON-NLS-1$
  110. leaderWrite(PowerLevel.inOrder(), SkillType.ALL); //$NON-NLS-1$
  111. }
  112. public static void leaderWrite(PlayerStat[] ps, SkillType skillType)
  113. {
  114. String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$
  115. //CHECK IF THE FILE EXISTS
  116. File theDir = new File(theLocation);
  117. if(!theDir.exists())
  118. {
  119. //properties = new PropertiesFile(location);
  120. FileWriter writer = null;
  121. try {
  122. writer = new FileWriter(theLocation);
  123. } catch (Exception e) {
  124. log.log(Level.SEVERE, "Exception while creating " + theLocation, e); //$NON-NLS-1$
  125. } finally {
  126. try {
  127. if (writer != null) {
  128. writer.close();
  129. }
  130. } catch (IOException e) {
  131. log.log(Level.SEVERE, "Exception while closing writer for " + theLocation, e); //$NON-NLS-1$
  132. }
  133. }
  134. } else {
  135. try {
  136. FileReader file = new FileReader(theLocation);
  137. //HERP
  138. BufferedReader in = new BufferedReader(file);
  139. StringBuilder writer = new StringBuilder();
  140. for(PlayerStat p : ps)
  141. {
  142. if(p.name.equals("$mcMMO_DummyInfo")) //$NON-NLS-1$
  143. continue;
  144. if(p.statVal == 0)
  145. continue;
  146. writer.append(p.name + ":" + p.statVal); //$NON-NLS-1$
  147. writer.append("\r\n"); //$NON-NLS-1$
  148. }
  149. in.close();
  150. //Write the new file
  151. FileWriter out = new FileWriter(theLocation);
  152. out.write(writer.toString());
  153. out.close();
  154. } catch (Exception e) {
  155. log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$ //$NON-NLS-2$
  156. }
  157. }
  158. //Create/open the file
  159. //Loop through backward writing each player
  160. //Close the file
  161. }
  162. public static String[] retrieveInfo(String skillName, int pagenumber)
  163. {
  164. String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillName + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$
  165. try {
  166. FileReader file = new FileReader(theLocation);
  167. BufferedReader in = new BufferedReader(file);
  168. int destination = (pagenumber - 1) * 10; //How many lines to skip through
  169. int x = 0; //how many lines we've gone through
  170. int y = 0; //going through the lines
  171. String line = ""; //$NON-NLS-1$
  172. String[] info = new String[10]; //what to return
  173. while((line = in.readLine()) != null && y < 10)
  174. {
  175. x++;
  176. if(x >= destination && y < 10){
  177. info[y] = line.toString();
  178. y++;
  179. }
  180. }
  181. in.close();
  182. return info;
  183. } catch (Exception e) {
  184. log.log(Level.SEVERE, "Exception while reading " //$NON-NLS-1$
  185. + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$
  186. }
  187. return null; //Shouldn't get here
  188. }
  189. public static void updateLeaderboard(PlayerStat ps, SkillType skillType)
  190. {
  191. if(LoadProperties.useMySQL)
  192. return;
  193. String theLocation = "plugins/mcMMO/FlatFileStuff/Leaderboards/" + skillType + ".mcmmo"; //$NON-NLS-1$ //$NON-NLS-2$
  194. try {
  195. //Open the file
  196. FileReader file = new FileReader(theLocation);
  197. BufferedReader in = new BufferedReader(file);
  198. StringBuilder writer = new StringBuilder();
  199. String line = ""; //$NON-NLS-1$
  200. Boolean inserted = false;
  201. //While not at the end of the file
  202. while((line = in.readLine()) != null)
  203. {
  204. //Insert the player into the line before it finds a smaller one
  205. if(Integer.valueOf(line.split(":")[1]) < ps.statVal && !inserted) //$NON-NLS-1$
  206. {
  207. writer.append(ps.name + ":" + ps.statVal).append("\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
  208. inserted = true;
  209. }
  210. //Write anything that isn't the player already in the file so we remove the duplicate
  211. if(!line.split(":")[0].equalsIgnoreCase(ps.name)) //$NON-NLS-1$
  212. {
  213. writer.append(line).append("\r\n"); //$NON-NLS-1$
  214. }
  215. }
  216. if(!inserted)
  217. {
  218. writer.append(ps.name + ":" + ps.statVal).append("\r\n"); //$NON-NLS-1$ //$NON-NLS-2$
  219. }
  220. in.close();
  221. //Write the new file
  222. FileWriter out = new FileWriter(theLocation);
  223. out.write(writer.toString());
  224. out.close();
  225. } catch (Exception e) {
  226. log.log(Level.SEVERE, "Exception while writing to " + theLocation + " (Are you sure you formatted it correctly?)", e); //$NON-NLS-1$ //$NON-NLS-2$
  227. }
  228. }
  229. }