Hello, I was wondering how to set a cooldown of 5 seconds to something like this: PHP: if($hand->getCustomName() === "§aLeap §r§7§o(Click)") { $block = $player->getLevel()->getBlock(new Vector3($player->x, $player->y - 1, $player->z)); $item = $player->getInventory()->getItemInHand()->getId(); $xd = $player->getDirectionVector()->x; $zd = $player->getDirectionVector()->z; $player->knockback($player, 0, $xd, $zd, .900);
This topic has been discussed many times, you should use the search bar before creating a thread. What you can do is create a class property, lets name it "cooldowns". PHP: private $cooldowns = []; then get and set values. Every time the player interacts with the item, handle the class property according to what you want to do. Use something unique to identify the player (eg: player's UUID) as the key for the property. PHP: const COOLDOWN_DURATION = 20 * 5;//20 ticks = 1 second.$uuid = $player->getUniqueId()->toString();if(isset($this->cooldowns[$uuid])){ $duration = $this->cooldowns[$uuid] - $player->ticksLived;//duration (in ticks) for how until the cooldown expires. if($duration < 0){//cooldown expired. $this->cooldowns[$uuid] = $player->ticksLived + COOLDOWN_DURATION;//renewing expired cooldown. }else{//cooldown hasn't expired return;//cooldown ends in ($duration / 20) seconds. }}else{ $this->cooldowns[$uuid] = $player->ticksLived + COOLDOWN_DURATION;//setting cooldown for the first time.}