Hey there, I want to remove 10 bricks from a player inventory. I got this code: PHP: if($player->getInventory()->contains(new \pocketmine\item\Brick(0, 10))) { $player->getInventory()->remove(new \pocketmine\item\Brick(0, 10)); // more...} It checks if the player has 10 bricks or more and that works fine. If the player has 10 or more it runs the next line, but there I got a problem. The plugin removes every brick of the stack and not only 10. How can I only remove 10 bricks from the inventory? Thanks in advance.
PHP: if($player->getInventory()->contains(new \pocketmine\item\Brick(0,10)){ for($i=0;$i<36;$i++){ $item = $player->getInventory()->getItem($i); if($item instanceof \pocketmine\item\Brick){ if($item->getCount() >= 10){ $item->setCount($item->getCount() - 10); $player->getInventory()->setItem($i, $item); $player->getInventory()->sendContents($player); break; } } }} I wrote this on my phone so sorry for syntax errors.
Actually, you should try to use this. Not sure if it will work though. PHP: if($player->getInventory()->contains(new \pocketmine\item\Brick(0,10)){ $removed = 0; for($i=0;$i<36;$i++){ $item = $player->getInventory()->getItem($i); if($item instanceof \pocketmine\item\Brick){ if($item->getCount() >= 10){ $item->setCount($item->getCount() - 10); $player->getInventory()->setItem($i, $item); $player->getInventory()->sendContents($player); break; }else{ if($item->getCount() + $removed >= 10){ $item->setCount($item->getCount() - (10 - $removed)); $player->getInventory()->setItem($i, $item); $player->getInventory()->sendContents($player); break; }else{ $removed += $item->getCount(); $item->setCount(0); $player->getInventory()->setItem($i, $item); } } } }}