Solved How do I get all players online?

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

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

    DinoZauruZ

    I (me) cannot find any possible way to get all players online this annoys me alot. I try the
    Code:
    for(Player p : Bukkit.getServer().getOnlinePlayers()){
    //Code here
    }
    That doesn't work tho, the thing that works is
    Code:
    for(Player p : Bukkit.getServer().getWorld("AWorldName, example: 'world'".getPlayers()){
    //code here
    }
    But that only gets players in the world "world" :/ I want to get all players online? Any ideas Please comment if!
     
  2. Offline

    Skionz

  3. Offline

    DinoZauruZ

    It doesn't thanks for responding almost all my help threads @Skionz but still doesn't I get an error in console that it's Deprecated or whatever u say. Please help!

    Using Bukkit 1.7.10 on plugin, CraftBukkit 1.7.9 on TestServer

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Dec 20, 2014
  4. Offline

    Skionz

    @DinoZauruZ Post the exception please.
    @Evaluations What are you talking about? He's using 'p' as the current Player instance not the CommandSender assuming sender is one.
     
  5. Offline

    DinoZauruZ

    2 Sec.

    Here: http://prntscr.com/5irush and http://prntscr.com/5iruz6

    This plugin worked with
    for(Player all : p.getWorld().getPlayers()){
    }
    but that only gets player in his world :/ still for every world @Skionz

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than triple posting.>
     
    Last edited by a moderator: Dec 20, 2014
  6. Offline

    theon

    What is your whole code?
     
  7. Offline

    DinoZauruZ

    A sec.

    Code for main;

    Code:
    package me.DinoZaur.staffchat;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    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);
                      
                        StringBuilder sb = new StringBuilder();
                      
                        for(Player all : Bukkit.getServer().getOnlinePlayers()){
                          
                            if(SC.contains(all)){
                            sb.append(all.getName());
                            sb.append(", ");
                            p.sendMessage(ChatColor.GRAY + "Players in StaffChat: " + sb.toString());
                          
                          
                                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 : Bukkit.getServer().getOnlinePlayers()){
                            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);
        }
      
    }
    Listener;

    Code:
    package me.DinoZaur.staffchat;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;
    
    public class ChatListener implements Listener {
      
    public static StaffChat plugin;
      
        public ChatListener(StaffChat instance){
            plugin = instance;
        }
        @EventHandler
        public void PlayerChatEvent(AsyncPlayerChatEvent e){
            Player p = e.getPlayer();
            if(plugin.SC.contains(p)){
            for(Player all : Bukkit.getServer().getOnlinePlayers()){
                if(plugin.SC.contains(all)){
                    e.setFormat(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + "StaffChat" + ChatColor.DARK_GRAY + "] " + ChatColor.GOLD + p.getName() + ChatColor.GRAY + ": " + ChatColor.DARK_AQUA + e.getMessage());
                    all.sendMessage(e.getFormat());
                    e.setCancelled(true);
                    }
                }
            }
        }
    }
    Edit @theon

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Dec 20, 2014
  8. Offline

    theon

    why don't you just do
    for(Player all : SC){
    }
     
  9. Offline

    Skionz

    @DinoZauruZ You should get rid of all the unnecessary static, and use the instanceof keyword before casting.
     
  10. Offline

    teej107

    1. Raise hand to eye level.
    2. Point the palm of your hand to toward your face.
    3. Move your hand toward your face at a great velocity.
    4. Make sure your palm comes in contact with your face.
    5. Repeat steps 3 and 4 if needed.
    There is nothing wrong with the getOnlinePlayers() method. Deprecation usually means that the method will be removed and/or there is a better alternative out there. In Bukkit's case, it is to raise awareness. You should really read the reason behind the deprecation of methods. BTW, the console doesn't show anything about deprecated methods. It's your IDE that does.
     
  11. Offline

    DinoZauruZ

    http://prntscr.com/5israt

    says - "No such error method" @teej107 @Skionz

    Cause this is not what I want to know I want to know how to get all players that are online on the server.

    <Edited by bwfcwalshy: Merged posts, please use the edit button rather than double posting.>
     
    Last edited by a moderator: Dec 20, 2014
  12. Offline

    Webbeh

    Are you, by any means, using a Bungee server and trying to get all the players in all the worlds ?
     
  13. Offline

    drpk

    @DinoZauruZ try using the same bukkit version as your craftbukkit.
     
  14. @teej107 You're getting confused. getOnlinePlayers() is only deprecated for the invalid one, not to raise awareness :)


    @DinoZauruZ Build against Bukkit, not CraftBukkit, or ensure Bukkit is higher on your build path if you need both.
     
  15. Offline

    teej107

    Well I was referring to the getPlayer() methods and about the switch to UUIDs. My bad about the confusion.
     
    AdamQpzm likes this.
  16. Offline

    JordyPwner

    try: Bukkit.getOnlinePlayers()
     
  17. @JordyPwner Wouldn't make a difference, Bukkit.getOnlinePlayers() calls the Server's method.
     
  18. Offline

    teej107

    Build with 1.7.9 or move your server to 1.7.10.
     
  19. Offline

    DinoZauruZ

    Thanks everyone for all replies. Sorry for being gone for a while :/, but I figured out what was wrong. As @drpk said that I should have same version for booth. I tried it and it worked ;) Thanks Alot!
     
Thread Status:
Not open for further replies.

Share This Page