Checking Amount of items and getting top 3 players.

Discussion in 'Spigot Plugin Development' started by Remy2402, May 10, 2015.

  1. Hey peeeeeople. I am having a bit of problem with me code. I want to get all online players, check how much gold is in their inventory and get the top 3 players with the most amount of gold. Any ideas on a good way to do this?
     
  2. Iterate through all online players, get their inventories, iterate through their inventories, check if the ItemStack is gold, increment an integer representing their gold, etc.
     
  3. Could you please do an example? <3
     
  4. Hint & Neat help:
    BEWARE OF THIS THING! You need a != null check
    Code (Text):
    PlayerInventory inventory = player.getInventory();
    for (ItemStack itemstack : inventory.getContents()){
        if (itemstack != null){
            //do more stuff here
        }
    }
     
    More help:
    Code (Text):
    if (itemstack.getType() == Material.GOLD_INGOT)
     
     
  5. I know how to check the inventory for stuff but I don't know how to get all online players inventorys and check who the top 3 people are with the most amount.
     
  6. How dumb do you think I am? Of course I know that I am going to need to create a method and of course there is no method for checking the top 3 gold. I just don't know what to do in that method.
     
  7. 1 - get amount of gold in the player inventory
    2 - store it in a hashmap
    3 - sort the hashmap by value
     
  8. Code (Java):
                    for (Player all : Bukkit.getOnlinePlayers()) {
                        PlayerInventory i = all.getInventory();
                        for (ItemStack items : i.getContents()) {
                            if (items != null) {
                                if (items.getType() == Material.GOLD_INGOT) {
                                    ArrayList<Integer> numbers = new ArrayList<Integer>();
                                    numbers.add(items.getAmount());
                                   // Fetch the 3 highest integers from the ArrayList.
                                }
                            }
                        }
                    }
     
  9. I'll just say the following and you should catch your mistake.
    Code (Text):
      ArrayList<Integer> numbers =new ArrayList<Integer>();