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

EntityInventoryChangeEvent

Discussion in 'Development' started by DogTheBlock, May 21, 2017.

  1. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    ok so am using EntityInventoryChangeEvent to make a CrateKey plugin , now when a player taps a glass pane the event is cancelled which means they can't do anything with that item, but the item also removes , how can i make the item not remove but still players can't touch it or put it into their inventory?
    PHP:
        public function onWin(EntityInventoryChangeEvent $event){
            
    $p $event->getEntity();
            
    $win $event->getNewItem();

            if(
    $win->getId() === 102 and $win->getCustomName() === ""){
                if(
    $p instanceof Player){
                    
    $item Item::get(10301)->setCustomName("");
                    
    $event->setNewItem($item);
                    
    $event->setCancelled(true);
                }
            }
       }
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Use InventoryTransactionEvent.
     
  3. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    teach me :p
     
  4. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    The item is still in the inventory, the item just goes invisible after you cancel the event. Just get the items slot & reset the item back in the slot
     
  5. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    hmm so like $event->getSlot(what goes here?)->setNewItem($glass);?
     
  6. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
  7. Michael

    Michael Baby Zombie

    Messages:
    113
    GitHub:
    michaelm04
    :)

    So InventoryTransactionEvent,
    PHP:
    public function onTransaction(InventoryTransactionEvent $event){
        
    $transactions $event->getTransaction()->getTransactions();
        
    $inventories $event->getTransaction()->getInventories();
        foreach(
    $transactions as $trans){
            foreach(
    $inventories as $invs){
                if(
    $invs instanceof PlayerInventory){
                    
    $item $trans->getTargetItem();
                    if(
    $item->getId() === 102 and $item->getCustomName() === ""){
                        
    $event->setCancelled(true);
                    }
                }
            }
        }
    }
    Learn what this does to understand, then continue.
     
    Last edited: May 24, 2017
    jasonwynn10 likes this.
  8. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    thanks
     
    Remarkabless likes this.
  9. DogTheBlock

    DogTheBlock Spider Jockey

    Messages:
    29
    doesn't work :(
     
  10. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    It won't if you only copied the code
     
  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.