I know that when I want to make countdown I should use task! PHP: $this->getScheduler()->scheduleRepeatingTask(new NameTask($this), 20); and I know 20 mean 1 second so we will manage minutes to convert it to seconds. * every minute has 60 seconds ! PHP: // $i mean Minutes !// $s mean Seconds !if($i >= 0 && $s > 0){ $s--;}if($s == 0 && $i > 0){ $i--; $s = 59;} so I tried making this coding for converting 10 minutes to seconds counting down and every 60 second a minute gone PHP: <?phpclass NameTask extends Task{ public function __construct(Main $plugin){ $this->plugin = $plugin; $this->miuntes = 10; $this->seconds = 0; } public function onRun($tick){ $i = $this->miuntes; $s = $this->seconds; if($i >= 0 && $s > 0){ $s--; } if($s == 0 && $i > 0){ $i--; $s = 59; } $OnlinePlayer = $this->plugin->getServer()->getOnlinePlayers(); foreach($OnlinePlayer as $player){ if($s <= 59 && $s >= 10){ $player->sendTip(TextFormat::GREEN . $i." : ".$s); }else{ $player->sendTip(TextFormat::GREEN . $i." : 0".$s); } } } public function cancel() { $this->getHandler()->cancel(); }} and I don't know what's wrong It moved just one second and stopped at " 9:59 " I hope you understand what I want to do and what the issue !
By doing this: PHP: $i = $this->miuntes; $s = $this->seconds; you're making a copy of the variable, so instead of decrementing the class property, you just decrement a local variable (and then overwrite it with the unchanged class property on the next run). One fix would be to replace $i with $this->miuntes and $s with $this->seconds. Another way would be to use a reference instead: PHP: $i = &$this->miuntes; $s = &$this->seconds; although I don't think this is very good practise. btw you misspelled minutes
I figured this out and I tried to think harder until I get it. Because I made this questions and I solve the issue so look why it's stopped at " 9:59 " Because the task repeat it self so I can't put a variable in it for second and miunte it will repeat the action the variable will take the number and the code will do it. After I fix this problem I got new one the countdown missing number 0 in second before move to anther minutes " 9:00 ", " 8:00 ", " 7:00 " ... so I coded the countdown again and finally I got it like this: PHP: if($this->miuntes >= 0 && $this->seconds >= 0){ $this->seconds--;}if($this->seconds < 0 && $this->minutes > 0){ $this->miuntes--; $this->seconds = 59;} and the hole countdown coding goes like this: PHP: <?phpclass NameTask extends Task{ public function __construct(Main $plugin){ $this->plugin = $plugin; $this->minutes= 10; $this->seconds = 0; } public function onRun($tick){ if($this->minutes>= 0 && $this->seconds >= 0){ $this->seconds--; } if($this->seconds < 0 && $this->minutes > 0){ $this->minutes--; $this->seconds = 59; } $OnlinePlayer = $this->plugin->getServer()->getOnlinePlayers(); foreach($OnlinePlayer as $player){ if($s <= 59 && $s >= 10){ $player->sendTip(TextFormat::GREEN . $this->minutes." : ".$this->seconds); }else{ $player->sendTip(TextFormat::GREEN . $this->minutes." : 0".$this->seconds); } } if($this->minutes== 0 && $this->seconds == 0){ $this->cancel(); } } public function cancel() { $this->getHandler()->cancel(); }}
Using references unnecessarily is a bad idea. Consider writing assigning back $this->minutes = $i; after the last mutation of $i, vice versa $thi-s>seconds = $s;. Alternatively just use the full name `$this->seconds` every time and don't create local variables. See https://php.net/sprintf There is a flag that lets you add zero padding to a number.
Of course, a much simpler way is to decrement only seconds, and assign PHP: $this->seconds--;$i = (int) floor($this->seconds / 60);$s = $this->seconds % 60;
Thx a lot you helped with let me know how to let variable as reference I think this will do help me a lot! I make a big progress, Thx a lot .
I'd even call it an antipattern in an untyped language without variable scopes. At least it would be more sane if they had behaviour like JavaScript's `let`.