CooldDown on command

Discussion in 'General discussion' started by MusicNOvas, Apr 12, 2019.

  1. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    PHP:
        public function onCommand(CommandSender $senderCommand $cmdstring $label, array $args) : bool{
           
            switch(
    $cmd->getName()){
               
                case 
    "joueur":
                    if(
    $sender instanceof Player){
                        
    $sender->sendMessage("§5Feed§f >> TMax food.")
                        
    $sender->getPlayer()->setFood(20);
                        
    $sender->cooldown(50sec);
                        return 
    true;
                    }
            }
    How i can add cooldown in the command please ?
     
  2. HimbeersaftLP

    HimbeersaftLP Wither Skeleton

    Messages:
    1,487
    GitHub:
    HimbeersaftLP
    You can use time() to get the current time, save it to an array in combination with the player's name or UUID and then compare the current time with the saved time if the command is ran again.
     
  3. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    Sorry i'm not understand. I'm not a pro in development.
     
  4. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    124
    Use task instead
     
  5. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    How can i use Task ?
     
  6. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    124
    PHP:
    //Add this after classs....
        
    public $tasks = [];
        public 
    $task;

        public function 
    onCommand(CommandSender $senderCommand $cmdstring $label, array $args) : bool{
           
            switch(
    $cmd->getName()){
               
                case 
    "joueur":
                    if(
    $sender instanceof Player){
                        if(
    in_array($name$this->tasks)){//Check if player in array
                           
    $sender->sendMessage("You cant use this. Wait");
                           return 
    true;
                        }

                        
    $sender->sendMessage("§5Feed§f >> TMax food.")
                        
    $sender->setFood(20);
                        
    $name $sender->getName();
                        
    $task = new CountdownTask($this$sender);
                        
    $this->getScheduler()->scheduleRepeatingTask($task20);
                        
    $this->tasks[$sender->getId()] = $task;
                        
    $this->tasks[] = $name;

                        return 
    true;
                    }
            }
    Task file:
    PHP:

    <?php

    namespace namespacehere;

    use 
    filemainhere;
    use 
    pocketmine\scheduler\Task;
    use 
    pocketmine\Player;

    class 
    CountdownTask extends Task{
       
        public 
    $seconds 20;
       
        public function 
    __construct(Main $pluginPlayer $player){
            
    $this->player $player;
            
    $this->plugin $plugin;
        }
       
        public function 
    onRun($tick) : void{
            
    $this->player->sendPopup("You can use /joueur after $this->seconds");
                if(
    $this->seconds === 0){
                    
    $name $this->player->getName();
                    if(
    in_array($name$this->plugin->tasks)){
                        unset(
    $this->plugin->tasks[array_search($name$this->plugin->tasks)]);
                        
    $this->plugin->tasks[$this->player->getId()]->getHandler()->cancel();
                        
    $this->player->sendPopup("You can use /joueur again!");   
                    }
                }
            
    $this->seconds--;
        }
    }
     
  7. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    Thank you I would use this tonight..
     
  8. HimbeersaftLP

    HimbeersaftLP Wither Skeleton

    Messages:
    1,487
    GitHub:
    HimbeersaftLP
    That just adds bloat.
     
  9. Diduhless

    Diduhless Slime

    Messages:
    86
    GitHub:
    Diduhless
    HimbeersaftLP likes this.
  10. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    How can i add the file task please ?
     
  11. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    124
    create a .php file and paste the task code inside that
     
  12. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    124
    or make like 2 other people said
     
  13. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    NO, the code to add the task
     
  14. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    124
    i have already done that in your command code. Just copy, paste and understand it.
     
  15. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    Okay thanks.
     
  16. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    Code:
    [Server thread/CRITICAL]: ParseError: "syntax error, unexpected '$sender' (T_VARIABLE)" (EXCEPTION) in "plugins/YuniePL/src/lx/yuniepl/Main" at line 39(B
    My server has crash.
     
  17. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    Noo! Sorry it's perfect! Thank you !
     
  18. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    When the player leave, the cooldown has register in folder like this the player has always the cooldown.
    When the player leave, the cooldown has register in folder like this the player has always the cooldown when he reconnect
     
  19. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    124
    i think it unnecessary
     
  20. MusicNOvas

    MusicNOvas Silverfish

    Messages:
    22
    Yes, because I want cooldown everyday, bug when the player disconnect, and reconnect, he can do this command.
     

Share This Page

  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.