1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

How to add a command cooldown?

Discussion in 'Development' started by rektpixel, Oct 19, 2017.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    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->1$player->z));
                
    $item $player->getInventory()->getItemInHand()->getId();
                
    $xd $player->getDirectionVector()->x;
                
    $zd $player->getDirectionVector()->z;
                
    $player->knockback($player0$xd$zd.900);
     
    xXRedWrathXx likes this.
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
    }
     
    Last edited: Oct 19, 2017
    rektpixel likes this.
  3. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    ok, thanks! :)
     
    Last edited: Oct 19, 2017
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.