Iterate over hashmap with player names as keys and charge players accordingly

Discussion in 'Plugin Development' started by ron975, Sep 10, 2012.

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

    ron975

    Say I have a HashMap<String, Integer>, and I want to use Vault to charge players every so and so ticks using a scheduler. How do I get an array of the key names (Player names) so I can cast the Integers to Doubles and have Vault charge my players accordingly?
     
  2. Offline

    kyle1320

    ron975
    Code:
    for (String s : stringMap.keySet()) {
        stringMap.get(s);
    }
    Should work
     
    ron975 likes this.
  3. Offline

    Courier

    Something like this:
    Code:java
    1. for(Map.Entry<String, Integer> entry : hashMap.entrySet())
    2. {
    3. Player player = Bukkit.getPlayerExact(entry.getKey());
    4. int value = entry.getValue();
    5. //...
    6. }
    However, there is a more efficient way. See the section here titled "Getting Players from a List of Names".
     
  4. Offline

    ron975

    So, theoretically, this should work?
    Code:java
    1.  
    2. HashMap<String, Integer> map = new HashMap<String, Integer>();
    3. this.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable()
    4. {
    5.  
    6. public void run()
    7. {
    8. for (String player : map.keySet())
    9. {
    10. economy.withdrawPlayer(player, (double) map.get(player));
    11. }
    12. ;
    13. }
    14. }, 60L, 24000L);
    15.  
     
  5. Offline

    kyle1320

    ron975
    I think so, yes, is it not?
     
    ron975 likes this.
  6. Offline

    Firefly

    If withdrawPlayer() accepts a String as its first parameter, then yes.
     
Thread Status:
Not open for further replies.

Share This Page