From 6dbceda3e854004df6bc76e396753d3ffd9ad3f3 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 10 Dec 2018 18:35:26 +0000 Subject: [PATCH] Tile: Fill defaults and move code to constructors where appropriate this is leftovers from when NBT was required to construct a tile. --- src/pocketmine/tile/Banner.php | 13 ++++++++--- src/pocketmine/tile/Chest.php | 8 +++++-- src/pocketmine/tile/ContainerTrait.php | 6 ++++- src/pocketmine/tile/FlowerPot.php | 13 ++++++++++- src/pocketmine/tile/Furnace.php | 31 +++++++++++++++----------- src/pocketmine/tile/ItemFrame.php | 17 +++++++++----- src/pocketmine/tile/NameableTrait.php | 2 +- src/pocketmine/tile/Skull.php | 8 +++---- 8 files changed, 67 insertions(+), 31 deletions(-) diff --git a/src/pocketmine/tile/Banner.php b/src/pocketmine/tile/Banner.php index 622744619..af14afad0 100644 --- a/src/pocketmine/tile/Banner.php +++ b/src/pocketmine/tile/Banner.php @@ -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); } diff --git a/src/pocketmine/tile/Chest.php b/src/pocketmine/tile/Chest.php index 2a7a16faa..9ce0fe09e 100644 --- a/src/pocketmine/tile/Chest.php +++ b/src/pocketmine/tile/Chest.php @@ -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); } diff --git a/src/pocketmine/tile/ContainerTrait.php b/src/pocketmine/tile/ContainerTrait.php index 330092460..71fe63152 100644 --- a/src/pocketmine/tile/ContainerTrait.php +++ b/src/pocketmine/tile/ContainerTrait.php @@ -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)){ diff --git a/src/pocketmine/tile/FlowerPot.php b/src/pocketmine/tile/FlowerPot.php index 7b69d69af..7ee5c7e68 100644 --- a/src/pocketmine/tile/FlowerPot.php +++ b/src/pocketmine/tile/FlowerPot.php @@ -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{ diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index ad03f9734..8cbee9aa1 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -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{ diff --git a/src/pocketmine/tile/ItemFrame.php b/src/pocketmine/tile/ItemFrame.php index 4b573395f..2fbb1f4b5 100644 --- a/src/pocketmine/tile/ItemFrame.php +++ b/src/pocketmine/tile/ItemFrame.php @@ -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{ diff --git a/src/pocketmine/tile/NameableTrait.php b/src/pocketmine/tile/NameableTrait.php index b623df8db..c65a679fd 100644 --- a/src/pocketmine/tile/NameableTrait.php +++ b/src/pocketmine/tile/NameableTrait.php @@ -32,7 +32,7 @@ use pocketmine\nbt\tag\StringTag; */ trait NameableTrait{ /** @var string|null */ - private $customName; + private $customName = null; /** * @return string diff --git a/src/pocketmine/tile/Skull.php b/src/pocketmine/tile/Skull.php index e49460bdf..1421fe5db 100644 --- a/src/pocketmine/tile/Skull.php +++ b/src/pocketmine/tile/Skull.php @@ -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{