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

PlayerRespawnEvent

Discussion in 'Development' started by Jonas, Apr 18, 2017.

  1. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You can schedule the task with
    PHP:
    PluginBase::getServer()->getScheduler()->scheduleDelayedTask(new TaskObject($this$player), $delay 20); 
    In the Task class, you can construct it like this:
    PHP:
    <?php

      
    use pocketmine\Player;
      use 
    pocketmine\scheduler\PluginTask;
      use 
    pocketmine\entity\Effect;

      class 
    TaskObject extends PluginTask{

        private 
    $plugin;

        public function 
    __construct(Main $pluginPlayer $player){
          
    parent::__construct($plugin);
          
    $this->plugin $plugin;
          
    $this->player $player;
        }

        public function 
    onRun($tick){
          
    $this->player->addEffect(/*Your effect*/);
          
    //Anything here will be run approximately 1 second after the PlayerRespawnEvent is called.
        
    }
      }
     
  2. Jonas

    Jonas Baby Zombie

    Messages:
    192
    Then come this error
    Code:
    [Server] [PocketMine] > Could not execute task Main\EffectTask: Argument 1 passed to pocketmine\Player::addEffect() must be an instance of pocketmine\entity\Effect, integer given, called in /storage/emulated/0/PocketMine/plugins/Test/src/Main/Test.php on line 245
    [Server] [PocketMine] > TypeError: "Argument 1 passed to pocketmine\Player::addEffect() must be an instance of pocketmine\entity\Effect, integer given, called in /storage/emulated/0/PocketMine/plugins/Test/src/Main/Test.php on line 245" (EXCEPTION) in "/src/pocketmine/Player" at line 4070
    
    Line 244 - 245
    PHP:
    public function onRun($tick){
          
    $this->player->addEffect(Effect::JUMP);
     
  3. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Use this to define an effect:
    PHP:
     Effect::getEffect(Effect::JUMP); 
    You can also set amplifier and duration (in ticks) with $effect->setAmplifier() and $effect->setDuration().
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Note that you're better off passing the name of a player to a task, instead of the player object. If you don't do it, it'll use the player data from the moment the task got scheduled, which means it's no longer tracking the player. Once you have the player name in the task, you can use $server->getPlayer($this->name);
     
    jasonwynn10 and corytortoise like this.
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Basic OOP: Objects are, unless cloned, passed by implicit memory reference, i.e. the data of an object will always be the same set of data unless it is cloned. Unless it is a new player (rejoined), which is probably not what you want.
    However it is a good idea not to persist the player object to prevent memory leak, as player instance should no longer be referenced anywhere after player quit.
     
  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.