Use InventoryTransactionEvent. When it gets cancelled, the Item doesn't get moved so you won't need to remove the Item once you've already cancelled the event. PHP: /** @var InventoryTransactionEvent $event */foreach($event->getActions() as $action){ if($action instanceof SlotChangeAction){ $inventory = $action->getInventory(); if($inventory instanceof ChestInventory){ $theItemThatPlayerClicked = $action->getSourceItem(); //^that is the item that the player clicked. //cancelling the event will disallow player from moving the item anywhere. $event->setCancelled(); break; } }}
I want to prevent specific item from being put in the chest Can you give an example function please? @Muqsit
$theItemThatPlayerClicked is an instance of the Item class. You can use something like this to prevent player from transferring dirt to their inventory. PHP: if($theItemThatPlayerClicked->getId() === Item::DIRT){ $event->setCancelled(); break;}
Here is my code but its not working PHP: public function onInventoryTransaction(InventoryTransactionEvent $event) { $player = $event->getTransaction()->getSource(); foreach($event->getTransaction()->getActions() as $action){ if($action instanceof SlotChangeAction){ $inventory = $action->getInventory(); if($inventory instanceof ChestInventory){ $theItemThatPlayerClicked = $action->getSourceItem(); if($theItemThatPlayerClickedget->getId() == 2){ $event->setCancelled(); break; } } } } }