Checking if a specific player is online

Discussion in 'Plugin Development' started by Benpat9, Jun 12, 2014.

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

    Benpat9

    basicaly i want the command isvitalogonline to say if the player vitalog is online i have tried different ways but if he ISNT online i get an internal error and i dont know why...

    Code:java
    1. else if(commandLabel.equalsIgnoreCase("isvitalogonline")){
    2. if(player.hasPermission("Legacy.vitalog")) {
    3. if(Bukkit.getPlayerExact("vitalog").isOnline()) {
    4. player.sendMessage(ChatColor.GREEN + "Vitalog Is Online");
    5. }
    6. else{
    7. player.sendMessage(ChatColor.RED + "Vitalog Is Offline");
    8. }
     
  2. Code:java
    1. Player target = Bukkit.getPlayer("vitalog");
    2.  
    3. if (target == null){
    4.  
    5. player.sendMessage(ChatColor.RED + "Vitalog Is Offline");
    6.  
    7. }else{
    8.  
    9. player.sendMessage(ChatColor.GREEN + "Vitalog Is Online");
    10.  
    11. }
     
  3. Offline

    Chlorek

    It's because getPlayerExact() returns null if there is no such player. And you CAN'T call methods for null stuff... logic - there is no object. Simply check if getPlayerExact() != null.
     
Thread Status:
Not open for further replies.

Share This Page