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

Solved Task dont do what its made for

Discussion in 'Development' started by Khaled, Mar 18, 2018.

  1. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    i use this code in the task, but it excutes one time only, why? idk
    PHP:
    <?php

    namespace onevsoneda;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    onevsoneda\Main;
    use 
    pocketmine\utils\{
        
    TextFormat,
        
    Config
    };
    use 
    pocketmine\level\{PositionLevel};

    class 
    GameSender extends PluginTask{
     
        public 
    $plugin;
     
        public function 
    __construct(Main $plugin){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
        }
     
        public function 
    onRun(int $currentTick){
            
    $cf = new Config($this->plugin->getDataFolder()."/config.yml"Config::YAML);
            
    $arenas $cf->get("arenas");
            if(!empty(
    $arenas)){
                foreach(
    $arenas as $arena){
                    
    $time $cf->get($arena."PlayTime");
                    
    $timeToStart $cf->get($arena."StartTime");
                    
    $levelArena $this->plugin->getServer()->getLevelByName($arena);
                    if(
    $levelArena instanceof Level){
                        
    $allplayers $this->plugin->getServer()->getOnlinePlayers();
                        
    $playersArena = array();
                        foreach(
    $allplayers as $play){
                            if(
    $play->getLevel()->getFolderName() == $arena){
                                
    array_push($playersArena$play);
                            }
                        }
                        if(
    count($this->plugin->getServer()->getLevelByName($arena)->getPlayers()) == 0){
                            
    $cf->set($arena."PlayTime"400);
                            
    $cf->set($arena."StartTime"20);
                        } else {
                            if(
    count($this->plugin->getServer()->getLevelByName($arena)->getPlayers()) >= 2){
                                if(
    $timeToStart 0){
                                    
    $timeToStart--;
                                    foreach(
    $this->plugin->getServer()->getLevelByName($arena)->getPlayers() as $pl){
                                        
    $pl->sendTip(TextFormat::YELLOW."0 : ".$timeToStart);
                                    }
                                    if(
    $timeToStart <= 0){
                                        
    $cf = new Config($this->plugin->getDataFolder()."/config.yml"Config::YAML);
                                        
    $cf->set($arena."Move"true);
                                        
    $cf->set($arena."StartTime"$timeToStart);
                                        
    $cf->save();
                                    } else {
                                        
    $aop count($levelArena->getPlayers());
                                        if(
    $aop == 1){
                                            foreach(
    $this->plugin->getServer()->getLevelByName($arena)->getPlayers() as $pl){
                                                foreach(
    $this->plugin->getServer()->getOnlinePlayers() as $plpl){
                                                    
    $plpl->sendMessage($this->plugin->prefix.TextFormat::GREEN.$pl->getName()." Won in 1vs1-DA, arena: ".$pl->getLevel()->getFolderName());
                                                }
                                            }
                                            
    $cf->set($arena."PlayTime"400);
                                            
    $cf->set($arena."StartTime"20);
                                            
    $cf->save();
                                        }
                                        
    $time--;
                                        if(
    $time == 0){
                                            foreach(
    $this->plugin->getServer()->getLevelByName($name)->getPlayers() as $pla){
                                                
    $pla->teleport($this->plugin->getServer()->getDefaultLevel()->getSafeSpawn());
                                                
    $pla->sendMessage($this->plugin->prefix."§aYou won the game!");
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            
    $cf->save();
        }
    }
    ?>
    and i'm using scheduleRepeatingTask
    When the there is 2 players in the arena the sendTip() works but the seconds dosnt decrease, decreases only 1 time.
     
    Last edited: Mar 18, 2018
  2. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    what you mean by executing only one time? this should work..
    use that to register the task
    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new GameSender($this), 20);
    if you think it's executing only one time, maybe you can add a Popup into the task to test, but that's it's not supposed to happen there
     
  3. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    I know how to register tasks
     
  4. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Yeah, but have you tested the task correctly? I mean you don't explain what is executing only one time, this should work correctly, use sendPopup to test if the popup is receiving every time or only one time as you sayd, if so, the task is working, the problem is not the task but your code is.
     
  5. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    i tested it and its supposted to be:
    20
    19
    18
    etc..
    but when it goes the 19 it stops there.
     
  6. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    The problem is that you update variable from config every time you decrease it. You should load variable from config once and then decrement it.
    Look at this thread, i have the same issue one day..
    https://forums.pmmp.io/threads/how-...e-skywarsdodo-from-kaitododo.5449/#post-48857
    You have several ways of doing this:
    - register the $config->get variable on construct_
    or
    - you can register in onRun function, but you need to use $config->set and $config->save if you want that update everytime
     
    Last edited: Mar 18, 2018
  7. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Ok, i'll try
     
  8. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    I think you're getting a code example from an old plugin, or maybe an ImagicalMine plugin, because in the old days this should work good, but now you need to use $config->save to update everytime, i don't know why but it's update. #blameDylanKTaylor
     
  9. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Nope
    Nope its the skywars kaitododo plugin
     
  10. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    Yeah, SkyWars from KaitoDoDo is old and made for ImagicalMine plugins.
     
  11. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Sir, How do i add it to the constructor?
     
  12. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    So when you had this issue what did you do exactly
     
  13. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    PHP:
        $timeToStart--;
                             
    $cf->set($arena."StartTime"$timeToStart);
                              
    $cf->save();
     
  14. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Ohhhh ok i will try
     
  15. Khaled

    Khaled Slime

    Messages:
    81
    GitHub:
    xXKHaLeD098Xx
    Holy shit it worked thank you soooo much you are a life savior thank you :eek:
     
    Last edited: Mar 19, 2018
    OnTheVerge and LewBr like 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.