Use SplFixedArrays in inventory, added more typehints and cleaned up some duplicated code

This commit is contained in:
Dylan K. Taylor
2017-08-09 13:12:07 +01:00
parent 75644b5df2
commit 260179197b
9 changed files with 152 additions and 175 deletions

View File

@ -54,6 +54,9 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
return $this;
}
/**
* @return Chest
*/
public function getHolder(){
return $this->left->getHolder();
}
@ -62,17 +65,17 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
return $index < $this->left->getSize() ? $this->left->getItem($index) : $this->right->getItem($index - $this->right->getSize());
}
public function setItem(int $index, Item $item) : bool{
return $index < $this->left->getSize() ? $this->left->setItem($index, $item) : $this->right->setItem($index - $this->right->getSize(), $item);
public function setItem(int $index, Item $item, bool $send = true) : bool{
return $index < $this->left->getSize() ? $this->left->setItem($index, $item, $send) : $this->right->setItem($index - $this->right->getSize(), $item, $send);
}
public function clear(int $index) : bool{
return $index < $this->left->getSize() ? $this->left->clear($index) : $this->right->clear($index - $this->right->getSize());
public function clear(int $index, bool $send = true) : bool{
return $index < $this->left->getSize() ? $this->left->clear($index, $send) : $this->right->clear($index - $this->right->getSize(), $send);
}
public function getContents() : array{
$contents = [];
for($i = 0; $i < $this->getSize(); ++$i){
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
$contents[$i] = $this->getItem($i);
}
@ -83,24 +86,24 @@ class DoubleChestInventory extends ChestInventory implements InventoryHolder{
* @param Item[] $items
*/
public function setContents(array $items){
if(count($items) > $this->size){
$items = array_slice($items, 0, $this->size, true);
$size = $this->getSize();
if(count($items) > $size){
$items = array_slice($items, 0, $size, true);
}
$leftSize = $this->left->getSize();
for($i = 0; $i < $this->size; ++$i){
for($i = 0; $i < $size; ++$i){
if(!isset($items[$i])){
if($i < $this->left->size){
if(isset($this->left->slots[$i])){
$this->clear($i);
}
}elseif(isset($this->right->slots[$i - $this->left->size])){
$this->clear($i);
if(($i < $leftSize and isset($this->left->slots[$i])) or isset($this->right->slots[$i - $leftSize])){
$this->clear($i, false);
}
}elseif(!$this->setItem($i, $items[$i])){
$this->clear($i);
}elseif(!$this->setItem($i, $items[$i], false)){
$this->clear($i, false);
}
}
$this->sendContents($this->getViewers());
}
public function onOpen(Player $who){