HashMap help - "Requesting something"

Discussion in 'Plugin Development' started by Googlelover1234, Jun 20, 2014.

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

    Googlelover1234

    Hey, I needed some help with my 1v1 plugin I was making. In order to make it have somewhat of a "request", I thought a good way to do it would be by using a HashMap. For example, when you right click a player, if he hasn't already requested a 1v1, you put them in a HashMap. That's all quick and easy, but what I'm having trouble with is if they did request a 1v1. I'll give some of my code to see what I'm talking about.
    Code:java
    1. HashMap<Player, Player> request = new HashMap<Player, Player>();
    2.  
    3.  
    4. if (p.getItemInHand().getType() == Material.STICK
    5. && p.getItemInHand().getItemMeta().getDisplayName()
    6. .equalsIgnoreCase(ChatColor.stripColor("1v1"))) {
    7. if(request.containsValue(p)) {
    8.  
    9. }
    10. // How to check if they are already in the hashmap?
    11. }


    So, I just need a way to check if they already have right clicked him. Any ideas?
     
  2. Offline

    AoH_Ruthless

    Googlelover1234
    You already checked if they are in the hashmap by using request.containsValue(p)
     
  3. Offline

    EgyptianKing

    Umm this is off-topic, but I heard that it isn't good to use players when making arraylists and maps. i think you should use player's names. but i might be wrong
     
  4. Offline

    teej107

    EgyptianKing It isn't a good idea to put players in the lists because they could cause memory leaks when the player leaves. The garbage collector can't garbage collect the player because it is still being used by the list. You could remove the player from the list when he leaves but it'll be better to just stick with UUID or player names. It would be preferred to use UUID but you could use player names if it is just a one session thing. But it'll be better to get in the habit of using UUID.

    Googlelover1234 Since you are using Player as the key and the value, it'll be better to stick to using an arraylist.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
    EgyptianKing likes this.
  5. Offline

    Gater12

    teej107
    He wants to associate another player with some other player for 1v1. A List wouldn't do such things.
     
  6. Offline

    teej107

    Gater12 Ah I see now. I thought he was using the same player for the key and value. :p my error
     
  7. Offline

    1Rogue

    TheSpherret likes this.
  8. Offline

    Googlelover1234

    EgyptianKing Okay, I'll note that :3
    AoH_Ruthless Okay, so should I just check if "ent" is in the HashMap?

    So here's what I'm trying to do. When a person right clicks them with a stick or something, it'll put them in a HashMap, and if a player right clicks someone that already has asked them, they start a 1v1. What I need help with is checking if the person a player right clicks is already in a HashMap with them.
     
  9. Offline

    Googlelover1234

  10. Offline

    BrennyMullan

    Code:java
    1. HashMap<Player, Player> request = new HashMap<Player, Player>();
    2.  
    3. Player p = event.getPlayer();
    4. Player B //whatever code you have for the player being clicked
    5. if (p.getItemInHand().getType() == Material.STICK && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.stripColor("1v1"))) {
    6. if(request.containsKey(p) && request.containsValue(PLAYER B)) {
    7. //already in
    8. }else{
    9. request.put(p, PLAYER B);
    10. }
    11. }
     
Thread Status:
Not open for further replies.

Share This Page