Economy plugin - baltop?

Discussion in 'Plugin Development' started by KAM202, Jul 20, 2019.

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

    KAM202

    I wonder how to do top 10 the richest players. I have file (config.yml):
    Code:
    money:
      <NickName>: '10000'
    And when the server starts, it loads all players into HashMap <String, BigDecimal>.
    My command /money <offline-player>/<online-player> shows the current balance so i must convert online player(if he used the command) to offlineplayer and get her balance from hashmap.

    I come to the question whether it will be quite optimal? When at the start of the game loads all players from the config (even those who only entered once)?

    Next question. Downloading BalTop - sort the hashmap every x minutes and getting only the first 10 records?
    Because I understand that sorting every time you use the command will be a bit senseless.
    Do you have any suggestions?
     
    Last edited: Jul 20, 2019
  2. Offline

    KarimAKL

    @KAM202 You cannot sort a HashMap. You can instead make a List<Entry<K,V>> with the entries from the map, and then sort that by the value using 'Collections.sort()'.
     
  3. Offline

    KAM202

    @KarimAKL

    sortByValue function:
    Code:
    public static HashMap<String, BigDecimal> sortByValue(HashMap<String, BigDecimal> hm)
        {
            // Create a list from elements of HashMap
            List<Map.Entry<String, BigDecimal> > list =
                   new LinkedList<Map.Entry<String, BigDecimal> >(hm.entrySet());
     
            // Sort the list
            Collections.sort(list, new Comparator<Map.Entry<String, BigDecimal> >() {
                public int compare(Map.Entry<String, BigDecimal> o1, 
                                   Map.Entry<String, BigDecimal> o2)
                {
                    return (o1.getValue()).compareTo(o2.getValue());
                }
            });
             
            // put data from sorted list to hashmap 
            HashMap<String, BigDecimal> temp = new LinkedHashMap<String, BigDecimal>();
            for (Map.Entry<String, BigDecimal> aa : list) {
                temp.put(aa.getKey(), aa.getValue());
            }
            return temp;
        }
    and next i'm using:
    Code:
    Map<String, BigDecimal> hm1 = sortByValue(EconomyManager.bal);
    
    +
    
    int i = 0;
                for (Entry<String, BigDecimal> en : hm1.entrySet()) {
                    if(i == 10) break;
                    sender.sendMessage(ChatColor.GOLD + "" + (i+1) + ". " + en.getKey() + " - " + ChatColor.GREEN + en.getValue() + "$");
                    i++;
                } 
     
  4. Offline

    DiegoMartines

    There's easiest solution for all this, use vaultapi
     
  5. Offline

    KAM202

  6. Offline

    KarimAKL

    @DiegoMartines There's no need to use Vault for this.

    @KAM202 You just pasted a lot of code, what's the problem with it?
     
  7. Offline

    KAM202

    @KarimAKL

    With this? nothing.

    I wonder how to do top 10 the richest players.
    Downloading BalTop - sort the hashmap every x minutes and getting only the first 10 records?
    Because I understand that sorting every time you use the command will be a bit senseless.
     
  8. Offline

    KarimAKL

    @KAM202 I already answered how to sort it.
    What's the problem with what i said?
     
  9. Offline

    KAM202

    @KarimAKL I showed you that it saves as you say.
    I'm asking how I can sort this list optimally.
    Is it possible to store players' money in some other form? (now HashMap with all player's)
     
  10. Offline

    KarimAKL

    @KAM202 I don't think so, i think a map is one of the best choices, though if you have a lot of players, you should probably consider getting the balance from the file, instead of storing it in a map.
     
  11. Offline

    KAM202

    @KarimAKL The data is saved in a file, but everything is in HashMap when the server is working.
     
  12. Offline

    Kars

    You say you have a Map<String, BigDecimal>.
    What you do is, you make a List<String> for your toplist. You copy your Map without reference, then find the richest player with this
    PHP:
    Collections.max(countMap.entrySet(), Comparator.comparingInt(Map.Entry::getValue)).getKey();
    Add it to your list and remove the entry from the map. Repeat.

    After that you can call the Map to get the value for the players.

    Depending on what you want to do with it Karim's option might be better though. But if you want only the top 10 entries this is a good way of going about it.
     
    Last edited: Jul 20, 2019
Thread Status:
Not open for further replies.

Share This Page