1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

InventoryTransactionEvent

Discussion in 'Development' started by Karanpatel567, Oct 7, 2017.

  1. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Hey all, I want help please. I want to to make it so when I pick up a item from a virtual inventory (meaning I created a tile) it closes the window and sends a message. I have tried everything looked at other people plugins but never actually worked. Please guys can you provide me some code?
     
  2. themestl

    themestl Spider Jockey

    Messages:
    39
    GitHub:
    themestl
    Yes, I can.
    PHP:
        public function onInventoryTransaction(InventoryTransactionEvent $event){
            
    $chest null;
            
    $player null;
            
    $oldItemPlayer null;
            
    $newItemPlayer null;
            
    $oldItemChest null;
            
    $newItemChest null;
            
    $chestSlot null;
            
    $playerSlot null;
            
    $viewers = [];
            foreach(
    $event->getTransaction()->getInventories() as $inventory){
                if(
    $inventory->getHolder() instanceof Player){
                    
    $player $inventory->getHolder();
                }else{
                    
    $chest $inventory->getHolder();
                    
    $viewers $inventory->getViewers();
                }
            }
            foreach(
    $event->getTransaction()->getAction() as $transaction){
                if(
    $oldItemChest === null and $newItemChest === null){
                    
    $oldItemChest $transaction->getSourceItem();
                    
    $newItemChest $transaction->getTargetItem();
                }else{
                    
    $oldItemPlayer $transaction->getSourceItem();
                    
    $newItemPlayer $transaction->getTargetItem();
                }
                if(
    $chestSlot !== null){
                    
    $playerSlot $transaction->getSlot();
                }else{
                    
    $chestSlot $transaction->getSlot();
                }
            }
            if(
    $player !== null and $chest !== null and $oldItemPlayer !== null and $newItemPlayer !== null and $oldItemChest !== null and $newItemChest !== null and $chestSlot !== null and $playerSlot !== null){
                
    $action $oldItemChest->getCount() > $newItemChest->getCount();//true if player take an item
                
    $diff $action $oldItemChest->getCount() - $newItemChest->getCount() : $newItemChest->getCount() - $oldItemChest->getCount();
                
    //TODO
            
    }
        }
     
    Last edited: Oct 7, 2017
    jasonwynn10 likes this.
  3. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    You have to change
    PHP:
    $event->getTransaction()->getTransactions()
    to
    PHP:
    $event->getTransaction()->getActions()
    There was a API change
     
    jasonwynn10 likes this.
  4. themestl

    themestl Spider Jockey

    Messages:
    39
    GitHub:
    themestl
    Thanks, I'm correct it. I just use only packets and not know about it. But update is really useless, many plugins who authors inactive won't work. And users of this plugins will spaming in this forum that plugins aren't works. Facepalm, PMMP:facepalm:
     
    jasonwynn10 likes this.
  5. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    So, what do i do so when i pick up the bucket it closes the window and just send a message saying "hello" how would i do this code?
     
  6. themestl

    themestl Spider Jockey

    Messages:
    39
    GitHub:
    themestl
    if($oldItemChest->getId() === Item::BUCKET){
    $player->closeWindow($chest->getInventory());
    $player->sendMessage("hello");
    }
     
    SalmonDE likes this.
  7. McpeBooster

    McpeBooster Baby Zombie

    Messages:
    190
    GitHub:
    mcpebooster
    Thats wrong...

    it should be
    PHP:
    if($oldItemChest->getId() === 325){
    $player->closeWindow($chest->getInventory());
    $player->sendMessage("hello");
    }
     
  8. themestl

    themestl Spider Jockey

    Messages:
    39
    GitHub:
    themestl
    Why it's wrong? It's also work
     
    SalmonDE likes this.
  9. SalmonDE

    SalmonDE Zombie Pigman

    Messages:
    739
    GitHub:
    SalmonDE
    Both ways are right (well they are mostly the same anyway). However @themestl 's way seems better. It tells the reader that the item we're talking about is a bucket.
     
    Last edited: Oct 8, 2017
    jasonwynn10 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.