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

[Solved] Can't generate chests with items, plz help me

Discussion in 'Development' started by cd9r, Dec 15, 2016.

  1. cd9r

    cd9r Spider

    Messages:
    7
    GitHub:
    cd9r
    SOLVED
    So I was trying to generate chests with items filled, and it generated chests but there's no item in it, please help me!! thanks.
    here's my code:

    PHP:
    $level->setBlock($pos, new Chest(), true);
            
    $nbt = new CompoundTag("", [
                new 
    ListTag("Items", []),
                new 
    StringTag("id"Tile::CHEST),
                new 
    IntTag("x"$x),
                new 
    IntTag("y"$y),
                new 
    IntTag("z"$z)
            ]);

            if(
    $level->getChunk($x$z) == null) {
                
    $level->loadChunk($x$z);
            }
            
    $chunk $level->getChunk($x$z);
       
            
    $tile = new \pocketmine\tile\Chest($chunk$nbt);


            if(
    $tile instanceof \pocketmine\tile\Chest) {

                foreach (
    $item_drop as $index => $value) {
                    
    $item explode(':'$value);
                    
    $tile->getInventory()->setItem($indexItem::get($item[0], $item[1], $item[2]));
                }

            }
     
    Last edited: Dec 16, 2016
  2. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Is the array $item_drop empty? Use var_dump() to check it.
     
  3. HBIDamian

    HBIDamian HBIDamian Staff Member

    Messages:
    365
    GitHub:
    HBIDamian
    CONSIDER CHANGING THE NAME OF THIS TOPIC SO SOMETHING RELATABLE!
    *************************************
    Basically, it helps keep the forum neat and tidy. It also helps people who might have the same issue as you.
    For example "How to generate chests with items"
     
  4. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    May we know what is the value of $item_drop?
     
  5. cd9r

    cd9r Spider

    Messages:
    7
    GitHub:
    cd9r
    No, it's not empty.
    $item_drop = array('17:0:5', '3:0:1');
     
  6. cd9r

    cd9r Spider

    Messages:
    7
    GitHub:
    cd9r
    Thank you, I've changed the title.
     
  7. cd9r

    cd9r Spider

    Messages:
    7
    GitHub:
    cd9r
    Yes, it's been set to like array('17:0:5', '3:0:1')
     
  8. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    PHP:
    $level->setBlock($pos, new Chest(), true);
            
    $nbt = new CompoundTag("", [
                new 
    ListTag("Items", []),
                new 
    StringTag("id"Tile::CHEST),
                new 
    IntTag("x"$x),
                new 
    IntTag("y"$y),
                new 
    IntTag("z"$z)
            ]);

            if(
    $level->getChunk($x >> 4$z >> 4) == null) {
                
    $level->loadChunk($x >> 4$z >> 4);
            }
            
    $chunk $level->getChunk($x >> 4$z >> 4);
      
            
    $tile = new \pocketmine\tile\Chest($chunk$nbt);


            if(
    $tile instanceof \pocketmine\tile\Chest) {

                foreach (
    $item_drop as $index => $value) {
                    
    $item explode(':'$value);
                    
    $tile->getInventory()->setItem($indexItem::get($item[0], $item[1], $item[2]));
                }

            }
    I think you need to shift the bits of $x and $z to the right 4 times. Why? "Because chunks are 16*16 groups of X/Z-coordinates." (PeMapModder, online).
     
    Last edited: Dec 16, 2016
    cd9r likes this.
  9. cd9r

    cd9r Spider

    Messages:
    7
    GitHub:
    cd9r
    It works!! Thank you so much!!
     
  10. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    No problem. Please add "[Solved]" to te beginning of the title. ;)
     
  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.