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
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->x = floor($tile->x); $block->y = floor($tile->y); $block->z = floor($tile->z); $block->level = $tile->getLevel(); $block->level->sendBlocks([$player], [$block]); $inventory = $tile->getInventory(); $inventory->setContents(array_fill(0, 27, Item::get(Item::TNT))); //$inventory->addItem(Item::get(Item::TNT)): $player->addWindow($inventory); }
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->x = floor($tile->x); $block->y = floor($tile->y); $block->z = floor($tile->z); $block->level = $tile->getLevel(); $block->level->sendBlocks([$player], [$block]); $inventory = $tile->getInventory(); $inventory->setContents(array_fill(0, 27, Item::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; } }