1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Plugin Help

Discussion in 'Development' started by CryptoKey, Mar 11, 2019.

  1. CryptoKey

    CryptoKey Spider

    Messages:
    6
    GitHub:
    cryptokey98
    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.
     
  2. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    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(001);
    $adirtblock->setCustomName("Special Name");
    //Add Item back
    $player->getInventory()->addItem($slot$adirtblock);
     
  3. CryptoKey

    CryptoKey Spider

    Messages:
    6
    GitHub:
    cryptokey98
    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] = [ 256257258267268269270271272273274275276277278279 ];

    $player->getInventory()->removeItem($chestItems[0], 01); 
    But I have trouble at making the arrays work. Any ideas? I'm probably not coding the array right.
     
  4. CryptoKey

    CryptoKey Spider

    Messages:
    6
    GitHub:
    cryptokey98
    I see, maybe the first Param of removeItem is the slot number?
     
  5. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    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);
    }
     
    Diduhless and CryptoKey like this.
  6. CryptoKey

    CryptoKey Spider

    Messages:
    6
    GitHub:
    cryptokey98
    Thank you KielKing, I will use this as a learning experience. I'm quite new to coding, again thanks for the help.
     
  7. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    no problem
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.