[SOLVED] Get online players

Discussion in 'Spigot Plugin Development' started by jessegeerts, Jun 4, 2015.

Thread Status:
Not open for further replies.
  1. hey guys i wanted to make a MOTD that gets the online players


    here is my current code:
    Code (Text):

    package cf.GiantCraft;

    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.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Core extends JavaPlugin implements Listener{
       
       
        public void onEnable(){
            Bukkit.getPluginManager().registerEvents(this, this);
        }
       
       
       
       
       
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            Player p = e.getPlayer();
            e.setJoinMessage(p.getName() + " Heeft de server gejoined!");
            for(int i=0; i < 100; i ++)
           {
           p.sendMessage("");
           }
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage("");
            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7&l-----------------------------------"));
            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&bWelkom &6 " + p.getName() + " &bop GiantCraft!"));
            p.sendMessage("");
            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&8Doneer>> &6Shop.GiantCraft.cf"));
            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7Vote>> &6Gebruik /vote om de vote sites te bekijken!"));
            p.sendMessage("");
            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Veel plezier op onze server!"));
            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&6Er zijn &c" + Bukkit.getOnlinePlayers() + " &6Spelers online!"));
            p.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7&l-----------------------------------"));
           
        }
       
       
        @EventHandler
        public void onQuit(PlayerQuitEvent e){
            Player p = e.getPlayer();
            e.setQuitMessage(p.getName() + " Heeft de server verlaten!");
        }
       

    }

     
    Thanks and cheers!
     
  2. As in the player amount? Use Bukkit.getOnlinePlayers().size() to get the amount of players online.
     
  3. You can use this code:
    Code (Text):
    for (Player p : Bukkit.getServer().getOnlinePlayers()) {
       
        // save p to a list
       
    }
    For each player that is online, it will assign them to "p". By using a list, you could add all players to a list and then display them all.
     
  4. getServer().getOnlinePlayers().length will return the amount of players online.
     
  5. The getOnlinePlayers() method returns the players online, the .length returns the amount.
     
  6. Serializator

    Supporter

    The getOnlinePlayers(); method returns a Collection. You can't use .length to get the amount of online players anymore, you need to use .size();.
     
    • Agree Agree x 2
  7. @Serializator Yeah, not with 1.7 though, that's what I'm making my plugins with, sorry :S
     
  8. Serializator

    Supporter

    N000BBBB0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0B0 <3
     
    • Creative Creative x 1
  9. Serializator

    Supporter

    ProtocolLib?
     
  10. int onlineP = 0;
    for(Player p : Bukkit.getOnlinePlayers()){
    onlineP++;
    }
     
  11. Bukkit.getOnlinePlayers().length
     
  12. No. Just no.
     
    • Agree Agree x 5
  13. Serializator

    Supporter

    If you where reading the comments from other people to post a helpfull comment based on the comments other people posted, the getOnlinePlayers(); method returns a Collection. You need to use .size(); to get the size, not .length, it isn't an Player[] anymore.
     
  14. Use getOnlinePlayers.size() it isn't that hard...
     
  15. It's not hard. It's also not hard to read that his question has been answered with exactly the same thing as yours making your post quite useless.
     
  16. @jesseke55 unlike everyone above me, let's answer your question:

    If you want to set the MOTD dynamically, use ServerListPingEvent#setMotd(String)

    If you want to get it from another server, you need to manually ping it. (There are probably a few tutorials and libraries for)

    in 1.8 those packets are added in the net.Minecraft.server.<version> package.
     
  17. Calm down. I was trying to suggest something and then you pound me. This is not that spigot community is for. People make mistakes. Deal with it.
     
  18. @DarkSeraphim Still, I find it more efficient and works perfectly, so why not? :D
     
Thread Status:
Not open for further replies.