hey, how do I make a 500 second cooldown after I execute the command /test? My attempt: PHP: public function onCommand(CommandSender $sender, Command $command, string $label, array $args):bool{ if($command->getName() == "test"){ $player = $sender $sender->getInventory()->addItem(Item::get(2, 0, 4)); $this->getServer()->getScheduler()->scheduleDelayedTask(new Task($this, $player), 500 * 20); // what do i do i do in Task? } }
If you want a countdown every second, you have to create a repeat task. PHP: class CountDownTask extends PluginTask { /** @var int $countDown */ public $countDown = 500; /** * CountDownTask constructor. * @param \pocketmine\plugin\Plugin $plugin */ public function __construct($plugin) { parent::__construct($plugin); } /** * @param int $currentTick */ public function onRun(int $currentTick) { if($this->countDown <= 0) { $this->end(); } if($this->countDown > 0) { $this->tick(); } $this->countDown = $this->countDown-1; } public function tick() { // your code } public function end() { $this->onCancel(); }}
it's counter intuitive to use a task, UNLESS you want it to be displayed in realtime it's better to use time() asy an array endCooldown containing player as key and when as time you would set $array[playername] = time() + "how long the cooldown is" and to check do if (!isset($array[playername]) OR time() > $array[playername]){ //cooldown over }else{ //not over }
PHP: public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool { if($command->getName() === "test") { $sender->getInventory()->addItem(Item::get(2, 0, 4)); $this->getServer()->getScheduler()->scheduleDelayedRepeatingTask(new class extends PluginTask($this, $sender->getName()) { private $player, $tick = 0; public function __construct(Plugin $plugin, string $player) { $this->player = $player; parent::__construct($plugin); } public function onRun(int $currentTick) { if($this->tick === 0) { $this->tick = $currentTick; return; } $player = $this->getOwner()->getServer()->getPlayer($this->player); $counter = $currentTick - $this->tick; if($player !== null) { $player->sendMessage("Countdown: ".$counter); }else{ $this->getHandler()->remove(); // player might have quit } if($counter === 0) { $this->getHandler()->remove(); } } }, 500 * 20, 20); } }
am i doing it right? PHP: public $cooldown = [];if($cmd->getName() === "triggered"){$this->cooldown[$sender->getName() = time() + "300";if(!isset(this->cooldown[$sender->getName())){$sender->sendMessage("Cooldown over");}else{$sender->sendMessage("That command is on a cooldown."); }
PHP: time() + "300" Its always better to use PHP: time() + 300 its the same in php, but not in other languages About your code, time() returns how many seconds have past since 1 january 1970, Now, you want to disable the command to a player for 300 seconds, so time() + 300 will return how many seconds since 1970 plus your 300 seconds (future 300 seconds). so to check if 300 seconds have past from your player array with the time() + 300 you can use PHP: if($timeArray[$player->getName()] <= time()){//code}
partly incorrect the check cooldown over part is not correct it's encouraged to check weather the cooldown array key exists before trying to evaluate it