I am trying to use an AsyncTask to make an HTTP call; however, it does absolutely nothing. Code: PHP: <?phpnamespace 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?
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());
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
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.