PEMapModder / PocketMine-Plugin-Tutorials

See wiki for old version. See this website for new version:
https://pemapmodder.github.io/PocketMine-Plugin-Tutorials/
Other
36 stars 15 forks source link

timer #13

Closed Ivanzar closed 9 years ago

Ivanzar commented 9 years ago

timer Hi, I have a question. How to stop the player for some time, I use this code, but it does not work (

public function StopPlayer(PlayerMoveEvent $event){
        if(isset($this->sp[$event->getPlayer()->getName()])){

            $event->getPlayer()->sendMessage("stop");
            $event->setCancelled(true);
            $event->getPlayer()->sendMessage("stop");
sleep(10);
$event->setCancelled(false);
$event->getPlayer()->sendMessage("GO");
        }

    }
PEMapModder commented 9 years ago

sleep() should basically never be used by plugins unless you have something to do with threads. You can use a scheduler. Also, $event only refers to one move event. setCancelled does not do anything except marking the event is cancelled. After your handler (and handlers from other plugins) finishes executing, PocketMine will check if the event is marked cancelled, if yes, revert the player motion. Using sleep() will stop PocketMine from executing, and therefore will not do anything. You should cancel the move event when the time is within that 10 seconds, and do nothing when the player can move.