Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved Problem with InventoryTransactionEvent

Comments in 'Plugin Development' started by AppPhil, Oct 13, 2015.

  1. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    Hey there,

    I got this code which gets called when a player tries to get an item from a chest:
    PHP:
    // $otherInventory is the chest inventory

    // I check if the chest inventory is the same like another one
    if($otherInventory->getContents() == $this->getShopInventorys()->getStartInvContents()) {
        
    // then I cancel the event, so that the player isn't getting the item
        
    $event->setCancelled(true);
        
    // then I think I have to send the contents so that the item from the chest isn't shown in players inventory
        
    $player->getInventory()->sendContents($player);
        
    // then I do a foreach to check all the transactions
        
    foreach($event->getTransaction()->getTransactions() as $t) {
            
    // then I check if the player gets the item from the 0. slot
            
    if($t->getSlot() === 0) {
                
    // then I change the inventory of the chest
                
    $otherInventory->setContents($this->getShopInventorys()->getBlockInvContents());
                 
    $player->getInventory()->sendContents($player);
            }
            
    // other if($t->getSlot() === 1,2,3, ....)
        // end from foreach
        
    }
    // end from if
    }
    I want that the player can click on the items and then the chest inventory changes.

    Now the problem is, that often when I get the item from one slot it shows the inventory it should show when clicking on another slot. So as an example: I click on slot 0 but it shows the inventory it should show when I click on slot 2 or something else than 0.
    I don't know how to fix that. Maybe someone can help. Thanks in advance.
  2. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    Because there are two transactions
    1. From chest
    2. To player inventory

    So slot is probably of player inventory
    AppPhil likes this.
  3. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    So try this (ore something like this)
    PHP:
    if($transaction->getInventory() instanceof ChestInventory) {
        
    //code
    }
    AppPhil likes this.
  4. AppPhil
    Offline

    AppPhil Active Member

    Joined:
    Oct 3, 2015
    Posts:
    54
    Thanks! That was the problem, now it works fine.

Share This Page

Advertisement