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

Solved How can I find out if the player disconnect?

Comments in 'Plugin Development' started by cake23, Apr 6, 2016.

  1. cake23
    Offline

    cake23 Active Member

    Joined:
    Aug 10, 2015
    Posts:
    124
    Minecraft User:
    TheEnd
    Hey! I write a little minigame. And I want to find out, if the player left the game. Because if a player have not so much health, he disconnect and the another player dont get the kill. Does anyone know how can I fix that or so? :'D
  2. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    You should use PlayerQuitEvent.
    PHP:
    public function OnQuit(PlayerQuitEvent $event)
    {
    if(
    $this->isInGame /*?*/($event->getPlayer()){
    // Code.
    }
    }
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    Player::isConnected()
    I think this also should work
    PHP:
    if($player instanceof Player)
  4. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    But where you'll use it? In a task?
    I think that using a event will bring, at least in this case, a better performance.
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Thats the way how to check if player has been disconnected but you show how to run code when player disconnects. Of course thats a great idea in a mini-game when all participants are in array and you can remove that player from the array and don't waste time checking each time we have to loop through or use that array.
    cake23 likes this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What is $player? If $player has been a Player instance all the time, it won't suddenly become null because the player joins. Once it is an object, unless the variable was defined by reference such that it can be modified from other places, it is always an object despite its internal properties get changed.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    If I save a player who joined into variable and player quits, will the variable stay Player object and won't get destructed?
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes. As long as anyone, including your plugin, has a non-weak reference (non-WeakRef) to the object, the object will not be collected by the GC. This is why we tell you to avoid saving instances of Player or Level in class properties without special care - there would be a memory leak.

    PocketMine is just PHP code. It can't tell PHP to destruct an object.
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Oh yeah. I was confused because this looked similar for me
    PHP:
    Server::getPlayer($name) instanceof Player
    HotFireyDeath likes this.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    This is a function where it can return null sometimes even if given the same parameter.
    HotFireyDeath likes this.

Share This Page

Advertisement