Solved Top Player help! HashMap

Discussion in 'Plugin Development' started by mrgreen33gamer, Nov 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    mrgreen33gamer

    Hello,

    I need some help with my coding, I am trying to figure out how to get the top 10 players using a HashMap<String, Integer>. I have the coding setup for it. But I get an error whenever performing the command.

    Error:

    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'topp
    layer' in plugin GFKits v4.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at org.bukkit.craftbukkit.v1_7_R4.CraftServer.dispatchCommand(CraftServe
    r.java:740) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PlayerConnection.handleCommand(PlayerCon
    nection.java:957) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java
    :818) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:2
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
            at com.mrgreen33gamer.kits.OtherCommands.onCommand(OtherCommands.java:41
    6) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.9-R0.2-20-g0b2ed13-b3108jnks]
            ... 13 more
    
    Code:

    Code:java
    1.  
    2. if(commandLabel.equalsIgnoreCase("topplayer")){
    3. if(sender instanceof Player){
    4. Player player = (Player)sender;
    5. player.sendMessage(ChatColor.GOLD + "===== Top 10 players ===== ");
    6. player.sendMessage(EconManager.credits.keySet().toString());
    7. for(int i = (EconManager.credits.size() > 3 ? 3 : EconManager.credits.size()); i >= 1; i--) {
    8. String firstname = (String) EconManager.credits.keySet().toArray()[I]; //Line 416[/I]
    9. [I] int firstscore = (int) EconManager.credits.values().toArray()[I];[/I][/I]
    10. [I] player.sendMessage(i + ". " + firstname + ": " + firstscore);[/I]
    11. [I] }[/I]
    12.  
    13. [I] }[/I]
    14. [I] }[/I]
    15. [I][/I]


    Any help?
     
  2. Offline

    Skionz

  3. Offline

    mrgreen33gamer

  4. Offline

    teej107

    mrgreen33gamer Do your own research. I found your answer within a minute of clicking that link.
     
    Skionz likes this.
  5. Offline

    mrgreen33gamer

    Yeah yeah I tried to find it. But I end up sorting from Lowest to Highest. I want to sort from Highest to lowest -_-.

    Code:
    Object[] a = EconManager.credits.entrySet().toArray();
       Arrays.sort(a, new Comparator<Object>() {
           public int compare(Object o1, Object o2) {
               return ((Map.Entry<String, Integer>) o2).getValue().compareTo(
                       ((Map.Entry<String, Integer>) o1).getValue());
           }
       });
     
  6. Offline

    xTrollxDudex

    Reverse the comparison
     
  7. Offline

    mrgreen33gamer

    xTrollxDudex I have made a different code, I still don't know how to get the top 10/5 values in the Hash.
    Code:
               class ValueComparator implements Comparator<String> {
     
                  Map<String, Integer> base;
                  public ValueComparator(HashMap<String, Integer> map) {
                      this.base = map;
                  }
                  
                  public int compare(String a, String b) {
                      if (base.get(a) >= base.get(b)) {
                          return -1;
                      } else {
                          return 1;
                      }
                  }
              }
       
       
       HashMap<String, Integer> map = new HashMap<String, Integer>();
              ValueComparator bvc =  new ValueComparator(map);
              TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);
     
              map.put("1", 96);
              map.put("2", 35);
              map.put("3", 74);
              map.put("4", 61);
              map.put("5", 12);
              map.put("6", 61);
              map.put("7", 54);
              map.put("8", 45);
              map.put("9", 61);
              map.put("10", 47);
              map.put("11", 75);
     
              player.sendMessage("unsorted map: " + map);
     
              sorted_map.putAll(map);
     
              player.sendMessage("results: " + sorted_map);
                
     
  8. Offline

    xTrollxDudex

    mrgreen33gamer
    You aren't using a Comparator correctly. The index returned by the compare method is used to determine the relationship between the position of the 2 compared items, so you'd have to use the difference negated in some instances, and left alone in others.

    Anyways, realize that you'd have to place the item inside the TreeMap to sort them, and use a for loop with bounds 0 and < 15 through the TreeMap, you can't sort a HashMap, and because Iterators retain on order of map, you should get the top 15 values this way.
     
  9. Offline

    mrgreen33gamer

Thread Status:
Not open for further replies.

Share This Page