Getting the server PlayerCount without ServerListPingEvent?

Discussion in 'Spigot Plugin Development' started by Cervinakuy, May 28, 2016.

  1. Hey guys!

    I feel like this is a super simple question, but I want it so when a player joins, it checks if the player count is at a certain number that is set in the config.yml, but I have no idea how to do that without ServerListPingEvent. Here's what I have so far:

    Code (Text):
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
           
            if (/*somehow get player count of the bukkit server  */ == (plugin.getConfig().getInt("Players-Needed"))) {
                Bukkit.getConsoleSender().sendMessage("§7[§b§lSERVERMANAGER§7] §bThe Minimum Player Count has been reached.");
                doSomething();
               
            }
           
        }
    Any help would be appreciated, again, sorry if this is such a simple question to answer :p.

    Thanks,
    - Cervinakuy
     
  2. Bukkit.getOnlinePlayers().size(); ?
     
    • Agree Agree x 3
    • Winner Winner x 2
  3. Sometimes we are forgetting the obvious... I do that things myself sometimes
     
  4. Try this

    Edit:
    Code (Text):
       
              int playercount = Bukkit.getServer().getOnlinePlayers().length;
           
              if(playercount == 1){
                  //do stuff
              }
         
     
    #4 iNoXs, May 28, 2016
    Last edited: May 28, 2016
    • Optimistic Optimistic x 3
    • Creative Creative x 1
  5. ???????
    Not even one thing in this method makes sense. First you define a Player with a collection of players and then you compare it with an integer.

    @StalexDev 's method is the right thing to do
     
    • Agree Agree x 1
  6. Than it's still not correct.
     
  7. yep i know this is correct
    Code (Text):
              int playercount = Bukkit.getServer().getOnlinePlayers().length;
           
              if(playercount == 1){
                  //do stuff
              }
     
    #7 iNoXs, May 28, 2016
    Last edited: May 28, 2016
    • Optimistic Optimistic x 1
  8. Nope still not.
     
  9. why this thing work for me ...
     
  10. The .getOnlinePlayers() method changed, so depending on your version you have to use .size() or .length
     
  11. I use
    Code (Text):
    if(Bukkit.getOnlinePlayers().size() == 1)
      //Do stuff
     
  12. I knew it had to be that simple! I must have accidently skimmed over it in the dropdown for Bukkit., anyways, thank you guys for the help!

    Very true.

    Thanks,
    - Cervinakuy