BoxOfDevs / CommandShop

Players have to pay items or money to use specific commands! A PocketMine plugin.
Other
31 stars 10 forks source link

Item counter is severely broken and no one ever noticed #17

Closed HimbeersaftLP closed 4 years ago

HimbeersaftLP commented 5 years ago

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.

aircwo commented 4 years ago

Fix Works 👍

HimbeersaftLP commented 4 years ago

@MCPEATECH Yay, thanks for testing

aircwo commented 4 years ago

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