Getting 10 of the online players?

Discussion in 'Plugin Development' started by iSexyChocobo, Nov 20, 2014.

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

    iSexyChocobo

    Hello. I just want some way of getting 10 random playernames who are online on the server. I have no idea where to start.

    Example:

    Bukkit.BroadcastMessage(tenrandom);

    would result in

    Player1 Player2 Player3.(and all the way to).Player 10

    Then of course a check if there aren't 10 people online

    Code:
    if(Bukkit.getServer().getOnlinePlayers().length > 10);
     
  2. Offline

    JordyPwner

    Make a random int
     
  3. Offline

    iSexyChocobo

    JordyPwner

    I was more wondering how to get 10 names specifically and not all or one of them.
     
  4. Offline

    TheOatBaron

    Didn't have time to test this, but it should work. As a side note, a regular list should work.

    Code:java
    1. if(Bukkit.getServer().getOnlinePlayers().length > 10){
    2. Random rand;
    3. LinkedList<String> pList= new LinkedList<String>();
    4. for(Player all:getServer().getOnlinePlayers()){
    5. pList.add(all.getName());
    6. }
    7. LinkedList<String> tenPlayers= new LinkedList<String>();
    8. for(int i=0;i<10;i++){
    9. rand = new Random();
    10. int randInt = rand.nextInt(i);
    11. tenPlayers.add(pList.get(randInt));
    12. pList.remove(randInt);
    13. }
    14. //TODO Get players from string in tenPlayers
    15. }
     
  5. TheOatBaron
    Why are you creating a new Random on each iteration?
     
  6. Offline

    TheOatBaron

    Assist

    Else it would use the same number I believe.

    EDIT: It would work without it, the old one would keep getting a new String

    EDIT 2: Just realized, it would be completely useless either way.
     
  7. Offline

    msnijder30

    iSexyChocobo
    Code:java
    1. List<String> list = new ArrayList<String>();
    2. for(int i = 0; list.size() < 10; i = new Random().nextInt(list.size() - 1)){
    3. Player random = Bukkit.getOnlinePlayers()[i];
    4. if(!list.contains(random.getName())) list.add(random.getName());
    5. }[/i]

    Now you have a list of playernames, you can get the players from the list now :)
     
  8. Offline

    mythbusterma



    Not only is that code difficult to read and very prone to errors, it doesn't even work in the first place.
     
    Skionz likes this.
  9. Offline

    coasterman10

    Here's what I would do:
    Code:java
    1. // Copy the list of online players, then randomize the order
    2. List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
    3. Collections.shuffle(players);
    4. // nPlayers is the number of players you would like to get
    5. for (int i = 0; i < nPlayers; i++) {
    6. Player p = players.get(i);
    7. // ...
    8. }


    I have found Collections.shuffle to be useful for these types of things.
     
  10. Offline

    msnijder30

    mythbusterma
    Yeah I don't know what happened there, it should work now. I had some issues when posting it here because it would not [ i ] post but it would make the text cursive, it worked now :)
     
  11. Offline

    iSexyChocobo

    coasterman10

    I'm getting a Dead Code warning on the

    Code:
    for (int i = 0; i < nPlayers; i++) {
    line on the i++ part.
     
  12. Offline

    mythbusterma

    iSexyChocobo

    Because you literally copy/pasted his code without thinking about it or even reading the comments....
     
  13. Offline

    iSexyChocobo

    mythbusterma

    Yeah thanks for not being helpful.. and being wrong, I did read all the comments as well as the code.

    Can someone actually help me now? I suppose that's what this forum is for? I didn't post here because I'm the best at java, I posted because I needed help with it.
     
  14. Offline

    TheOatBaron

    msnijder30
    That method wouldn't work if called again. Assuming no one leaves or joins, the same people would be called again.
     
  15. Offline

    mythbusterma



    Alright, well instead of being ignorant, I guess you're just not very good at this. The point is to understand the point of his code instead of just copy/pasting it. If you would've understand what he was saying you could've utilised his comment, but instead you don't. That was very helpful and the thread should be over, because it's answered.
     
    Jaaakee224 likes this.
  16. Offline

    iSexyChocobo

    mythbusterma
    coasterman10

    Alright I'll play it your way, I'll show you what I understand and then you can fill in the blanks.


    List<Player> players = new ArrayList<>(Bukkit.getOnlinePlayers());
    //This simply lists all the players into an arraylist
    Collections.shuffle(players);
    //Shuffling, randomizing the order of the list
    for (int i = 0; i < nPlayers; i++) {
    //I'm assuming I could either change nPlayers straight to the number or assign it trough Int nPlayers = 10
    //The < signals "if nPlayers is bigger than int i"
    // The left side states that i will now equal zero
    // I have no idea what comes after the nPlayers thingy
    Player p = players.get(i);
    //Gets the 10 players. Now I can use it.
    }
     
  17. Offline

    msnijder30

  18. Offline

    teej107

    1. Put all online players in a list.
    2. Get a randomly chosen player from list
    3. Remove that player from list
    4. Repeat
    You might have to do some more things then just that to make sure you don't get an OutOfBoundsException.
     
    iSexyChocobo likes this.
Thread Status:
Not open for further replies.

Share This Page