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

How to cancel a task?

Comments in 'Plugin Development' started by Hotshot_9930, Aug 31, 2015.

  1. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    It will all make sense once the plugin is released and you see the code. What am I doing wrong when cancelling it?
  2. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Just to be clear, it is a delayed task and I want to cancel it before it happens when a player is not hacking.
  3. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
  4. Humerus
    Offline

    Humerus Staff Member Plugin Reviewer

    Joined:
    Aug 23, 2013
    Posts:
    114
    Plugins:
    1
    PHP:
    //schedule task
    $task = new dummyTask($this$player);
    $handler $this->getServer()->getScheduler()->scheduleDelayedTask($task20);
    //cancel task
    $this->getServer()->getScheduler()->cancelTask($handler->getTaskId());
    schedule*(); returns a task handler that you can use to access task properties.
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    I tried this, the task still doesnt cancel it just happens anyways.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Because you aren't using the same $handler maybe?
  7. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    OMG, that snippet of code only schedule the task, does not cancel it. ....

    PHP:
    $task = new kickTask($this$player);
    $h this->getServer()->getScheduler()->scheduleDelayedTask($task20);
    $task->setHandler($h);
    $this->getServer()->getScheduler()->cancelTask($task->getTaskId());
    I myself use something else in GrabBag (i.e. I only save the TaskId, which is the only thing that is needed to cancel the task). GrabBag is indeed able to cancel tasks.
    Jazzwhistle likes this.
  8. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Here is a rough example of how I'm trying to cancel:
    PHP:
    public function onJoin(PlayerJoinEvent $event) {
                
    $player $event->getPlayer();
                
    $task = new Task($this$player);
                
    $handler $this->getServer()->getScheduler()->scheduleDelayedTask($task100);
          
                
    $task->setHandler($handler);
                if(
    $player->getName() == "Test") {
                    
    $this->getServer()->getScheduler()->scheduleDelayedTask($task100);
                }
                else {
                    
    $this->getServer()->getScheduler()->cancelTask($task->getTaskId());
                }
                    }
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    :screams:
    Why did you schedule the task twice! Just remove the scheduleDelayedTask code, and only cancel it in the if block, so you won't need to cancel it at all because you didn't schedule it! If you don't understand what I mean, learn PHP. Read http://php.net/if http://php.net/else
  10. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Woah, just take it easy man. You don't have to say "Learn PHP" all the time. I know PHP, and obviously, like anyone else I'm not perfect at it. There is always something you wont understand and need someone else to explain to you thoroughly.
    Anyways, I figured out what I was doing wrong.
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That's because that's the only explanation I can think of.

    Schedule.
    If condition, schedule again.
    Else, cancel schedule.

    Two problems. First, you schedule twice. Second, you could simply have taken our the first and third line.
    Hotshot_9930 likes this.

Share This Page

Advertisement