If you want your servers main thread to completely stop for 4 seconds while it's executing that loop.
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.
I tried this (But it don't work): PHP: class ChestCloseTask extends PluginTask { public $plugin; public $player; public $detonate = 4 * 20; public function __construct(Plugin $plugin, Player $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{ } }}
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 ('&').
PHP: class ChestCloseTask extends PluginTask { public $plugin; public $player; public $detonate = 80; public function __construct(Plugin $plugin, Player $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?
PHP: class ChestCloseTask extends PluginTask { public $plugin; public $player; public $detonate = 80; public function __construct(Plugin $plugin, Player $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; }}
If you know $chest (tile)... 2 ways: 1. Remove chest: PHP: $chest->getLevel()->setBlock($chest, Block::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.
Ok. And is this right? PHP: $task = new ChestCloseTask($this, $player);$taskid = $this->getServer()->getScheduler()->scheduleDelayedTask($task, 20);$task->setHandler($taskid);
PHP: $this->getServer()->getScheduler()->scheduleDelayedTask($task = new ChestCloseTask($this, $player), 20);$taskId = $task->getId();
In the task... PHP: if ($this->detonate <= 0) { $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId()); //Task is now cancelled. It isn't running anymore.}
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();