You can try something like this: PHP: /** @var Player $player */ foreach($player->getInventory->getContents() as $item) { if($item->getCustomName() === "Custom Name") { $player->getInventory()->removeItem($item); } }
9.04 10:07:24 [Server] Server thread/CRITICAL Error: "Call to a member function getContents() on null" (EXCEPTION) in "/EnchantSpin/src/TeamB/main" at line 75
09.04 10:07:24 [Server] INFO Notice: Undefined property: pocketmine\Player::$getInventory in /plugins/EnchantSpins/src/TeamB/main.php on line 75
Add to your uses PHP: use pocketmine\inventory\Inventory; If you haven't already... Second error: why have you added an $ before getInventory()?
nvm i fixed the problem! thx<3 one thing.. when it removes the item with the custom name it removes the entire stack how can i make it remove only one from the stack?
PHP: $player->getInventory()->removeItem(Item::get(<item_id>, 0, 1);// item_id is replaced with ID of item obviously. This would be for normal items. I don't think it's possible with custom named ones though. I'm really not sure
After you have determined that the inventory contains the item, you could try this: PHP: /** @var Item $item */ $item->setCount(1); $player->getInventory()->removeItem($item);
Can you send me the entire code that you used? Are there any errors? Does it produce any behavior at all?
PHP: if ($item === 340 and $itemn === "§r§f§l§bCommon §r§f§cBook"){ $random = $names[mt_rand(0, count($names) - 1)]; $item = Item::get(403, 0, 1); $item->setCustomName($random); $player->getInventory()->addItem($item); foreach($player->getInventory()->getContents() as $item) { if($item->getId() === 340 and $item->getCustomName() === "§r§f§l§bCommon §r§f§cBook") { $item->setCount(1); $player->getInventory()->removeItem($item); } } } }
that doesn't help , my problem is when the player right clicks the book it removes the entire stack i only want one to get removed
PHP: foreach($player->getInventory()->getContents() as $slot=>$item) { if($item->getCustomName() === "Name") { $item->setCount($item->getCount() - 1); $player->getInventory()->setItem($slot, $item); } }