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

using time() as cooldown

Discussion in 'Development' started by Levi, Jun 23, 2018.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how do i get the time left of cooldown

    command cooldown
    PHP:
    if ((!isset($this->cd[$player->getName()])) || (($this->cd[$player->getName()] + 15 time() <= 0))) {
                    
    $this->cd[$player->getName()] = time();
                } else {
                    
    $event->setCancelled(true);
                    
    $player->sendMessage("wait cooldowntime seconds");
                }
     
    xXNiceAssassinlo YT likes this.
  2. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
  4. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Are you trying to:
    1) send a live countdown or 2) just when the player does a specific thing that requires cool down?
     
  5. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    Don't forget to use parentheses to make sure your math is interpreted correctly by PHP.
     
  6. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    SO my plugin is a command cooldown for 5 second. So if player spam /test

    they will just get the message of how many seconds are left
     
  7. Eduardo

    Eduardo Baby Zombie

    Messages:
    100
    GitHub:
    xBeastMode
    Use this function, if player is on cool down it will return the time left, if player can use the command again it will return -1.

    PHP:
            /**
             * @var int[]
             */
            
    public $coolDown = [];
            
    /**
             * @param     $name
             * @param int $seconds
             *
             * @return int
             */
            
    public function checkCoolDown($name$seconds 5): int{
                    if(!isset(
    $this->coolDown[$name])){
                            
    $this->coolDown[$name] = time();
                            return -
    1;
                    }
                    if(((
    time() - $this->coolDown[$name]) <= $seconds)){
                            
    $this->coolDown[$name] = time();
                            return -
    1;
                    }
                    
    $timeLeft = ($seconds - (time() - $this->coolDown[$name]))
                    return 
    $timeLeft;
            }
     
  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.