Advertisement
Guest User

Untitled

a guest
May 29th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. package de.DevCodePvP.Stats;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.util.Arrays;
  7. import java.util.Comparator;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10. import java.util.Set;
  11.  
  12. import org.bukkit.command.Command;
  13. import org.bukkit.command.CommandExecutor;
  14. import org.bukkit.command.CommandSender;
  15. import org.bukkit.configuration.ConfigurationSection;
  16. import org.bukkit.configuration.InvalidConfigurationException;
  17. import org.bukkit.configuration.file.YamlConfiguration;
  18. import org.bukkit.entity.Player;
  19.  
  20.  
  21. public class CMDTop10 implements CommandExecutor {
  22.  
  23.  
  24.  
  25.  
  26. public static File f = new File("plugins/Stats/Stats.yml");
  27. public static YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);
  28.  
  29.  
  30. @SuppressWarnings({ "unchecked", "rawtypes" })
  31. public static void loadTopTen(Player p, int b) {
  32. File f = new File("plugins/Stats/Stats.yml");
  33. YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);
  34. try {
  35. yamlFile.load(f);
  36. } catch (FileNotFoundException e1) {
  37. // TODO Auto-generated catch block
  38. e1.printStackTrace();
  39. } catch (IOException e1) {
  40. // TODO Auto-generated catch block
  41. e1.printStackTrace();
  42. } catch (InvalidConfigurationException e1) {
  43. // TODO Auto-generated catch block
  44. e1.printStackTrace();
  45. }
  46. Map<String, Integer> unsorted = new HashMap<>();
  47.  
  48. ConfigurationSection sSection = yamlFile.getConfigurationSection("Spieler"); // Wir holen die Section als Object
  49. Set<String> keys = sSection.getKeys(false); // Wir holen alle Subkeys in der Spieler Object
  50. for(String k : keys) { // Wir gehen jedes durch und adden sie in die "unsorted" List
  51. ConfigurationSection s = sSection.getConfigurationSection(k);
  52. unsorted.put(k, s.getInt("Kills"));
  53. }
  54.  
  55. Object[] a = unsorted.entrySet().toArray(); // Wir verwandeln die Map in einen Array
  56. Arrays.sort(a, new Comparator() { // Wir sortieren den Array nach grösse
  57. public int compare(Object o1, Object o2) {
  58. return ((Map.Entry<String, Integer>) o2).getValue().compareTo(
  59. ((Map.Entry<String, Integer>) o1).getValue());
  60. }
  61. });
  62.  
  63. int count = 1; // Counter (Platz)
  64. p.sendMessage("§8§m----------------------------------");
  65. p.sendMessage("");
  66. p.sendMessage("§6Top10 §8» §a[Platz]# §8> §6[Spieler] §7mit §6[Kills] §7Kills");
  67. p.sendMessage("");
  68. for(Object e : a) {
  69. if(count <= b) {
  70. String player = ((Map.Entry<String, Integer>) e).getKey(); //Spieler
  71. int kills = ((Map.Entry<String, Integer>) e).getValue(); //Kills
  72.  
  73.  
  74. p.sendMessage("§6Top10 §8» §a" + count + "# §8> §6" + player + " §7mit §6" + kills + " §7Kills");
  75.  
  76. count++; // Vergrössern den Count
  77. }
  78. }
  79. p.sendMessage("");
  80. p.sendMessage("§8§m----------------------------------");
  81.  
  82.  
  83. }
  84. @Override
  85. public boolean onCommand(CommandSender s, Command cmd, String arg2, String[] args) {
  86. Player p = (Player)s;
  87.  
  88. if(args.length == 0)
  89. {
  90. loadTopTen(p, 10);
  91. }else if(args.length == 1)
  92. {
  93. int i = Integer.parseInt(args[0]);
  94. if(i > 50)
  95. {
  96. p.sendMessage("§6Top10 §8» §7Benutzung §8> §6/Top [1-50]");
  97. }else
  98. {
  99. loadTopTen(p, i);
  100. }
  101. }else
  102. {
  103. p.sendMessage(Main.P + "§7Benutzung §8> §6/Top [1-50]");
  104. }
  105. return false;
  106. }
  107.  
  108. @SuppressWarnings({ "unchecked", "rawtypes" })
  109. public static void loadRank(Player p, String pl) {
  110. File f = new File("plugins/Stats/Stats.yml");
  111. YamlConfiguration yamlFile = YamlConfiguration.loadConfiguration(f);
  112. try {
  113. yamlFile.load(f);
  114. } catch (FileNotFoundException e1) {
  115. // TODO Auto-generated catch block
  116. e1.printStackTrace();
  117. } catch (IOException e1) {
  118. // TODO Auto-generated catch block
  119. e1.printStackTrace();
  120. } catch (InvalidConfigurationException e1) {
  121. // TODO Auto-generated catch block
  122. e1.printStackTrace();
  123. }
  124. Map<String, Integer> unsorted = new HashMap<>();
  125.  
  126. ConfigurationSection sSection = yamlFile.getConfigurationSection("Spieler"); // Wir holen die Section als Object
  127. Set<String> keys = sSection.getKeys(false); // Wir holen alle Subkeys in der Spieler Object
  128. for(String k : keys) { // Wir gehen jedes durch und adden sie in die "unsorted" List
  129. ConfigurationSection s = sSection.getConfigurationSection(k);
  130. unsorted.put(k, s.getInt("Kills"));
  131. }
  132.  
  133. Object[] a = unsorted.entrySet().toArray(); // Wir verwandeln die Map in einen Array
  134. Arrays.sort(a, new Comparator() { // Wir sortieren den Array nach grösse
  135. public int compare(Object o1, Object o2) {
  136. return ((Map.Entry<String, Integer>) o2).getValue().compareTo(
  137. ((Map.Entry<String, Integer>) o1).getValue());
  138. }
  139. });
  140.  
  141. int count = 1; // Counter (Platz)
  142. p.sendMessage("§8§m----------------------------------");
  143. p.sendMessage("");
  144. p.sendMessage("§8» §a[Platz]# §8> §6[Spieler] §7mit §6[Kills] §7Kills");
  145. p.sendMessage("");
  146. for(Object e : a) {
  147. String player = ((Map.Entry<String, Integer>) e).getKey(); //Spieler
  148. int kills = ((Map.Entry<String, Integer>) e).getValue(); //Kills
  149.  
  150.  
  151. p.sendMessage("§8» §a" + count + "# §8> §6" + player + " §7mit §6" + kills + " §7Kills");
  152.  
  153. }
  154. p.sendMessage("");
  155. p.sendMessage("§8§m----------------------------------");
  156.  
  157.  
  158. }
  159.  
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement