Tiles no longer store their NBT at runtime

This is a significant breaking change for anything utilizing Tiles as they now do not store their NBT at runtime anymore.
This is another step in the process of ridding PocketMine-MP of runtime NBT.

It can be noticed that all tiles are now using class fields to store properties instead of NBT, which is much faster, uses less memory and is also more concise when written in code.

Highlights:
- Tile->namedtag has been removed.
- Tile->saveNBT() now accepts a CompoundTag() parameter. Typically it's expected that this will be fed a newly-created CompoundTag (this part may be improved in the future).
- New internal methods Tile->readSaveData() and Tile->writeSaveData() have been added. Instead of overriding __construct() and saveNBT() to load and save properties from NBT, you should now implement these methods instead.

This is not final and will see further changes before it's done.
This commit is contained in:
Dylan K. Taylor
2018-06-03 18:29:08 +01:00
parent a22e5616f6
commit fa21cd96c5
14 changed files with 96 additions and 131 deletions

View File

@ -24,7 +24,6 @@ declare(strict_types=1);
namespace pocketmine\tile;
use pocketmine\event\block\SignChangeEvent;
use pocketmine\level\Level;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\Player;
@ -37,7 +36,7 @@ class Sign extends Spawnable{
/** @var string[] */
protected $text = ["", "", "", ""];
public function __construct(Level $level, CompoundTag $nbt){
protected function readSaveData(CompoundTag $nbt) : void{
if($nbt->hasTag(self::TAG_TEXT_BLOB, StringTag::class)){ //MCPE 1.2 save format
$this->text = array_pad(explode("\n", $nbt->getString(self::TAG_TEXT_BLOB)), 4, "");
assert(count($this->text) === 4, "Too many lines!");
@ -51,17 +50,14 @@ class Sign extends Spawnable{
}
}
}
parent::__construct($level, $nbt);
}
public function saveNBT() : void{
parent::saveNBT();
$this->namedtag->setString(self::TAG_TEXT_BLOB, implode("\n", $this->text));
protected function writeSaveData(CompoundTag $nbt) : void{
$nbt->setString(self::TAG_TEXT_BLOB, implode("\n", $this->text));
for($i = 1; $i <= 4; ++$i){ //Backwards-compatibility
$textKey = sprintf(self::TAG_TEXT_LINE, $i);
$this->namedtag->setString($textKey, $this->getLine($i - 1));
$nbt->setString($textKey, $this->getLine($i - 1));
}
}