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. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Yes using sleep() anywhere in a plugin is bad practice and not recommended.
     
    HimbeersaftLP likes this.
  2. #A6543

    #A6543 Zombie

    Messages:
    267
    Can someone tell me now a working way how to wait 5 seconds???
     
  3. #A6543

    #A6543 Zombie

    Messages:
    267
    Cloud I use this?
    PHP:
    while(time() + <= $this->time) {}
     
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    If you want your servers main thread to completely stop for 4 seconds while it's executing that loop.
     
    HimbeersaftLP and Sandertv like this.
  5. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    No, using it on the main thread is a big no no. Using it in an AsyncTask is bad practice but doesn't necessarily affect anything. Using it to stop your own threads is fine, it's why these functions exist.
     
    0x15f likes this.
  6. #A6543

    #A6543 Zombie

    Messages:
    267
    Can u give me an example ?
     
  7. #A6543

    #A6543 Zombie

    Messages:
    267
    I tried this (But it don't work):
    PHP:
    class ChestCloseTask extends PluginTask {

        public 
    $plugin;
        public 
    $player;
        public 
    $detonate 20;

        public function 
    __construct(Plugin $pluginPlayer $player){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->player $player;
        }
        public function 
    onRun($currentTick)
        {
           
    $detonate $this->detonate;
            
    $detonate--;
            
    $this->plugin->getServer()->broadcastMessage($detonate);
            if(
    $this->detonate == 4){
                
    $this->plugin->getLogger()->info("4!");
            }

            if(
    $this->detonate == 3){
                
    $this->plugin->getLogger()->info("3!");
            }

            if(
    $this->detonate == 2){
                
    $this->plugin->getLogger()->info("2!");
            }

            if(
    $this->detonate == 1){
                
    $this->plugin->getLogger()->info("1!");
         }else{ 
    }
           }
    }
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Why not '80' instead of '4 * 20'?
    And it's better you use '===' instead of '==' in this situation.
    It isn't working because every time the task is executed (or ran), $detonate is just a "COPY" of $this->detonate. So $detonate-- will just decrease the value of the "COPY" of $this->detonate and this won't affect the GLOBAL var, $detonate.

    PHP:
    $detonate $this->detonate;//detonate = 80
    $detonate--;//detonate = 79
    You should do this instead...
    PHP:
    --$this->detonate;
    //Preferably use it without declaring variable $detonate here.
    OR if you want to still live the variable life, use references ('&').
     
  9. #A6543

    #A6543 Zombie

    Messages:
    267
    PHP:
    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)
        {
            
    $this->detonate 20;
            
    $this->plugin->getServer()->broadcastMessage($this->detonate);
            if(
    $this->detonate == 80){
                
    $this->plugin->getLogger()->info("4!");
            }

            if(
    $this->detonate == 60){
                
    $this->plugin->getLogger()->info("3!");
            }

            if(
    $this->detonate == 40){
                
    $this->plugin->getLogger()->info("2!");
            }

            if(
    $this->detonate == 20){
                
    $this->plugin->getLogger()->info("1!");
         }else{ 
    }
           }
    }
    This only sends 4! To the Console.

    And how can I close the chest I'd the timer ist 0?
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    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)
        {
            
    $this->plugin->getServer()->broadcastMessage($this->detonate);
            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!");
            }else{
            }
            --
    $this->detonate;
        }
    }
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    If you know $chest (tile)...
    2 ways:

    1. Remove chest:
    PHP:
    $chest->getLevel()->setBlock($chestBlock::get(Block::AIR));
    $chest->getLevel()->removeTile($tile);
    if (
    $chest instanceof Tile$chest->close();
    2. Remove inventory from $player:
    PHP:
    $chestwindow $chest->getInventory();
    $player->removeWindow($chestwindow);
    You might be like, "why should I remove the chest?". I'd actually go for removing the block. And thats because... sometimes, when you use Player::removeWindow(), the player's GUI just vanishes.
     
  12. #A6543

    #A6543 Zombie

    Messages:
    267
    But how can I check in the event if the Timer is finished?
     
  13. #A6543

    #A6543 Zombie

    Messages:
    267
    This changed nothing
     
  14. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    on the main $task->detonate if it is 0 that means it is finished?
     
  15. #A6543

    #A6543 Zombie

    Messages:
    267
    Ok. And is this right?
    PHP:
    $task = new ChestCloseTask($this$player);
    $taskid $this->getServer()->getScheduler()->scheduleDelayedTask($task20);
    $task->setHandler($taskid);
     
  16. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    $this->getServer()->getScheduler()->scheduleDelayedTask($task = new ChestCloseTask($this$player), 20);
    $taskId $task->getId();
     
    Muqsit likes this.
  17. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    In the task...
    PHP:
    if ($this->detonate <= 0) {
        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
        
    //Task is now cancelled. It isn't running anymore.
    }
     
  18. Aviv

    Aviv Baby Zombie

    Messages:
    156
    replace that with
    PHP:
    $this->detonate $this->detonate 20;
     
  19. Aviv

    Aviv Baby Zombie

    Messages:
    156
    When the timer is 0, run this
    PHP:
    // you need to have your chest tile, we will define it as $tile
    $tile->close();
    // make sure no players is in the chest, if you have players in the chest you want to add this into your code
    $packet = new ContainerClosePacket();
    $packet->windowid 0;
    $this->getServer()->broadcastPacket($tile->getViewers(), $pk);
    $tile->close();
     
  20. #A6543

    #A6543 Zombie

    Messages:
    267
    Code:
    Call to undefined method Pl\ChestCloseT
    ask::getId()
     
  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.