diff --git a/src/pocketmine/block/BurningFurnace.php b/src/pocketmine/block/BurningFurnace.php index 72f3453e1..01bbd6779 100644 --- a/src/pocketmine/block/BurningFurnace.php +++ b/src/pocketmine/block/BurningFurnace.php @@ -26,7 +26,6 @@ namespace pocketmine\block; use pocketmine\item\Item; use pocketmine\item\TieredTool; use pocketmine\math\Vector3; -use pocketmine\nbt\tag\StringTag; use pocketmine\Player; use pocketmine\tile\Furnace as TileFurnace; use pocketmine\tile\Tile; @@ -83,7 +82,7 @@ class BurningFurnace extends Solid{ $furnace = Tile::createTile(Tile::FURNACE, $this->getLevel(), TileFurnace::createNBT($this)); } - if($furnace->namedtag->hasTag("Lock", StringTag::class) and $furnace->namedtag->getString("Lock") !== $item->getCustomName()){ + if(!$furnace->canOpenWith($item->getCustomName())){ return true; } diff --git a/src/pocketmine/block/Chest.php b/src/pocketmine/block/Chest.php index edd1077ae..b11809ef2 100644 --- a/src/pocketmine/block/Chest.php +++ b/src/pocketmine/block/Chest.php @@ -26,7 +26,6 @@ namespace pocketmine\block; use pocketmine\item\Item; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Vector3; -use pocketmine\nbt\tag\StringTag; use pocketmine\Player; use pocketmine\tile\Chest as TileChest; use pocketmine\tile\Tile; @@ -115,7 +114,7 @@ class Chest extends Transparent{ if( !$this->getSide(Vector3::SIDE_UP)->isTransparent() or ($chest->isPaired() and !$chest->getPair()->getBlock()->getSide(Vector3::SIDE_UP)->isTransparent()) or - ($chest->namedtag->hasTag("Lock", StringTag::class) and $chest->namedtag->getString("Lock") !== $item->getCustomName()) + !$chest->canOpenWith($item->getCustomName()) ){ return true; } diff --git a/src/pocketmine/tile/Container.php b/src/pocketmine/tile/Container.php index b88c340c5..1e98dc5a1 100644 --- a/src/pocketmine/tile/Container.php +++ b/src/pocketmine/tile/Container.php @@ -27,6 +27,7 @@ use pocketmine\inventory\Inventory; interface Container{ public const TAG_ITEMS = "Items"; + public const TAG_LOCK = "Lock"; /** * @return Inventory @@ -37,4 +38,13 @@ interface Container{ * @return Inventory */ public function getRealInventory(); + + /** + * Returns whether this container can be opened by an item with the given custom name. + * + * @param string $key + * + * @return bool + */ + public function canOpenWith(string $key) : bool; } diff --git a/src/pocketmine/tile/ContainerTrait.php b/src/pocketmine/tile/ContainerTrait.php index 7cdc6f5f0..10501c9cc 100644 --- a/src/pocketmine/tile/ContainerTrait.php +++ b/src/pocketmine/tile/ContainerTrait.php @@ -28,11 +28,14 @@ use pocketmine\item\Item; use pocketmine\nbt\NBT; use pocketmine\nbt\tag\CompoundTag; use pocketmine\nbt\tag\ListTag; +use pocketmine\nbt\tag\StringTag; /** * This trait implements most methods in the {@link Container} interface. It should only be used by Tiles. */ trait ContainerTrait{ + /** @var string|null */ + private $lock; abstract public function getNBT() : CompoundTag; @@ -51,6 +54,10 @@ trait ContainerTrait{ $inventory->setItem($itemNBT->getByte("Slot"), Item::nbtDeserialize($itemNBT)); } } + + if($this->getNBT()->hasTag(Container::TAG_LOCK, StringTag::class)){ + $this->lock = $this->getNBT()->getString(Container::TAG_LOCK); + } } protected function saveItems() : void{ @@ -60,5 +67,20 @@ trait ContainerTrait{ } $this->getNBT()->setTag(new ListTag(Container::TAG_ITEMS, $items, NBT::TAG_Compound)); + + if($this->lock !== null){ + $this->getNBT()->setString(Container::TAG_LOCK, $this->lock); + } + } + + /** + * @see Container::canOpenWith() + * + * @param string $key + * + * @return bool + */ + public function canOpenWith(string $key) : bool{ + return $this->lock === null or $this->lock === $key; } }