PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pocketmine/level/format/mcregion/McRegion.php

https://gitlab.com/wesleyvanneck/ImagicalMine
PHP | 367 lines | 101 code | 26 blank | 240 comment | 9 complexity | ae3102f0a380b1cc4deb43baf561e6d2 MD5 | raw file
  1. <?php
  2. /*
  3. *
  4. * _ _ _ __ __ _
  5. * (_) (_) | | \/ (_)
  6. * _ _ __ ___ __ _ __ _ _ ___ __ _| | \ / |_ _ __ ___
  7. * | | '_ ` _ \ / _` |/ _` | |/ __/ _` | | |\/| | | '_ \ / _ \
  8. * | | | | | | | (_| | (_| | | (_| (_| | | | | | | | | | __/
  9. * |_|_| |_| |_|\__,_|\__, |_|\___\__,_|_|_| |_|_|_| |_|\___|
  10. * __/ |
  11. * |___/
  12. *
  13. * This program is a third party build by ImagicalMine.
  14. *
  15. * PocketMine is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Lesser General Public License as published by
  17. * the Free Software Foundation, either version 3 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * @author ImagicalMine Team
  21. * @link http://forums.imagicalcorp.ml/
  22. *
  23. *
  24. */
  25. namespace pocketmine\level\format\mcregion;
  26. use pocketmine\level\format\FullChunk;
  27. use pocketmine\level\format\generic\BaseLevelProvider;
  28. use pocketmine\level\generator\Generator;
  29. use pocketmine\level\Level;
  30. use pocketmine\nbt\NBT;
  31. use pocketmine\tile\Spawnable;
  32. use pocketmine\utils\BinaryStream;
  33. use pocketmine\utils\ChunkException;
  34. use pocketmine\nbt\tag\CompoundTag;
  35. use pocketmine\nbt\tag\ByteTag;
  36. use pocketmine\nbt\tag\IntTag;
  37. use pocketmine\nbt\tag\LongTag;
  38. use pocketmine\nbt\tag\StringTag;
  39. class McRegion extends BaseLevelProvider
  40. {
  41. /** @var RegionLoader[] */
  42. protected $regions = [];
  43. /** @var Chunk[] */
  44. protected $chunks = [];
  45. public static function getProviderName()
  46. {
  47. return "mcregion";
  48. }
  49. public static function getProviderOrder()
  50. {
  51. return self::ORDER_ZXY;
  52. }
  53. public static function usesChunkSection()
  54. {
  55. return false;
  56. }
  57. public static function isValid($path)
  58. {
  59. $isValid = (file_exists($path . "/level.dat") and is_dir($path . "/region/"));
  60. if ($isValid) {
  61. $files = glob($path . "/region/*.mc*");
  62. foreach ($files as $f) {
  63. if (strpos($f, ".mca") !== false) { //Anvil
  64. $isValid = false;
  65. break;
  66. }
  67. }
  68. }
  69. return $isValid;
  70. }
  71. public static function generate($path, $name, $seed, $generator, array $options = [])
  72. {
  73. if (!file_exists($path)) {
  74. mkdir($path, 0777, true);
  75. }
  76. if (!file_exists($path . "/region")) {
  77. mkdir($path . "/region", 0777);
  78. }
  79. //TODO, add extra details
  80. $levelData = new CompoundTag("Data", [
  81. "hardcore" => new ByteTag("hardcore", 0),
  82. "initialized" => new ByteTag("initialized", 1),
  83. "GameType" => new IntTag("GameType", 0),
  84. "generatorVersion" => new IntTag("generatorVersion", 1), //2 in MCPE
  85. "SpawnX" => new IntTag("SpawnX", 128),
  86. "SpawnY" => new IntTag("SpawnY", 70),
  87. "SpawnZ" => new IntTag("SpawnZ", 128),
  88. "version" => new IntTag("version", 19133),
  89. "DayTime" => new LongTag("DayTime", 0),
  90. "LastPlayed" => new LongTag("LastPlayed", microtime(true) * 1000),
  91. "RandomSeed" => new LongTag("RandomSeed", $seed),
  92. "SizeOnDisk" => new LongTag("SizeOnDisk", 0),
  93. "Time" => new LongTag("Time", 0),
  94. "RainTime" => new IntTag("RainTime", 0),
  95. "Raining" => new StringTag("Raining", false),
  96. "ThunderTime" => new IntTag("ThunderTime", 0),
  97. "Thundering" => new StringTag("Thundering", false),
  98. "generatorName" => new StringTag("generatorName", Generator::getGeneratorName($generator)),
  99. "generatorOptions" => new StringTag("generatorOptions", isset($options["preset"]) ? $options["preset"] : ""),
  100. "LevelName" => new StringTag("LevelName", $name),
  101. "GameRules" => new CompoundTag("GameRules", [])
  102. ]);
  103. $nbt = new NBT(NBT::BIG_ENDIAN);
  104. $nbt->setData(new CompoundTag("", [
  105. "Data" => $levelData
  106. ]));
  107. $buffer = $nbt->writeCompressed();
  108. file_put_contents($path . "level.dat", $buffer);
  109. }
  110. public static function getRegionIndex($chunkX, $chunkZ, &$x, &$z)
  111. {
  112. $x = $chunkX >> 5;
  113. $z = $chunkZ >> 5;
  114. }
  115. public function requestChunkTask($x, $z)
  116. {
  117. $chunk = $this->getChunk($x, $z, false);
  118. if (!($chunk instanceof Chunk)) {
  119. throw new ChunkException("Invalid Chunk sent");
  120. }
  121. $tiles = "";
  122. if (count($chunk->getTiles()) > 0) {
  123. $nbt = new NBT(NBT::LITTLE_ENDIAN);
  124. $list = [];
  125. foreach ($chunk->getTiles() as $tile) {
  126. if ($tile instanceof Spawnable) {
  127. $list[] = $tile->getSpawnCompound();
  128. }
  129. }
  130. $nbt->setData($list);
  131. $tiles = $nbt->write();
  132. }
  133. $extraData = new BinaryStream();
  134. $extraData->putLInt(count($chunk->getBlockExtraDataArray()));
  135. foreach ($chunk->getBlockExtraDataArray() as $key => $value) {
  136. $extraData->putLInt($key);
  137. $extraData->putLShort($value);
  138. }
  139. $ordered = $chunk->getBlockIdArray() .
  140. $chunk->getBlockDataArray() .
  141. $chunk->getBlockSkyLightArray() .
  142. $chunk->getBlockLightArray() .
  143. pack("C*", ...$chunk->getHeightMapArray()) .
  144. pack("N*", ...$chunk->getBiomeColorArray()) .
  145. $extraData->getBuffer() .
  146. $tiles;
  147. $this->getLevel()->chunkRequestCallback($x, $z, $ordered);
  148. return null;
  149. }
  150. public function unloadChunks()
  151. {
  152. foreach ($this->chunks as $chunk) {
  153. $this->unloadChunk($chunk->getX(), $chunk->getZ(), false);
  154. }
  155. $this->chunks = [];
  156. }
  157. public function isThundering()
  158. {
  159. return $this->levelData["Thundering"];
  160. }
  161. public function getGenerator()
  162. {
  163. return $this->levelData["generatorName"];
  164. }
  165. public function getGeneratorOptions()
  166. {
  167. return ["preset" => $this->levelData["generatorOptions"]];
  168. }
  169. public function getLoadedChunks()
  170. {
  171. return $this->chunks;
  172. }
  173. public function isChunkLoaded($x, $z)
  174. {
  175. return isset($this->chunks[Level::chunkHash($x, $z)]);
  176. }
  177. public function saveChunks()
  178. {
  179. foreach ($this->chunks as $chunk) {
  180. $this->saveChunk($chunk->getX(), $chunk->getZ());
  181. }
  182. }
  183. public function doGarbageCollection()
  184. {
  185. $limit = time() - 300;
  186. foreach ($this->regions as $index => $region) {
  187. if ($region->lastUsed <= $limit) {
  188. $region->close();
  189. unset($this->regions[$index]);
  190. }
  191. }
  192. }
  193. public function loadChunk($chunkX, $chunkZ, $create = false)
  194. {
  195. $index = Level::chunkHash($chunkX, $chunkZ);
  196. if (isset($this->chunks[$index])) {
  197. return true;
  198. }
  199. $regionX = $regionZ = null;
  200. self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
  201. $this->loadRegion($regionX, $regionZ);
  202. $this->level->timings->syncChunkLoadDataTimer->startTiming();
  203. $chunk = $this->getRegion($regionX, $regionZ)->readChunk($chunkX - $regionX * 32, $chunkZ - $regionZ * 32);
  204. if ($chunk === null and $create) {
  205. $chunk = $this->getEmptyChunk($chunkX, $chunkZ);
  206. }
  207. $this->level->timings->syncChunkLoadDataTimer->stopTiming();
  208. if ($chunk !== null) {
  209. $this->chunks[$index] = $chunk;
  210. return true;
  211. } else {
  212. return false;
  213. }
  214. }
  215. public function getEmptyChunk($chunkX, $chunkZ)
  216. {
  217. return Chunk::getEmptyChunk($chunkX, $chunkZ, $this);
  218. }
  219. public function unloadChunk($x, $z, $safe = true)
  220. {
  221. $chunk = isset($this->chunks[$index = Level::chunkHash($x, $z)]) ? $this->chunks[$index] : null;
  222. if ($chunk instanceof FullChunk and $chunk->unload(false, $safe)) {
  223. unset($this->chunks[$index]);
  224. return true;
  225. }
  226. return false;
  227. }
  228. public function saveChunk($x, $z)
  229. {
  230. if ($this->isChunkLoaded($x, $z)) {
  231. $this->getRegion($x >> 5, $z >> 5)->writeChunk($this->getChunk($x, $z));
  232. return true;
  233. }
  234. return false;
  235. }
  236. /**
  237. * @param $x
  238. * @param $z
  239. *
  240. * @return RegionLoader
  241. */
  242. protected function getRegion($x, $z)
  243. {
  244. return isset($this->regions[$index = Level::chunkHash($x, $z)]) ? $this->regions[$index] : null;
  245. }
  246. /**
  247. * @param int $chunkX
  248. * @param int $chunkZ
  249. * @param bool $create
  250. *
  251. * @return Chunk
  252. */
  253. public function getChunk($chunkX, $chunkZ, $create = false)
  254. {
  255. $index = Level::chunkHash($chunkX, $chunkZ);
  256. if (isset($this->chunks[$index])) {
  257. return $this->chunks[$index];
  258. } else {
  259. $this->loadChunk($chunkX, $chunkZ, $create);
  260. return isset($this->chunks[$index]) ? $this->chunks[$index] : null;
  261. }
  262. }
  263. public function setChunk($chunkX, $chunkZ, FullChunk $chunk)
  264. {
  265. if (!($chunk instanceof Chunk)) {
  266. throw new ChunkException("Invalid Chunk class");
  267. }
  268. $chunk->setProvider($this);
  269. self::getRegionIndex($chunkX, $chunkZ, $regionX, $regionZ);
  270. $this->loadRegion($regionX, $regionZ);
  271. $chunk->setX($chunkX);
  272. $chunk->setZ($chunkZ);
  273. if (isset($this->chunks[$index = Level::chunkHash($chunkX, $chunkZ)]) and $this->chunks[$index] !== $chunk) {
  274. $this->unloadChunk($chunkX, $chunkZ, false);
  275. }
  276. $this->chunks[$index] = $chunk;
  277. }
  278. public static function createChunkSection($Y)
  279. {
  280. return null;
  281. }
  282. public function isChunkGenerated($chunkX, $chunkZ)
  283. {
  284. if (($region = $this->getRegion($chunkX >> 5, $chunkZ >> 5)) !== null) {
  285. return $region->chunkExists($chunkX - $region->getX() * 32, $chunkZ - $region->getZ() * 32) and $this->getChunk($chunkX - $region->getX() * 32, $chunkZ - $region->getZ() * 32, true)->isGenerated();
  286. }
  287. return false;
  288. }
  289. public function isChunkPopulated($chunkX, $chunkZ)
  290. {
  291. $chunk = $this->getChunk($chunkX, $chunkZ);
  292. if ($chunk !== null) {
  293. return $chunk->isPopulated();
  294. } else {
  295. return false;
  296. }
  297. }
  298. protected function loadRegion($x, $z)
  299. {
  300. if (!isset($this->regions[$index = Level::chunkHash($x, $z)])) {
  301. $this->regions[$index] = new RegionLoader($this, $x, $z);
  302. }
  303. }
  304. public function close()
  305. {
  306. $this->unloadChunks();
  307. foreach ($this->regions as $index => $region) {
  308. $region->close();
  309. unset($this->regions[$index]);
  310. }
  311. $this->level = null;
  312. }
  313. }