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

How to cancel task when player quit?

Discussion in 'Plugin Help' started by AmalCraft, Jun 4, 2021.

  1. AmalCraft

    AmalCraft Creeper

    Messages:
    5
    I made a minigame plugin with some countdown when you run the command, but when the player quits the game while the countdown still playing, the server is crashes. How to cancel the task when the player quits the server??

    Heres my attempt :
    PHP:
    public function onQuit(PlayerQuitEvent $event) {
            
    $player $event->getPlayer();
            
    $task = new GameTask($this$player);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
        }
     
    Primus likes this.
  2. Axon

    Axon Zombie

    Messages:
    276
    Agent, Primus and minijaham like this.
  3. AmalCraft

    AmalCraft Creeper

    Messages:
    5
  4. Mcbeany

    Mcbeany Spider Jockey

    Messages:
    33
    GitHub:
    mcbeany
    save the task's id:
    PHP:
    $task = new GameTask($this$player);
    $taskId $task->getTaskId();
    $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
    then cancel it when player quits:
    PHP:
    $this->getScheduler()->cancelTask($taskId);
     
    Primus and minijaham like this.
  5. minijaham

    minijaham Skeleton

    Messages:
    801
    GitHub:
    minijaham
    That's exactly what you are looking for. It has the code you need, but just with a different concept.
     
    Primus, Axon and Mcbeany like this.
  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.