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

Inventory menu

Discussion in 'Development' started by #A6543, Feb 8, 2017.

  1. #A6543

    #A6543 Zombie

    Messages:
    267
    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
    }
     
  2. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I'm wondering this also
     
  3. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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.
     
  4. #A6543

    #A6543 Zombie

    Messages:
    267
    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..
     
  5. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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());
      }
    }
     
  6. Aviv

    Aviv Baby Zombie

    Messages:
    156
    btw its a nice way to start InventoryTransactionEvent (just saying) :p
     
  7. Aviv

    Aviv Baby Zombie

    Messages:
    156
    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.
     
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    My code for my inventory shop
    PHP:
    public function InventoryTransactionEvent(InventoryTransactionEvent $event){
           
    $invs $event->getTransaction()->getInventories();
           if(
    count($invs) > && 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;
                               }
                   }
               }
           }
       }
     
    Last edited: Feb 12, 2017
  9. #A6543

    #A6543 Zombie

    Messages:
    267
    If I place the item from the chest back in the chest, it works (win10). How can I fix that?
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Send your code.
     
    InspectorGadget likes this.
  11. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Coming in hot? xD
     
    Muqsit likes this.
  12. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You are not using pocketmine right? I tested my code on pocketmine it works fine
     
    Muqsit likes this.
  13. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    How can he not?
     
  14. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    I have tested my code and it works fine using pocketmine, how can it not work if he is using pocketmine?
     
  15. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    He meant that..
     
  16. #A6543

    #A6543 Zombie

    Messages:
    267
    I mean my code
     
  17. #A6543

    #A6543 Zombie

    Messages:
    267
    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?
     
  18. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Oh my bad, have fun debugging then
     
  19. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
  20. #A6543

    #A6543 Zombie

    Messages:
    267
    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
     
  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.