Tile: Fill defaults and move code to constructors where appropriate

this is leftovers from when NBT was required to construct a tile.
This commit is contained in:
Dylan K. Taylor 2018-12-10 18:35:26 +00:00
parent 7d594ac6d8
commit 6dbceda3e8
8 changed files with 67 additions and 31 deletions

View File

@ -25,6 +25,8 @@ namespace pocketmine\tile;
use pocketmine\item\Banner as ItemBanner;
use pocketmine\item\Item;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ListTag;
@ -98,16 +100,21 @@ class Banner extends Spawnable implements Nameable{
public const COLOR_WHITE = 15;
/** @var int */
private $baseColor;
private $baseColor = self::COLOR_BLACK;
/**
* @var ListTag
* TODO: break this down further and remove runtime NBT from here entirely
*/
private $patterns;
public function __construct(Level $level, Vector3 $pos){
$this->patterns = new ListTag(self::TAG_PATTERNS);
parent::__construct($level, $pos);
}
protected function readSaveData(CompoundTag $nbt) : void{
$this->baseColor = $nbt->getInt(self::TAG_BASE, self::COLOR_BLACK, true);
$this->patterns = $nbt->getListTag(self::TAG_PATTERNS) ?? new ListTag(self::TAG_PATTERNS);
$this->baseColor = $nbt->getInt(self::TAG_BASE, $this->baseColor, true);
$this->patterns = $nbt->getListTag(self::TAG_PATTERNS) ?? $this->patterns;
$this->loadName($nbt);
}

View File

@ -26,6 +26,7 @@ namespace pocketmine\tile;
use pocketmine\inventory\ChestInventory;
use pocketmine\inventory\DoubleChestInventory;
use pocketmine\inventory\InventoryHolder;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
@ -50,14 +51,17 @@ class Chest extends Spawnable implements InventoryHolder, Container, Nameable{
/** @var int|null */
private $pairZ;
public function __construct(Level $level, Vector3 $pos){
$this->inventory = new ChestInventory($this);
parent::__construct($level, $pos);
}
protected function readSaveData(CompoundTag $nbt) : void{
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);
}
$this->loadName($nbt);
$this->inventory = new ChestInventory($this);
$this->loadItems($nbt);
}

View File

@ -35,7 +35,7 @@ use pocketmine\nbt\tag\StringTag;
*/
trait ContainerTrait{
/** @var string|null */
private $lock;
private $lock = null;
/**
* @return Inventory
@ -47,10 +47,14 @@ trait ContainerTrait{
$inventoryTag = $tag->getListTag(Container::TAG_ITEMS);
$inventory = $this->getRealInventory();
$slotChangeListener = $inventory->getSlotChangeListener();
$inventory->setSlotChangeListener(null); //prevent any events being fired by initialization
$inventory->clearAll();
/** @var CompoundTag $itemNBT */
foreach($inventoryTag as $itemNBT){
$inventory->setItem($itemNBT->getByte("Slot"), Item::nbtDeserialize($itemNBT));
}
$inventory->setSlotChangeListener($slotChangeListener);
}
if($tag->hasTag(Container::TAG_LOCK, StringTag::class)){

View File

@ -25,7 +25,11 @@ namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\ShortTag;
class FlowerPot extends Spawnable{
public const TAG_ITEM = "item";
@ -34,8 +38,15 @@ class FlowerPot extends Spawnable{
/** @var Item */
private $item;
public function __construct(Level $level, Vector3 $pos){
$this->item = ItemFactory::get(Item::AIR, 0, 0);
parent::__construct($level, $pos);
}
protected function readSaveData(CompoundTag $nbt) : void{
$this->item = ItemFactory::get($nbt->getShort(self::TAG_ITEM, 0, true), $nbt->getInt(self::TAG_ITEM_DATA, 0, true), 1);
if($nbt->hasTag(self::TAG_ITEM, ShortTag::class) and $nbt->hasTag(self::TAG_ITEM_DATA, IntTag::class)){
$this->item = ItemFactory::get($nbt->getShort(self::TAG_ITEM, 0), $nbt->getInt(self::TAG_ITEM_DATA, 0), 1);
}
}
protected function writeSaveData(CompoundTag $nbt) : void{

View File

@ -32,6 +32,8 @@ use pocketmine\inventory\Inventory;
use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\ContainerSetDataPacket;
@ -48,34 +50,37 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
/** @var FurnaceInventory */
protected $inventory;
/** @var int */
private $burnTime;
private $burnTime = 0;
/** @var int */
private $cookTime;
private $cookTime = 0;
/** @var int */
private $maxTime;
private $maxTime = 0;
public function __construct(Level $level, Vector3 $pos){
$this->inventory = new FurnaceInventory($this);
$this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
$this->scheduleUpdate();
return $newItem;
});
parent::__construct($level, $pos);
}
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, $this->burnTime, true));
$this->cookTime = $nbt->getShort(self::TAG_COOK_TIME, 0, true);
$this->cookTime = $nbt->getShort(self::TAG_COOK_TIME, $this->cookTime, true);
if($this->burnTime === 0){
$this->cookTime = 0;
}
$this->maxTime = $nbt->getShort(self::TAG_MAX_TIME, 0, true);
$this->maxTime = $nbt->getShort(self::TAG_MAX_TIME, $this->maxTime, true);
if($this->maxTime === 0){
$this->maxTime = $this->burnTime;
}
$this->loadName($nbt);
$this->inventory = new FurnaceInventory($this);
$this->loadItems($nbt);
$this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
$this->scheduleUpdate();
return $newItem;
});
}
protected function writeSaveData(CompoundTag $nbt) : void{

View File

@ -25,6 +25,8 @@ namespace pocketmine\tile;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\level\Level;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
class ItemFrame extends Spawnable{
@ -35,18 +37,21 @@ class ItemFrame extends Spawnable{
/** @var Item */
private $item;
/** @var int */
private $itemRotation;
private $itemRotation = 0;
/** @var float */
private $itemDropChance;
private $itemDropChance = 1.0;
public function __construct(Level $level, Vector3 $pos){
$this->item = ItemFactory::get(Item::AIR, 0, 0);
parent::__construct($level, $pos);
}
protected function readSaveData(CompoundTag $nbt) : void{
if(($itemTag = $nbt->getCompoundTag(self::TAG_ITEM)) !== null){
$this->item = Item::nbtDeserialize($itemTag);
}else{
$this->item = ItemFactory::get(Item::AIR, 0, 0);
}
$this->itemRotation = $nbt->getByte(self::TAG_ITEM_ROTATION, 0, true);
$this->itemDropChance = $nbt->getFloat(self::TAG_ITEM_DROP_CHANCE, 1.0, true);
$this->itemRotation = $nbt->getByte(self::TAG_ITEM_ROTATION, $this->itemRotation, true);
$this->itemDropChance = $nbt->getFloat(self::TAG_ITEM_DROP_CHANCE, $this->itemDropChance, true);
}
protected function writeSaveData(CompoundTag $nbt) : void{

View File

@ -32,7 +32,7 @@ use pocketmine\nbt\tag\StringTag;
*/
trait NameableTrait{
/** @var string|null */
private $customName;
private $customName = null;
/**
* @return string

View File

@ -39,13 +39,13 @@ class Skull extends Spawnable{
public const TAG_MOUTH_TICK_COUNT = "MouthTickCount"; //TAG_Int
/** @var int */
private $skullType;
private $skullType = self::TYPE_SKELETON;
/** @var int */
private $skullRotation;
private $skullRotation = 0;
protected function readSaveData(CompoundTag $nbt) : void{
$this->skullType = $nbt->getByte(self::TAG_SKULL_TYPE, self::TYPE_SKELETON, true);
$this->skullRotation = $nbt->getByte(self::TAG_ROT, 0, true);
$this->skullType = $nbt->getByte(self::TAG_SKULL_TYPE, $this->skullType, true);
$this->skullRotation = $nbt->getByte(self::TAG_ROT, $this->skullRotation, true);
}
protected function writeSaveData(CompoundTag $nbt) : void{