Help HashMap

Discussion in 'Plugin Development' started by 2w7, Mar 28, 2020.

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

    2w7

    Hey,
    I want to know how to get a the number of player of role with an HashMap !
    Code:
    public static HashMap<UUID, Role> playerRole = new HashMap<>(); //Role is an Enum
    And this is an exemple :/
    Code:
    if(playerRole.contains(Role.CIVIL).size() < 1){ playerRole.put(p.getUniqueID, Role.CIVIL); }
    My enum class:
    Code:
    package fr.etn.utils;
    import java.util.Random;
    public enum Role {
    SHERIF,TUEUR,MEDECIN,CIVIL;
    public static Role selectRandomRole(){
    Random rand = new Random();
    Role[] roles = Role.values();
    return roles[rand.nextInt(roles.length)]; 
     }
    }
    Thanks, and sorry for my bad english i'm french :/
     
  2. Online

    timtower Administrator Administrator Moderator

    @2w7 Loop over it.
     
  3. Offline

    2w7

  4. Online

    timtower Administrator Administrator Moderator

  5. Offline

    2w7

    @timtower so if i do
    Code:
     if(playerRole.forEach((key,value) -> value == Role.CIVIL.size())){ playerRole.put(p.getUniqueID, Role.CIVIL); } 
    Is that good ?
     
  6. Online

    timtower Administrator Administrator Moderator

    @2w7 And what would that do?
     
  7. Offline

    2w7

    @timtower if there is no player in playerRole at Role.CIVIL so it will put a player in ?
     
  8. Online

    timtower Administrator Administrator Moderator

    @2w7 Your initial post says count, now you are setting.
    What do you want to do? Step by step?
     
  9. Offline

    2w7

    @timtower yes, because I want to understand but putting the player in the HashMap is the final step :/

    @timtower The first step is checking how many player are in the ROLE.CIVIL and if it's < 1 put in the hashmap to Role.CIVIL

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Mar 28, 2020
  10. Online

    timtower Administrator Administrator Moderator

    @2w7 Start with looping over the map to check the values.
    Only counting.
     
  11. Offline

    2w7

    @timtower I don't understand how to do it sorry :/
     
  12. Online

    timtower Administrator Administrator Moderator

    @2w7 Check the link I posted, it contains everything you need.
     
Thread Status:
Not open for further replies.

Share This Page