getting a list of players and getting a random player

Discussion in 'Plugin Development' started by nitrousspark, Oct 19, 2012.

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

    nitrousspark

    ok so i want to get an arraylist of players then be able to get a random player of them all to play a role in the game. how would i get the random player?
     
  2. Offline

    Vandrake

    1- Store the players into a list.
    2- Create a random number.
    3- Assign that number to a list's index.
    In other words
    List[] Players = Bukkit.getOnlinePLayers();
    random number = i
    Player random = Players[ i ];

    get it?
     
    ferrybig likes this.
  3. Offline

    nitrousspark

    how would i make so its not all the online players but the players in an arraylist
     
  4. Offline

    Vandrake

    lol replace the BUkkit.getonlineplayers with the list? lol
     
    kroltan likes this.
  5. Offline

    nitrousspark

    no i tried that. it didnt work :/
     
  6. Offline

    Vandrake

    Player random = Players[ i ];
    This.
    replace Players with your arraylist name.
     
  7. Offline

    nitrousspark

  8. Offline

    Vandrake

    replace the arraylist with a regular list :p

    you are aware this is quite hard without seeing the code xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  9. Offline

    nitrousspark

    ok well now im getting an error in the list

    List<Player> lobby = new List<Player>();

    and the second list is an error
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    You can't instantiate an interface, pick a implementation like ArrayList.
     
  11. Offline

    nitrousspark

    ok well it still gives me an error

    List<Player> lobby = new ArrayList<Player>();

    int i = new Random().nextInt(1); // random number 0 - 1
    Player random = lobby;

    lobby is an error

    and it says The type of the expression must be an array type but it resolved to List<Player>
     
  12. Offline

    Vandrake

    lobby[ i ];
     
  13. Offline

    nitrousspark

    oh... well its still giving me the same error
     
  14. Offline

    Vandrake

    just show us the damn code xD
     
  15. Offline

    nitrousspark

    i though i did -_-

    ArrayList<Player> lobby = new ArrayList<Player>();

    int i = new Random().nextInt(1); // random number 0 - 1
    Player random = lobby;
     
  16. Offline

    Vandrake

    No this is wrong.
    Player random = lobby[ i ];
     
  17. Offline

    nitrousspark

    wtf im pasting that in but its deleting it. well i have it and its still not working

    lobby[.i.] is an error

    int i = new Random().nextInt(1); // random number 0 - 1
    Player random = lobby[.i.];
     
  18. Offline

    Vandrake

    hmmm I was playing around with the arraylists a little and You're right this returns an error.
    I am afraid I cannot help you on this one xD I am playing around a little more with this. If I get to somewhere I'll tell you

    lol its so easy im banging the head on the wall...
    lobby.get(int index);

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  19. Offline

    Pandemoneus

    Code:java
    1. List<Player> lobby = new ArrayList<Player>();
    2. // add your players here
    3.  
    4. int randomNumber = new Random().nextInt(lobby.size());
    5. Player randomPlayer = lobby.get(randomNumber);


    That should do it, wrote that by heart.
     
  20. Offline

    nitrousspark

    well.. now its giving me an error when i start up the server

    int i = new Random().nextInt(lobby.size());
    final Player random = lobby.get(i);

    the line the error is on is this

    int randomNumber = new Random().nextInt(lobby.size());

    13:04:21 [SEVERE] Could not load 'plugins\HideAndGoGortume.jar' in folder 'plugi
    ns'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalArgumentException: n
    must be positive
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:153)
    at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:305)
    at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:230)
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:222)
    at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:198)
    at net.minecraft.server.ServerConfigurationManagerAbstract.<init>(Server
    ConfigurationManagerAbstract.java:50)
    at net.minecraft.server.ServerConfigurationManager.<init>(SourceFile:11)

    at net.minecraft.server.DedicatedServer.init(DedicatedServer.java:105)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:378)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:539)
    Caused by: java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Unknown Source)
    at hide.go.gortume.Gortume.<init>(Gortume.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Sou
    rce)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:149)
    ... 9 more
     
  21. Offline

    Vandrake

    wats at line 27?
     
  22. Offline

    nitrousspark

    int randomNumber = new Random().nextInt(lobby.size());
     
  23. Offline

    Vandrake

    hmmm I do believe 0(zero) is not considered positive right? or is it?
     
  24. Offline

    ferrybig

    then your lobby array is empty, how you would except the code getting a random player from nothing?
     
  25. Offline

    Vandrake

    ye I was about to ask that XD I wonder when he initializes the arraylist, is he putting anything in it.
     
  26. Offline

    Technius

    It's really simple.
    Code:
    ArrayList<Player> players = new ArrayList<Player>(); //Make a list
    if(!players.isEmpty())//If it's not empty...
    {
        Player p = players.get(new Random().nextInt(players.size()); //Get a random player
        //Do something
    }
    
     
Thread Status:
Not open for further replies.

Share This Page