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?
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.
I am sure you can figure it out yourself. Here is everything you need that is Bukkit related. https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/Inventory.html https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/inventory/ItemStack.html https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Bukkit.html
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)
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.
There is no method to get the top three players with the most gold. You are going to have to write that yourself. You can find the methods you need here. https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Bukkit.html https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/Player.html
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.
1 - get amount of gold in the player inventory 2 - store it in a hashmap 3 - sort the hashmap by value
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. } } } }
I'll just say the following and you should catch your mistake. Code (Text): ArrayList<Integer> numbers =new ArrayList<Integer>();