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

Solved Removing item

Discussion in 'Development' started by Teamblocket, Apr 9, 2017.

  1. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    how can i remove a item with a custom name?
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Remove it from what? A player inventory?
     
  3. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
  4. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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);
      }
     }
     
    WhoAreMe likes this.
  5. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
  6. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    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
     
  7. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    09.04 10:07:24 [Server] INFO Notice: Undefined property: pocketmine\Player::$getInventory in /plugins/EnchantSpins/src/TeamB/main.php on line 75
     
  8. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    Add to your uses

    PHP:
    use pocketmine\inventory\Inventory;
    If you haven't already...
    Second error: why have you added an $ before getInventory()?
     
  9. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    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?
     
  10. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    PHP:
    $player->getInventory()->removeItem(Item::get(<item_id>, 01);
    // 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
    o_O
     
  11. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    ik how to remove items but i want to remove a item with a custom name.
     
  12. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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); 
     
    Zuruki likes this.
  13. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    that didn't work :(
     
  14. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Can you send me the entire code that you used? Are there any errors? Does it produce any behavior at all?
     
  15. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    PHP:
            if ($item === 340 and $itemn === "§r§f§l§bCommon §r§f§cBook"){
                   
    $random $names[mt_rand(0count($names) - 1)];
                   
    $item Item::get(40301);
                   
    $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);
                      }
                 }
                 } 
            }
        
     
  16. Primus

    Primus Zombie Pigman

    Messages:
    749
    Take a look how Economy(Shop/Sell) does this.
     
    Zuruki likes this.
  17. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    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
     
  18. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    EconomySell has this feature, but not with custom item names unfortunately @Primus
     
  19. Aviv

    Aviv Baby Zombie

    Messages:
    156
    PHP:
    foreach($player->getInventory()->getContents() as $slot=>$item) {
       if(
    $item->getCustomName() === "Name") {
         
    $item->setCount($item->getCount() - 1);
         
    $player->getInventory()->setItem($slot$item);
       }
     }
     
  20. Aviv

    Aviv Baby Zombie

    Messages:
    156
    You can also use Item::deepEquals to check, its easier
     
    Primus likes this.
  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.