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

Issue with InventoryTransactionEvent and setItem in the chest

Discussion in 'Development' started by InspectorGadget, Jan 7, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi! I'm not sure if this is correct but i'm having issues with cancelling Inv Transaction and setting items in a Chest... Please help and advise. Thanks :)
    PHP:
     public function sendChestInventory(Player $playerInventoryTransactionEvent $e){
        
    $block Block::get(54);
        
    $player->getLevel()->setBlock(new Vector3($player->x$player->2$player->z), $blocktruetrue);
        
    $nbt = new CompoundTag("", [
          new 
    ListTag("Items", []),
          new 
    StringTag("id"Tile::CHEST),
          new 
    IntTag("x"floor($player->x)),
          new 
    IntTag("y"floor($player->y) - 2),
          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);
        
    /* Items */
        
    $item Item::get(31001);
        
    $item2 Item::get(27601);
        
    $item2->setCustomName("Lmao");
        
    $tile->getInventory()->getSlotIndex(1)->setItem($item);
        
    $tile->getInventory()->addItem(2$item);
        
    $player->addWindow($tile->getInventory());
       
            foreach(
    $e->getTransaction()->getTransactions() as $t) {
                if(
    $t->getInventory() instanceof ChestInventory) {
                    
    $e->setCancelled(true);
                ]
            ]
      }
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    you should listen for the event after creating the chest. the event you passed doesn't know of your new chest yet
     
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Can u provide me with some example? I don't understand this yet..
     
  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.