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

How remove item from player inventory?

Discussion in 'Development' started by DanielYTK, Feb 19, 2017.

  1. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    I don't know how do this, i tested this:
    PHP:
    $player->getInventory()->remove(52); #don't work for me
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    This doesn't work because the parameter for remove() must be an instance of an Item. Try defining an item with Item::get(id, damage, count). Here's an example:

    PHP:
    /*Assuming $player is an instance of Player */
     
    $player->getInventory()->remove(Item::get(Item::COBBLESTONE01)); 
     
  3. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    This also does not work, what I really want to do is:

    When the player puts item 52 on the floor I want to cancel the event and remove the inventory item from it!
     
  4. xZeroMCPE

    xZeroMCPE Witch

    Messages:
    67
    GitHub:
    xZeroMCPE
    Detects when player drops the item, and cancels it. If its itemID 52..

    Code:
        public function onDrop(PlayerDropItemEvent $event){
    
            $player = $event->getPlayer();
    
            if($event->getItem()->getId() == 52){ // Checks if the itemId is 52
                $event->setCancelled(); // Cancels it if the condition above is true
                $player->getInventory()->removeItem(Item::get(52)); // Removes the item from the player inventory after cancelling
            }
        }
        /*
         *  You might aswell do this too:
         */
       
        public function onJoin(PlayerJoinEvent $event){
           
            $player = $event->getPlayer();
           
            if($player->getInventory()->contains(Item::get(52))){ // Checks if player inventory contains itemID 52
                $player->getInventory()->removeItem(Item::get(52)); //Removes the item if the condition above is true
            }
        }
     
    corytortoise likes this.
  5. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Eu não preciso disso, olhe
    PHP:
    if($block == 52){
                if(
    $player->isCreative()){
                    return 
    true;
                }
                if(
    $this->spawner[$player->getName()]){
                    
    $inv $player->getInventory();
                    
    $enable mt_rand(112);
                    
    $comandos = [
                        
    "doarspawner zombie ".$player->getName(),
                        
    "doarspawner skeleton ".$player->getName(),
                        
    "doarspawner spider ".$player->getName(),
                        
    "doarspawner pigman ".$player->getName(),
                        
    "doarspawner iron_golem ".$player->getName(),
                        
    "doarspawner blaze ".$player->getName(),
                        
    "doarspawner pig ".$player->getName(),
                        
    "doarspawner cow ".$player->getName(),
                        
    "doarspawner slime ".$player->getName(),
                        
    "doarspawner squid ".$player->getName(),
                        
    Item::get(466032),
                    ];
                    switch(
    $enable){
                        case 
    1:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[0]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de zombie");
                            break;
                        case 
    2:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[1]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de skeleton");
                            break;
                        case 
    3:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[2]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de aranha");
                            break;
                        case 
    4:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[3]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de pigman");
                            break;
                        case 
    5:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[4]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de §6IRON GOLEM");
                            
    $this->getServer()->broadcastMessage("§6§lCAIXAS§r§f O jogador ".$player->getName()." abriu uma §6CAIXA SPAWNER§f e ganhou um §6SPAWNER RARO§f  (§6IRON-GOLEM§f)");
                            break;
                        case 
    6:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[5]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de blaze");
                            break;
                        case 
    7:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[6]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de porco");
                            break;
                        case 
    8:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[7]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de vaca");
                            
    $this->getServer()->broadcastMessage("§6§lCAIXAS§r§f O jogador ".$player->getName()." abriu uma §6CAIXA SPAWNER§f e ganhou um §6SPAWNER ULTRA-RARO§f (§6Vaca§f)");
                            break;
                        case 
    9:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[8]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de §6SLIME");
                            break;
                        case 
    10:
                            
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), $comandos[9]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou um spawner de lula");
                            break;
                        case 
    11:
                            
    $inv->addItem($comandos[10]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§r§f você ganhou §632§f Maças douradas encantadas");
                            
    $this->getServer()->broadcastMessage("§l§6CAIXAS§r§f O jogador ".$player->getName()." abriu uma §6CAIXA SPAWNER§f e ganhou §6ITEM ULTRA-RARO§f (§632 Maças dourada encantada§f)");
                            break;
                        case 
    12:
                            
    $inv->addItem($comandos[11]);
                            
    $player->sendMessage("§6§lCAIXA-SPAWNER§f você ganhou §632§f Blocos de diamante");
                            
    $this->getServer()->broadcastMessage("§l§6CAIXAS§r§f O jogador ".$player->getName()." abriu uma §6CAIXA SPAWNER§f e ganhou §6ITEM RARO§f (§632 Blocos de diamante§f)");
                    }
                    
    $ev->setCancelled(true);
                    unset(
    $this->spawner[$player->getName()]);
                }
            }
     [/ 
    php]
     
  6. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    After the player puts the block on the ground, will execute my script and soon after the event is canceled! I just need to remove the item from the player's inventory after this happens.
     
  7. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    What is $block? What context is this in(What event is being called)?


    We have already said that $event->getPlayer()->getInventory()->remove(/*Item here*/) is the correct function. Let us know what you have tested if it didn't work for you.
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    if($this->spawner[$player->getName()]){
    Do you mean...
    PHP:
    if(isset($this->spawner[$player->getName()])){
     
  9. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    PHP:
    $block $ev->getBlock()->getId(); //  $ev is pocketmine\event\block\BlockPlaceEvent;
     
  10. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    So, I did some tests here and when a item hasCustomName () the item is not removed from the inventory, plus if the name is default, it is removed, how can I remove the item that hasCustomName ()?
     
  11. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    look a completed event:
    PHP:
        public function aoColocar(BlockPlaceEvent $ev){
            
    $player $ev->getPlayer();
            
    $block $ev->getBlock()->getId();
            if(
    $block == 146){
                if(
    $block->getCustomName() == "§6Caixa Básica"){
                    if(
    $player->isCreative()){
                        return 
    true;
                    }
                    
    $inv $player->getInventory();
                    
    $enable mt_rand(18);
                    
    $itens = [
                      
    Item::get(42016),
                      
    Item::get(41016),
                      
    Item::get(57016),
                      
    Item::get(31001),
                     
    Item::get(31101),
                     
    Item::get(27801),
                     
    Item::get(388010),
                     
    Item::get(7032),
                     
    Item::get(49032),
                    ];
                    switch(
    $enable){
                       case 
    1:
                      
    $inv->addItem($itens[0]);
                      
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §616§f blocos de ferro");
                           break;
                       case 
    2:
                      
    $inv->addItem($itens[1]);
                      
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §616§f blocos de ouro");
                           break;
                       case 
    3:
                      
    $inv->addItem($itens[2]);
                      
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §616§f blocos de dima");
                      
    $this->getServer()->broadcastMessage("§6§lCAIXAS§r§f O jogador ".$player->getName()." abriu uma §6CAIXA BASICA§f e ganhou §6ITEM RARO§f (16 blocos de diamante)");
                           break;
                       case 
    4:
                      
    $protection Enchantment::getEnchantment(0);
                      
    $protection->setLevel(3);
                      
    $itens[3]->addEnchantment($protection);
                      
    $itens[4]->addEnchantment($protection);
                      
    $inv->addItem($itens[3]);
                      
    $inv->addItem($itens[4]);
                      
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §61§f peitoral e §61§f capacete §6P3");
                           break;
                       case 
    5:
                      
    $eficiencia Enchantment::getEnchantment(15);
                      
    $eficiencia->setLevel(3);
                      
    $inquebravel Enchantment::getEnchantment(17);
                      
    $inquebravel->setLevel(2);
                      
    $itens[5]->addEnchantment($inquebravel);
                      
    $itens[5]->addEnchantment($eficiencia);
                      
    $inv->addItem($itens[5]);
                      
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §61§f picareta de §6diamante§f (§6Eficiencia: 3 / Inquebravel: 2§f)");
                           break;
                       case 
    6:
                      
    $inv->addItem($itens[6]);
                      
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §610§f esmeraldas");
                           break;
                        case 
    7:
                        
    $inv->addItem($itens[7]);
                        
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §632§f bedrocks");
                        break;
                        case 
    8:
                        
    $inv->addItem($itens[8]);
                        
    $player->sendMessage("§6§lCAIXA-BASICA§r§f você ganhou §632§f obsidians");
                        break;
                    }
                    
    $ev->setCancelled(true);
                    
    $inv->remove(Item::get(Item::$block));
                }
        }
    }
     
    uses:
    PHP:
    use pocketmine\Player;
    use 
    pocketmine\inventory;
    use 
    pocketmine\inventory\PlayerInventory;
    use 
    pocketmine\inventory\BaseInventory;

    use 
    pocketmine\item\Item;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\event\block\BlockPlaceEvent;
    use 
    pocketmine\block\Block;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\ConsoleCommandSender;

    use 
    onebone\economyapi\EconomyAPI;

    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\item\enchantment\EnchantmentEntry;
    use 
    pocketmine\item\enchantment\EnchantmenList;
     
     
  12. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    I got to solve for me this worked:
    PHP:
     $player->getInventory()->setItemInHand(52); #firsh set item with id 52 in the player hand
    $air Item::get(0,0,1); #Create air
    $player->getInventory()->setItemInHand($air); #set air in the hand of player
     
     
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That isn't the solution. That is a hack.
    PHP:
    $player->getInventory()->setItemInHand(Item::get(0));
     
  14. SkySeven

    SkySeven Baby Zombie

    Messages:
    145
    GitHub:
    SkySevenMC
    How to remove a specific number of items fro player inventory please ?
     
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    One question per thread please. Also, that topic has already been discussed.
     
    SkySeven likes this.
  16. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    more like no hijacking threads plzzz
     
    Muqsit 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.