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

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