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

Command cooldown

Discussion in 'Development' started by Levi, Oct 11, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    hey, how do I make a 500 second cooldown after I execute the command /test?

    My attempt:

    PHP:
    public function onCommand(CommandSender $senderCommand $commandstring $label, array $args):bool{
            if(
    $command->getName() == "test"){
                
    $player $sender
                $sender
    ->getInventory()->addItem(Item::get(204));
                
    $this->getServer()->getScheduler()->scheduleDelayedTask(new Task($this$player), 500 20);
                
    // what do i do i do in Task?
            
    }
        }
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    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();
        }
    }
     
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    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
    }
     
  4. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    too complex for me to understand :sadface:
     
    HyperxXxHound likes this.
  5. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    public function onCommand(CommandSender $senderCommand $commandstring $label, array $args) : bool {
            if(
    $command->getName() === "test") {
                
    $sender->getInventory()->addItem(Item::get(204));
                
    $this->getServer()->getScheduler()->scheduleDelayedRepeatingTask(new class extends PluginTask($this$sender->getName()) {
                    private 
    $player$tick 0;
                    public function 
    __construct(Plugin $pluginstring $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 2020);
            }
        }
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    honestly that seem way simpler then using bunch of inefficient tasks
     
    jasonwynn10 likes this.
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    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.");
    }
     
  8. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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
    }
     
    Last edited: Oct 12, 2017
    HyperxXxHound likes this.
  9. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    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
     
  10. Aviv

    Aviv Baby Zombie

    Messages:
    156
    just an example :p
     
  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.