Message only for arraylist

Discussion in 'Plugin Development' started by NickDEV, Sep 12, 2015.

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


    How to send a message only to the players that are in arraylist?
  2. Loop through the list with a nested for-loop
  3. Offline


    Why would you use a nested for loop?

    Use an enhanced for each loop and send it to each player in the list.
  4. I meant enhanced (the for (object o : list) {)
  5. Offline


    I would recommend sending the message to only the players with the permission.
    Bukkit.broadcast("MESSAGE", "PERMISSION.NODE");
    If the list contained a player's name, and the player wasn't online, it would throw an error. With this method, it would be safer (in my opinion). If you wanted to keep the list, this is a way of doing it:
    ArrayList<String> list = new ArrayList<String>();
            for(String player : list){
                Player onlinePlayer = Bukkit.getPlayer(player);
                if(!onlinePlayer.isOnline() || onlinePlayer == null) return;
  6. That's so wrong..
    1. First check if it is null or .isOnline() will throw a NullPointerException if it's null
    2. Use the continue; statement instead of returning to continue the loop ignoring the offline players
Thread Status:
Not open for further replies.

Share This Page