PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
BaseWorldProvider.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
22declare(strict_types=1);
23
24namespace pocketmine\world\format\io;
25
32use pocketmine\world\format\PalettedBlockArray;
34use function count;
35use function file_exists;
36use function implode;
37
38abstract class BaseWorldProvider implements WorldProvider{
39 protected WorldData $worldData;
40
41 protected BlockStateDeserializer $blockStateDeserializer;
42 protected BlockDataUpgrader $blockDataUpgrader;
43 protected BlockStateSerializer $blockStateSerializer;
44
45 public function __construct(
46 protected string $path,
47 protected \Logger $logger
48 ){
49 if(!file_exists($path)){
50 throw new WorldException("World does not exist");
51 }
52
53 //TODO: this should not rely on singletons
54 $this->blockStateDeserializer = GlobalBlockStateHandlers::getDeserializer();
55 $this->blockDataUpgrader = GlobalBlockStateHandlers::getUpgrader();
56 $this->blockStateSerializer = GlobalBlockStateHandlers::getSerializer();
57
58 $this->worldData = $this->loadLevelData();
59 }
60
65 abstract protected function loadLevelData() : WorldData;
66
67 private function translatePalette(PalettedBlockArray $blockArray, \Logger $logger) : PalettedBlockArray{
68 //TODO: missing type info in stubs
70 $palette = $blockArray->getPalette();
71
72 $newPalette = [];
73 $blockDecodeErrors = [];
74 foreach($palette as $k => $legacyIdMeta){
75 //TODO: remember data for unknown states so we can implement them later
76 $id = $legacyIdMeta >> 4;
77 $meta = $legacyIdMeta & 0xf;
78 try{
79 $newStateData = $this->blockDataUpgrader->upgradeIntIdMeta($id, $meta);
81 $blockDecodeErrors[] = "Palette offset $k / Failed to upgrade legacy ID/meta $id:$meta: " . $e->getMessage();
82 $newStateData = GlobalBlockStateHandlers::getUnknownBlockStateData();
83 }
84
85 try{
86 $newPalette[$k] = $this->blockStateDeserializer->deserialize($newStateData);
88 //this should never happen anyway - if the upgrader returned an invalid state, we have bigger problems
89 $blockDecodeErrors[] = "Palette offset $k / Failed to deserialize upgraded state $id:$meta: " . $e->getMessage();
90 $newPalette[$k] = $this->blockStateDeserializer->deserialize(GlobalBlockStateHandlers::getUnknownBlockStateData());
91 }
92 }
93
94 if(count($blockDecodeErrors) > 0){
95 $logger->error("Errors decoding/upgrading blocks:\n - " . implode("\n - ", $blockDecodeErrors));
96 }
97
98 //TODO: this is sub-optimal since it reallocates the offset table multiple times
99 return PalettedBlockArray::fromData(
100 $blockArray->getBitsPerBlock(),
101 $blockArray->getWordArray(),
102 $newPalette
103 );
104 }
105
106 protected function palettizeLegacySubChunkXZY(string $idArray, string $metaArray, \Logger $logger) : PalettedBlockArray{
107 return $this->translatePalette(SubChunkConverter::convertSubChunkXZY($idArray, $metaArray), $logger);
108 }
109
110 protected function palettizeLegacySubChunkYZX(string $idArray, string $metaArray, \Logger $logger) : PalettedBlockArray{
111 return $this->translatePalette(SubChunkConverter::convertSubChunkYZX($idArray, $metaArray), $logger);
112 }
113
114 protected function palettizeLegacySubChunkFromColumn(string $idArray, string $metaArray, int $yOffset, \Logger $logger) : PalettedBlockArray{
115 return $this->translatePalette(SubChunkConverter::convertSubChunkFromLegacyColumn($idArray, $metaArray, $yOffset), $logger);
116 }
117
118 public function getPath() : string{
119 return $this->path;
120 }
121
122 public function getWorldData() : WorldData{
123 return $this->worldData;
124 }
125}
error($message)