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

Call player on Plugin Task

Comments in 'Plugin Development' started by Artide, Oct 9, 2015.

  1. Artide
    Offline

    Artide Active Member

    Joined:
    Apr 20, 2015
    Posts:
    214
    Minecraft User:
    Artide5
    How to send a message to specific player on plugin task? Please and thank you. :D
  2. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    PHP:
    public function onRun($t){
    //don't forget to define $this->plugin using __construct
     
    try {$this->plugin->getServer()->getPlayerExact($namestring)->sendMessage("Message");} catch (Exception $e){}
    }
    I added code with try {} catch {} cuz when you don't add them and player who you want to send message is offline, the server will CRASH
    TheKofola12 likes this.
  3. Artide
    Offline

    Artide Active Member

    Joined:
    Apr 20, 2015
    Posts:
    214
    Minecraft User:
    Artide5
    $p = $this->getServer()->getPlayerExact($name);
    if($p !== null){
    $p->sendMessage("hi");
    }else{
    return;
    }
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Please be more specific. Which kind of plugin task? Repeating? Delayed? DelayedRepeating?
    There's actually a simpler way to check. :p
    PHP:
    if($this->plugin->getServer()->getPlayerExact($name) !== null){
        
    //code here
    }
    Why do you need to return? onRun() does't need a return value. :p
  5. Artide
    Offline

    Artide Active Member

    Joined:
    Apr 20, 2015
    Posts:
    214
    Minecraft User:
    Artide5
    return nothing.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    There is actually $this->owner that you can use directly.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You can actually just pass the Player object to the PluginTask.

    Note that you cannot do that with AsyncTask, since any objects that have direct or indirect reference to the Server object would most likely crash it.
  8. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    Add this or take you want from the Main :
    PHP:
    $player $event->getPlayer();
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new FileName($this$player), /*Time*/);//added $player
    Task file :
    PHP:
    <?php

    namespace Namespace;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Player;

    class 
    ClassName extends PluginTask {

    public function 
    __construct(Main $pluginPlayer $player) {

    parent::__construct($plugin);
    $this->plugin $plugin;
    $this->player $player;
    }
    public function 
    onRun($tick) {
    $this->player->sendMessage("Lol");// that's an example.
    }
    }
    Last edited: Oct 12, 2015
  9. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    How much do you want him to be specific?
    Last edited: Oct 12, 2015
    PEMapModder likes this.
  10. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    It's parent::__construct($plugin) not parent::__construct($plugin, $player). Please learn OOP. Look here: https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/scheduler/PluginTask.php
    TonyDroide likes this.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Is there any difference?
    Taha_The_Hacker likes this.

Share This Page

Advertisement