PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
Tile.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 
26 namespace pocketmine\tile;
27 
37 
38 abstract class Tile extends Position{
39  const SIGN = "Sign";
40  const CHEST = "Chest";
41  const FURNACE = "Furnace";
42 
43  public static $tileCount = 1;
44 
45  private static $knownTiles = [];
46  private static $shortNames = [];
47 
49  public $chunk;
50  public $name;
51  public $id;
52  public $x;
53  public $y;
54  public $z;
55  public $attach;
56  public $metadata;
57  public $closed = false;
58  public $namedtag;
59  protected $lastUpdate;
60  protected $server;
61  protected $timings;
62 
64  public $tickTimer;
65 
74  public static function createTile($type, FullChunk $chunk, Compound $nbt, ...$args){
75  if(isset(self::$knownTiles[$type])){
76  $class = self::$knownTiles[$type];
77  return new $class($chunk, $nbt, ...$args);
78  }
79 
80  return null;
81  }
82 
88  public static function registerTile($className){
89  $class = new \ReflectionClass($className);
90  if(is_a($className, Tile::class, true) and !$class->isAbstract()){
91  self::$knownTiles[$class->getShortName()] = $className;
92  self::$shortNames[$className] = $class->getShortName();
93  return true;
94  }
95 
96  return false;
97  }
98 
104  public function getSaveId(){
105  return self::$shortNames[static::class];
106  }
107 
108  public function __construct(FullChunk $chunk, Compound $nbt){
109  if($chunk === null or $chunk->getProvider() === null){
110  throw new ChunkException("Invalid garbage Chunk given to Tile");
111  }
112 
113  $this->timings = Timings::getTileEntityTimings($this);
114 
115  $this->server = $chunk->getProvider()->getLevel()->getServer();
116  $this->chunk = $chunk;
117  $this->setLevel($chunk->getProvider()->getLevel());
118  $this->namedtag = $nbt;
119  $this->name = "";
120  $this->lastUpdate = microtime(true);
121  $this->id = Tile::$tileCount++;
122  $this->x = (int) $this->namedtag["x"];
123  $this->y = (int) $this->namedtag["y"];
124  $this->z = (int) $this->namedtag["z"];
125 
126  $this->chunk->addTile($this);
127  $this->getLevel()->addTile($this);
128  $this->tickTimer = Timings::getTileEntityTimings($this);
129  }
130 
131  public function getId(){
132  return $this->id;
133  }
134 
135  public function saveNBT(){
136  $this->namedtag->id = new String("id", $this->getSaveId());
137  $this->namedtag->x = new Int("x", $this->x);
138  $this->namedtag->y = new Int("y", $this->y);
139  $this->namedtag->z = new Int("z", $this->z);
140  }
141 
145  public function getBlock(){
146  return $this->level->getBlock($this);
147  }
148 
149  public function onUpdate(){
150  return false;
151  }
152 
153  public final function scheduleUpdate(){
154  $this->level->updateTiles[$this->id] = $this;
155  }
156 
157  public function __destruct(){
158  $this->close();
159  }
160 
161  public function close(){
162  if(!$this->closed){
163  $this->closed = true;
164  unset($this->level->updateTiles[$this->id]);
165  if($this->chunk instanceof FullChunk){
166  $this->chunk->removeTile($this);
167  }
168  if(($level = $this->getLevel()) instanceof Level){
169  $level->removeTile($this);
170  }
171  $this->level = null;
172  }
173  }
174 
175  public function getName(){
176  return $this->name;
177  }
178 
179 }
static registerTile($className)
Definition: Tile.php:88
static getTileEntityTimings(Tile $tile)
Definition: Timings.php:181
static createTile($type, FullChunk $chunk, Compound $nbt,...$args)
Definition: Tile.php:74