Need help to get names of players in StaffChat (plugin I'm making) read more below!

Discussion in 'Plugin Development' started by DinoZauruZ, Dec 13, 2014.

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

    DinoZauruZ

    I'm trying to make a plugin called "StaffChat" that works great but I just want to add a little feature to it.
    When you execute the command /sc or /staffchat you get a message saying that you're now in staffchat and all other players in staffchat also gets a message saying you joined the staffchat but I want to make so when the player joins the staffchat they get a message saying all the names of players inside the ArrayList. With commas example:
    "Players in StaffChat: (player1Instaffchat), (player2instaffchat)"

    Code:
    Code:
    package me.DinoZaur.staffchat;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class StaffChat extends JavaPlugin{
    
        public ArrayList<Player> SC = new ArrayList<Player>();
       
        public final ChatListener cl = new ChatListener(this);
       
        @Override
        public void onEnable(){
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvents(cl, this);
        }
       
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, String[] args) {
           
            Player p = (Player) sender;
           
            if(cmd.getName().equalsIgnoreCase("staffchat") || (cmd.getName().equalsIgnoreCase("sc"))){
                if(p.hasPermission("StaffChat.StaffChat")){
                    if(!SC.contains(p)){
                        sendMessage(p, "You just joined the chat.");
                        SC.add(p);
                        for(Player all : p.getWorld().getPlayers()){
                            String alls = all.getName();
                           
                            sendMessage(p, "Currently In StaffChat: ");
                            p.sendMessage(alls);
                            if(SC.contains(all)){
                                all.sendMessage(ChatColor.DARK_GRAY + p.getName() + " Just joined the chat.");
                               
                           
                        }
                       
                    }
                    }else{
                        sendMessage(p, "You just left the chat.");
                        SC.remove(p);
                        for(Player all : p.getWorld().getPlayers()){
                            if(SC.contains(all)){
                                sendMessage(all, p.getName() + " Just left the chat.");
                       
                            }
                       
                   
                            }
                    }
            }else{
                p.sendMessage(ChatColor.DARK_RED + "You do not have permission to access StaffChat!");
            }
        }
        return false;
    }
    
        public void sendMessage(Player sendMessageTo, String MessageToBeSent) {
            sendMessageTo.sendMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA
                    + "StaffChat" + ChatColor.DARK_GRAY + "] " + ChatColor.GRAY
                    + MessageToBeSent);
        }
    }
    This code sends the player a message (when joining) all players names on the server like
    "Players in staffchat (player1)"
    "Players in staffchat (player2)"
    "Players in staffchat (player3)"
    "Players in staffchat (player4)"

    and the player gets spammed...

    I'd also like to add if you guys don't mind, how to get all online players on the server not only the players in the world that the player is in. if you read the code I made a for loop to get all players in the world the player is in I made same for loop in ChatListener
    for(Player all : p.getWorld().getPlayers()){

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

    Skionz

    @DinoZauruZ Create a StringBuilder, Iterate through the players in your list, add them to the StringBuilder with commas and such, turn the StringBuilder into a String.
     
  3. Offline

    DinoZauruZ

    I'll try do that the best I can thanks.

    It didn't work could you help me a bit @Skionz
    Code:
    Code:
    if(cmd.getName().equalsIgnoreCase("staffchat") || (cmd.getName().equalsIgnoreCase("sc"))){
                if(p.hasPermission("StaffChat.StaffChat")){
                    if(!SC.contains(p)){
                        sendMessage(p, "You just joined the chat.");
                        SC.add(p);
                        for(Player all : p.getWorld().getPlayers()){
                           
                           
                            if(SC.contains(all)){
                                all.sendMessage(ChatColor.DARK_GRAY + p.getName() + " Just joined the chat.");
                                StringBuilder sb = new StringBuilder();
                                for (int i = 0; i < args.length; i++) {
                                    sb.append(all.getName());
                                    sb.append(", ");
                                    p.sendMessage(ChatColor.GRAY + "Players in StaffChat: " + sb);
    EDIT by TImtower: merged posts

    http://prntscr.com/5gfl19
    It seems like the player gets as many messages as there are players in staffchat :/
    Code:
    Code:
    if(cmd.getName().equalsIgnoreCase("staffchat") || (cmd.getName().equalsIgnoreCase("sc"))){
                if(p.hasPermission("StaffChat.StaffChat")){
                    if(!SC.contains(p)){
                        sendMessage(p, "You just joined the chat.");
                        SC.add(p);
                        for(Player all : p.getWorld().getPlayers()){
                          
                          
                            if(SC.contains(all)){
                                all.sendMessage(ChatColor.DARK_GRAY + p.getName() + " Just joined the chat.");
                                StringBuilder sb = new StringBuilder();
                                    sb.append(all.getName());
                                    sb.append(", ");
                                    p.sendMessage(ChatColor.GRAY + "Players in StaffChat: " + sb.toString());
                            
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  4. Offline

    Skionz

    @DinoZauruZ That is because your sending them a message every time it loops. Create the StringBuilder above the enhanced for loop and send them the message below it.
     
  5. Offline

    DinoZauruZ

    I need to create it under to be able to use all.getName(); :/ Thanks anyways @Skionz any other idea?
     
  6. Offline

    Skionz

    @DinoZauruZ You append the StringBuilder in the enhanced for loop, but declare and initialize the StringBuilder outside.
     
  7. Offline

    DinoZauruZ

    Okay... Now I do not understand anything... Show this in code? I'm not the best on english, sorry! @Skionz

    Code:
    if(!SC.contains(p)){
                        sendMessage(p, "You just joined the chat.");
                        SC.add(p);
                      
                        StringBuilder sb = new StringBuilder();
                      
                        for(Player all : p.getWorld().getPlayers()){
                          
                          
                            sb.append(all.getName());
                            sb.append(", ");
                            p.sendMessage(ChatColor.GRAY + "Players in StaffChat: " + sb.toString());
                          
                            if(SC.contains(all)){
                                all.sendMessage(ChatColor.DARK_GRAY + p.getName() + " Just joined the chat.");
                              
                              
                                
    Player gets one message saying if there are 2 online:
    "Players on StaffChat: (player1)"
    "Players on StaffChat: (player1), (player2)"

    It also says players that are not in staff chat :/ @Skionz

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

    All_Fire69

    If you want to get all the online players, it would be:
    Code:
    for(Player all : Bukkit.getServer().getOnlinePlayers(){
    
    }
     
  9. Offline

    Skionz

    @DinoZauruZ Why wouldn't it show players who aren't in the staffchat? Look at your code more clearly.
     
  10. Offline

    DinoZauruZ

    for(player all : bukkit.getServer().getOnlinePlayers()){
    doesn't work :/ @All_Fire69

    and Okay @Skionz

    @Skionz thanks for advice xD, but still the player gets 2 messages one saying
    "Players on staffchat: (staffchatplayer1)"
    "Players on staffchat: (staffchatplayer1), (staffchatplayer2)"

    "Players on staffchat: (staffchatplayer1)"

    http://prntscr.com/5gk34x - Check link for picture.

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

    Skionz

    @DinoZauruZ It doesn't work because there is no 'player' class.
     
    All_Fire69 likes this.
  12. Offline

    DinoZauruZ

  13. Offline

    All_Fire69

    Case sensitive.. for(Player all : Bukkit.getServer().getOnlinePlayers()){
    }
     
  14. Offline

    DinoZauruZ

  15. Offline

    RainoBoy97

    Make sure you remove the player from the list when they log out/plugin disables, or else you will end up with nasty memory leaks.
     
  16. Offline

    DinoZauruZ

  17. Offline

    DinoZauruZ

    Another bump?
     
  18. Offline

    stoneminer02

    Try place the sendMessage outside the for loop?
    Code:
    for(stuff){ //stuff }
    player.sendMessage("Players online: " + sb.toString());
     
Thread Status:
Not open for further replies.

Share This Page