ChunkPopulateEvent help

Discussion in 'Plugin Development' started by nateracecar5, Jun 25, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    nateracecar5

    Okay, so I have this code:
    Code:java
    1. @EventHandler
    2. public void onGen(ChunkPopulateEvent e){
    3. BlockState[] tileEnts = e.getChunk().getTileEntities();
    4. for (BlockState state : tileEnts) {
    5. if (state.getType() != Material.CHEST)
    6. continue;
    7. //A chest has been generated, what would you like to do?
    8. }
    9. }


    I know that this code checks if a chest has been generated from the ChunkPopulateEvent, but I want to know how to add an item to that chest. Basically add custom dungeon loot to that chest.
     
  2. I've never used ChunkPopulateEvent and so I have no idea what getTileEntities() does.

    Code:
    @EventHandler
    public void onGen(ChunkPopulateEvent event) {
        BlockState[] tileEnts = event.getChunk().getTileEntities();
        for (BlockState state : tileEnts) {
            if (state != null && state.getType() == Material.CHEST) {
                Chest chest = (Chest) state;
                chest.getBlockInventory().add(new ItemStack(Material.DIAMOND));
            }
        }
    }
    
     
  3. Offline

    nateracecar5

    That's all I needed. Thank you!

    EDIT: For anyone using this code, the line
    Code:java
    1. chest.getBlockInventory().add(new ItemStack(Material.DIAMOND));


    needs to be
    Code:java
    1. chest.getBlockInventory().addItem(new ItemStack(Material.DIAMOND));
     
Thread Status:
Not open for further replies.

Share This Page