1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Can i get player in task??

Discussion in 'Development' started by kaliiks, Jan 19, 2017.

  1. kaliiks

    kaliiks Zombie

    Messages:
    250
    I dont know if i cant, but i have one attempt
    PHP:
    <?php

    namespace Core;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    Core\Core;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\TextFormat as C;

    class 
    ParticleTask extends PluginTask
    {

    private 
    $plugin;
    private 
    $playergg = [];

    public function 
    __construct(Core $plugin)
    {
    $this->plugin $plugin;
    parent::__construct($plugin);
    }

    public function 
    getPlayer(Player $p)
    {
    $this->playergg[$p->toString] = $p;
    }


    public function 
    onRun($currentTick)
    {
    foreach (
    $this->playergg as $p) {
    $this->testPlayer($p);
    $this->plugin->getServer()->broadcastMessage("It work");
    }
    }
    }
     
    Last edited by a moderator: Feb 16, 2017
  2. VentroxStudio

    VentroxStudio Witch

    Messages:
    71
    Lol.

    PHP:
    foreach ($this->plugin->getServer()->getOnlinePlayers() as $player) {
    $player->sendMessage("It works!");
    }
     
  3. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    PHP:
    public $playerName;
    public function 
    __construct(Core $plugin,$name)
    {
    $this->plugin $plugin;
    $this->playerName=$name;
    parent::__construct($plugin);
    }

    public function 
    onRun($currentTick)
    {
    if(
    $player $this->plugin->getServer()->getPlayerExact($this->playerName)) $player->sendMessage("It works");
    }
     
    applqpak likes this.
  4. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    you can add in the constructor the Player Object ^^ then you can send him directly a message ^^

    he want to send a message to one player, not all who are online ^^
     
  5. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    That's very unsafe. If the player leaves the game you end up crashing the server.
    PHP:
    public $playerName;
    public function 
    __construct(Core $plugin,string $name){
      
    $this->plugin $plugin;
      
    $this->playerName=$name;
      
    parent::__construct($plugin);
    }

    public function 
    onRun($currentTick){
      if((
    $player $this->plugin->getServer()->getPlayerExact($this->playerName)) instanceof Player){
        
    $player->sendMessage("It works");
      }
    }
     
  6. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    you can check if the player object is not null ^^ then there should be no issues anymore xD
     
  7. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    a player object cannot be null, because it wouldn't be a player object then. If you meant to check if the variable is not null, that's also wrong, because it could be an object of an offline player, or an object of something else, or a string. just do an instanceof check. and, if required an online check after that.
     
  8. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    lol i am using a lot of times this to check if the player is offline or online and it works fine!

    PHP:
    $player /* pocketmine\Player Object */
    if($player != null){
        
    //player is online
    } else {
        
    //player is offline
    }
     
  9. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    :facepalm:
    Nope, I can gurantee you that "//player is offline" will NEVER be reached, if $player really contains a pocketmine\Player Object. Do a vardump on $player, and you'll see it is null if "//player is offline" is reached. So you can't say it's containing a Player Object.
     
  10. Bluplayz

    Bluplayz Spider Jockey

    Messages:
    43
    GitHub:
    bluplayz
    hmm okay seems that your right but the condition works perfect and everytime says me the right ^^
     
  11. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    it does work with function returning null if no player is found/the player is offline. But don't use it for checking whether a player object still points to an online player.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.