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

(SOLVED Event Not Canceling

Discussion in 'Development' started by Junkdude, Feb 4, 2017.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Im having trouble with inventory transactionevent, im trying to cancel it but its not working.
    Send Inventory code:
    PHP:
    $player $p;
                
    $block Block::get(54,27);
                
    $player->getLevel()->setBlock(new Vector3($player->x$player->4$player->z), $blocktruetrue);
                
    $nbt = new CompoundTag("", [
                new 
    ListTag("Items", []),
                new 
    StringTag("id"Tile::CHEST),
                new 
    StringTag("CustomName""My custom chest"),
                new 
    IntTag("x"floor($player->x)),
                new 
    IntTag("y"floor($player->y) - 4),
                new 
    IntTag("z"floor($player->z))
            ]);
                
    $nbt->Items->setTagType(NBT::TAG_Compound);
                
    $tile Tile::createTile('Chest'$player->getLevel()->getChunk($player->getX() >> 4$player->getZ() >> 4), $nbt);
                
    $tile->getInventory()->setItem(5Item::get(38401));
                
    $tile->getInventory()->setItem(4Item::get(38401));
                
    $tile->getInventory()->setSize(8);
            
    $player->addWindow($tile->getInventory());
    TransactionCode:
    PHP:
    public function onTransaction(InventoryTransactionEvent $event){
            
    $transactions $event->getTransaction()->getTransactions();
            
    $player null;
            
    $chestinv null;
            
    $action null;
            foreach(
    $transactions as $transaction){
                if((
    $inv $transaction->getInventory()) instanceof PlayerInventory){
                    foreach(
    $inv->getViewers() as $assumed)
                        if(
    $assumed instanceof Player) {
                            if(
    $inv->getTargetItem()->getId() === 384)
                            {
                                
    $event->setCancelled(true);
                            
    $player $assumed;
                            
    $chestinv $inv;
                            
            }
        }
    }
    }
    }
     
  2. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Forgot to mention, theres no error on spawning the chest/transaction event.
     
  3. StuntzCo

    StuntzCo Baby Zombie

    Messages:
    197
    Help! I need somebody. Help!.. not just anybody.. Help!.. I need someone. Heeelllppp!!
    sorry I can't help you but maybe if you tagged a staff member....
     
    HimbeersaftLP likes this.
  4. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Why would I tag a staff member? They are for moderation arent they?
     
  5. StuntzCo

    StuntzCo Baby Zombie

    Messages:
    197
    I've had staff and verified people help me before on the forums and they are definitely more qualified to answer questions than a lot of people on the forum not that there gonna be excited to answer a question or even that they will but they might but hey idk if your desperate for help it might work
     
  6. SuperDude36

    SuperDude36 Spider Jockey

    Messages:
    26
    GitHub:
    superdude36
    Junk check your twitter DMs
     
  7. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Can you at least tell us wat doesnt work? Does the inventory show up? Does the items get transferred? Can you use the transferred item?
    Try doing a var_dump where you cancel the event, if it shows up but item is transferred then there is something wrong with your server core
     
  8. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    if(($inv = $transaction->getInventory()) instanceof PlayerInventory){, this is causing it, if u dont have this line, it cancels
     
  9. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You need to
    PHP:
     use pocketmine/inventory/PlayerInventory;
     
  10. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Yea i figured that out now, thank though. Thread solved!
     
  11. SuperDude36

    SuperDude36 Spider Jockey

    Messages:
    26
    GitHub:
    superdude36
    Yay for junk xD
     
  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.