How to select a random player thats Online?

Discussion in 'Plugin Development' started by Jag.1000, Aug 5, 2012.

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

    Jag.1000

    What would be the code to pick a random online player?
     
  2. Offline

    evilmidget38

    First you need a random number between 0 and the total number of players, to get your random player.
    That would be something like
    Code:
    int i = new Random.nextInt(getServer().getOnlinePlayers().length);
    Second you need to get a player from the online players with the specified index. It's an array, so that would look like this
    Code:
    Player p = getServer().getOnlinePlayers()[i];
     
  3. Offline

    Jag.1000

    Code:
    Bukkit.broadcastMessage(ChatColor.GREEN + p +"has been selected");
    Is that how I would broadcast it?
     
  4. Offline

    evilmidget38

    I get this feeling you're extremely unfamiliar with java, so I suggest you learn more about it before you continue.

    As for your question, no, that's not how you broadcast it. You cannot concatenate a Player, so you need to use p.getName() or p.getDisplayName(), rather than p.
     
  5. Offline

    Jag.1000

    evilmidget38

    Also there is an error on Random.nextInt that says it cannot be resolved to at type. Change to Random.Int?

    I realized that right after I posted it....

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

    evilmidget38

    Make sure to import java.util.Random, and learn more about Java. I hate to sound like a jerk, but you're asking very simple java related questions, you should learn how to use Java before you start trying to create a program in java.
     
  7. Offline

    TheSmallBones

    The confusion was Bukkit related, not Java. I made that mistake awhile ago and I'm fairly experienced with Java...
     
  8. Offline

    Jag.1000

    I am well on my way to making a full plugin thanks to a lot of peoples help, and the fact that I'm decent at Java and Bukkit. You dont have to help if you dont want to. This is the plugin development section.
     
  9. Offline

    TheSmallBones

    I COMPLETELY agree with you. People who come in here with tons of Bukkit experience and that tell you to "learn Java please" when you don't know something about Bukkit is complete bull.
     
    LeonTG77 likes this.
  10. Offline

    firecopy

    Evilmidget was giving you the exact help you needed. Therefore, wouldn't it be safe to believe he does want to help you out! Given the situation of your problem, I agree that you should learn more about java. I always encourage people to keep learning. Learning or being pushed to learn more isn't bad at all. Please accept his advise. It will help you in the long run!
     
  11. Offline

    AmberK

    Just a little expansion upon this, people wouldn't keep repeating it if there wasn't a reasoning behind it. People say "learn java" a lot, because that's what needs to be done. Now, they're not saying "go out and become a master programmer", they're saying learn the basics first and get a general understanding, and by that point well over half of the questions in this section would probably of been self-resolved (even my own, I asked when I had done java for about a week, and learned much faster after googling tutorials).
     
    firecopy likes this.
Thread Status:
Not open for further replies.

Share This Page