Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do i put items in a chest with code

Comments in 'Plugin Development' started by BlubberBoy333, Jul 22, 2015.

  1. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    If I placed a chest with this code
    PHP:
    $level->setBlock(new Vector3($x$y$z), 54);
    How can I put items in that chest by code?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
  3. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Chest inventory is like player inventory. But at first, you need get the chest as a tile:
    PHP:
    $tile $lv->getTile(new Vector3($x,$y,$z));
    Then check if this tile is really chest(not sign...):
    PHP:
     if ($tile instanceof Chest){
    Then, just get inventory with $tile->getInventory() and you can use addItem(Item::get($id, $meta, $cnt)), removeItem....
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    His chest is new. There isn't a tile entity for the cheat yet.
    jojoe77777 likes this.
  5. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Edited version: "place chest, restart server and do what I wrote"
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Restarting server will still not initialize the tile. You need to do that explicitly.
  7. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    So, tile is automatically created only when I join game and I place the chest?
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes. The link I posted above is the code PocketMine executed when you place a chest.
  9. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    Thanks! Now, with this code I can randomly spawn chests in the whole SkyWars world. You'll no longer know where chest can appear :3
    PEMapModder likes this.
  10. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    Glad to know your problem was solved. But um.. OP's question remains unsolved.
    BlubberBoy333 likes this.
  11. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    To put items in the chest, you need to acquire the inventory of the chest first.

    PHP:
    $chestInventory $tileChest->getInventory();
    Then set the items:

    Code:
    $chestInventory->setItem($itemIndex, Item::get($itemId, $itemMeta, $itemCount));
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Our question is about the tile of a newly created chest, not the inventory.
    TigerHix likes this.
  13. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    I did this
    PHP:
    $chest $level->createTile("chest", new Vector3($randX+4$randY+1$Z+4));
    $chestInv $chest->getInventory();
    $config $this->getConfig()->get("Items");
    foreach(
    $config as $i){
        
    $chestInv->addItem($i);
    }
    Would that work? (i haven't tested it yet)
    Andrey Nazarchuk and PEMapModder like this.

Share This Page

Advertisement