hey , so am making a plugin which uses InventoryTransaction but it doesn't work idk why am also using Muqsit example in ChestShop PHP: public function onTransaction(InventoryTransactionEvent $event){ $transactions = $event->getTransaction()->getTransactions(); $player = null; $chestinv = null; $action = null; foreach($transactions as $transaction){ if(($inv = $transaction->getInventory()) instanceof ChestInventory){ foreach($inv->getViewers() as $assumed){ if($assumed instanceof Player){ $player = $assumed; $chestinv = $inv; $action = $transaction; break 2; } } } } if(($player ?? $chestinv ?? $action) === null){ return; } /* * $player => Player interacting with the GUI. * $chestinv => The chest's inventory. * $action => BaseTransaction|Transaction|SimpleTransactionGroup */ $event->setCancelled(); $item = $action->getTargetItem(); if($item->getId() === 279){ $player->sendMessage("I wish this was working.. Wait it is!"); return; }
Ladies and Gentlemen, I present you with a magic event; an event that no one has ever heard of or used before. The EntityInventoryChangeEvent PHP: public function onItemPut(EntityInventoryChangeEvent $event){ $newItem = $event->getNewItem(); $oldItem = $event->getOldItem(); var_dump("New Item: ".$newItem->getName()); var_dump("Old Item: ".$oldItem->getName());}
Yeah. You can use InventoryTransaction and save the inventory in an array, then delete the element of the array on InventoryCloseEvent. But InventoryTransactionEvent will be much better in this case.
Code: 02.05 15:16:42 [Server] CRITICAL "Could not pass event 'pocketmine\event\entity\EntityInventoryChangeEvent' to 'Test v1': Call to a member function getNewItem() on integer on TB\Main 02.05 15:16:41 [Server] CRITICAL Error: "Call to a member function getNewItem() on integer" (EXCEPTION) in "/Test/src/TB/Main" at line 62
this code PHP: public function onTest(EntityInventoryChangeEvent $event){ $player = $event->getEntity(); $newItem = $event->getNewItem(); var_dump("New Item: ".$newItem->getName()); if($newItem === "TEST"){ $player->sendMessage("test"); return; } }
That's odd. It seems like the event is returning as an integer. You could try var_dumping $event to see what it is, then go from there.