Solved Sending messages to all players in array list help!

Discussion in 'Plugin Development' started by gamingod, Jan 21, 2014.

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

    gamingod

    Hello there,
    does anybody know how to send a message to all of the players that are in a array list?
    Thanks!
     
  2. Offline

    Warreo

    I'm assuming the it's an ArrayList of player's names so:
    Code:
    for(String s : list){
      Bukkit.getPlayer(s).sendMessage("message");
    }
    :)
     
  3. Offline

    gamingod

    When I do that is says to change it from string to player and once I change it to a player it tells me to change it back. This is the array list:
    ArrayList<Player> ingame = new ArrayList<Player>();
     
  4. Offline

    Jake6177


    Modifying slightly to avoid an error:

    Code:java
    1. for (String s : list) {
    2. Player p = Bukkit.getPlayerExact(s);
    3. if (! p == null) p.sendMessage("msg");
    4. }


    gamingod Then it's an Arraylist of players (bad!...)

    this will work, but it's not very efficient:

    Code:java
    1. for (Player p : list) {
    2. if (! p == null) //player hasn't logged out
    3. p.sendMessage("blabla");
    4. }
     
  5. Offline

    gamingod

    Same thing :(
    It tells me to change it to a player instead of String s: list but then it tells to change it back :(
     
  6. Offline

    Jake6177


    Code please...I seriously hope you're not just sitting here copying and pasting..
     
  7. Offline

    gamingod

    the second one doesn't get an error, I am going to test it now
     
  8. Offline

    beastman3226

    Change your ArrayList to a list of Strings, that is where your error is.
     
  9. Offline

    gamingod

    I am not

    Then how do I add players to it?

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

    Jake6177


    Stop double-posting. Did the second method work?

    If you use the more efficient list (player names rather than full player objects) then you'd just populate the list with Player.getName() rather than Player.
     
  11. Offline

    gamingod

    got it to work. I was typing it into the wrong spot XD
    Thanks Jake6177
     
  12. Offline

    Jake6177


    No problem, but I'd still use player names rather than players.
     
  13. Offline

    LCastr0

    I know it is solved, but I want to add a comment.
    You could also use
    Code:java
    1. ArrayList<String> players = new ArrayList<String>();
    2.  
    3. for(Player p : Bukkit.getOnlinePlayers()){
    4. if(players.contains(p.getName())){
    5. p.sendMessage(Message here);
    6. }
    7. }
     
    asdfcesar and gamingod like this.
Thread Status:
Not open for further replies.

Share This Page