mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 02:42:58 +00:00
Cleaned up implementations for EnderChestInventory/DoubleChestInventory
previously, these were forced to extend BaseInventory because of the amount of crap in Inventory's interface. This meant that these inventories had their own slots storage, which would be _mostly_ unused because these inventories aren't real inventories, but rather just delegates. This lead to a variety of bugs in the past, such as certain API methods on BaseInventory not working correctly for DoubleChestInventory in particular. Now, BaseInventory just implements the functional part of the inventory implementation, leaving the storage system up to the implementation. A SimpleInventory class is provided with a simple SplFixedArray storage backing, which is used by most inventories. EnderChestInventory and DoubleChestInventory now extend BaseInventory directly, and implement custom methods for dealing with their delegates.
This commit is contained in:
@ -23,11 +23,16 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\block\inventory;
|
||||
|
||||
use pocketmine\inventory\BaseInventory;
|
||||
use pocketmine\inventory\InventoryHolder;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\world\sound\ChestCloseSound;
|
||||
use pocketmine\world\sound\ChestOpenSound;
|
||||
use pocketmine\world\sound\Sound;
|
||||
|
||||
class DoubleChestInventory extends AnimatedBlockInventory implements InventoryHolder{
|
||||
class DoubleChestInventory extends BaseInventory implements BlockInventory, InventoryHolder{
|
||||
use AnimatedBlockInventoryTrait;
|
||||
|
||||
/** @var ChestInventory */
|
||||
private $left;
|
||||
/** @var ChestInventory */
|
||||
@ -36,21 +41,24 @@ class DoubleChestInventory extends AnimatedBlockInventory implements InventoryHo
|
||||
public function __construct(ChestInventory $left, ChestInventory $right){
|
||||
$this->left = $left;
|
||||
$this->right = $right;
|
||||
parent::__construct($this->left->getHolder(), $this->left->getSize() + $this->right->getSize());
|
||||
$this->holder = $this->left->getHolder();
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getInventory(){
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getSize() : int{
|
||||
return $this->left->getSize() + $this->right->getSize();
|
||||
}
|
||||
|
||||
public function getItem(int $index) : Item{
|
||||
return $index < $this->left->getSize() ? $this->left->getItem($index) : $this->right->getItem($index - $this->left->getSize());
|
||||
}
|
||||
|
||||
public function setItem(int $index, Item $item) : void{
|
||||
$old = $this->getItem($index);
|
||||
protected function internalSetItem(int $index, Item $item) : void{
|
||||
$index < $this->left->getSize() ? $this->left->setItem($index, $item) : $this->right->setItem($index - $this->left->getSize(), $item);
|
||||
$this->onSlotChange($index, $old);
|
||||
}
|
||||
|
||||
public function getContents(bool $includeEmpty = false) : array{
|
||||
@ -64,9 +72,26 @@ class DoubleChestInventory extends AnimatedBlockInventory implements InventoryHo
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function getOpenSound() : Sound{ return $this->left->getOpenSound(); }
|
||||
protected function internalSetContents(array $items) : void{
|
||||
$leftSize = $this->left->getSize();
|
||||
|
||||
protected function getCloseSound() : Sound{ return $this->left->getCloseSound(); }
|
||||
$leftContents = [];
|
||||
$rightContents = [];
|
||||
|
||||
foreach($items as $i => $item){
|
||||
if($i < $this->left->getSize()){
|
||||
$leftContents[$i] = $item;
|
||||
}else{
|
||||
$rightContents[$i - $leftSize] = $item;
|
||||
}
|
||||
}
|
||||
$this->left->setContents($leftContents);
|
||||
$this->right->setContents($rightContents);
|
||||
}
|
||||
|
||||
protected function getOpenSound() : Sound{ return new ChestOpenSound(); }
|
||||
|
||||
protected function getCloseSound() : Sound{ return new ChestCloseSound(); }
|
||||
|
||||
protected function animateBlock(bool $isOpen) : void{
|
||||
$this->left->animateBlock($isOpen);
|
||||
|
Reference in New Issue
Block a user