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

how do you make scheduleRepeatingTask's period go higher in some time?

Discussion in 'Development' started by Levi, Jun 12, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    PHP:
    private $time 4;

        public function 
    __construct(Plugin $owner)
        {
            
    parent::__construct($owner);
        }

        public function 
    onRun(int $currentTick)
        {
                    
    $this->time--;
    foreach(
    $this->getOwner()->getServer()->getOnlinePlayers() as $player){
    $player->sendMessage("This is when going 3 ticks");
    }
              

                if(
    $this->time == 1){
    ## change ticks to 5? it was 3 btw

                
    }
    }
    main
    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new MyTask3);
     
  2. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    PHP:
    $this->time 5;
    Lol
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Did you read my post?
     
  4. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    I'm not sure what you asked, maybe explain better. If you want to change the task period you can't, once you schedule the task that's how it stays unless you cancel it and schedule a new one with higher ticks.
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    How do others does it? The archon crates spinning goes fast first and then slower and slower until stopped
     
  6. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Link?
     
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
     
  8. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    It didn't only go slower but it also went faster
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Set the task delay to 1 tick and internally delay the task more after a certain amount of ticks.
    PHP:
    $this->time++;

    if(
    $currentTick $this->delay){
        return;
    }

    if(
    $this->time 100){
        
    $this->delay $currentTick;
    }elseif(
    time 200){
        
    $this->delay 10 $currentTick;
    }

    //do stuff
     
    Last edited: Jun 12, 2018
  10. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    do i send the inventory every if($this->time < 100.,200,300){?
     
  11. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    because it wont work
    PHP:
    if ($this->time 50) {
                
    $this->delay 20 $currentTick;
                
    Crate::$player->sendMessage("20 tick");

               
    $rand Main::getInstance()->crates->get("Items");

                
    $random1 explode(":"$rand[mt_rand(0count($rand) - 2)]);

                
    $randomAmount mt_rand(1$random[2]);
                
    $menu InvMenu::create(InvMenu::TYPE_CHEST);
                
    $menu->getInventory()->setContents([

       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
     
                ]);
                
    $menu->readonly();
                
    $menu->send($player);
            } elseif (
    $this->time 100) {
                
    $this->delay 100 $currentTick;
                
    Crater::$player->sendMessage("100 tick");
             
               
    $rand Main::getInstance()->crates->get("Items");
                
    $random1 explode(":"$rand[mt_rand(0count($rand) - 2)]);
                
    $menu InvMenu::create(InvMenu::TYPE_CHEST);
                
    $menu->getInventory()->setContents([

       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
     
                ]);
                
    $menu->readonly();
                
    $menu->send($player);
            }
     
  12. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    the task tick dont change
     
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I mean like
    PHP:

    /** @var int */
    private $delay 0;

    public function 
    onRun(int $currentTick){
        
    $this->time++;

        if(
    $currentTick $this->delay){
            return;
        }

        if(
    $this->time 100){
            
    $this->delay $currentTick;
        }elseif(
    time 200){
            
    $this->delay 10 $currentTick;
        }

        
    //set item in chest HERE.
        //you're supposed to execute the code here
        //the code above is solely to handle the delaying
    }
    It'll be more performant if you create one InvMenu instance and use it throughout.
    PHP:

    /** @var InvMenu */
    private $menu;

    public function 
    __construct(){
        
    $this->menu InvMenu::create(InvMenu::TYPE_CHEST);
        
    $this->menu->readonly();
    }
    About these
    PHP:
                $menu->getInventory()->setContents([

       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
       
    Item::get($random1[0], $random1[1], $random1[2]),
     
                ]);
    You could use array_fill() to make your code look better.
    PHP:
    $this->menu->getInventory()->setContents(
        
    array_fill(06Item::get($random1[0], $random1[1], $random1[2]))//6 copies of Item, array indexed from 0
    );
     
    Last edited: Jun 12, 2018
    Lolipop78709, Angel and HimbeersaftLP 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.