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

Solved Cancel a certain task

Discussion in 'Development' started by Saxavlax001, Jan 28, 2020.

  1. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    Hey,I was trying to make a cosmetic for my server(colorful armor) and it worked after some tries but I don't know how to "turn it off",I mean I don't know how to cancel the colorful armor task with the result of the armor stop sending to player,I have just found and know how to cancel all tasks to all players but not a certain task for a player
    PHP:
                case "on":
        
    $colorfularmortask = new ColorfulArmorTask($this->plugin$sender);
            
    //close possible task
            
    $this->plugin->getScheduler()->cancelTask($colorfularmortask->getTaskId());
        
    //use task
        
    $colorfularmorhandler $this->plugin->getScheduler()->scheduleRepeatingTask($colorfularmortask20);
        
    //$colorfularmortask->setHandler($colorfularmorhandler);
           
    return true;
                case 
    "off":
            
    $sender->getArmorInventory()->clearAll();
            
    $this->plugin->getScheduler()->cancelAllTasks($this->plugin$sender);
             
    //doesn't work idk
            /**$colorfularmortask = new ColorfulArmorTask($this->plugin, $sender);
            $this->plugin->getScheduler()->cancelTask($colorfularmortask->getTaskId());*/
            
    return true;
                }
    The task works fine but I can't understand how to cancel it,I would like some help if its possible.
     
  2. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    Save The Task Id
    public $taskIds = [];

    $task = ....//YOUR TASK
    $this->taskIds[$player->getName()] = $task;

    //To cancel
    $this->getScheduler()->cancelTask($this->taskIds[$player->getName()]->getTaskId());
     
  3. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    PHP:
            $colorfularmortask = new ColorfulArmorTask($this->plugin$sender);
            
    $this->taskIds[$sender->getName()] = $colorfularmortask;
            
    $this->plugin->getScheduler()->cancelTask($this->taskIds[$sender->getName()]->getTaskId());
    Like that?Because it doesn't work.
     
  4. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    And I use public $taskIds = [];
     
  5. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    PHP:
    register the public $taskId = [];
    case 
    "on":
        
    $colorfularmortask = new ColorfulArmorTask($this->plugin$sender);
            
    //close possible task
           
        //use task
        
    $colorfularmorhandler $this->plugin->getScheduler()->scheduleRepeatingTask($colorfularmortask20);
        
    $this->taskId[$sender->getName()] = $colorfularmorhandler;
        
    //$colorfularmortask->setHandler($colorfularmorhandler);
           
    return true;
                case 
    "off":
            
    $sender->getArmorInventory()->clearAll();
            if(isset((
    $task $this->taskId[$sender->getName()]))){
            
    $this->plugin->getScheduler()->cancelTask($task->getTaskId());
            }
             
    //doesn't work idk
            /**$colorfularmortask = new ColorfulArmorTask($this->plugin, $sender);
            $this->plugin->getScheduler()->cancelTask($colorfularmortask->getTaskId());*/
            
    return true;
                }
     
  6. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
  7. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    I used what you sent.
     
  8. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    sorry, please edit this code
    PHP:
    if(isset(($task $this->taskId[$sender->getName()]))){
            
    $this->plugin->getScheduler()->cancelTask($task->getTaskId());
            }
    to
    PHP:
    if(isset($this->taskId[$sender->getName()])){
    $task $this->taskId[$sender->getName()];
    $this->getServer()->getScheduler()->cancelTask($task->getId());
    }
     
  9. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    PHP:
         if ($sender instanceof Player) {
            if(isset(
    $args[0])) {
            switch(
    $args[0]) {
                case 
    "on":
        
    $colorfularmortask = new ColorfulArmorTask($this->plugin$sender);
        
    //use task
        
    $colorfularmorhandler $this->plugin->getScheduler()->scheduleRepeatingTask($colorfularmortask20);
        
    $this->taskId[$sender->getName()] = $colorfularmorhandler;
        
    //$colorfularmortask->setHandler($colorfularmorhandler);
           
    return true;
                case 
    "off":
            
    $sender->getArmorInventory()->clearAll();
            
    $colorfularmortask = new ColorfulArmorTask($this->plugin$sender);
            
    $this->taskId[$sender->getName()] = $colorfularmortask;
             if(isset(
    $this->taskId[$sender->getName()])){
    $colorfularmortask $this->taskId[$sender->getName()];
    $this->plugin->getScheduler()->cancelTask($colorfularmortask->getTaskId());
             }
            
    /**$this->plugin->getScheduler()->cancelAllTasks($this->plugin, $sender);
             //doesn't work idk
            $colorfularmortask = new ColorfulArmorTask($this->plugin, $sender);
            $this->plugin->getScheduler()->cancelTask($colorfularmortask->getTaskId());*/
            
    return true;
                }
          } else {
               
    $sender->sendMessage("§c/colorfularmor <on: off>");
               return 
    true;
             }
          }
         if (
    $sender instanceof ConsoleCommandSender) {
         
    $sender->sendMessage("Run this command in-game!");
      return 
    true;
      }
     }
    Ok isset error doesn't exist now,but task still doesn't stop.
     
  10. TestDevelopment

    TestDevelopment Spider Jockey

    Messages:
    41
    GitHub:
    non
    Is it important to say getTaskId?

    Try without
     
  11. xLeakDev Enzo

    xLeakDev Enzo Spider Jockey

    Messages:
    38
    GitHub:
    nya-enzo
    dude, you just copy pasted
     
  12. Saxavlax001

    Saxavlax001 Witch

    Messages:
    63
    GitHub:
    Saxavlax001
    I had solved this problem by myself before a year ago,so I mark this as "fixed".
     
  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.