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

CooldDown on command

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

  1. MusicNOvas

    MusicNOvas Spider Jockey

    Messages:
    43
    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 Fish

    Messages:
    2,402
    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 Spider Jockey

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

    ZackyVN Baby Zombie

    Messages:
    150
    Use task instead
     
  5. MusicNOvas

    MusicNOvas Spider Jockey

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

    ZackyVN Baby Zombie

    Messages:
    150
    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 Spider Jockey

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

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    That just adds bloat.
     
  9. Diduhless

    Diduhless Baby Zombie

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

    MusicNOvas Spider Jockey

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

    ZackyVN Baby Zombie

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

    ZackyVN Baby Zombie

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

    MusicNOvas Spider Jockey

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

    ZackyVN Baby Zombie

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

    MusicNOvas Spider Jockey

    Messages:
    43
    Okay thanks.
     
  16. MusicNOvas

    MusicNOvas Spider Jockey

    Messages:
    43
    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 Spider Jockey

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

    MusicNOvas Spider Jockey

    Messages:
    43
    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:
    150
    i think it unnecessary
     
  20. MusicNOvas

    MusicNOvas Spider Jockey

    Messages:
    43
    Yes, because I want cooldown everyday, bug when the player disconnect, and reconnect, he can do this command.
     
  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.