I don't know how do this, i tested this: PHP: $player->getInventory()->remove(52); #don't work for me
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::COBBLESTONE, 0, 1));
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!
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 } }
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(1, 12); $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(466, 0, 32), ]; 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]
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.
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.
PHP: if($this->spawner[$player->getName()]){ Do you mean... PHP: if(isset($this->spawner[$player->getName()])){
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 ()?
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(1, 8); $itens = [ Item::get(42, 0, 16), Item::get(41, 0, 16), Item::get(57, 0, 16), Item::get(310, 0, 1), Item::get(311, 0, 1), Item::get(278, 0, 1), Item::get(388, 0, 10), Item::get(7, 0, 32), Item::get(49, 0, 32), ]; 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;
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