Gistrec

Async Enumeration All Chunks and Regions

Aug 2nd, 2017
261
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace test;
  4.  
  5. use pocketmine\Server;
  6. use pocketmine\scheduler\AsyncTask;
  7.  
  8. /*
  9.  * Пример перебора блоков в AsyncTask'е
  10.  * Как использовать:
  11.  * $task = new AsyncLoadChunk ("$dataPath/worlds/RPG");
  12.  * $serverScheduler->scheduleAsyncTask($task);
  13.  */
  14.  
  15. class AsyncLoadChunk extends AsyncTask {
  16.  
  17.     public $levelPath;
  18.  
  19.     /**
  20.      * @param string  $levelPath  Путь к миру, пример /root/worlds/MyWorld
  21.      */
  22.     public function __construct($levelPath) {
  23.         $this->levelPath = $levelPath;
  24.         print 'Перебираем все чанки в мире: ' + $levelPath;
  25.     }
  26.  
  27.  
  28.     public function countBlock($chunk) {
  29.         for ($x = 0; $x < 16; $x++) {
  30.             for ($z = 0; $z < 16; $z++) {
  31.                 for ($y = 0; $y < 256; $y++) {
  32.                     # code...
  33.                }
  34.             }
  35.         }
  36.     }
  37.  
  38.  
  39.     public function onRun(){
  40.         foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->levelPath)) as $file){
  41.             if ($file->getFilename()[0] != 'r') continue;
  42.             $data = explode(".", $file->getFilename());
  43.             $regionX = $data[1];
  44.             $regionY = $data[2];
  45.             $region = new RegionLoader($this->levelPath, $regionX, $regionY);
  46.             for ($x = 0; $x < 32; $x++) {
  47.                 for ($y = 0; $y < 32; $y++) {
  48.                     if ($region->chunkExists($x, $y)) {
  49.                         $chunk = $region->readChunk($x, $y);
  50.                         $this->countBlock($chunk);
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.  
  58.     public function onCompletion(Server $server){
  59.         # code...
  60.    }
  61. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment