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

Getting an Item

Discussion in 'Development' started by RoyalMCPE, Jan 26, 2017.

  1. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    How would I be able to check the item that the player selects in the InventoryTransactionEvent?
     
  2. GreWs

    GreWs Creeper

    Messages:
    5
    $inventory = $event->getInventory();
    $id = $inventory->getItem()->getId();
    if($inventory instanceof ChestInventory{
    if($id == /*ItemId I use 2*/1){
    //Your Code Here

    If this dont work sorry for bad post i dont tested it
     
  3. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    It just spams the console.
     
  4. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Or you can refer to my plugin and see how I did it there.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
  7. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    I was just refered to your plugin the event gets cancelled but I wont execute what I want it to
     
  8. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    Implied from the last link anyway ¯\_(ツ)_/¯
    What do you mean? If you want the transaction to happen, simply don't cancel the event.
     
  9. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    You need to cancel the event or It will just keep the item in the players inventory instead of going back into the chest inventory.
     
  10. KnownUnown

    KnownUnown Spider Jockey Poggit Reviewer

    Messages:
    47
    GitHub:
    knownunown
    Right, as I said, the transaction would happen unless you cancel the event. What was your previous question?
     
  11. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    I'm not cancelling the event now and it still isn't doing anything.
     
  12. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    I should have been more specific on what I want the plugin to do.

    What I want to happen is when a player takes a item from the custom inventory, I want it to send them a message and give them a item. But every time the player takes and item out if just cancels the event but wont to anything.
     
  13. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Of course... you have to add in your own code at the line $event->setCancelled(), you cant just use other's code and expect it to do what you want...
     
  14. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    I have added my own code to it, but the it just cancels the event and doesn't execute it.
     
  15. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Did you try do put a var_dump there and see if it is executed?
     
  16. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    Ok so now I know that it isn't executing anything. I think it is because I cancels too early for it to execute anything, so what could I do to fix this?
     
  17. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Cancelling an event does not stop the code from executing, it just prevents the event's result from happening
    Check your if statements
     
  18. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    This is the code that I am using rn

    PHP:
    public function onTransaction(InventoryTransactionEvent $event) {
            
    $transactions $event->getTransaction ()->getTransactions ();
            
    $player null;
            
    $hopperinv null;
            
    $action null;
            foreach ( 
    $transactions as $transaction ) {
                if ((
    $inv $transaction->getInventory ()) instanceof HopperInventory) {
                    foreach ( 
    $inv->getViewers () as $assumed )
                        if (
    $assumed instanceof Player) {
                            
    $player $assumed;
                            
    $chestinv $inv;
                            break;
                        }
                }
                
    $action $transaction;
            }
            if (
    $hopperinv === null)
                return;
            
    $event->setCancelled ();
            
    $item $action->getTargetItem ();
            if (
    $item->getName () == "Test") {
                
    $player->sendMessage "Test" );
            }
        }
     
    XFizzer likes this.
  19. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You're just blindly copying code from ChestShop.
    There is a chance that $action variable might not be set correctly, but at the same time, $chest and $player are.
     
  20. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You did not change $hopperinv, so it will be null, then it would return and nothing is done...
     
  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.