Cleaned up Container lock handling

This commit is contained in:
Dylan K. Taylor 2018-06-03 13:42:04 +01:00
parent 6aaaaefd2f
commit 7b7917939a
4 changed files with 34 additions and 4 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}
}