How can I create a AsyncTask? Do I need to create a new class or like a repeating task? I found nothing
This might help you: https://forums.pmmp.io/threads/creating-async-task-and-call-methods-from-api.1000/ And also, you may want to take a look at a (bad) example: https://github.com/robske110/SignServerStats/blob/master/SSS/src/robske_110/SSS/rootPlugin.php#L279 https://github.com/robske110/SignServerStats/blob/master/SSS/src/robske_110/SSS/rootPlugin.php#L72 why bad? because i didn't check if my plugin still exists onCompletion
I tried the method in your plugin. But I don't get it how I can display the infos if I run the command /info ip port
AsyncTask is not a repeating task. It's just a task that's ran parallel to your Minecraft server (main thread). It's used to call heavy (time-consuming) functions.
I tried this. But nothing happens PHP: public function getPlayers($ip, $port) {$this->getServer()->getScheduler()->scheduleAsyncTask(new SSSAsyncTask($this->doCheckServers, $this->debug, 20));return $this->asyncTaskPlayers;}//echo getPlayers() onCommand...//Taskclass SSSAsyncTask extends AsyncTask{ private $serverFINALdata; private $doCheckServer; private $startTick; public function __construct(array $doCheckServers, bool $debug, int $startTick){ $this->doCheckServer = $doCheckServers; $this->debug = $debug; $this->startTick = $startTick; } private function doQuery($ip, int $port): array{ if($this->debug){ echo("doQuery:\n"); } $sock = @fsockopen("udp://".$ip,$port); if(!$sock){return [-1, NULL];} socket_set_timeout($sock, 0, 500000); if(!@fwrite($sock, "\xFE\xFD\x09\x10\x20\x30\x40\xFF\xFF\xFF\x01")){return [0, NULL];} $challenge = fread($sock, 1400); if(!$challenge){return [0, NULL];} $challenge = substr(preg_replace("/[^0-9\-]/si", "", $challenge ), 1); $query = sprintf( "\xFE\xFD\x00\x10\x20\x30\x40%c%c%c%c\xFF\xFF\xFF\x01", ($challenge >> 24), ($challenge >> 16), ($challenge >> 8), ($challenge >> 0) ); if(!@fwrite($sock, $query)){return [0, NULL];} $response = array(); for($x = 0; $x < 2; $x++){ $response[] = @fread($sock,2048); } if($this->debug){ var_dump($response); } $response = implode($response); $response = substr($response,16); $response = explode("\0",$response); if($this->debug){ var_dump($response); } array_pop($response); array_pop($response); array_pop($response); array_pop($response); $return = []; $type = 0; if($this->debug){ var_dump($response); } foreach ($response as $key){ if ($type == 0) $val = $key; if ($type == 1) $return[$val] = $key; $type == 0 ? $type = 1 : $type = 0; } return [1, $return]; if($this->debug){ echo("DoQueryEnd\n"); } } public function onRun(){ if($this->debug){ echo("DoCheckServer:\n"); var_dump($this->doCheckServer); } foreach($this->doCheckServer as $server){ $doCheck = $server[1]; if($doCheck){ $ip = $server[0]; $deParsedIP = $ip[0]; $port = $ip[1]; $return = $this->doQuery($deParsedIP, $port); $returnState = $return[0]; $queryResult = $return[1]; $serverData = []; if($this->debug){ echo("returnState:\n"); var_dump($returnState); } switch($returnState){ case -1; $serverData[2] = false; break; case 0: $serverData[2] = false; break; case 1: $serverData[0] = [$queryResult['numplayers'], $queryResult['maxplayers']]; $serverData[1] = $queryResult['hostname']; $serverData[2] = true; } $serverFINALdata[$deParsedIP.$port] = $serverData; $this->setResult($serverFINALdata); } } if($this->debug){ echo("\n"); } } }}
If I run this code every second, I get *this as result PHP: class SSSAsyncTask extends AsyncTask{ private $serverFINALdata; private $doCheckServer; private $startTick; public function __construct(array $doCheckServers, bool $debug, int $startTick){ $this->doCheckServer = $doCheckServers; $this->debug = $debug; $this->startTick = $startTick; } private function doQuery($ip = "sg.lbsg.net", $port = 19132): array{ if($this->debug){ echo("doQuery:\n"); } $sock = @fsockopen("udp://".$ip,$port); if(!$sock){return [-1, NULL];} socket_set_timeout($sock, 0, 500000); if(!@fwrite($sock, "\xFE\xFD\x09\x10\x20\x30\x40\xFF\xFF\xFF\x01")){return [0, NULL];} $challenge = fread($sock, 1400); if(!$challenge){return [0, NULL];} $challenge = substr(preg_replace("/[^0-9\-]/si", "", $challenge ), 1); $query = sprintf( "\xFE\xFD\x00\x10\x20\x30\x40%c%c%c%c\xFF\xFF\xFF\x01", ($challenge >> 24), ($challenge >> 16), ($challenge >> 8), ($challenge >> 0) ); if(!@fwrite($sock, $query)){return [0, NULL];} $response = array(); for($x = 0; $x < 2; $x++){ $response[] = @fread($sock,2048); } if($this->debug){ var_dump($response); } $response = implode($response); $response = substr($response,16); $response = explode("\0",$response); if($this->debug){ var_dump($response); } array_pop($response); array_pop($response); array_pop($response); array_pop($response); $return = []; $type = 0; if($this->debug){ var_dump($response); } foreach ($response as $key){ if ($type == 0) $val = $key; if ($type == 1) $return[$val] = $key; $type == 0 ? $type = 1 : $type = 0; } return [1, $return]; if($this->debug){ echo("DoQueryEnd\n"); } } public function onRun(){ if($this->debug){ echo("DoCheckServer:\n"); var_dump($this->doCheckServer); } foreach($this->doCheckServer as $server){ $doCheck = $server[1]; if($doCheck){ $ip = $server[0]; $deParsedIP = $ip[0]; $port = $ip[1]; $return = $this->doQuery($deParsedIP, $port); $returnState = $return[0]; $queryResult = $return[1]; $serverData = []; if($this->debug){ echo("returnState:\n"); var_dump($returnState); } switch($returnState){ case -1; $serverData[2] = false; break; case 0: $serverData[2] = false; break; case 1: $serverData[0] = [$queryResult['numplayers'], $queryResult['maxplayers']]; $serverData[1] = $queryResult['hostname']; $serverData[2] = true; } $serverFINALdata[$deParsedIP.$port] = $serverData; $this->setResult($serverFINALdata); } } if($this->debug){ echo("\n"); } } public function onCompletion(Server $server){ $server->getPluginManager()->getPlugin("myPlugin")->asyncTaskCallBack($this->getResult(), $this->startTick); }} *This Result Code: DoCheckServer: object(Volatile)#4314 (0) { }
Add a debug line at the point exactly before you set the result to see what you are setting as result.
Also you might want to not simply copy the async task class out of my plugin. It won't do much if you don't pass correct data to it.
I tried something around and now I have these: PHP: class API extends AsyncTask{public function getPlayers($ip, $port) {//MY CODE} This crashes my server on load with this error. rror: Class myPlugin\API contains 1 abstract method and must therefore be de clared abstract or implement the remaining methods (pocketmine\scheduler\AsyncTask:: onRun) in
Looks like you haven't had a glimpse of the class you were extending the class with? https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/scheduler/AsyncTask.php
PHP: use pocketmine\scheduler\AsyncTask;class API{public function onRun() {}public function getPlayers($ip, $port) {}} This gives me no error. Do I need to add something?