mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-25 04:44:03 +00:00
BaseInventory: clean up max stack size handling
we can safely assume that: - the inventory's max stack size won't change during the operation - two items which stack together have the same max stack size - the item's max stack size won't change during the operation
This commit is contained in:
parent
07dc10d6e6
commit
709d874204
@ -171,13 +171,15 @@ abstract class BaseInventory implements Inventory{
|
|||||||
|
|
||||||
public function getAddableItemQuantity(Item $item) : int{
|
public function getAddableItemQuantity(Item $item) : int{
|
||||||
$count = $item->getCount();
|
$count = $item->getCount();
|
||||||
|
$maxStackSize = min($this->getMaxStackSize(), $item->getMaxStackSize());
|
||||||
|
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
if($this->isSlotEmpty($i)){
|
if($this->isSlotEmpty($i)){
|
||||||
$count -= min($this->getMaxStackSize(), $item->getMaxStackSize());
|
$count -= $maxStackSize;
|
||||||
}else{
|
}else{
|
||||||
$slot = $this->getItem($i);
|
$slot = $this->getItem($i);
|
||||||
if($item->canStackWith($slot)){
|
if($item->canStackWith($slot)){
|
||||||
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
|
if(($diff = $maxStackSize - $slot->getCount()) > 0){
|
||||||
$count -= $diff;
|
$count -= $diff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -217,6 +219,8 @@ abstract class BaseInventory implements Inventory{
|
|||||||
private function internalAddItem(Item $slot) : Item{
|
private function internalAddItem(Item $slot) : Item{
|
||||||
$emptySlots = [];
|
$emptySlots = [];
|
||||||
|
|
||||||
|
$maxStackSize = min($this->getMaxStackSize(), $slot->getMaxStackSize());
|
||||||
|
|
||||||
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
|
||||||
if($this->isSlotEmpty($i)){
|
if($this->isSlotEmpty($i)){
|
||||||
$emptySlots[] = $i;
|
$emptySlots[] = $i;
|
||||||
@ -224,8 +228,8 @@ abstract class BaseInventory implements Inventory{
|
|||||||
}
|
}
|
||||||
$item = $this->getItem($i);
|
$item = $this->getItem($i);
|
||||||
|
|
||||||
if($slot->canStackWith($item) && $item->getCount() < $item->getMaxStackSize()){
|
if($slot->canStackWith($item) && $item->getCount() < $maxStackSize){
|
||||||
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
|
$amount = min($maxStackSize - $item->getCount(), $slot->getCount());
|
||||||
if($amount > 0){
|
if($amount > 0){
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
$item->setCount($item->getCount() + $amount);
|
$item->setCount($item->getCount() + $amount);
|
||||||
@ -239,7 +243,7 @@ abstract class BaseInventory implements Inventory{
|
|||||||
|
|
||||||
if(count($emptySlots) > 0){
|
if(count($emptySlots) > 0){
|
||||||
foreach($emptySlots as $slotIndex){
|
foreach($emptySlots as $slotIndex){
|
||||||
$amount = min($slot->getMaxStackSize(), $slot->getCount(), $this->getMaxStackSize());
|
$amount = min($maxStackSize, $slot->getCount());
|
||||||
$slot->setCount($slot->getCount() - $amount);
|
$slot->setCount($slot->getCount() - $amount);
|
||||||
$item = clone $slot;
|
$item = clone $slot;
|
||||||
$item->setCount($amount);
|
$item->setCount($amount);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user