Message only for arraylist

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

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

    NickDEV

    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

    xMrPoi

    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

    Ricecutter0

    I would recommend sending the message to only the players with the permission.
    Code:
    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:
    Code:
    ArrayList<String> list = new ArrayList<String>();
    
            for(String player : list){
                Player onlinePlayer = Bukkit.getPlayer(player);
                if(!onlinePlayer.isOnline() || onlinePlayer == null) return;
                onlinePlayer.sendMessage("MESSAGE");
            }
     
  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