Solved Get all online players, and select only one at random

Discussion in 'Plugin Development' started by Ward1246, Jun 28, 2015.

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

    Ward1246

    Hello, I am trying to select only one online player at random, but cannot find a way just yet. I have tried
    Player randomplayer = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
    But that has a possibility of selecting more than one player. I am currently trying to find a way to solve this, but I will stop soon, it's very late. I hope that this can have a post later, or something if I get back quick, not likely. Thanks in advance.

    EDIT: I was wrong. That code does not select many players. If you have this error, a likely issue is that you have the code in a bad spot, IE somewhere where it might take a bit to get to and will fire off many times.
     
    Last edited: Jul 1, 2015
  2. Offline

    ArmyArmy

    Untested, but should work
    Code:
    Players[] allplayers = Bukkit.getOnlinePlayers();
    Random random = new Random();
    Player randomplayer = allplayers[random.nextInt(allplayers.length)];
    
     
  3. No, no it doesn't.
     
  4. Offline

    Ward1246

    It has for me. I ran it a few times, and it seems to select more than one player. I will show code once i put it in again.

    Thank you! Testing now.
     
  5. In that case you're calling it more than once, it wouldn't make sense that it would be able to select more than one. And the code provided to you by @ArmyArmy is the exact same code, except that it's on multiple lines.
     
  6. Offline

    Ward1246

    Just figured it out: I had it in a bad spot, so it was not put many times, but it was fired off many times. May bad, but i fixed it. Marked as solved.


    Let this be a lesson to all who mess up just like me. It's funny actually, I had this very same error with onDeathEvent a few days ago.
     
  7. Offline

    ArmyArmy

    @AdamQpzm Not rly, "Player randomplayer = Bukkit.getOnlinePlayers([newRandom().nextInt(Bukkit.getOnlinePlayers().length)];" this doesn't seem right, these )] are misplaced and if he's using getOnlinePlayers([newRandom().nextInt(Bukkit.getOnlinePlayers().length]); that's not even a valid method.
     
  8. Offline

    Ward1246

    If you got that from my first post on the OP, i was trying to get it on 1 lint, but it wasn't working, it didn't show up but it is. I accidentally deleted some of the code, let me fix it.

    EDIT: The code should look like this:
    Player randomplayer = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
     
  9. @ArmyArmy Okay fine, so it's the same but on multiple lines and the syntax corrected :) Either way, it would still do the same, obviously since the OP wasn't complaining about compilation errors, the error was just when he wrote it here, not in his actual code.
     
  10. Offline

    Ward1246

    Thank you both for helping me! And one more thing: If you don't mind me changing the topic of the problem, I have gotten another bug I need help with. I have a random number generator, and this sets up a block at a random location based on the number. Now, I don't get any errors in eclipse or any console errors, but when I go to the location, there is no block. Can I get any help with this?

    Code (open)

    // perhaps this is the reason? I was using this to define most player stuff, so I threw this part in aswell. I will try to change it, I remember I was going to at one point.
    Player player = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];

    Random loc4 = new Random();
    int escape;

    for (int counter2 = 1; counter2<=1;counter2++) {
    escape = 1+loc4.nextInt(2);
    escape = 1+loc4.nextInt(3); // all this was the random number stuff

    if (escape == 1) {
    World world = player.getWorld();
    Block block = world.getBlockAt(-170, 75, -20);
    block.setType(Material.QUARTZ_ORE);
    Block block1 = world.getBlockAt(203, 136, -34);
    block1.setType(Material.COAL_BLOCK);
    Block block2 = world.getBlockAt(114, 126, 131);
    block2.setType(Material.COAL_BLOCK); // Setting the block and all that. I am not sure what's wrong.
    } else {

    if (escape == 2) {
    World world = player.getWorld();
    Block block3 = world.getBlockAt(-170, 75, -20);
    block3.setType(Material.COAL_BLOCK);
    Block block4 = world.getBlockAt(203, 136, -34);
    block4.setType(Material.QUARTZ_ORE);
    Block block5 = world1.getBlockAt(114, 126, 131);
    block5.setType(Material.COAL_BLOCK);
    } else {

    if (escape == 3) {
    World world1 = player.getWorld();
    Block block6 = world.getBlockAt(-170, 75, -20);
    block6.setType(Material.COAL_BLOCK);
    Block block7 = world.getBlockAt(203, 136, -34);
    block7.setType(Material.COAL_BLOCK);
    Block block8 = world.getBlockAt(114, 126, 131);
    block8.setType(Material.QUARTZ_ORE);

    I am almost certain that the random number is not the problem, and I have this very same thing used again, so I think it's the set block method. Thanks again!
     
  11. Offline

    ArmyArmy

    Okay, few things:
    First of, you said "random location" but you're using static coords.
    Second why this?
    Code:
    for (int counter2 = 1; counter2<=1;counter2++) {
     
  12. Offline

    Ward1246

    That's how *I believe at least* it set's up some of the random numbers. I got it from a tutorial a while back, and used it ever since. And it should be a random selector, that selects the location, but yeah static cords. I meant random location by a randomly selected location, my bad.
     
  13. Offline

    ArmyArmy

    Well, I have an idea about what you're trying to do.
    This is how I would probably do it.
    Code:
        Player player = Bukkit.getOnlinePlayers()[new Random().nextInt(Bukkit.getOnlinePlayers().length)];
    
         Random rand = new Random();
         int escape = 1+rand.nextInt(2);  // 1 + ( 0 - 2 ) so we can get 1,2,3
         World world = player.getWorld();
         Block block, block1, block2;
         switch(escape){
         case 1: // if it's 1
           block = world.getBlockAt(-170, 75, -20);
           block.setType(Material.QUARTZ_ORE);
           block1 = world.getBlockAt(203, 136, -34);
           block1.setType(Material.COAL_BLOCK);
           block2 = world.getBlockAt(114, 126, 131);
           block2.setType(Material.COAL_BLOCK);
           break;
         case 2: // if it's 2
           block = world.getBlockAt(-170, 75, -20);
           block.setType(Material.COAL_BLOCK);
           block1 = world.getBlockAt(203, 136, -34);
           block1.setType(Material.QUARTZ_ORE);
           block2 = world.getBlockAt(114, 126, 131);
           block2.setType(Material.COAL_BLOCK);
           break;
         case 3: // if it's 3
           block = world.getBlockAt(-170, 75, -20);
           block.setType(Material.COAL_BLOCK);
           block1 = world.getBlockAt(203, 136, -34);
           block1.setType(Material.COAL_BLOCK);
           block2 = world.getBlockAt(114, 126, 131);
           block2.setType(Material.QUARTZ_ORE);
           break;
         }
    
    Also, tell me, when do you execute that code?
     
  14. Offline

    Funergy

    @Ward1246 The Chunk where the block is you are changing needs to be loaded!
    @ArmyArmy Will not work probably. look the above reason.
     
  15. Offline

    Ward1246

    I have it put it after I get the random player actually.
    If I do
    Code:
                Location loc = new Location(player.getWorld(), x, y, z); // I'll put each cordinates here
                Chunk chunk = loc.getChunk();
                chunk.load(true);
    I will try it, and if that fails I will report back.

    I got it to work, I needed to move this as well, I updated the chunks and set the block, That seemed to fix it. Thanks for all the help everyone! I will remember this next time before I make a new post.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
Thread Status:
Not open for further replies.

Share This Page