PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
22declare(strict_types=1);
23
28namespace pocketmine\block\tile;
29
40use function get_class;
41
42abstract class Tile{
43
44 public const TAG_ID = "id";
45 public const TAG_X = "x";
46 public const TAG_Y = "y";
47 public const TAG_Z = "z";
48
49 protected Position $position;
50 public bool $closed = false;
51 protected TimingsHandler $timings;
52
53 public function __construct(World $world, Vector3 $pos){
54 $this->position = Position::fromObject($pos, $world);
55 $this->timings = Timings::getTileEntityTimings($this);
56 }
57
63 abstract public function readSaveData(CompoundTag $nbt) : void;
64
68 abstract protected function writeSaveData(CompoundTag $nbt) : void;
69
70 public function saveNBT() : CompoundTag{
71 $nbt = CompoundTag::create()
72 ->setString(self::TAG_ID, TileFactory::getInstance()->getSaveId(get_class($this)))
73 ->setInt(self::TAG_X, $this->position->getFloorX())
74 ->setInt(self::TAG_Y, $this->position->getFloorY())
75 ->setInt(self::TAG_Z, $this->position->getFloorZ())
76 ->setLong(VersionInfo::TAG_WORLD_DATA_VERSION, VersionInfo::WORLD_DATA_VERSION);
77 $this->writeSaveData($nbt);
78
79 return $nbt;
80 }
81
82 public function getCleanedNBT() : ?CompoundTag{
83 $this->writeSaveData($tag = new CompoundTag());
84 return $tag->getCount() > 0 ? $tag : null;
85 }
86
92 public function copyDataFromItem(Item $item) : void{
93 if(($blockNbt = $item->getCustomBlockData()) !== null){ //TODO: check item root tag (MCPE doesn't use BlockEntityTag)
94 $this->readSaveData($blockNbt);
95 }
96 }
97
98 public function getBlock() : Block{
99 return $this->position->getWorld()->getBlock($this->position);
100 }
101
102 public function getPosition() : Position{
103 return $this->position;
104 }
105
106 public function isClosed() : bool{
107 return $this->closed;
108 }
109
110 public function __destruct(){
111 $this->close();
112 }
113
117 final public function onBlockDestroyed() : void{
118 $this->onBlockDestroyedHook();
119 $this->close();
120 }
121
125 protected function onBlockDestroyedHook() : void{
126
127 }
128
129 public function close() : void{
130 if(!$this->closed){
131 $this->closed = true;
132
133 if($this->position->isValid()){
134 $this->position->getWorld()->removeTile($this);
135 }
136 }
137 }
138}
writeSaveData(CompoundTag $nbt)
static fromObject(Vector3 $pos, ?World $world)
Definition: Position.php:45