Getting random players and put them in to a HashSet and change namecolor

Discussion in 'Plugin Development' started by jimbo8, Feb 9, 2014.

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

    jimbo8

    Hi,

    I'm trying to loop through every single player on the server and put them into HashSets.
    Let's say i've got team red, blue and green, and there are 15 players on the server.

    Now, when i type a command, 5 players will be put into red team, 5 into blue and 5 into green.

    But if there are let's say 17 players, it wil ex. be like this:

    6 in red team, 6 in blue and 5 in green.


    I tried to get the players this way, but i'm not sure how i could get only 5 and add them into a hashset.

    Code:java
    1. Player random = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
    2.  


    Also, i need to change the color above the player's head(his/her name), i guess this is done with packets.

    Sorry if i didn't explain this well enough,

    thank you.
     
  2. Offline

    Freelix2000

    I would use a 'for' loop for this. Here's an example:
    Code:java
    1. HashMap<String, String> teams = new HashMap<String, String>();
    2. Integer red = 0;
    3. Integer blue = 0;
    4. Integer green = 0;
    5. for(Player p : Bukkit.getOnlinePlayers()){
    6. if(!(red == 5)){
    7. teams.put(p.getName(), "red");
    8. red = red + 1;
    9. }else{
    10. if(!(blue == 5)){
    11. teams.put(p.getName(), "blue");
    12. blue = blue + 1;
    13. }else{
    14. if(!(green == 5)){
    15. teams.put(p.getName(), "green");
    16. green = green + 1;
    17. }else{
    18. break;
    19. }
    20. }
    21. }
    22. }
    23.  

    That is sort of a sloppy way to do it, but you get the idea. The HashMap should also be public so that you can access the team then.
     
Thread Status:
Not open for further replies.

Share This Page