Like the title, how can i cancel PlayerMoveEvent on classTask? PHP: class MPCast extends Task{ public $main; public $player; public $second = 5; public function __construct(Loader $main,Player $player){ $this->main = $main; $this->player = $player; } public function onRun(int $tick){ if($this->second > 0){ //code here $ev->setCancelled(true); } if($this->second == 0){ $food = $this->player->getFood()+5; if($food >= 20){$food = 20;} $this->player->setFood($food); $this->player->addTitle('+5 MP'); $this->main->getScheduler()->cancelTask($this->getTaskId(),$this->player); } $this->player->sendPopup($this->second); $this->second--; }}
In your PlayerMoveEvent handler, you could check if the task is running. If it is, cancel the event. Or, you could just use Player->setImmobile(), since that will prevent any movement at all until you set them mobile again. However, you can't do it like you intended, because you can't just conjure up an event to cancel when the task is running. You can, however, do like I mentioned.
Off topic, but great one-liner PHP: $this->player->setFood(min(20, $this->player->getFood() + 5));// Hardcoded values FTW? You can use something like this PHP: # __constructMainPlugin::freeze(Player)# MainPlugin::freeze$this->frozen[Player] = true;# PlayerMoveEventif($this->frozen[Player]) $event->setCancelled(); Edit: I know that I've mixed up static and non-static properties, but it's only proof of concept
You can't do in a task. You can try with PHP: $player->setImmobile() as @corytortoise suggests or you can do a small trick using PHP time() function in a way like this: PHP: class YourEventListener implements Listener { private $freezed = array(); public function onPlayerMove(PlayerMoveEvent $ev){ $p = $ev->getPlayer(); if(isset($this->freezed[$p->getName()]){ if(time() - $this->freezed[$p->getName()] < 10){ //The player will be freezed for 10 seconds $ev->setCancelled(); }else{ unset($this->freezed[$p->getName()]); } } }} Then to freeze a player: PHP: $this->freezed["player_name"] = time(); This is only an example code. Change the code according to your needs.
This question has been asked many times. Please search the forums. https://forums.pmmp.io/threads/cooldown-on-event.1744/ (More than one year ago!) https://forums.pmmp.io/threads/learning-timer-or-task.57/#post-826 (The 57th thread on this forum, back in 2016!) (And many more threads of the same question on old forums!)