I know how to remove all items in inventory; $player->getInventory()->clearAll(); But how can I remove all items besides one? For example. I don't want the compass item removed in my cursor inventory, but I want all other items removed.
I think the simplest is clear all item and add back the item your want Example, if u want the dirt block back after clear item, u can do this PHP: //Clear all item$player->getInventory()->clearAll();// The slot u want to add back$slot = "1";//Item$adirtblock = Item::get(0, 0, 1);$adirtblock->setCustomName("Special Name");//Add Item back$player->getInventory()->addItem($slot, $adirtblock);
Unfortunately this won't work for what I'm doing. The item I don't want deleted is a compass that sends you to spawn when you right click. Adding it back will just give you a regular compass. I've tried looking into removeItem by adding an array to it; PHP: $chestItems[0] = [ 256, 257, 258, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279 ];$player->getInventory()->removeItem($chestItems[0], 0, 1); But I have trouble at making the arrays work. Any ideas? I'm probably not coding the array right.
PHP: /** @var Player $player */foreach($player->getInventory()->getContents() as $item){ if($item->getId() === Item::COMPASS) continue; // Or anything that differentiates the compass from the rest of the items $player->getInventory()->removeItem($item);}
Thank you KielKing, I will use this as a learning experience. I'm quite new to coding, again thanks for the help.