BoxOfDevs/CommandShop

Item counter is severely broken and no one ever noticed

Closed this issue ยท 3 comments

Brief description

Unfairly, too many items get removed.

Steps to reproduce

  1. Try removing one item when you have multiple stacks of only one item (e.g. swords)
  2. All of them get removed

Expected result

Only one gets removed

Actual result

All of them get removed

The reason for this problem is this function by Ad5001 that's supposed to remove the correct amount of items, but apparently no one ever tested it with non-stacking items lol.

Possible fix (not yet tested):

private function removeItems(Item $item, BaseInventory $inventory){
     $checkDamage = !$item->hasAnyDamageValue();
     $checkTags = $item->hasCompoundTag();
     $checkCount = $item->getCount() === null ? false : true;
     $count = $item->getCount();
     foreach($inventory->getContents() as $index => $i){
          if($item->equals($i, $checkDamage, $checkTags)){
               if($checkCount && $i->getCount() > $count) {
                    $i->setCount($i->getCount() - $count);
                    $inventory->setItem($index, $i);
                    return;
               } elseif($checkCount && $i->getCount() < $count) {
                    $count -= $i->getCount();
                    $inventory->clear($index);
               } else {
                    $inventory->clear($index);
                    return;
               }
          }
     }
}

Thanks to @MCPEATECH for finding this out.

Fix Works ๐Ÿ‘

@MCPEATECH Yay, thanks for testing

Sorry for the late reply, I use this code on servers and it works fine.