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

inventory help

Discussion in 'Development' started by Teamblocket, May 2, 2017.

  1. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    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;
            }
     
  2. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
  3. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Try to use $ Item = $ action-> getSourceItem ();
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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());
    }
     
  5. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    This can be used on transaction inventory ? .-.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Yeah, but I don't know if it lacks things that InventoryTransactionEvent doesn't.
     
    Last edited: May 2, 2017
  7. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    Apparently, we can't get the inventory where are transacted items :/
     
  8. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
    Last edited: May 2, 2017
  10. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
  11. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    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
     
  12. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    What code produces this error?
     
    Muqsit likes this.
  13. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    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;
              }
       }
     
  14. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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.
     
  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.