From 066eadd687f8312b11530c7b887f596b27a41cb7 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 25 May 2019 19:00:11 +0100 Subject: [PATCH] Move responsibility of copying block-picked item NBT to Block instead of Player --- src/pocketmine/Player.php | 13 +------------ src/pocketmine/block/Beetroot.php | 2 +- src/pocketmine/block/Block.php | 20 ++++++++++++++++++-- src/pocketmine/block/Carrot.php | 2 +- src/pocketmine/block/CocoaBlock.php | 2 +- src/pocketmine/block/Farmland.php | 2 +- src/pocketmine/block/FlowerPot.php | 4 ++-- src/pocketmine/block/ItemFrame.php | 4 ++-- src/pocketmine/block/Potato.php | 2 +- src/pocketmine/block/Wheat.php | 2 +- 10 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index ba6559837..7d44ed42d 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -105,7 +105,6 @@ use pocketmine\permission\PermissionAttachment; use pocketmine\permission\PermissionAttachmentInfo; use pocketmine\permission\PermissionManager; use pocketmine\plugin\Plugin; -use pocketmine\tile\Tile; use pocketmine\timings\Timings; use pocketmine\utils\TextFormat; use pocketmine\utils\UUID; @@ -1837,17 +1836,7 @@ class Player extends Human implements CommandSender, ChunkLoader, ChunkListener, return true; } - $item = $block->getPickedItem(); - if($addTileNBT){ - $tile = $this->getWorld()->getTile($block); - if($tile instanceof Tile){ - $nbt = $tile->getCleanedNBT(); - if($nbt instanceof CompoundTag){ - $item->setCustomBlockData($nbt); - $item->setLore(["+(DATA)"]); - } - } - } + $item = $block->getPickedItem($addTileNBT); $ev = new PlayerBlockPickEvent($this, $block, $item); $ev->call(); diff --git a/src/pocketmine/block/Beetroot.php b/src/pocketmine/block/Beetroot.php index 5158c8fc4..418f16a4e 100644 --- a/src/pocketmine/block/Beetroot.php +++ b/src/pocketmine/block/Beetroot.php @@ -42,7 +42,7 @@ class Beetroot extends Crops{ ]; } - public function getPickedItem() : Item{ + public function getPickedItem(bool $addUserData = false) : Item{ return ItemFactory::get(Item::BEETROOT_SEEDS); } } diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index 5d3f5b32a..a050a12bf 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -37,9 +37,11 @@ use pocketmine\math\RayTraceResult; use pocketmine\math\Vector3; use pocketmine\metadata\Metadatable; use pocketmine\metadata\MetadataValue; +use pocketmine\nbt\tag\CompoundTag; use pocketmine\network\mcpe\protocol\types\RuntimeBlockMapping; use pocketmine\Player; use pocketmine\plugin\Plugin; +use pocketmine\tile\Tile; use pocketmine\tile\TileFactory; use pocketmine\world\Position; use pocketmine\world\World; @@ -500,10 +502,24 @@ class Block extends Position implements BlockLegacyIds, Metadatable{ /** * Returns the item that players will equip when middle-clicking on this block. + * + * @param bool $addUserData + * * @return Item */ - public function getPickedItem() : Item{ - return $this->asItem(); + public function getPickedItem(bool $addUserData = false) : Item{ + $item = $this->asItem(); + if($addUserData){ + $tile = $this->world->getTile($this); + if($tile instanceof Tile){ + $nbt = $tile->getCleanedNBT(); + if($nbt instanceof CompoundTag){ + $item->setCustomBlockData($nbt); + $item->setLore(["+(DATA)"]); + } + } + } + return $item; } /** diff --git a/src/pocketmine/block/Carrot.php b/src/pocketmine/block/Carrot.php index 1e2c8b459..e34435111 100644 --- a/src/pocketmine/block/Carrot.php +++ b/src/pocketmine/block/Carrot.php @@ -35,7 +35,7 @@ class Carrot extends Crops{ ]; } - public function getPickedItem() : Item{ + public function getPickedItem(bool $addUserData = false) : Item{ return ItemFactory::get(Item::CARROT); } } diff --git a/src/pocketmine/block/CocoaBlock.php b/src/pocketmine/block/CocoaBlock.php index a7d5a876e..7448074af 100644 --- a/src/pocketmine/block/CocoaBlock.php +++ b/src/pocketmine/block/CocoaBlock.php @@ -118,7 +118,7 @@ class CocoaBlock extends Transparent{ ]; } - public function getPickedItem() : Item{ + public function getPickedItem(bool $addUserData = false) : Item{ return ItemFactory::get(Item::DYE, 3); //cocoa beans } } diff --git a/src/pocketmine/block/Farmland.php b/src/pocketmine/block/Farmland.php index bcff0c773..70a535330 100644 --- a/src/pocketmine/block/Farmland.php +++ b/src/pocketmine/block/Farmland.php @@ -105,7 +105,7 @@ class Farmland extends Transparent{ return false; } - public function getPickedItem() : Item{ + public function getPickedItem(bool $addUserData = false) : Item{ return ItemFactory::get(Item::DIRT); } } diff --git a/src/pocketmine/block/FlowerPot.php b/src/pocketmine/block/FlowerPot.php index 087bbf1a9..476e194ba 100644 --- a/src/pocketmine/block/FlowerPot.php +++ b/src/pocketmine/block/FlowerPot.php @@ -150,8 +150,8 @@ class FlowerPot extends Flowable{ return $items; } - public function getPickedItem() : Item{ - return $this->plant !== null ? $this->plant->asItem() : parent::getPickedItem(); + public function getPickedItem(bool $addUserData = false) : Item{ + return $this->plant !== null ? $this->plant->asItem() : parent::getPickedItem($addUserData); } public function isAffectedBySilkTouch() : bool{ diff --git a/src/pocketmine/block/ItemFrame.php b/src/pocketmine/block/ItemFrame.php index 63291e357..16e7e90c4 100644 --- a/src/pocketmine/block/ItemFrame.php +++ b/src/pocketmine/block/ItemFrame.php @@ -197,8 +197,8 @@ class ItemFrame extends Flowable{ return $drops; } - public function getPickedItem() : Item{ - return $this->framedItem !== null ? clone $this->framedItem : parent::getPickedItem(); + public function getPickedItem(bool $addUserData = false) : Item{ + return $this->framedItem !== null ? clone $this->framedItem : parent::getPickedItem($addUserData); } public function isAffectedBySilkTouch() : bool{ diff --git a/src/pocketmine/block/Potato.php b/src/pocketmine/block/Potato.php index 62aa45caf..937c17eca 100644 --- a/src/pocketmine/block/Potato.php +++ b/src/pocketmine/block/Potato.php @@ -35,7 +35,7 @@ class Potato extends Crops{ ]; } - public function getPickedItem() : Item{ + public function getPickedItem(bool $addUserData = false) : Item{ return ItemFactory::get(Item::POTATO); } } diff --git a/src/pocketmine/block/Wheat.php b/src/pocketmine/block/Wheat.php index 59bd52256..664f565a6 100644 --- a/src/pocketmine/block/Wheat.php +++ b/src/pocketmine/block/Wheat.php @@ -42,7 +42,7 @@ class Wheat extends Crops{ } } - public function getPickedItem() : Item{ + public function getPickedItem(bool $addUserData = false) : Item{ return ItemFactory::get(Item::WHEAT_SEEDS); } }