mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-05 09:26:06 +00:00
Cleaned up Container lock handling
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user