Hi. I tried to make a menu inside a chestinventory. But it doesn't work very well (but it works). How can I make it better? 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; break; } } $action = $transaction; } if($chestinv === null) return; $event->setCancelled(); $item = $action->getTargetItem();if($item->getName() == "Site 1") {$chestinv->clearAll();//Site 1}
same problem. The event runs before the item is taken from the chest & before the item is in the player's inventory. the problem is that it runs before the item is in the player's inventory so that cancelling it wont effect like when its still in the chest inventory. & if youre using w10 you must put the item in the inventory to get the event going. that sucks. Tried getting only the chest. still same problem.
Do u know if there is a better way? I need to spamm the item to make the event work :/ * If I put the item from the players inventory to the chest it works..
just debug the whole way 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 Inventory){ foreach($inv->getViewers() as $assumed) if($assumed instanceof Player) { $player = $assumed; $chestinv = $inv; break; } } $action = $transaction; } echo("debug "); if($chestinv === null) return; if(!$chestinv->getHolder() instanceof ChestBlock) return; $item = $action->getTargetItem(); echo("debug: ".$item->getName()); }}
seems like it only runs after you wait some seconds then run the transaction. right? Message to devs: maybe alittle investigment on this feature to plugin developers needed.
My code for my inventory shop PHP: public function InventoryTransactionEvent(InventoryTransactionEvent $event){ $invs = $event->getTransaction()->getInventories(); if(count($invs) > 0 && count($invs) !== 2){ $inv = $invs[key($invs)]; if($inv->getHolder() instanceof Chest){ $player = $inv->getViewers()[key($inv->getViewers())]; $chest = $inv->getHolder(); foreach($event->getTransaction()->getTransactions() as $transaction){ if($transaction->getInventory()->getHolder() instanceof Chest){ $event->setCancelled(); $sourceItem = $transaction->getInventory()->getItem($transaction->getSlot()); #do smth Chest $chest, Player $player, Item $sourceItem break; } } } }elseif(count($invs) === 2){ $playerInvCount = 0; foreach($event->getTransaction()->getInventories() as $inv){ if($inv->getHolder() instanceof Player){ $player = $inv->getHolder(); $playerInvCount++; } if($inv->getHolder() instanceof Chest) $chest = $inv->getHolder(); } if(!isset($player)){ #player not found return; } if(!isset($chest)){ #chest not found if($playerInvCount > 1){ var_dump("Prevented bug for ".$player->getName()); $event->setCancelled(); } return; } foreach($event->getTransaction()->getInventories() as $inv){ if($inv->getHolder() instanceof Chest){ $event->setCancelled(); foreach($event->getTransaction()->getTransactions() as $trans){ if(method_exists($trans, 'getSourceItem')) $sourceItem = $trans->getSourceItem();//0.14 else $sourceItem = $trans->getInventory()->getItem($trans->getSlot());//0.15 #do smth Chest $chest, Player $player, Item $sourceItem break; } } } } }
I have tested my code and it works fine using pocketmine, how can it not work if he is using pocketmine?
If I don't use if($inv instanceof ChestInventory) { }, it works fine in the players inv. But how can I get it work in the chest?
Isn't this where you got the code from? If the plugin works, then so should your code. Do tell us what your aim is. We don't know whether you are trying to cancel... Transaction: PlayerInventory -> ChestInventory Or Transaction: ChestInventory -> PlayerInventory Also, hope you're using pmmp. Their InventoryTransactionEvent works flawlessly. I'm guessing you're using the gspoon so that's why it wouldn't work.
I use pmmp... And the mistake is in my code! I want to change the items in the chest, If the players selects a block with a special name