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

Chests

Discussion in 'Development' started by #A6543, Dec 31, 2016.

  1. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    Does your task extend Plugintask?
     
  2. #A6543

    #A6543 Zombie

    Messages:
    267
    Yes
     
  3. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    wtf?
     
  4. #A6543

    #A6543 Zombie

    Messages:
    267
    PHP:
    $task = new ChestCloseTask($this$player);
            
    $this->getServer()->getScheduler()->scheduleDelayedTask($task20);
    //.....
    class ChestCloseTask extends PluginTask {

        public 
    $plugin;
        public 
    $player;
        public 
    $detonate 80;

        public function 
    __construct(Plugin $pluginPlayer $player){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
        }
        public function 
    onRun($currentTick)
        {
            if(
    $this->detonate === 80){
                
    $this->plugin->getLogger()->info("4!");
            } elseif(
    $this->detonate === 60){
                
    $this->plugin->getLogger()->info("3!");
            } elseif(
    $this->detonate === 40){
                
    $this->plugin->getLogger()->info("2!");
            } elseif(
    $this->detonate === 20){
                
    $this->plugin->getLogger()->info("1!");
    } elseif (
    $this->detonate <= 0) {
        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
            }else{
            }
            
    $this->detonate 20;
        }
    }
    But it displays only the 4
     
    Last edited: Jan 1, 2017
  5. #A6543

    #A6543 Zombie

    Messages:
    267
    UPDATE:
    My countdown works now.
    PHP:
    class ChestCloseTask extends PluginTask {

        public function 
    __construct(Plugin $owner){
            
    parent::__construct($owner);
        }
        public function 
    onRun($currentTick)
        {
            
    $this->getOwner()->sec = --$this->getOwner()->sec;
            if(
    $this->getOwner()->sec >= 0) {
            
    $this->getOwner()->getLogger()->info($this->getOwner()->sec);
            } else {
            
    $this->getOwner()->getLogger()->info("Ende");
            
    $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getTaskId());
            }
        }
    }
    But I tried to check if the counter is 0. That don't worked.
    PHP:
    $this->sec 4;
    $task = new ChestCloseTask($this$player);
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
    if(
    $this->sec <= 0) {
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    note that using the sec on your main means you cant do two of these [aka concurrent] action at once which may be a bit of bad practice
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Why would you use
    PHP:
    $this->detonate 20
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    What you are trying to do is probably...
    PHP:
    $this->detonate $this->detonate 20;
     
  9. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    PHP:
    $this->detonate -= 20;
     
    corytortoise and TheDragonRing like this.
  10. #A6543

    #A6543 Zombie

    Messages:
    267
    Um. Pls read my last post :p
     
  11. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    That's what I meant ;). Never stop the main thread.
     
  12. #A6543

    #A6543 Zombie

    Messages:
    267
    But why can't I get it with $this->sec?ยข
     
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I don't know why no one is calling you for bad coding lol.
     
  14. #A6543

    #A6543 Zombie

    Messages:
    267
    .....
    How would you do it?
     
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I wouldn't even use a task in the first place.
    Elaborate when you'd like the chest to close.

    As for the task...
    PHP:
    $task = new ChestCloseTask($this$player);
            
    $this->getServer()->getScheduler()->scheduleDelayedTask($task20);
    //.....
    class ChestCloseTask extends PluginTask {

        private 
    $plugin$player;
        private 
    $detonate 5//seconds
        
    public function __construct(Plugin $pluginPlayer $player){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
        }
        public function 
    onRun($currentTick)
        {
            if(
    $this->detonate === 4){
                
    $this->plugin->getLogger()->info("4!");
            } elseif(
    $this->detonate === 3){
                
    $this->plugin->getLogger()->info("3!");
            } elseif(
    $this->detonate === 2){
                
    $this->plugin->getLogger()->info("2!");
            } elseif(
    $this->detonate === 1){
                
    $this->plugin->getLogger()->info("1!");
    } elseif (
    $this->detonate <= 0) {
        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
            }
            --
    $this->detonate;
        }
    }
     
  16. Aviv

    Aviv Baby Zombie

    Messages:
    156
    maybe instead of running a task which counts down 4 seconds then run the code run a task which runs after 4 seconds and cancel itself?
    please think
     
  17. Aviv

    Aviv Baby Zombie

    Messages:
    156
    i think i was unclear,
    instead of running a task that runs every 20 ticks(1 second) and counts down a timer for 4 seconds.
    just run a task that runs after 4 seconds, does the code and cancel itself.
    most efficient way. I dont blame you, most new players doesnt understand how to use tasks ;)
     
  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.