Added API method Item::canStackWith()

This commit is contained in:
Dylan K. Taylor
2021-06-29 19:52:52 +01:00
parent 32d7b1e6af
commit 7ba573db77
6 changed files with 13 additions and 5 deletions

View File

@ -118,7 +118,7 @@ trait InventoryHelpersTrait{
$count = $item->getCount();
for($i = 0, $size = $this->getSize(); $i < $size; ++$i){
$slot = $this->getItem($i);
if($item->equals($slot)){
if($item->canStackWith($slot)){
if(($diff = min($slot->getMaxStackSize(), $item->getMaxStackSize()) - $slot->getCount()) > 0){
$count -= $diff;
}
@ -166,7 +166,7 @@ trait InventoryHelpersTrait{
$emptySlots[] = $i;
}
if($slot->equals($item) and $item->getCount() < $item->getMaxStackSize()){
if($slot->canStackWith($item) and $item->getCount() < $item->getMaxStackSize()){
$amount = min($item->getMaxStackSize() - $item->getCount(), $slot->getCount(), $this->getMaxStackSize());
if($amount > 0){
$slot->setCount($slot->getCount() - $amount);

View File

@ -84,7 +84,7 @@ class CraftingTransaction extends InventoryTransaction{
$recipeItem = array_pop($recipeItems);
$needCount = $recipeItem->getCount();
foreach($recipeItems as $i => $otherRecipeItem){
if($otherRecipeItem->equals($recipeItem)){ //make sure they have the same wildcards set
if($otherRecipeItem->canStackWith($recipeItem)){ //make sure they have the same wildcards set
$needCount += $otherRecipeItem->getCount();
unset($recipeItems[$i]);
}

View File

@ -158,7 +158,7 @@ class InventoryTransaction{
foreach($needItems as $i => $needItem){
foreach($haveItems as $j => $haveItem){
if($needItem->equals($haveItem)){
if($needItem->canStackWith($haveItem)){
$amount = min($needItem->getCount(), $haveItem->getCount());
$needItem->setCount($needItem->getCount() - $amount);
$haveItem->setCount($haveItem->getCount() - $amount);