Make tiles less dependent on runtime NBT, use properties instead

This will ultimately culminate in the complete removal of runtime NBT, so plugins should also follow these steps if they have custom data.
This commit is contained in:
Dylan K. Taylor
2018-06-03 12:50:16 +01:00
parent 1bb0337420
commit 6aaaaefd2f
8 changed files with 153 additions and 104 deletions

View File

@ -28,6 +28,7 @@ use pocketmine\inventory\DoubleChestInventory;
use pocketmine\inventory\InventoryHolder;
use pocketmine\level\Level;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
use NameableTrait {
@ -44,8 +45,19 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
/** @var DoubleChestInventory */
protected $doubleInventory = null;
/** @var int|null */
private $pairX;
/** @var int|null */
private $pairZ;
public function __construct(Level $level, CompoundTag $nbt){
if($nbt->hasTag(self::TAG_PAIRX, IntTag::class) and $nbt->hasTag(self::TAG_PAIRZ, IntTag::class)){
$this->pairX = $nbt->getInt(self::TAG_PAIRX);
$this->pairZ = $nbt->getInt(self::TAG_PAIRZ);
}
$nbt->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
parent::__construct($level, $nbt);
$this->inventory = new ChestInventory($this);
$this->loadItems();
}
@ -68,6 +80,12 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
public function saveNBT() : void{
parent::saveNBT();
if($this->isPaired()){
$this->namedtag->setInt(self::TAG_PAIRX, $this->pairX);
$this->namedtag->setInt(self::TAG_PAIRZ, $this->pairZ);
}else{
$this->namedtag->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
}
$this->saveItems();
}
@ -89,7 +107,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
}
protected function checkPairing(){
if($this->isPaired() and !$this->getLevel()->isChunkLoaded($this->namedtag->getInt(self::TAG_PAIRX) >> 4, $this->namedtag->getInt(self::TAG_PAIRZ) >> 4)){
if($this->isPaired() and !$this->getLevel()->isChunkLoaded($this->pairX >> 4, $this->pairZ >> 4)){
//paired to a tile in an unloaded chunk
$this->doubleInventory = null;
@ -107,7 +125,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
}
}else{
$this->doubleInventory = null;
$this->namedtag->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
$this->pairX = $this->pairZ = null;
}
}
@ -119,7 +137,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
}
public function isPaired(){
return $this->namedtag->hasTag(self::TAG_PAIRX) and $this->namedtag->hasTag(self::TAG_PAIRZ);
return $this->pairX !== null and $this->pairZ !== null;
}
/**
@ -127,7 +145,7 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
*/
public function getPair() : ?Chest{
if($this->isPaired()){
$tile = $this->getLevel()->getTileAt($this->namedtag->getInt(self::TAG_PAIRX), $this->y, $this->namedtag->getInt(self::TAG_PAIRZ));
$tile = $this->getLevel()->getTileAt($this->pairX, $this->y, $this->pairZ);
if($tile instanceof Chest){
return $tile;
}
@ -151,11 +169,11 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
}
private function createPair(Chest $tile){
$this->namedtag->setInt(self::TAG_PAIRX, $tile->x);
$this->namedtag->setInt(self::TAG_PAIRZ, $tile->z);
$this->pairX = $tile->x;
$this->pairZ = $tile->z;
$tile->namedtag->setInt(self::TAG_PAIRX, $this->x);
$tile->namedtag->setInt(self::TAG_PAIRZ, $this->z);
$tile->pairX = $this->x;
$tile->pairZ = $this->z;
}
public function unpair(){
@ -164,12 +182,12 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
}
$tile = $this->getPair();
$this->namedtag->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
$this->pairX = $this->pairZ = null;
$this->onChanged();
if($tile instanceof Chest){
$tile->namedtag->removeTag(self::TAG_PAIRX, self::TAG_PAIRZ);
$tile->pairX = $tile->pairZ = null;
$tile->checkPairing();
$tile->onChanged();
}
@ -180,8 +198,8 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
public function addAdditionalSpawnData(CompoundTag $nbt) : void{
if($this->isPaired()){
$nbt->setTag($this->namedtag->getTag(self::TAG_PAIRX));
$nbt->setTag($this->namedtag->getTag(self::TAG_PAIRZ));
$nbt->setInt(self::TAG_PAIRX, $this->pairX);
$nbt->setInt(self::TAG_PAIRZ, $this->pairZ);
}
$this->addNameSpawnData($nbt);