Solved Getting chest inventories and filling them with items.

Discussion in 'Plugin Development' started by ActivisionDevReloaded, Jul 14, 2014.

Thread Status:
Not open for further replies.
  1. Does anybody know a way to get all the chest in a world and fill them with items. I know how to add the items but how do I get the chest inventory? If anybody knows please help!
     
  2. Offline

    TheMcScavenger

    Add chests to a list when they're placed down:
    Code:java
    1. public List<Location> chests = new ArrayList<Location>();
    2.  
    3. @EventHandler
    4. public void onBlockPlace(BlockPlaceEvent event){
    5. if(event.getBlockPlaced().getType().equals(Material.CHEST){
    6. chests.add(event.getBlockPlaced().getLocation());
    7. }
    8. }

    Fill them:
    Code:java
    1. public void fillChests(){
    2. for(Location location : chests){
    3. Chest chest = (Chest) location.getBlock();
    4. chest.getInventory().addItem(new ItemStack(Material.STONE, 1));
    5. }
    6. }

    Note: You may want to save them to a YAML file...
     
  3. Offline

    CorrieKay

    If you dont already have all of your chests registered, you'll want to register the chunk load event, and parse all the blocks within the chunk bounds for the chests.

    TheMcScavenger I think you're looking for the blockstate before you can cast it into a chest, i think. Going off of memory on this one, so i could be wrong.
     
  4. Offline

    TheMcScavenger

  5. bump

    Found out how to do this!

    Thread Solved.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page