Get random online player

Discussion in 'Plugin Development' started by Eskillu, Jan 10, 2015.

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

    Eskillu

    Hey, i need get a random online player, i use this variable (Player p = Bukkit.getOnlinePlayers()[random.nextInt(Bukkit.getOnlinePlayers().size())];) but it don't work in 1.7.10+, because have this error in variable: The type of the expression must be an array type but it resolved to Collection<capture#1-of ? extends Player>
     
    Last edited: Jan 10, 2015
  2. @Eskillu How exactly does it not work anymore? It might have been changed because of the switch to UUID, but I wouldn't think so.
     
    Eskillu likes this.
  3. Offline

    drpk

    @BorisTheTerrible I think it's because they changed the return type of getOnlinePlayers


    @Eskillu try using the deprecated method getOnlinePlayers(if there is one)
     
    Eskillu likes this.
  4. Offline

    fandemangas42

    Code:
    int onlinePlayers =  Bukkit.getOnlinePlayers().length;
    Player p = Bukkit.getOnlinePlayers()[onlinePlayers];
    It should work ;)
     
    Eskillu likes this.
  5. Offline

    Eskillu

    Have this error in eclipse:
    The type of the expression must be an array type but it resolved to Collection<capture#1-of ? extends Player>


    In Bukkit 1.7.10 don't have length
     
  6. Offline

    1Rogue

    You're trying to use the return of the .getOnlinePlayers() method as both an array and a collection (re: .size()).
     
    Eskillu likes this.
  7. Offline

    fandemangas42

    Code:
            int onlinePlayers = Bukkit.getOnlinePlayers().size();
            Player p = (Player) Arrays.asList(Bukkit.getOnlinePlayers()).get(random.nextInt(onlinePlayers));
    Maybe this ? I didn't test it.
     
    Eskillu likes this.
  8. Offline

    Eskillu

    I'm going try it. Thank you :)

    I found this error in console (It's in line of this variable):

    10.01 20:54:44 [Server] INFO java.lang.ClassCastException: java.util.Collections$UnmodifiableRandomAccessList cannot be cast to org.bukkit.entity.Player
     
    Last edited by a moderator: Jan 10, 2015
  9. Offline

    ColaCraft

    Do this.
    Code:
    List<Player> players = new ArrayList<Player>();
            for (Player player : Bukkit.getOnlinePlayers()) {
                players.add(player);
            }
    
            Collections.shuffle(players);
    Player p = player.get(3);
    You can change 3 to whatever number you feel comfortable with, and works in 1.8
     
  10. Offline

    mythbusterma

    @ColaCraft

    Or....don't.

    Code:
    Random random = new Random();
    Player player = Bukkit.getOnlinePlayers().toArray()[random.nextInt(Bukkit.getOnlinePlayers().size())];
    Much simpler and doesn't waste anywhere near as much CPU as yours.
     
  11. Offline

    1Rogue

    This will fail and throw an exception if you don't have more than 3 people online. It also creates an entirely new collection (unnecessary), and then shuffles it (also unnecessary compared to using java.util.Random)
     
    Last edited: Jan 11, 2015
  12. Offline

    Noahz123

  13. Offline

    mythbusterma

    @Noahz123

    Do you mean like exactly what I posted two posts ago...?
     
  14. Offline

    Noahz123

    @mythbusterma

    Bukkit.getOnlinePlayers().toArray() will return an array of objects, of course they can all be cast to Player since each one of the objects can be cast to player 100% of the time, but just thought that should be put out there so the OP knows what it does...

    toarray(); == toarray(new Object[0]);

    When I post solutions, I like to make my answer broad and informative. If you give someone a copy-paste answer you should at least tell them what it does.
     
  15. Offline

    mythbusterma

    @Noahz123

    I would assume it was implied it was a not stupid version of what was posted above. Also, with a minimal amount of effort, someone could check what Collections#toArray() does, if it's name wasn't abundantly clear.

    Also, what is
    supposed to be? Those statements do not provide any clarity and are incorrect.
     
  16. Offline

    Noahz123

    @mythbusterma

    Oracle docs themselves say these exact words.

    "Note that toArray(new Object[0]) is identical in function to toArray()."

    It is correct, toArray() returns object[] and toArray(new Object[0]); returns object[]...
     
  17. Offline

    teej107

    @Noahz123 toArray(array) returns an array of the array data type. toArray() just returns an Object array.
     
  18. Offline

    mythbusterma

    @Noahz123

    Wow, you pulled that one right of context, you could be a campaign manager!

    See @teej107's post for why you're wrong.
     
  19. Offline

    Noahz123

    I removed this post to prevent this from going further. At This point I'm confused to what the argument even is. I'm wrong depending on what you took what I said as.(Yes the methods do much different things, but my argument was that my instance pretty much did the same thing).

    I probably should have said "toArray is like using toArray(new Object[0])" To make the point that the new Object[0] is what kind of array will be returned. Sorry for causing drama.

    But what I meant by "attacking" is he pointlessly made a rather condescending remark, which isn't really the right thing to do. He could have pointed out in detail why he thought why I was wrong, rather than just saying "No".

    #derailing threads iz fun
     
    Last edited: Jan 11, 2015
  20. Offline

    nverdier

    @Noahz123
    1)
    doesn't mean exact.
    2) The methods do different things.
    3) He's not "attacking" you, he's helping this community. If you don't want to get that kind of feedback, then don't post incorrect 'things'.
     
Thread Status:
Not open for further replies.

Share This Page