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
    I've been using
    PHP:
    $task = new kickTask($this$player);
    $this->getServer()->getScheduler()->cancelTask($task->getTaskId());
    Which doesn't work.
    CrazedMiner likes this.
  2. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Post the full code. :p
    MCPEPIG likes this.
  3. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Its not something I want people to see. You need to see my full code to help with one part?
  4. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Well if you don't want to post it then fine. It just means we won't be able to see what you're doing wrong. This is the annoying part about "private" plugins. Post all your code or don't ask for help at all.
    PEMapModder likes this.
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    PHP:
    public function punishHackerCancel($player) {
            
    $task = new punishTask($this$player);
            
    $this->getServer()->getScheduler()->cancelTask($task->getTaskId());
        }
        if(
    $this->isHacking($player)) {
                    
    $this->punishHacker($player);
                    }
                        else {
                        
    $this->punishHackerCancel($player);
                    }
    '$this->punishHacker()' Is a delayed task, which works. The only thing that's not working is cancelling the task which is '$this->punishHackerCancel()'
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why would you want to cancel a task immediately after scheduling it?
    Hotshot_9930 likes this.
  7. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    You would have to understand what the isHacking() function does.
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    See, you cancel the task immediately after the task is created. How can you cancel a task that isn't scheduled?
    Please learn PHP. new punishTask returns a new different instance of it every time. Hence, it has a different task ID.
    Moreover, you did it in an if else. If player is not hacking, if is not run, so you have nothing to cancel at all.[/PHP][/QUOTE]
    Last edited: Sep 1, 2015
  9. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    The task is scheduled and defined within the isHacking() function, as I said before. The task already works, all I need to know is how to cancel it.
  10. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    So if new punishTask returns a new instance should $task = the task I already scheduled?
    Example:
    PHP:
    $task = new dummyTask($this$player);
    $this->getServer()->getScheduler()->scheduleDelayedTask($task), 20);

    // And to cancel it
    $task $this->getServer()->getScheduler()->scheduleDelayedTask(new dummyTask($this$player), 20);
    $this->getServer()->getScheduler()->cancelTask($task->getTaskId());
    ?
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You don't need to schedule it again...
    And as I have already said, `new dummyTask` returns a new task every time. You need to use class properties .
  12. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Then how would I cancel "dummyTask"? That's all I want to know, really.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    As I said, you have to use class properties to save the task.

    Actually you shouldn't schedule it at all. Schedule task if is hacking. Don't schedule task if not hacking.
  14. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    punishHacker() function is the schedule, so it is scheduled when the player is hacking.
  15. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    I'm not 100% sure what you mean. Could you give an example? I assume you mean something like this
    PHP:
    $this->task = new punishTask($this$player);
    Idk, I could be wrong.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes, quite right. But note that $this->task I'd overwritten when another player gets punished.
    Hotshot_9930 likes this.
  17. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Still doesn't work. This is what my cancel() function Looks like in the task
    PHP:
    public function cancel() {
            
    $this->getHandler()->cancel();
        }
  18. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Post the code where you start the task
  19. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    PHP:
    $this->task = new punishTask($this$player);
    if(
    $this->isHacking($player)) {
                    
    $this->punishHacker($player);
                    }
                        else {
                        
    $this->punishHackerCancel($player);
                    }
      Public function 
    punishHackCancel($player) {
        
    $this->getServer()->getScheduler()->cancelTask($this->task->getTaskId());
    }
      
    punishHacker($player) calls the delayed task.
  20. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But why do you cancel? Why don't you just do nothing in the first place?

Share This Page

Advertisement