PHP: public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args) : bool{ switch($cmd->getName()){ case "joueur": if($sender instanceof Player){ $sender->sendMessage("§5Feed§f >> TMax food.") $sender->getPlayer()->setFood(20); $sender->cooldown(50sec); return true; } } How i can add cooldown in the command please ?
You can use time() to get the current time, save it to an array in combination with the player's name or UUID and then compare the current time with the saved time if the command is ran again.
PHP: //Add this after classs.... public $tasks = []; public $task; public function onCommand(CommandSender $sender, Command $cmd, string $label, array $args) : bool{ switch($cmd->getName()){ case "joueur": if($sender instanceof Player){ if(in_array($name, $this->tasks)){//Check if player in array $sender->sendMessage("You cant use this. Wait"); return true; } $sender->sendMessage("§5Feed§f >> TMax food.") $sender->setFood(20); $name = $sender->getName(); $task = new CountdownTask($this, $sender); $this->getScheduler()->scheduleRepeatingTask($task, 20); $this->tasks[$sender->getId()] = $task; $this->tasks[] = $name; return true; } } Task file: PHP: <?phpnamespace namespacehere;use filemainhere;use pocketmine\scheduler\Task;use pocketmine\Player;class CountdownTask extends Task{ public $seconds = 20; public function __construct(Main $plugin, Player $player){ $this->player = $player; $this->plugin = $plugin; } public function onRun($tick) : void{ $this->player->sendPopup("You can use /joueur after $this->seconds"); if($this->seconds === 0){ $name = $this->player->getName(); if(in_array($name, $this->plugin->tasks)){ unset($this->plugin->tasks[array_search($name, $this->plugin->tasks)]); $this->plugin->tasks[$this->player->getId()]->getHandler()->cancel(); $this->player->sendPopup("You can use /joueur again!"); } } $this->seconds--; }}
Why using a task when you can use time() instead? And it's much simplier. https://www.php.net/manual/en/function.time.php
Code: [Server thread/CRITICAL]: ParseError: "syntax error, unexpected '$sender' (T_VARIABLE)" (EXCEPTION) in "plugins/YuniePL/src/lx/yuniepl/Main" at line 39(B My server has crash.
When the player leave, the cooldown has register in folder like this the player has always the cooldown. When the player leave, the cooldown has register in folder like this the player has always the cooldown when he reconnect
Yes, because I want cooldown everyday, bug when the player disconnect, and reconnect, he can do this command.