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

Why Won't My Schedule Cancel?

Comments in 'Plugin Development' started by Tethered_, Nov 25, 2014.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I'm trying to create a countdown that will broadcast every second (starting at 30) Game Will Start In x Seconds. It was working, but I had a suspicicion that after it had finished, it was running in the background, so I added a getLogger->info->("STILL RUNNING") and I found that no matter what I tried, the schedule continued to check every second. What is going on? (The value for the counter is stored in count.txt for the moment)
    PHP:
    class Countdown extends PluginTask{
        public function 
    onRun($currentTick) {
            
    $sec file_get_contents($this->getOwner()->getDataFolder() . "count.txt");
            if(
    $sec != 0) {
            
    $this->getOwner()->getServer()->broadcastMessage("Game Starts In $sec Seconds");
            
    $sec--;
            
    file_put_contents($this->getOwner()->getDataFolder() .  "count.txt"$sec);
            }
            
    $this->getOwner()->getServer()->broadcastMessage("STILL RUNNING");
        }
        public function 
    cancel() {
            
    file_put_contents($this->getOwner()->getDataFolder() .  "count.txt"30);
            
    $this->getOwner()->getServer()->broadcastMessage("Game Will Now Start!");
            
    $this->getHandler()->cancel();
        }
    }
    PHP:
    public function onBlockBreak(BlockBreakEvent $BBE) {
            
    $task = new Countdown($this);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
        }
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Reference:
    https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/plugin/PluginManager.php#L640 (shall be the uppermost call because this function is not specific for scheduling)
    https://github.com/PocketMine/Pocke...etmine/scheduler/ServerScheduler.php#L136-137 ( https://github.com/PocketMine/Pocke.../pocketmine/scheduler/ServerScheduler.php#L41 $task is a TaskHandler instance)
    https://github.com/PocketMine/Pocke...pocketmine/scheduler/TaskHandler.php#L130-140

    Therefore, the correct sequence of code call to cancel an event is:
    1. ServerScheduler->cancelTask($taskId) (by comparing https://github.com/PocketMine/Pocke...etmine/scheduler/ServerScheduler.php#L124-125 and https://github.com/PocketMine/Pocke...etmine/scheduler/ServerScheduler.php#L136-137 , we can see that cancelTasks(Plugin) executes code similar to cancel($taskId)
    2. TaskHandler->cancel()
    3. Task->onCancel()
    You are calling 2., so the last part of cancelling a task (ServerScheduler->tasks[$taskId] unsetting) is skipped, so it doesn't work.
    EvolSoft likes this.

Share This Page

Advertisement