PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
BaseLevelProvider.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\format\generic;
23 
33 
34 abstract class BaseLevelProvider implements LevelProvider{
36  protected $level;
38  protected $path;
40  protected $levelData;
41 
42  public function __construct(Level $level, $path){
43  $this->level = $level;
44  $this->path = $path;
45  @mkdir($this->path, 0777, true);
46  $nbt = new NBT(NBT::BIG_ENDIAN);
47  $nbt->readCompressed(file_get_contents($this->getPath() . "level.dat"));
48  $levelData = $nbt->getData();
49  if($levelData->Data instanceof Compound){
50  $this->levelData = $levelData->Data;
51  }else{
52  throw new LevelException("Invalid level.dat");
53  }
54 
55  if(!isset($this->levelData->generatorName)){
56  $this->levelData->generatorName = new String("generatorName", Generator::getGenerator("DEFAULT"));
57  }
58 
59  if(!isset($this->levelData->generatorOptions)){
60  $this->levelData->generatorOptions = new String("generatorOptions", "");
61  }
62  }
63 
64  public function getPath(){
65  return $this->path;
66  }
67 
68  public function getServer(){
69  return $this->level->getServer();
70  }
71 
72  public function getLevel(){
73  return $this->level;
74  }
75 
76  public function getName(){
77  return $this->levelData["LevelName"];
78  }
79 
80  public function getTime(){
81  return $this->levelData["Time"];
82  }
83 
84  public function setTime($value){
85  $this->levelData->Time = new Int("Time", (int) $value);
86  }
87 
88  public function getSeed(){
89  return $this->levelData["RandomSeed"];
90  }
91 
92  public function setSeed($value){
93  $this->levelData->RandomSeed = new Int("RandomSeed", (int) $value);
94  }
95 
96  public function getSpawn(){
97  return new Vector3((float) $this->levelData["SpawnX"], (float) $this->levelData["SpawnY"], (float) $this->levelData["SpawnZ"]);
98  }
99 
100  public function setSpawn(Vector3 $pos){
101  $this->levelData->SpawnX = new Int("SpawnX", (int) $pos->x);
102  $this->levelData->SpawnY = new Int("SpawnY", (int) $pos->y);
103  $this->levelData->SpawnZ = new Int("SpawnZ", (int) $pos->z);
104  }
105 
109  public function getLevelData(){
110  return $this->levelData;
111  }
112 
113  public function saveLevelData(){
114  $nbt = new NBT(NBT::BIG_ENDIAN);
115  $nbt->setData(new Compound(null, [
116  "Data" => $this->levelData
117  ]));
118  $buffer = $nbt->writeCompressed();
119  file_put_contents($this->getPath() . "level.dat", $buffer);
120  }
121 
122 
123 }