[UNSOLVED] HashMap problems.

Discussion in 'Plugin Development' started by chriztopia, Feb 20, 2012.

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

    chriztopia

    Im a noob to hashmaps. I want to be able to allow a player to do something once an hour. I do use Sync on my server. I have read some tutorials and looked and other peoples code but I cant figure out the best way to go about doing this.
     
  2. Offline

    Milkywayz

    You could either use broCooldowns , or look at his code (he offers the source) and try to implement it into your plugin and give proper credit. Im not too sure hash maps will be the best for this, but i could be wrong, your best bet would be to just use that plugin since it requires the least effort :p.
     
  3. Offline

    Gravity

    Use a list for this. Once the player does something, insert the player into the list. Then, submit a new task to the server to remove the player from that list in 1 hour. Before you allow the player to do that action again, check if they are on the list. If they are, dont allow them to use it.

    Also,
    *Slaps*
    Bad!
    Never tell a developer to stop what he's doing and use someone elses plugin to do it.
    Bad.
     
  4. Offline

    Zimp

    Using the player's name as the key for the hash map you could store the Long value that represents last time they performed the action. Then you can compare that stored value against the system's current time (System.currentTimeMillis()) to determine if enough time has elapsed or not.
     
  5. Offline

    Milkywayz

    :eek: Sorry! :(
     
  6. Offline

    chriztopia

    Ok buy my biggest problem is the Hashmaps them selfs. Storing then retriving data
     
  7. Offline

    Firefly

    To store/load from a HashMap:

    Code:
    public static Map<String, String> someHashMap = new HashMap<String, String>();
     
    someHashMap.put("someString", "anotherString");
    String variableForMapValue = someHashMap.get(aValueYouPutIn);
    Of course, I made a simple HashMap that keys a String to a String, but you can replace those values. However, for what you are doing, I would suggest doing what h31ix suggested and use a list, since you don't have to key a Player to anything, you just need to externally store their names.

    Hope this helps!

    -Firefly
     
  8. Offline

    Zimp

    I am going to have to respectfully disagree with the list suggestion. It takes a simple problem and makes it very complicated and you run the risk of two tasks editing the list at the same time unless you make sure they all execute on the same thread.

    The more straightforward solution is:

    Code:
    HashMap<String, Long> cooldownMap = new HashMap<String, Long>();
    doAction(Player player) {
        String playername = player.getName();
        if(cooldownMap.containsKey(playername))
        {
              Long time = cooldownMap.get(playername);
              if(System.currentTimeMillis() - time < (60 * 60 * 1000)) //60 minutes * 60 seconds * 1000 milliseconds
                    return;
        }
       
        //do stuff
     
        cooldownMap.put(playername, System.currentTimeMillis());
    }
     
  9. Offline

    chriztopia

    Code:
    if(event.getRightClicked() instanceof Cow){
                 
                 
                 
                  HashMap<String, Long> cooldownMap = new HashMap<String, Long>();
                  doAction(Player player) {
                      String playername = player.getName();
                      if(pigpoop.containsKey(playername))
                      {
                            Long time = pigpoop.get(playername);
                            if(System.currentTimeMillis() - time < (60 * 60 * 1000)) //60 minutes * 60 seconds * 1000 milliseconds
                                  return;
                      }
                     
                     
                          pp.getWorld().playEffect(pp.getLocation(), Effect.SMOKE, 20);
                          pp.getWorld().dropItem(pp.getLocation(), new ItemStack(plugin.getConfig().getInt("cow.itemdropped"),1));
                   
                      pigpoop.put(playername, System.currentTimeMillis());
                  }
    }
     
  10. Offline

    Sir Savary

    chriztopia
    Try to add some context/information to your post, not just code.
     
  11. Offline

    Zimp

    chriztopia

    The code example I posted was not intended to be blindly cut and paste into your plugin. It was only meant to be a semi-concrete example that you would have to modify to fit your problem.
     
Thread Status:
Not open for further replies.

Share This Page