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

Solved How to detect item taken from chest

Discussion in 'Development' started by azk_, Mar 15, 2018.

  1. azk_

    azk_ Spider Jockey

    Messages:
    49
    How to detect item taken from chest?
    I want to cancel it and remove taken item
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Use InventoryTransactionEvent. When it gets cancelled, the Item doesn't get moved so you won't need to remove the Item once you've already cancelled the event.
    PHP:
    /** @var InventoryTransactionEvent $event */
    foreach($event->getActions() as $action){
        if(
    $action instanceof SlotChangeAction){
            
    $inventory $action->getInventory();
            if(
    $inventory instanceof ChestInventory){
                
    $theItemThatPlayerClicked $action->getSourceItem();
                
    //^that is the item that the player clicked.
                //cancelling the event will disallow player from moving the item anywhere.
                
    $event->setCancelled();
                break;
            }
        }
    }
     
    HimbeersaftLP and HyGlobalHD like this.
  3. azk_

    azk_ Spider Jockey

    Messages:
    49
    thank you
     
  4. azk_

    azk_ Spider Jockey

    Messages:
    49
    I want to prevent specific item from being put in the chest
    Can you give an example function please? @Muqsit
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    $theItemThatPlayerClicked is an instance of the Item class. You can use something like this to prevent player from transferring dirt to their inventory.
    PHP:
    if($theItemThatPlayerClicked->getId() === Item::DIRT){
        
    $event->setCancelled();
        break;
    }
     
    Last edited: Mar 15, 2018
  6. azk_

    azk_ Spider Jockey

    Messages:
    49
    Here is my code but its not working

    PHP:
    public function onInventoryTransaction(InventoryTransactionEvent $event) {
            
    $player $event->getTransaction()->getSource();
            foreach(
    $event->getTransaction()->getActions() as $action){
                if(
    $action instanceof SlotChangeAction){
                    
    $inventory $action->getInventory();
                    if(
    $inventory instanceof ChestInventory){
                    
    $theItemThatPlayerClicked $action->getSourceItem();
                        if(
    $theItemThatPlayerClickedget->getId() == 2){
                            
    $event->setCancelled();
                            break;
                        }
                    }
                }
            }
        }
     
  7. SleepSpace9

    SleepSpace9 Slime

    Messages:
    78
    GitHub:
    sleepspace9
    typo:
    ...
    if($theItemThatPlayerClickedget->getId() == 2){
    ...
     
  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.