PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
LevelImport.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\level;
23 
28 
30  private $path;
31 
32  public function __construct($path){
33  $this->path = $path;
34  }
35 
36  public function import(){
37  if(file_exists($this->path . "tileEntities.dat")){ //OldPM
38  $level = unserialize(file_get_contents($this->path . "level.dat"));
39  MainLogger::getLogger()->info("Importing OldPM level \"" . $level["LevelName"] . "\" to PMF format");
40  $entities = new Config($this->path . "entities.yml", Config::YAML, unserialize(file_get_contents($this->path . "entities.dat")));
41  $entities->save();
42  $tiles = new Config($this->path . "tiles.yml", Config::YAML, unserialize(file_get_contents($this->path . "tileEntities.dat")));
43  $tiles->save();
44  }elseif(file_exists($this->path . "chunks.dat") and file_exists($this->path . "level.dat")){ //Pocket
45  $nbt = new NBT(NBT::LITTLE_ENDIAN);
46  $nbt->read(substr(file_get_contents($this->path . "level.dat"), 8));
47  $level = $nbt->getData();
48  if($level["LevelName"] == ""){
49  $level["LevelName"] = "world" . time();
50  }
51  MainLogger::getLogger()->info("Importing Pocket level \"" . $level->LevelName . "\" to PMF format");
52  unset($level->Player);
53  $nbt->read(substr(file_get_contents($this->path . "entities.dat"), 12));
54  $entities = $nbt->getData();
55  if(!isset($entities->TileEntities)){
56  $entities->TileEntities = [];
57  }
58  $tiles = $entities->TileEntities;
59  $entities = $entities->Entities;
60  $entities = new Config($this->path . "entities.yml", Config::YAML, $entities);
61  $entities->save();
62  $tiles = new Config($this->path . "tiles.yml", Config::YAML, $tiles);
63  $tiles->save();
64  }else{
65  return false;
66  }
67 
68  $pmf = new LevelFormat($this->path . "level.pmf", [
69  "name" => $level->LevelName,
70  "seed" => $level->RandomSeed,
71  "time" => $level->Time,
72  "spawnX" => $level->SpawnX,
73  "spawnY" => $level->SpawnY,
74  "spawnZ" => $level->SpawnZ,
75  "height" => 8,
76  "generator" => "default",
77  "generatorSettings" => "",
78  "extra" => ""
79  ]);
80  $chunks = new PocketChunkParser();
81  $chunks->loadFile($this->path . "chunks.dat");
82  $chunks->loadMap();
83  for($Z = 0; $Z < 16; ++$Z){
84  for($X = 0; $X < 16; ++$X){
85  $chunk = [
86  0 => "",
87  1 => "",
88  2 => "",
89  3 => "",
90  4 => "",
91  5 => "",
92  6 => "",
93  7 => ""
94  ];
95 
96  $pmf->initCleanChunk($X, $Z);
97  for($z = 0; $z < 16; ++$z){
98  for($x = 0; $x < 16; ++$x){
99  $block = $chunks->getChunkColumn($X, $Z, $x, $z, 0);
100  $meta = $chunks->getChunkColumn($X, $Z, $x, $z, 1);
101  for($Y = 0; $Y < 8; ++$Y){
102  $chunk[$Y] .= substr($block, $Y << 4, 16);
103  $chunk[$Y] .= substr($meta, $Y << 3, 8);
104  $chunk[$Y] .= "\x00\x00\x00\x00\x00\x00\x00\x00";
105  }
106  }
107  }
108  foreach($chunk as $Y => $data){
109  $pmf->setMiniChunk($X, $Z, $Y, $data);
110  }
111  $pmf->setPopulated($X, $Z);
112  $pmf->saveChunk($X, $Z);
113  }
114  MainLogger::getLogger()->notice("Importing level " . ceil(($Z + 1) / 0.16) . "%");
115  }
116  $chunks->map = null;
117  $chunks = null;
118  @unlink($this->path . "level.dat");
119  @unlink($this->path . "level.dat_old");
120  @unlink($this->path . "player.dat");
121  @unlink($this->path . "entities.dat");
122  @unlink($this->path . "chunks.dat");
123  @unlink($this->path . "chunks.dat.gz");
124  @unlink($this->path . "tiles.dat");
125  unset($chunks, $level, $entities, $tiles, $nbt);
126 
127  return true;
128  }
129 
130 }