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

Get Item inventoryTransactionevent

Comments in 'Plugin Development' started by Omar2s2, Feb 19, 2016.

  1. Omar2s2
    Offline

    Omar2s2 New Member

    Joined:
    Dec 23, 2015
    Posts:
    12
    Minecraft User:
    Pop
    How can get item in inventoryTransactionevent
    For chest
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    InventoryTransactionEvent can refer to transactions between two inventories. For example, if hoppers are implemented, sucking an item from a chest to a hopper is also a transaction.

    Therefore, first thing you need to do is to check if the event is a player-chest event:
    PHP:
    $transactions $InventoryTransactionEvent->getTransactions()->getTransactions();
    foreach(
    $transactions as $tr){
        if(
    $tr->getInventory() instanceof ChestInventory){
            
    $chestTran $tr;
        }elseif(
    $tr->getInventory() instanceof PlayerInventory){
            
    $playerTran $tr;
        }
    }
    if(!isset(
    $chestTran) or !isset($playerTran)) return;
    Now, to know if it is chest-to-player or player-to-chest, either check the player inventory increase/decrease or chest inventory increase/decrease:
    PHP:
    $chestToPlayer $chestTran->getTargetItem()->getCount() < $chestTran->getSourceItem()->getCount();
    Check the source item of the source transaction, or the target item of the target transaction, to find out the item type.
    Use the minus operator to find out how many items have been transacted.

    Note that if the transaction involves more than one slot (e.g. if the target slot is full), my method would malfunction. You would have to add up every transaction.
    thebigsmileXD likes this.
  3. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I am sorry for bumping this, but the solution sounds good. Could i even use transaction->getInventories()?
  4. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Typo in the first line
    $transactions = $InventoryTransactionEvent->getTransaction()->getTransactions(); //first transaction then transactionssssss
  5. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Yes you can

Share This Page

Advertisement