mcLeaderboard.java 8.3 KB

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