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

Cancel InventoryTransactionEvent

Discussion in 'Development' started by gistrec, Nov 26, 2016.

  1. gistrec

    gistrec Witch

    Messages:
    68
    GitHub:
    gistrec
    How can I cancell InventoryTransactionEvent, when player change hotbar item?
    For example, I would like, that clock always were in first Invntory bar slot.

    Something like this?
    PHP:
    public function onSlotChange(InventoryTransactionEvent $event) {
            
    $newItem null;
            
    $player null;
            
    $transactions $event->getQueue()->getTransactions();
            foreach (
    $transactions as $tr){
                    if (
    $tr->getTargetItem() instanceof Item$newItem $tr->getTargetItem();
                    if (
    $tr->getInventory() instanceof PlayerInventory$player $tr->getInventory()->getHolder();
            }
            if (
    $player != null && $newItem != null){
                    
    $lastItem $player->getInventory()->getItemInHand();
                    if (
    $lastItem->getId() == 347 && $newItem->getId() != 347){ //compas
                        
    $event->setCancelled();
                    }
            }
    }
     
    Last edited: Nov 26, 2016
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    i think the last if statement should be inside the foreach loop?
     
    robske_110 (Tim) likes this.
  3. gistrec

    gistrec Witch

    Messages:
    68
    GitHub:
    gistrec
    Ops, event is works. I.e items don't drop, but items remove from Invntory bar, and appear in player inventory ;C
     
    Last edited: Nov 26, 2016
  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.