I want to remove TNT from players when they have it inside their inventory What I have now is PHP: public function held(PlayerItemHeldEvent $event){ $player = $event->getPlayer(); $item = $player->getInventory()->getItemInHand()->getId(); if($item === 46){ $player->getInventory()->remove(Item::get($item)); } } and this only removes the item when player holds it
EntityInventoryChangeEvent. I don't know why this event is soo underrated. People prefer using DataPacket* events for handling changing an item's name when a player receives an item while they could be using this event. PHP: /** @var EntityInventoryChangeEvent $event */$player = $event->getEntity();if($player instanceof Player){ if($event->getNewItem() === Item::TNT){ $event->setCancelled();//or $event->setNewItem(Item::get(Item::AIR)); }}
Exactly, that's why EntityInventoryChangeEvent is suitable here. It is called when an item from player's inventory is subtracted, added, removed, dropped etccitation needed