PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
BaseNbtWorldData.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\data;
25
32use function file_exists;
33
34abstract class BaseNbtWorldData implements WorldData{
35 protected const TAG_LEVEL_NAME = "LevelName";
36 protected const TAG_GENERATOR_NAME = "generatorName";
37 protected const TAG_GENERATOR_OPTIONS = "generatorOptions";
38 protected const TAG_RANDOM_SEED = "RandomSeed";
39 protected const TAG_TIME = "Time";
40 protected const TAG_SPAWN_X = "SpawnX";
41 protected const TAG_SPAWN_Y = "SpawnY";
42 protected const TAG_SPAWN_Z = "SpawnZ";
43
44 protected CompoundTag $compoundTag;
45
50 public function __construct(
51 protected string $dataPath
52 ){
53 if(!file_exists($this->dataPath)){
54 throw new CorruptedWorldException("World data not found at $dataPath");
55 }
56
57 try{
58 $this->compoundTag = $this->load();
59 }catch(CorruptedWorldException $e){
60 throw new CorruptedWorldException("Corrupted world data: " . $e->getMessage(), 0, $e);
61 }
62 $this->fix();
63 }
64
69 abstract protected function load() : CompoundTag;
70
75 abstract protected function fix() : void;
76
95 protected static function hackyFixForGeneratorClasspathInLevelDat(string $className) : ?string{
96 //THESE ARE DELIBERATELY HARDCODED, DO NOT CHANGE!
97 switch($className){
99 case 'pocketmine\level\generator\normal\Normal':
100 return "normal";
102 case 'pocketmine\level\generator\Flat':
103 return "flat";
104 }
105
106 return null;
107 }
108
109 public function getCompoundTag() : CompoundTag{
110 return $this->compoundTag;
111 }
112
113 /* The below are common between PC and PE */
114
115 public function getName() : string{
116 return $this->compoundTag->getString(self::TAG_LEVEL_NAME);
117 }
118
119 public function setName(string $value) : void{
120 $this->compoundTag->setString(self::TAG_LEVEL_NAME, $value);
121 }
122
123 public function getGenerator() : string{
124 return $this->compoundTag->getString(self::TAG_GENERATOR_NAME, "DEFAULT");
125 }
126
127 public function getGeneratorOptions() : string{
128 return $this->compoundTag->getString(self::TAG_GENERATOR_OPTIONS, "");
129 }
130
131 public function getSeed() : int{
132 return $this->compoundTag->getLong(self::TAG_RANDOM_SEED);
133 }
134
135 public function getTime() : int{
136 if(($timeTag = $this->compoundTag->getTag(self::TAG_TIME)) instanceof IntTag){ //some older PM worlds had this in the wrong format
137 return $timeTag->getValue();
138 }
139 return $this->compoundTag->getLong(self::TAG_TIME, 0);
140 }
141
142 public function setTime(int $value) : void{
143 $this->compoundTag->setLong(self::TAG_TIME, $value);
144 }
145
146 public function getSpawn() : Vector3{
147 return new Vector3($this->compoundTag->getInt(self::TAG_SPAWN_X), $this->compoundTag->getInt(self::TAG_SPAWN_Y), $this->compoundTag->getInt(self::TAG_SPAWN_Z));
148 }
149
150 public function setSpawn(Vector3 $pos) : void{
151 $this->compoundTag->setInt(self::TAG_SPAWN_X, $pos->getFloorX());
152 $this->compoundTag->setInt(self::TAG_SPAWN_Y, $pos->getFloorY());
153 $this->compoundTag->setInt(self::TAG_SPAWN_Z, $pos->getFloorZ());
154 }
155
156}
static hackyFixForGeneratorClasspathInLevelDat(string $className)