mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-04 00:55:14 +00:00
Introduce and use optimised versions of Inventory->isSlotEmpty()
this avoids useless cloning, improving the performance of several functions.
This commit is contained in:
@ -152,8 +152,8 @@ abstract class BaseInventory implements Inventory{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function firstEmpty() : int{
|
public function firstEmpty() : int{
|
||||||
foreach($this->getContents(true) as $i => $slot){
|
for($i = 0, $size = $this->getSize(); $i < $size; $i++){
|
||||||
if($slot->isNull()){
|
if($this->isSlotEmpty($i)){
|
||||||
return $i;
|
return $i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,13 +172,15 @@ abstract class BaseInventory implements Inventory{
|
|||||||
public function getAddableItemQuantity(Item $item) : int{
|
public function getAddableItemQuantity(Item $item) : int{
|
||||||
$count = $item->getCount();
|
$count = $item->getCount();
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
$slot = $this->getItem($i);
|
if($this->isSlotEmpty($i)){
|
||||||
if($item->canStackWith($slot)){
|
|
||||||
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
|
|
||||||
$count -= $diff;
|
|
||||||
}
|
|
||||||
}elseif($slot->isNull()){
|
|
||||||
$count -= min($this->getMaxStackSize(), $item->getMaxStackSize());
|
$count -= min($this->getMaxStackSize(), $item->getMaxStackSize());
|
||||||
|
}else{
|
||||||
|
$slot = $this->getItem($i);
|
||||||
|
if($item->canStackWith($slot)){
|
||||||
|
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
|
||||||
|
$count -= $diff;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($count <= 0){
|
if($count <= 0){
|
||||||
@ -216,11 +218,11 @@ abstract class BaseInventory implements Inventory{
|
|||||||
$emptySlots = [];
|
$emptySlots = [];
|
||||||
|
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
$item = $this->getItem($i);
|
if($this->isSlotEmpty($i)){
|
||||||
if($item->isNull()){
|
|
||||||
$emptySlots[] = $i;
|
$emptySlots[] = $i;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
$item = $this->getItem($i);
|
||||||
|
|
||||||
if($slot->canStackWith($item) && $item->getCount() < $item->getMaxStackSize()){
|
if($slot->canStackWith($item) && $item->getCount() < $item->getMaxStackSize()){
|
||||||
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
|
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
|
||||||
@ -273,10 +275,10 @@ abstract class BaseInventory implements Inventory{
|
|||||||
}
|
}
|
||||||
|
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
$item = $this->getItem($i);
|
if($this->isSlotEmpty($i)){
|
||||||
if($item->isNull()){
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
$item = $this->getItem($i);
|
||||||
|
|
||||||
foreach($itemSlots as $index => $slot){
|
foreach($itemSlots as $index => $slot){
|
||||||
if($slot->equals($item, !$slot->hasAnyDamageValue(), $slot->hasNamedTag())){
|
if($slot->equals($item, !$slot->hasAnyDamageValue(), $slot->hasNamedTag())){
|
||||||
|
@ -85,6 +85,10 @@ class DelegateInventory extends BaseInventory{
|
|||||||
$this->backingInventory->setContents($items);
|
$this->backingInventory->setContents($items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isSlotEmpty(int $index) : bool{
|
||||||
|
return $this->backingInventory->isSlotEmpty($index);
|
||||||
|
}
|
||||||
|
|
||||||
protected function onSlotChange(int $index, Item $before) : void{
|
protected function onSlotChange(int $index, Item $before) : void{
|
||||||
if($this->backingInventoryChanging){
|
if($this->backingInventoryChanging){
|
||||||
parent::onSlotChange($index, $before);
|
parent::onSlotChange($index, $before);
|
||||||
|
@ -83,4 +83,8 @@ class SimpleInventory extends BaseInventory{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isSlotEmpty(int $index) : bool{
|
||||||
|
return $this->slots[$index] === null || $this->slots[$index]->isNull();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user