2 keys in same hashmap

Discussion in 'Plugin Development' started by ElCreeperHD, Feb 25, 2016.

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

    ElCreeperHD

    I am trying to do an Swords Skills plugin, I am working in cooldown checks. I am using the code from another Bukkit Forum post (Modified), but I need to check for a player and a ability, to get the cooldown of the ability associated to the player. I have no idea on how to do it. To be more specific, I want to have 2 keys in the HashMap.
    (This is my first time using hashmaps, I saw documentation but I don't know how to do this)

    Code:
    Code:
    int cooldownTime = 60; // Get number of seconds from wherever you want
    if(cooldowns.containsKey(sender.getName())) { 
    long secondsLeft = ((cooldowns.get(sender.getName())/1000)+cooldownTime) (System.currentTimeMillis()/1000);
                if(secondsLeft>0) {
             //Still cooling down
                   p.sendMessage("Sorry, you have to wait " + secondsLeft + " seconds to use the ability!!");
    
                }
            }
           // No cooldown found or cooldown has expired, save new cooldown
            cooldowns.put(sender.getName(), System.currentTimeMillis());
     
  2. Offline

    Zombie_Striker

    The whole point of collections are so multiple objects can be stored in the same object.
    So you want to have one hashmap that stores all the player's cooldown and you want another hashmap full of how long each cooldown should be?
     
  3. Offline

    Lordloss

    You could create a new Class for your Abilities, maybe called "Ability" ;) And store the essential data like cooldown inside of it. The Constructor could look like this
    Code:
    new Ability(60, AbilityType.SOMETHING);
    Then you just put them in your hashmap (Player, Ability).
     
  4. Offline

    mine-care

    You should not store Player objects unless you really have to, and you are properly removing them onDisconnect. Otherwise you will cause RAM leaks.
     
  5. Offline

    ElCreeperHD

    @Zombie_Striker
    Yes, I want a hashmap to store the cooldowns per ability for the players.
     
  6. Offline

    tommyhoogstra

    Could use a WeakHashMap
     
  7. Offline

    mine-care

    @tommyhoogstra Very true, but i recomend HashMap because i cant really go into explaining what garbage collector is, ect. I did the mistake once and it took ~5 posts to explain what it is so i am not going to do that.... :p
    Good point though
     
  8. Offline

    Lordloss

    @mine-care he allready uses playernames, it was just an example for an custom object he could map to it
     
  9. Offline

    mine-care

    @Lordloss I see, but the way you wrote it,
    seems like you encourage the op to use a Player object as key (Player is with capital so i asumed you refered to the interface Player)
    In other words i try to make sure that you, the OP and the possible reader are aware of the consequences of using a Player object in storage without proper handling.
     
Thread Status:
Not open for further replies.

Share This Page