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

After command

Discussion in 'Requests' started by Groin, Jul 18, 2017.

  1. Groin

    Groin Baby Zombie

    Messages:
    159
    Command: /after <seconds> <command>
    It mean that the command will run after <seconds> ;)
    Thanks if you helped me!
     
    jasonwynn10, Marabou and SOFe like this.
  2. kiencool

    kiencool Spider Jockey

    Messages:
    36
    You can use plugin GrabBag
     
  3. Groin

    Groin Baby Zombie

    Messages:
    159
    Sorry! I cannot use the plugin because it will disable the protect from my server :(
     
  4. kiencool

    kiencool Spider Jockey

    Messages:
    36
  5. Groin

    Groin Baby Zombie

    Messages:
    159
    Come on!
    I arealy have code from GrabBag plugin. Please help me to make new plugin like it :(
    PHP:
    <?php

    namespace aliuly\grabbag;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    aliuly\grabbag\common\BasicCli;
    use 
    aliuly\grabbag\common\mc;
    use 
    aliuly\grabbag\common\PluginCallbackTask;
    class 
    CmdAfterAt extends BasicCli implements CommandExecutor {
        protected 
    $tasks;
        public function 
    __construct($owner) {
            
    parent::__construct($owner);
            
    $this->tasks = [];
            
    $this->enableCmd("after",
                                  [
    "description" => mc::_("schedule to run a command after x seconds"),
                                    
    "usage" => mc::_("/after <seconds> <command>|list|cancel <id>"),
                                    
    "permission" => "gb.cmd.after"]);
            
    $this->enableCmd("at",
                                  [
    "description" => mc::_("schedule to run a command at a certain time"),
                                    
    "usage" => mc::_("/at <time> <command>|list|cancel <id>"),
                                    
    "permission" => "gb.cmd.after"]);
        }
        public function 
    onCommand(CommandSender $sender,Command $cmd,$label, array $args) {
            
    // Collect expired tasks out of the tasks table...
            
    foreach (array_keys($this->tasks) as $tid) {
                if (!
    $this->owner->getServer()->getScheduler()->isQueued($tid)) {
                    unset(
    $this->tasks[$tid]);
                }
            }
            switch(
    $cmd->getName()) {
                case 
    "after":
                  if (
    $this->commonSubs($sender,$args)) return true;
                    return 
    $this->cmdAfter($sender,$args);
                case 
    "at":
                    if (
    $this->commonSubs($sender,$args)) return true;
                    return 
    $this->cmdAt($sender,$args);
            }
            return 
    false;
        }
        public function 
    runCommand($cmd) {
            
    $this->owner->getServer()->dispatchCommand(new ConsoleCommandSender(),$cmd);
        }
        private function 
    commonSubs(CommandSender $c,$args){
            if (
    count($args) == 0) return false;
            switch (
    strtolower($args[0])){
                case 
    "list":
                case 
    "ls":
                    if (
    count($this->tasks) == 0) {
                        
    $c->sendMessage(mc::_("No tasks currently scheduled"));
                        return 
    true;
                    }
                    
    $pageNumber $this->getPageNumber($args);
                    
    $tab = [ [    mc::_("Id"), mc::_("When"),
                                            
    mc::n(mc::_("One scheduled task"),
                                                 
    mc::_("%1% scheduled tasks",count($this->tasks)),
                                                
    count($this->tasks)) ] ];
                    foreach (
    $this->tasks as $tid => $cmd) {
                        list(
    $when,$line) = $cmd;
                        
    $tab[] = [ $tiddate(mc::_("d-M-Y H:i:s"),$when), $line ];
                    }
                    return 
    $this->paginateTable($c,$pageNumber,$tab);
                case 
    "cancel":
                    if (
    count($args) != 2) return false;
                    if (!isset(
    $this->tasks[$args[1]])){
                        
    $c->sendMessage(mc::_("Task %1% not found!",$args[1]));
                        return 
    true;
                    }
                    
    $this->owner->getServer()->getScheduler()->cancelTask($args[1]);
                    
    $c->sendMessage(mc::_("Cancelling Task %1%",$args[1]));
                    return 
    true;
            }
            return 
    false;
        }
        private function 
    cmdAfter(CommandSender $c,$args) {
            if (
    count($args) < 2) return false;
            if (!
    is_numeric($args[0])) {
                
    $c->sendMessage(mc::_("Unable to specify delay %1%",$args[0]));
                return 
    false;
            }
            
    $secs array_shift($args);
            
    $c->sendMessage(mc::_("Scheduled for %1%",date(DATE_RFC2822,time()+$secs)));
            
    $h $this->owner->getServer()->getScheduler()->scheduleDelayedTask(
                new 
    PluginCallbackTask($this->owner,[$this,"runCommand"],[implode(" ",$args)]),
                
    $secs 20
            
    );
            
    $this->tasks[$h->getTaskId()] = [time()+$secs,implode(" ",$args)];
            return 
    true;
        }
        private function 
    cmdAt(CommandSender $c,$args) {
            if (
    count($args) < 2) {
                
    $c->sendMessage(mc::_("Time now is: %1%",date(DATE_RFC2822)));
                return 
    false;
            }
            if ((
    $pos array_search(":",$args)) != false) {
                if (
    $pos == 0) return false;
                
    $ts = [];
                while (
    $pos--) {
                    
    $ts[] = array_shift($args);
                }
                
    array_shift($args);
                if (
    count($args) == 0) return false;
                
    $ts implode(" ",$ts);
                
    $when strtotime($ts);
            } else {
                for (
    $ts array_shift($args);
                      (
    $when strtotime($ts)) == false && count($args) > 1;
                      
    $ts .= ' '.array_shift($args)) ;
            }
            if (
    $when == false) {
                
    $c->sendMessage(mc::_("Unable to parse time specification %1%",$ts));
                return 
    false;
            }
            while (
    $when time()) {
                
    $when += 86400// We can not travel back in time...
            
    }
            
    $c->sendMessage(mc::_("Scheduled for %1%",date(DATE_RFC2822,$when)));
            
    $h $this->owner->getServer()->getScheduler()->scheduleDelayedTask(
                new 
    PluginCallbackTask($this->owner,[$this,"runCommand"],[implode(" ",$args)]),
                (
    $when time())*20
            
    );
            
    $this->tasks[$h->getTaskId()] = [$whenimplode(" ",$args)];
            return 
    true;
        }
    }
     
  6. kiencool

    kiencool Spider Jockey

    Messages:
    36
    I'm noob in the code
     
  7. hoyinm14mc

    hoyinm14mc Silverfish

    Messages:
    22
    I've just coded one for you but it isn't tested. Tell me if you experience any bugs.
    https://www.dropbox.com/sh/zkdcxhp0i3321ip/AACDn30Ik-p4BI8GHCgaRhD2a?dl=0
     
    Last edited: Jul 22, 2017
    NickTehUnicorn and Marabou like this.
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Having access to someone else's code is no less than having access to nothing. You need to know what the code is doing if you're copy pasting someone's code.
     
    Thunder33345 likes this.
  9. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    I made the requested plugin
     

    Attached Files:

    Muqsit likes this.
  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.