Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[HELP] Check if player is online

Comments in 'Plugin Development' started by Primus, Apr 10, 2015.

  1. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    I'm now creating simple plugin, just to learn programming!
    I want to check if player is online when i do /slay $player!
  2. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,434
    Plugins:
    14
    There's probably a better way, but this works.
    PHP:
    $isOnline in_array($player,$this->getServer()->getOnlinePlayers());
    EvolSoft and Lambo like this.
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    $isOnline will return bool?
  4. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    PHP:
    public function isPlayerOnline($username){
       if(
    $this->getServer()->getPlayer($username) instanceof Player){
          return 
    true;
       }else{
          return 
    false;
       }
    }
    Like @LDX said, there is probably a better way than this too, but it works, so yay!
    LDX and xiaoq like this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    These methods are too slow. You are looping through the players at least twice.
    PHP:
    $name $args[0];
    $player $this->getServer()->getPlayer($name);
    if(
    $player instanceof Player){
      
    $player->setHealth(0);
      
    $sender->sendMessage("Slayed.");
    }else{
      
    $sender->sendMessage("That player ain't online!");
    EvolSoft and luca28pet like this.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Omg :D Tnx @PEMapModder i was using
    PHP:
     if($player instanceof player/* Not "Player"*/){
    }
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Can't i use kill() instead of setHealth()?
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Same.
    I think yes. I forgot what setHealth() has over kill(). (Part of setHealth() calls kill() if health <= 0)

Share This Page

Advertisement