Decrements values in a Map<String, Integer> in a Scheduler

Discussion in 'Plugin Development' started by FTWin01Gurl, Jun 23, 2012.

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

    FTWin01Gurl

    I'm trying to make this plugin in which players are given a time limit in which they have to wait until they do an action again, and I schedule a task for this. One problem. How do I decrease the value without accessing any Player arguments in the methods, but still decrease the values? I've tried an Iterator but it does not work, sadly, because it only works for an ArrayList.
     
  2. Offline

    Sagacious_Zed Bukkit Docs

    You can iterate over the map's entryset
     
  3. Offline

    FTWin01Gurl

    But how would I put it back, though?
     
  4. Iterator works just fine for me, here's an example
    Code:
    public class Task implements Runnable
    {
    	@Override
    	public void run()
    	{
    		final Iterator<Entry<String, Integer>> iterator = hashmap.entrySet().iterator();
    
    		Entry<String, Integer> entry;
    
    		while(iterator.hasNext())
    		{
    			entry = iterator.next();
    
    			// examples:
    
    			iterator.remove(); // remove the entry
    
    			entry.setValue(0); // change the value
    		}
    	}
    }
     
    FTWin01Gurl likes this.
  5. Offline

    FTWin01Gurl

    Alright, thanks!

    One problem. How do I deduct it, without setting ints?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  6. Offline

    Everdras

    Code:
    Map<String, Integer> map;
    int decAmt = ... //However much you want it to decrement by
     
    for(String s : map.keys().toArray()) { //The Set returned by keys() is backed by the Map, so we can't modify it directly; we need a copy of it
        map.put(s, map.get(s) - decAmt);
    }
     
  7. Everdras It has a reason Digi used a entrySet/iterator.
    If you remove entries from within the loop always use the iterator, else the entrySet should be enough (correct me if I'm wrong).
    So your code should look like this:
    Code:java
    1. Map<String, Integer> map;
    2. int decAmt = ... //However much you want it to decrement by
    3.  
    4. for(Entry<String, Integer> e: entrySet())
    5. e.setValue(e.getValue() - decAmt);
     
  8. Offline

    Everdras

    Oh, that makes a lot more sense when you put it like that. Thanks for the tip.
     
  9. Offline

    Njol

    I suggest to use a Map<String, Long> where you put the last time a player did some action with
    Code:
    map.put(Player.getName(), System.currentTimeMillis());
    And when the player tries to do the action again you can easiliy get how long's it been since he last used the action:
    Code:
    long waited = System.currentTimeMillis() - map.get(player.getName());
    long waitedInSeconds = waited/1000;
    if (waitedInSeconds < config.getInt("wait")) {// if the player didn't wait long enough
        player.sendMessage("You have to wait another "+(config.getInt("wait") - waitedInSeconds)+" seconds to use this action!");
        return;
    }
    map.put(Player.getName(), System.currentTimeMillis());
    // do the action
    
     
Thread Status:
Not open for further replies.

Share This Page