1.14.4 How I can get the biggest in from config?

Discussion in 'Spigot Plugin Development' started by Ardno, Jan 28, 2020.

  1. Hello,
    I'm making a bank plugin, and I want to use placeholders to get TOP1 player, TOP2 player.... )in another placeholders), but I don't know how.
    There is type of code:
    Code (YAML):
    Ahojs:
      bank
    : 0
    Ardno
    :
      bank
    : 0
    MinerFriggo
    :
      bank
    : 0
    Xga
    :
      bank
    : 0
    SSD
    :
      bank
    : 0
    WsAd
    :
      bank
    : 0
    Woodcutter
    :
      bank
    : 0
    Ahoj
    :
      bank
    : 0
    Mami445
    :
      bank
    : 0
    Jsemxx1_
    :
      bank
    : 0
     
  2. You can have a variable with the value of the first item you want to compare, then you loop through all the remaining values and check if the value in variable is smaller than the value in the config, if it's smaller then replace the value in the variable with the value of the config.

    But because you want to retain those values and the players, it's better if you take all the values from the file and then compare them, of course it depends on the size of the config file.
     
  3. Well, I going to use the second idea. But how I can get all [username].bank and compare them? Because there is nicknames online and offline players.
     
  4. FrostedSnowman

    Resource Staff

    make a Comparable<ConfigurationSection>
     
  5. This can get expensive if you end up with a lot of players in the config file, so I would concentrate the effort on just the Top10.
    You could use a SortedMap for this:
    Code (Java):
    SortedMap<Integer, UUID>
    The Integer will be the bank balance, and the UUID will be the player's UUID that has that balance.
    The map will order itself from lowest balance to highest balance unless you use a comparator.

    When you load the plugin add the Top10 highest balances and players you find in your config to the map.
    When players add to their balance, check to see if their balance now exceeds someone in the top 10 map (no need to check config). If it does, add them to the map, and remove the person with the lowest balance. If it does not, no change needs to be made.
    When players remove from their balance, check to see if they are in the top 10. If they are not, no change to the top 10 needs to be made. If they are top 10, now we don't really have a choice but to go through all of the balances in the config and see if they dropped out, and someone else needs to be added in.
     
  6. Can you give me please an example for create the SortedMap and then test next player? On internet I could't found an exaple for create and test
     
  7. and if two people have the same amount of currency?
     
  8. My apologies, you need to use one of the implemented classes for the interface:
    Code (Java):
        TreeMap<Integer, UUID> top10 = new TreeMap<Integer, UUID>();
            if (player is adding to balance) {
                for (int balance : top10.keySet()) {
                    if (player.balance > balance) {
                        top10.remove(top10.firstKey());
                        top10.put(player.balance, player.getUniqueID());
                        break;
                    }
                }
            }
    The map will order them somehow, if 1 and 2 are tied I would imagine 1 would be the person that was added first. You can add extra code for that and have like a top 20 if tied or something, but that is up to @Ardno
     
    #8 ForbiddenSoul, Jan 28, 2020
    Last edited: Jan 28, 2020
  9. You can read the Javadoc (that was linked) to see the implementations

    Whoops, didn't see the reply above me
     
  10. what @Warren1001 pointed out is that, if two values are the same, a call to put will override the old value. This means that this approach cannot be taken without any modifications.
     
  11. Maybe, SortedMap<Integer, Set<UUID>>
     
  12. In that case, you can just use a SortedSetMultimap. Makes the code cleaner.
     
    • Agree Agree x 1