if ($command == "cc"){ if($player->getInventory()->getItem(Item::get(Item::STONE)) >= 10){ $player->getInventory()->removeItem(Item::get(Item::STONE, 0, 10)); $sender->sendMessage($tag."Success"); } } When I do /cc, it crash and says "Argument 1 passed to pocketmine\inventory\BaseInventory::getItem() must be of the type int, object given ~~~ So how can I solve this problem? I want to make it if I have more than 10 stones in my inventory and when i say /cc, remove 10 stones in my inventory.
Put this function into your plugin: https://forums.pmmp.io/threads/removing-items-from-players-invent-problem.8686/#post-67327 It allows you to remove a specific amount of items, even if they're spread around in the inventory. Then you can use this code: PHP: $item = Item::get(Item::STONE);$item->setCount(10);$inventory = $player->getInventory();if ($inventory->contains($item)) { removeItems($item, $inventory); $sender->sendMessage($tag . "Success");} else { $sender->sendMessage($tag . "You need " . $item->getCount() . " x " . $item->getName() . "!");}
I do what you said, but when I /cc, it crash and says "Call to undefined function ga\removeItems()" (EXCEPTION)~~ Here is the code 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; } } } } public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool { $command = $command->getName (); $player = $sender->getPlayer (); $name = $sender->getName (); $tag = "§l§b[ §l§fwow§l§b ] §f"; $a=array("cc"); $item = Item::get(Item::STONE); $item->setCount(8); $inventory = $player->getInventory(); if ($command==$a[0] && $inventory->contains($item)) { removeItems($item, $inventory); $sender->sendMessage($tag . "success"); } else { $sender->sendMessage($tag . "You need" . $item->getCount() . " x " . $item->getName() . "!"); } return true; } }
I edit to $this->removeItems() but it says TypeError: "Argument 2 passed to ga\GA::removeItems() must be an instance of ga\BaseInventory, instance of pocketmine\inventory\PlayerInventory given, called in~~~ 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; } } } } public function onCommand(CommandSender $sender, Command $command, string $label, array $args): bool { $command = $command->getName (); $player = $sender->getPlayer (); $name = $sender->getName (); $tag = "§l§b[ §l§fwow§l§b ] §f"; $a=array("cc"); $item = Item::get(Item::STONE); $item->setCount(8); $inventory = $player->getInventory(); if ($command==$a[0] && $inventory->contains($item)) { $this->removeItems($item, $inventory); $sender->sendMessage($tag . "Success"); } else { $sender->sendMessage($tag . "You Need " . $item->getCount() . " x " . $item->getName() . "!"); } return true; } }
You will need to add PHP: use pocketmine\inventory\BaseInventory; to your other imports. And please use php tags for pasting code (use the + button in the text editor to insert one)