I have a Repeatingtask with a count down that runs every second. How can I now run the countdown only every 2 seconds if the count is smaller than 10?
In my Repeatingtask: PHP: if($count >= 0 && $count <= 20) {$server->broadcastMessage($count);$count--;} else {$this->count = 20;}//But now I want to slow down the countdown when it reaches 10
A way would be to decrement the valueb 0.5. But this isn't flexible at all. This will only work with numbers between 0 and 1 that are multiples of 2 or 5. For example, using $speedAfter10 as 1/3 would change the value of $count from 10 to 9.7, 9.4, 9.1, 8.8 etc. Here, the code below would skip 9 totally because $count was never 9.0. The strpos is a hack btw, I thought of using if(is_float($count)) but that didn't work. PHP: $count = 20;$speedAfter10 = 1/2;if($count >= 0 && $count <= 20){ if($count >= 10) { $server->broadcastMessage($count); $count--; }else{ if(strpos($count, ".") === false){ $server->broadcastMessage($count); } $count -= $speedAfter10; }}else{ $count = 20;}
Or use a dual counter: PHP: class DualCounterTask extends PluginTask{private $primaryCounter;private $secondaryCounter;public function __construct(Plugin $owner){ parent::__construct($owner); $this->primaryCounter = 30; $this->secondaryCounter = $this->getTicksPerPrimaryCount();}public function onRun($ticks){ --$this->secondaryCounter; if($this->secondaryCounter <= 0){ --$this->primaryCounter; $this->secondaryCounter = $this->getTicksPerPrimaryCount(); if($this->primaryCounter <= 0){ // end of the counter, do something? } }}private function getTicksPerPrimaryCount() : int{ if($this->primaryCounter > 20) return 25; if($this->primaryCounter > 10) return 20; return 10;}} In the above snippet, there are two counters, namely "primary counter" and "secondary counter". They work like the seconds and minutes needles of a clock. When the secondary counter finishes a cycle (decreases from its highest value to 0), the primary counter decreases by one, and the secondary counter rewinds back to the highest value. The task should be scheduled at a repeating rate of 1 tick. getTicksPerPrimaryCount() evaluates the highest value of the secondary counter. In the above code logic, where the primary counter starts at 30: When the primary counter is greater than 20 (i.e. from 21 to 30), the primary counter decreases by one every 25 ticks; Otherwise, when it is greater than 10 (i.e. from 11 to 20), it decreases by one every 20 ticks; Otherwise (i.e. from 0 to 10), the primary counter deceases by one every 10 ticks. The secondary counter decreases by one every tick. Since the secondary counter's reset value varies with the primary count, the primary count decrements at an inconsistent rate depending on the secondary counter, so it decreases at 1/1.25 Hz from 30 to 20, at 1/1 Hz from 20 to 10, and at 1/0.5 Hz from 10 to 0.
strpos($count, ".") is a very hacky check. It'd be most favourable if your answers on the Development forum provide answers to similar cases rather than answers only to this specific case. Please also elaborate your answer. This forum is not a code writing service.
Yeah I was super confused before using that. I couldn't find a better substitute (maybe round(float) === float would work, yet kinda hacky). I thought is_float() would work, but the code converts the integer to float permanently.