Messages to Arraylists

Discussion in 'Plugin Development' started by Requadin, Sep 2, 2012.

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

    Requadin

    I want to send a message to all the players in an arraylist. For this i tried using that command but it didn't work. What is the right way to do it?

    for (String X : Players)
    X.sendMessage(ChatColor.GOLD + "Game will start in 2 minutes.");
     
  2. Offline

    escortkeel

    The following code should work:

    Code:
    for (String playerName : players) {
        Player player = Bukkit.getPlayer(playerName);
     
        if(player != null) {
            player.sendMessage(ChatColor.GOLD + "Game will start in 2 minutes.");
        }
    }
     
  3. Offline

    Courier

    escortkeel's method would work, but this would be more efficient:
    Code:java
    1. for(Player k : Bukkit.getOnlinePlayers())
    2. {
    3. if(playerNameList.contains(k.getName()))
    4. k.sendMessage("MESSAGE HERE");
    5. }
    Although that is assuming you preserved the case of their names in your list.
     
  4. Offline

    escortkeel

    You are correct.
     
  5. Offline

    Requadin

    escortkeel Courier
    Ok, thank you guys for the help but I have one more question. I have this boolean.
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    final Player player = (Player) sender;
    With your method, my plugin is sending messages to all the players in my specific arraylist. However i have a teleportation part, where the plugin teleports the sender to a x,y,z coord. Actually i thought plugin will register all the senders and teleport all of them but it does not. That's why i had asked about that code in this thread and i have this problem again. But I can't attach your method to my teleportation one:
    World w = player.getWorld();
    Location loc = new Location(player.getLocation().getWorld(), 100,60,100);
    player.teleport(loc);
    and k.getWorld() seems not working. :eek:
     
  6. Offline

    escortkeel

    Firstly, your variable w is not being used. Did you actually mean:

    Code:
    Location loc = new Location(w,100,60,100);
    ?

    Anyway, you'll want to use something like this:

    Code:
    for(Player k : Bukkit.getOnlinePlayers())
    {
        if(playerNameList.contains(k.getName())) {
            k.sendMessage("MESSAGE HERE");
            player.teleport(new Location(player.getLocation().getWorld(), 100, 60, 100));
        }
    }
     
    Requadin likes this.
  7. Offline

    Requadin

    Thanks a lot, that worked! :p
     
Thread Status:
Not open for further replies.

Share This Page