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

Command not working

Discussion in 'Development' started by rektpixel, Sep 18, 2017.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    I made this small plugin which basically sends a bunch of titles after one another but I have used the sleep function thingie so the whole server sleeps :/ and I don't know how to use $tick and schedule delay task. Someone please help! thanks
    PHP:
    <?php
    namespace ipe\lipe;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;

    class 
    Main extends PluginBase implements Listener{
      
        public function 
    execute(CommandSender $sender$commandLabel, array $args) {
            if(
    $sender instanceof Player) {
                if(isset(
    $args[0])) {
                    switch(
    $args[0]) {
                        case 
    "serverinfo":
                        
    $sender->addTitle("*****""*****"455545);
                        
    sleep(3);
                        
    $sender->addTitle("*****""*****"455545);
                        
    sleep(3);
                        
    $sender->addTitle("§eManaged by:""*****"455545);
                        
    sleep(3);
                        
    $sender->addTitle("§eRead the Rules @""*****"455545);
                         
    sleep(3);
                        
    $sender->addTitle("§eSend a support ticket @""*****"455545);
                        
    sleep(3);
                        
    $sender->addTitle("§ePurcahse a Rank @""*****"455545);
                        
    sleep(3);
                        
    $sender->addTitle("§eVote for us @""*****"455545);
                        
    sleep(3);
                        
    $sender->addTitle("§eFor More info Visit:""*****"455545);
                    }
                }
            }
        }
     
    Last edited: Sep 18, 2017
    WinterBuild7074 likes this.
  2. AshBull

    AshBull Spider Jockey

    Messages:
    31
    That won't fix your problem.
    Learn what sleep() does, it pauses the whole main thread, so nothing will happen on your server for 21 seconds after that command is run.

    You need to use PluginTasks
     
  3. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    How do i do this?
     
  4. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    You can use this:
    PHP:
    /*
     * Main.php
     * @var CommandSender $sender
     * @var array $sender
     * @var int $secs
     */
    $secs 3// time to wait in between every title message
    $messages = array(
                        
    "Message 1",
                        
    "Message 2",
                        
    "Message 3",
                        
    "Message 4"
                        
    );
    SendTitles::$times 0// reset the counter
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new SendTitles($this$sender$messages), $secs 20);


    /*
     * SendTitles.php
     * @var static $times
     */
    public function onRun(int $tick) {
            if(
    self::$times >= count($this->messages)) { // do this until the end of the 'messages' array is reached
                
    Server::getInstance()->getScheduler()->cancelTask($this->getTaskId()); // cancel task
            
    } else {
                
    $this->sender->addTitle($this->messages[self::$times]);
                
    self::$times self::$times 1;
            }
        }
     
  5. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    sharletsings123 and jasonwynn10 like this.
  6. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    I know, I already did. I just got an idea from another thread and fixed one the reply.
     
  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.