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

code chest

Discussion in 'Development' started by MadeUpadanaYT, Feb 18, 2017.

  1. MadeUpadanaYT

    MadeUpadanaYT Spider

    Messages:
    12
    Can you tell the code to make the chest
    like virtual chest
    Example:
    If we write the command / test. he would open a chest containing tnt
     
    MyNET likes this.
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
        public function sendChestShop(Player $player){
            
    $nbt = new CompoundTag('', [
                new 
    ListTag('Items', []),
                new 
    StringTag('id'Tile::CHEST),
                new 
    IntTag('x'floor($player->x)),
                new 
    IntTag('y'floor($player->y)),
                new 
    IntTag('z'floor($player->z))
            ]);
            
    /** @var Chest $tile */
            
    $tile Tile::createTile('Chest'$player->chunk$nbt);
            
    $block Block::get(Block::CHEST);
            
    $block->floor($tile->x);
            
    $block->floor($tile->y);
            
    $block->floor($tile->z);
            
    $block->level $tile->getLevel();
            
    $block->level->sendBlocks([$player], [$block]);
            
    $inventory $tile->getInventory();
            
    $inventory->setContents(array_fill(027Item::get(Item::TNT)));
            
    //$inventory->addItem(Item::get(Item::TNT)):
            
    $player->addWindow($inventory);
        }
     
    MyNET likes this.
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    With this code, how to make sure that the player can't add anything inside?
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You need to use InventoryTransactionEvent for that, and also, add an NBT tag to the tile for identification.
    PHP:
    public function sendChestShop(Player $player){
            
    $nbt = new CompoundTag('', [
                new 
    ListTag('Items', []),
                new 
    StringTag('id'Tile::CHEST),
                new 
    IntTag('x'floor($player->x)),
                new 
    IntTag('y'floor($player->y)),
                new 
    IntTag('z'floor($player->z))
            ]);
            
    /** @var Chest $tile */
            
    $tile Tile::createTile('Chest'$player->chunk$nbt);
            
    $tile->namedtag->blocktrans = new IntTag('blocktrans'1);
            
    $block Block::get(Block::CHEST);
            
    $block->floor($tile->x);
            
    $block->floor($tile->y);
            
    $block->floor($tile->z);
            
    $block->level $tile->getLevel();
            
    $block->level->sendBlocks([$player], [$block]);
            
    $inventory $tile->getInventory();
            
    $inventory->setContents(array_fill(027Item::get(Item::TNT)));
            
    //$inventory->addItem(Item::get(Item::TNT)):
            
    $player->addWindow($inventory);
        }
    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 ChestInventory && isset($inv->getHolder()->namedtag->blocktrans)){
                    
    //This means $inv is a custom inventory..
                    
    $event->setCancelled();
                    break;
                }
            }
     
    MyNET likes this.
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Thanks :)
     
  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.