Make Tile constructors non-dependent on NBT

This commit is contained in:
Dylan K. Taylor 2018-11-30 17:05:36 +00:00
parent a2253e9e7d
commit 16006f9175
4 changed files with 18 additions and 26 deletions

View File

@ -82,6 +82,7 @@ use pocketmine\plugin\Plugin;
use pocketmine\Server; use pocketmine\Server;
use pocketmine\tile\Chest; use pocketmine\tile\Chest;
use pocketmine\tile\Container; use pocketmine\tile\Container;
use pocketmine\tile\Spawnable;
use pocketmine\tile\Tile; use pocketmine\tile\Tile;
use pocketmine\timings\Timings; use pocketmine\timings\Timings;
use pocketmine\utils\ReversePriorityQueue; use pocketmine\utils\ReversePriorityQueue;
@ -2611,7 +2612,11 @@ class Level implements ChunkManager, Metadatable{
} }
$this->tiles[Level::blockHash($tile->x, $tile->y, $tile->z)] = $tile; $this->tiles[Level::blockHash($tile->x, $tile->y, $tile->z)] = $tile;
$this->clearChunkCache($chunkX, $chunkZ); $tile->scheduleUpdate();
if($tile instanceof Spawnable){
$this->clearChunkCache($chunkX, $chunkZ);
$tile->spawnToAll();
}
} }
/** /**

View File

@ -33,7 +33,6 @@ use pocketmine\inventory\InventoryEventProcessor;
use pocketmine\inventory\InventoryHolder; use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item; use pocketmine\item\Item;
use pocketmine\item\ItemFactory; use pocketmine\item\ItemFactory;
use pocketmine\level\Level;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\ContainerSetDataPacket; use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
@ -56,13 +55,6 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
/** @var int */ /** @var int */
private $maxTime; private $maxTime;
public function __construct(Level $level, CompoundTag $nbt){
parent::__construct($level, $nbt);
if($this->burnTime > 0){
$this->scheduleUpdate();
}
}
protected function readSaveData(CompoundTag $nbt) : void{ protected function readSaveData(CompoundTag $nbt) : void{
$this->burnTime = max(0, $nbt->getShort(self::TAG_BURN_TIME, 0, true)); $this->burnTime = max(0, $nbt->getShort(self::TAG_BURN_TIME, 0, true));

View File

@ -23,7 +23,6 @@ declare(strict_types=1);
namespace pocketmine\tile; namespace pocketmine\tile;
use pocketmine\level\Level;
use pocketmine\nbt\NetworkLittleEndianNBTStream; use pocketmine\nbt\NetworkLittleEndianNBTStream;
use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag; use pocketmine\nbt\tag\IntTag;
@ -57,11 +56,6 @@ abstract class Spawnable extends Tile{
return true; return true;
} }
public function __construct(Level $level, CompoundTag $nbt){
parent::__construct($level, $nbt);
$this->spawnToAll();
}
public function spawnToAll(){ public function spawnToAll(){
if($this->closed){ if($this->closed){
return; return;

View File

@ -65,7 +65,7 @@ abstract class Tile extends Position{
private static $saveNames = []; private static $saveNames = [];
/** @var string */ /** @var string */
public $name; public $name = "";
/** @var bool */ /** @var bool */
public $closed = false; public $closed = false;
/** @var TimingsHandler */ /** @var TimingsHandler */
@ -94,9 +94,16 @@ abstract class Tile extends Position{
*/ */
public static function createTile($type, Level $level, CompoundTag $nbt, ...$args) : ?Tile{ public static function createTile($type, Level $level, CompoundTag $nbt, ...$args) : ?Tile{
if(isset(self::$knownTiles[$type])){ if(isset(self::$knownTiles[$type])){
$pos = new Vector3($nbt->getInt(self::TAG_X), $nbt->getInt(self::TAG_Y), $nbt->getInt(self::TAG_Z));
$class = self::$knownTiles[$type]; $class = self::$knownTiles[$type];
/** @see Tile::__construct() */ /**
return new $class($level, $nbt, ...$args); * @var Tile $tile
* @see Tile::__construct()
*/
$tile = new $class($level, $pos);
$tile->readSaveData($nbt);
$level->addTile($tile);
return $tile;
} }
return null; return null;
@ -134,15 +141,9 @@ abstract class Tile extends Position{
return current(self::$saveNames[static::class]); return current(self::$saveNames[static::class]);
} }
public function __construct(Level $level, CompoundTag $nbt){ public function __construct(Level $level, Vector3 $pos){
$this->timings = Timings::getTileEntityTimings($this); $this->timings = Timings::getTileEntityTimings($this);
parent::__construct($pos->getFloorX(), $pos->getFloorY(), $pos->getFloorZ(), $level);
$this->name = "";
parent::__construct($nbt->getInt(self::TAG_X), $nbt->getInt(self::TAG_Y), $nbt->getInt(self::TAG_Z), $level);
$this->readSaveData($nbt);
$this->getLevel()->addTile($this);
} }
/** /**