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

AsyncTask not working

Discussion in 'Development' started by HittmanA, Jul 11, 2017.

  1. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    I am trying to use an AsyncTask to make an HTTP call; however, it does absolutely nothing. Code:
    PHP:
    <?php

    namespace HittmanA\PMPluginManager;

    use 
    pocketmine\event;
    use 
    pocketmine\Server;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\utils\Utils;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\scheduler\AsyncTask;

    class 
    Plugin extends AsyncTask
    {
        public function 
    __construct($name$sender)
        {
            
    $this->name $name;
            
    $this->sender $sender;
        }
        public function 
    onRun()
        {
            print(
    "test");
            
    $url "https://poggit.pmmp.io/releases.json?name=".$this->name;
            
    $register Utils::getURL($url);
            
    $JSONParsedRegister json_decode($register);
            if (
    count($JSONParsedRegister) != 0)
            {
                
    //$sender->sendMessage("'".$name."' was found on Poggit. Downloading now...");
                
    $latestVersion $JSONParsedRegister[0];
                
    $pluginVersion $latestVersion->version;
                
    $this->version $pluginVersion;
                
    //$sender->sendMessage("Found the requested version. Downloading version ".$this->version."...");
                
    $poggitURL $latestVersion->artifact_url;
                
    $this->url $poggitURL;
                
    $pluginName $latestVersion->project_name;
                
    $this->name $pluginName;
                
    $this->download_url $poggitURL."/".$pluginName.".phar";
                
    $this->ready_to_download true;
            } else {
                
    //$sender->sendMessage("Sorry but '".$name."' isn't listed on Poggit!");
                
    $this->ready_to_download false;
            }
            
    $code Utils::getURL($this->download_url);
            
    $this->setResult($code);
            
    //$this->sender->sendMessage($pluginToDownload." has been installed successfully!");
            //$this->sender->sendMessage("Reloading server...");
        
    }
        public function 
    onCompletion(Server $server)
        {
            
            
    $pluginCode $this->getResult();
            
    $fp fopen ($this->getServer()->getDataPath()."/plugins/".$this->name.".phar"'w+');
            
    fwrite($fp$pluginCode);
            
    fclose($fp);
            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), 'reload');
        }
    }
    Nothing every happens when I make a new instance of this class. No file is created and it doesn't even print out "test". Here is the code I use to instantiate the class:
    PHP:
    $plugin = new Plugin($pluginToDownload$sender);
    Why does the AsyncTask do nothing?
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You aren't scheduling the task, are you?
     
    SOFe likes this.
  3. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    You need to actually schedule the task. Constructing it won't do anything by itself.

    Assuming you have a Server reference by name `$server`:
    Code:
    $server->getScheduler()->scheduleAsyncTask(new WhateverAsyncTaskYouWantToSchedule());
    
     
  4. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    Oh ok. I assume that goes in the MainClass and not the actual class I want to run as a task, right?
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You can call it in your AsyncTask class's constructor if that helps.
     
  6. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    That would be best. Thanks @dktapps and @Muqsit!
     
    jasonwynn10 and Muqsit like this.
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    This is, if not bad practice, somewhat dangerous. You have to schedule the task at the end of the constructor; otherwise, the task may be started before its constructor finishes executing. This flowchart may help you understand:

    Therefore, you must keep in mind that the scheduleAsyncTask() method must be called after all property assignments.
    A trap is that OOP conventionally [citation needed] calls the parent constructor at the beginning of the subclass constructor. This may lead to scheduling the task before assigning subclass properties. Therefore, subclasses must also keep in mind that they must only call parent constructor at the end of the subclass constructor, which is kind of unnatural especially for people used to Java programming.
    I had a hard lesson figuring this bug out. I hope you won't fall into the same issue :)
     
    Awzaw, Miste, dktapps and 2 others like this.
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yeah, I don't recommend calling it in the constructor either.
     
  9. HittmanA

    HittmanA Zombie

    Messages:
    207
    GitHub:
    hittmana
    Thanks for the advice @SOFe ! I figured that something like this would probably be the case so I did indeed schedule the task at the end of the constructor.
     
  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.