PHP: $task = new ChestCloseTask($this, $player); $this->getServer()->getScheduler()->scheduleDelayedTask($task, 20);//.....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) { 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
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($task, 20);if($this->sec <= 0) {
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
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($task, 20);//.....class ChestCloseTask extends PluginTask { private $plugin, $player; private $detonate = 5; //seconds public function __construct(Plugin $plugin, Player $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; }}
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
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