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

InventoryTransactioEvent not cancelling

Discussion in 'Development' started by RoyalMCPE, Mar 27, 2017.

  1. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    When I take the selected item out the inventory it will sometimes cancel.

    PHP:
    public function onInventoryTransaction(InventoryTransactionEvent $event) {
            
    $hopper null;
            
    $player null;
            
    $transactions $event->getTransaction ()->getTransactions ();
            
    $inventories $event->getTransaction ()->getInventories ();
            foreach ( 
    $transactions as $transaction ) {
                foreach ( 
    $inventories as $inventory ) {
                    
    $inventory $inventory->getHolder ();
                    if (
    $inventory instanceof Player) {
                        
    $player $inventory;
                    }
                    if (
    $inventory instanceof Hopper) {
                        
    $hopper $inventory;
                    }
                }
                if (
    $hopper !== null) {
                    foreach ( 
    $inventories as $inventory ) {
                        
    $player $inventory->getHolder ();
                        if (
    $player instanceof Player && $hopper instanceof Hopper) {
                            
    $event->setCancelled true );
                            
    $item $transaction->getTargetItem ();
                            if (
    $item->getName () === c::BOLD c::DARK_PURPLE "Ranked Kits" c::RESET "\n" c::GRAY "Kits unlocked through rank purchases.") {
                                if (
    $player->hasPermission "mca.core.kit" )) {
                                    
    $hopper->getInventory ()->clearAll ();
                                    
    $hopper->getInventory ()->setItem 0Item::get Item::WOODEN_SWORD0)->setCustomName c::BOLD c::GREEN "Starter" c::RESET "\n\n" c::BOLD c::DARK_AQUA "Kit Information" c::RESET "\n" c::DARK_AQUA " Cooldown " c::GRAY "1 day" c::RESET "\n" c::DARK_AQUA " Number of Items " c::GRAY " 21" c::RESET "\n\n" c::BOLD c::GREEN "AVAILABLE" ) );
                                    
    $hopper->getInventory ()->setItem 1Item::get Item::GLASS_PANE0)->setCustomName c::BOLD c::RED "Survivor" c::RESET "\n\n" c::RED " You don't have permission to redeem this kit!" c::RESET "\n\n" c::BOLD c::AQUA "Kit information" c::RESET "\n" c::DARK_AQUA " Cooldown " c::GRAY " 1 day" c::RESET "\n" c::DARK_AQUA " Number of Items" c::GRAY " 11" c::RESET "\n\n" c::BOLD c::RED "LOCKED" ) );
                                    
    $hopper->getInventory ()->setItem 2Item::get Item::GLASS_PANE0)->setCustomName c::BOLD c::RED "Medic" c::RESET "\n\n" c::RED " You don't have permission to redeem this kit!" c::RESET "\n\n" c::BOLD c::AQUA "Kit information" c::RESET "\n" c::DARK_AQUA " Cooldown " c::GRAY " 1 day" c::RESET "\n" c::DARK_AQUA " Number of Items" c::GRAY " 11" c::RESET "\n\n" c::BOLD c::RED "LOCKED" ) );
                                    
    $hopper->getInventory ()->setItem 3Item::get Item::GLASS_PANE0)->setCustomName c::BOLD c::RED "Virus" c::RESET "\n\n" c::RED " You don't have permission to redeem this kit!" c::RESET "\n\n" c::BOLD c::AQUA "Kit information" c::RESET "\n" c::DARK_AQUA " Cooldown " c::GRAY " 1 day" c::RESET "\n" c::DARK_AQUA " Number of Items" c::GRAY " 11" c::RESET "\n\n" c::BOLD c::RED "LOCKED" ) );
                                } else {
                                    
    $player->sendMessage c::BOLD c::DARK_RED "(!)" c::RESET " You don't have permission to redeem this kit." );
                                }
                            }
                            if (
    $item->getName () === c::BOLD c::GREEN "Starter" c::RESET "\n\n" c::BOLD c::DARK_AQUA "Kit Information" c::RESET "\n" c::DARK_AQUA " Cooldown " c::GRAY "1 day" c::RESET "\n" c::DARK_AQUA " Number of Items " c::GRAY " 21" c::RESET "\n\n" c::BOLD c::GREEN "AVAILABLE") {
                                
    $player->sendMessage "Test" );
                                
    $player->removeWindow $hopper->getInventory () );
                            }
                        }
                    }
                } else {
                    return 
    true;
                }
            }
        }
     
  2. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    Need more infomations
    What are you trying to do?
    any errors it gave out?
     
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Try to debug code with echo.
     
    Primus likes this.
  4. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    have you tried to remove $event->setCancelled(true);?
     
  5. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    Then the event wouldn't cancel
     
  6. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    safe_var_dump() is prefered
     
  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.