Code: $level = $player->getLevel(); $x = $player->getX(); $y = $player->getY(); $z = $player->getZ(); $pos = new Vector3($x, $y + 2, $z); $pos1 = new Vector3($x, $y, $z); $level->addSound(new EndermanTeleportSound($pos1)); $level->addParticle(new LavaParticle($pos1)); $server = Server::getInstance(); $level = $server->getDefaultLevel(); $pos2 = new Vector3($x, $y, $z); $tile = $level->getTile($pos2); $chest = Block::get(Block::CHEST); $level->setBlock($pos2, $chest); if ($tile instanceof Chest) { $i = $tile->getInventory(); $i->addItem(Item::get(Item::DIRT, 0, 23)); } I am making a plugin where I can give everyone a chest using /chest and when they get the chest they can place it on he ground, open the chest and grab the items. I got the the plugin to give me a chest that I can place down and it will spawn a new chest but I don't know how to add the items inside of the chest. Can anyone help me with adding the items inside of the chest because my method i used didn't work and didn't give me any errors to look off of.
It's really not that hard There's an NBT tag for chests specific to your issue. PHP: $item = Item::get(Item::CHEST);$itemsInChest = [ Item::get(Item::DIRT, 0, 23), Item::get(Item::DIAMOND, 0, 1)];$tag = new ListTag("Items", []);foreach($itemsInChest as $item){ $tag[] = $item->nbtSerialize();}$item->setNamedTagEntry($tag); Source:
Code: $level = $player->getLevel(); $x = $player->getX(); $y = $player->getY(); $z = $player->getZ(); $pos = new Vector3($x, $y + 2, $z); $pos1 = new Vector3($x, $y, $z); $level->addSound(new EndermanTeleportSound($pos1)); $level->addParticle(new LavaParticle($pos1)); $server = Server::getInstance(); $level = $server->getDefaultLevel(); $pos2 = new Vector3($x, $y, $z); $tile = $level->getTile($pos2); $chest = Block::get(Block::CHEST); $level->setBlock($pos2, $chest); $item = Item::get(Item::CHEST); $itemsInChest = [ Item::get(Item::DIRT, 0, 23), Item::get(Item::DIAMOND, 0, 1) ]; $tag = new ListTag("Items", []); foreach($itemsInChest as $item){ $tag[] = $item->nbtSerialize(); } $item->setNamedTagEntry($tag); I tried your code but it didn't do anything. It doesn't show any errors or anything. Can you assist?
I gave the player a chest earlier up in the code. I gave the player a chest with id of 54,101,1 and on PlayerInteractEvent with the chest it runs the command I put above. The 54,101,1 places on the ground and the second chest spawns at my feet, but neither of the chest have items in it.
I don't think you got my point, there's no need to spawn a chest through code or use any events what so ever. Just add the chest from my code in the player's inventory.