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

Adding Items to chest using tiles

Discussion in 'Development' started by MonkleeGamer, Jan 25, 2018.

  1. MonkleeGamer

    MonkleeGamer Spider

    Messages:
    8
    GitHub:
    monkleegamer
    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.
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It's really not that hard :p
    There's an NBT tag for chests specific to your issue.
    PHP:
    $item Item::get(Item::CHEST);

    $itemsInChest = [
        
    Item::get(Item::DIRT023),
        
    Item::get(Item::DIAMOND01)
    ];

    $tag = new ListTag("Items", []);
    foreach(
    $itemsInChest as $item){
        
    $tag[] = $item->nbtSerialize();
    }
    $item->setNamedTagEntry($tag);
    Source:
     
  3. MonkleeGamer

    MonkleeGamer Spider

    Messages:
    8
    GitHub:
    monkleegamer
    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?
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Ofcourse you'll need to give the player the $chest :rolleyes:
     
  5. MonkleeGamer

    MonkleeGamer Spider

    Messages:
    8
    GitHub:
    monkleegamer
    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.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
  7. MonkleeGamer

    MonkleeGamer Spider

    Messages:
    8
    GitHub:
    monkleegamer
    Oohhhh, okay 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.