How do i check if a player is online?

Discussion in 'Plugin Development' started by filurp, Sep 29, 2012.

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

    filurp

    In my plugin i have a /heal and a /feed command. But if i do /heal <playername> and they are offline, it says "An internal error has occurred". Do i have to check if player is online? If so, how?
     
  2. Offline

    gregthegeek

    If you are using <server>.getPlayer(String) or <server>.getPlayerExact(String), they will return null when the player is offline. You can also check <player>.isOnline().
     
  3. Offline

    Courier

    Bukkit.getPlayerExact(name) will return the Player if they are online. If they are offline, it will return null. So check for null, and print an error yourself. Something like this:
    Code:java
    1. Player player = Bukkit.getPlayerExact(args[0]);
    2. if(player == null)
    3. {
    4. sender.sendMessage("The player could not be found");
    5. return true;
    6. }
     
  4. Offline

    Giorgio

    Code:
    if (commandLabel.equalsIgnoreCase("heal")) {
                if (args.length == 0) {
                    // heal = 0 args /heal giorgio = 1 args
                    player.setHealth(20);
                    player.setFireTicks(0);
                    player.sendMessage(ChatColor.GREEN + "Healed!");
                } else if (args.length == 1) {
                    if (player.getServer().getPlayer(args[0]) != null) {
                        Player targetPlayer = player.getServer().getPlayer(args[0]);
                        targetPlayer.setHealth(20);
                        player.setFireTicks(0);
                        player.sendMessage(ChatColor.GREEN + "Healed!");
                    } else {
                        player.sendMessage(ChatColor.RED + "Player not Online!");
                    }
                }
            }
    Something like this would work.
     
    ShoxX304 likes this.
  5. Offline

    mine-care

    Code:java
    1. if (!player.isOnline()){
    2. //Do somthing if he is not online
    3. }
    4. if (player.isOnline()){
    5. //Do somthying if player IS online
    6. }
     
  6. Offline

    StealerSlain

    Hawezo and Noxyro like this.
  7. Code:java
    1. Player p = getServer().getplayer(<insert player name here>);
    2. if(p != null){
    3. heal(p);
    4. } else {
    5. sender.sendMessage("Player offline!");
    6. return true;
    7. }

    Try this. When a player is offline getServer().getPlayer() returns null. So when you try to heal them it throws a NullPointerException by first checking if the player variable != null you avoid the exception.
     
    nelson2tm likes this.
  8. Offline

    haydenaa

    mine-care the other way is slightly better :p
     
  9. Offline

    Skionz

    You could always use a try and catch block.
     
  10. Offline

    leon3001

    Skionz Meh. I would prefer my code not to throw any erros at all, even if they are caught by a try-catch-block. :p
     
  11. Offline

    teej107

    Using try catch as an if statement replacement is terrible. Just use Bukkit#getPlayer(java.lang.String) and check if itthe value is null. If it's null, then the player is offline. If you try to call isOnline() on it, you throw an NPE. Other than that, this thread is 2 years old!
     
    To175 likes this.
Thread Status:
Not open for further replies.

Share This Page