$inventory = $event->getInventory(); $id = $inventory->getItem()->getId(); if($inventory instanceof ChestInventory{ if($id == /*ItemId I use 2*/1){ //Your Code Here If this dont work sorry for bad post i dont tested it
There is no getInventory() method in InventoryTransactionEvent. However, it does seem that you can get a TransactionGroup. Conveniently, the only class implementing the interface is SimpleTransactionGroup. From there, it would be a simple matter to call getTransactions() and check sourceItem.
Implied from the last link anyway ¯\_(ツ)_/¯ What do you mean? If you want the transaction to happen, simply don't cancel the event.
You need to cancel the event or It will just keep the item in the players inventory instead of going back into the chest inventory.
Right, as I said, the transaction would happen unless you cancel the event. What was your previous question?
I should have been more specific on what I want the plugin to do. What I want to happen is when a player takes a item from the custom inventory, I want it to send them a message and give them a item. But every time the player takes and item out if just cancels the event but wont to anything.
Of course... you have to add in your own code at the line $event->setCancelled(), you cant just use other's code and expect it to do what you want...
Ok so now I know that it isn't executing anything. I think it is because I cancels too early for it to execute anything, so what could I do to fix this?
Cancelling an event does not stop the code from executing, it just prevents the event's result from happening Check your if statements
This is the code that I am using rn PHP: public function onTransaction(InventoryTransactionEvent $event) { $transactions = $event->getTransaction ()->getTransactions (); $player = null; $hopperinv = null; $action = null; foreach ( $transactions as $transaction ) { if (($inv = $transaction->getInventory ()) instanceof HopperInventory) { foreach ( $inv->getViewers () as $assumed ) if ($assumed instanceof Player) { $player = $assumed; $chestinv = $inv; break; } } $action = $transaction; } if ($hopperinv === null) return; $event->setCancelled (); $item = $action->getTargetItem (); if ($item->getName () == "Test") { $player->sendMessage ( "Test" ); } }
You're just blindly copying code from ChestShop. There is a chance that $action variable might not be set correctly, but at the same time, $chest and $player are.