Arrays and Online Players

Discussion in 'Spigot Plugin Development' started by zThana, May 8, 2015.

Thread Status:
Not open for further replies.
  1. What exactly are you trying to achieve with this?
    Code (Text):
            Collection<? extends Player> arrayOfPlayer;
            int j = (arrayOfPlayer = Bukkit.getOnlinePlayers());
    If you want to iterate over all players, try using this:
    Code (Text):
    for (Player player : Bukkit.getOnlinePlayers())
    {
    //....
    }
    To get the amount of online Players, just invoke Collection#size().
     
    • Useful Useful x 1
  2. This gives me a error too:

    Code (Text):
            Main.guess.put("Reaction",
              RandomStringUtils.randomAlphanumeric(Main.this.getConfig().getInt("Length_Of_Guessing_Word")));
            Player[] arrayOfPlayer;
            int j = (arrayOfPlayer = Bukkit.getOnlinePlayers()).length;
            for (int i = 0; i < j; i++)
            {
     
  3. Yes, because you can't just cast a Collection to an array. Collection#toArray() can do that.
    From what i've seen in you code though, you want to send a Message to every Player on the server. There's no use of converting the Collection you get from Bukkit.getOnlinePlayers() into an Array, because you can simply iterate over this Collection directly.
     
  4. First of all, what is this?
    Code (Text):
    int j = (arrayOfPlayer = Bukkit.getOnlinePlayers());
    I don't think that's even makes sense.

    Second,
    Code (Text):
    Collection<? extends Player> arrayOfPlayer;
    Player p = arrayOfPlayer[i];
    You don't access collections that way. If you want to use it as an array, then declare arrayOfPlayer as an array and not a collection. I suggest you read up on some basic Java first.
     
    • Like Like x 2
  5. Where do I put that?..
     
  6. You are putting a ';' after your for loop?
    for (Player p : Bukkit.getOnlinePlayers()); ?
     
  7. You mean this?

    for (Player player : Bukkit.getOnlinePlayers());
     
  8. Yeah.
     
  9. What's wrong with it?
     
  10. Yes he is refering to that. With the semicolon at that point you prevent the loop from happening as you tell Java that your statement is finished.

    On a side Note. If you try to send all players the same message why not simply use Bukkit#broadcastMessage(String message);
     
    #12 L33m4n123, May 8, 2015
    Last edited: May 8, 2015
    • Useful Useful x 1
  11. I'm trying to make a ChatReaction thing, and I'm counting the amount of players online, then if it's above a specified number, it should broadcast the "code" and the winner should receive it's rewards.
     
  12. First, do you even know what a enhanced for loop is? You are just putting it there with no meaning.

    Second, why are you creating a array with players if you have your for loop with players? you can just send you message to each player with your player in you for loop.
    (if you want to send a message to each player)
     
    • Agree Agree x 1
  13. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

    Thread locked per request
     
Thread Status:
Not open for further replies.