How could I find all chests in a world?

Discussion in 'Plugin Development' started by NerdsWBNerds, May 18, 2012.

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

    NerdsWBNerds

    Would chests be saved as an entity (could world.getEntities() find them) is so, what would they be an instanceof? If not how would I find all of them? Would I have to loop through EVERY BLOCK, or is there a simpler way to do this?
     
  2. Offline

    r0306

    NerdsWBNerds
    If you plan on using this plugin for a new map, you could add a block place event to the plugin and if the block placed is a chest, store that location into a file. Then add a block break event and if the block broken is a chest, remove that location from the file.
     
    Pimp_like_me likes this.
  3. Offline

    Njol

    Chests blocks have so-called tile entities associated with them. These are not real entities (and thus not returned by world.getEntities) and are called BlockStates by Bukkit. You can get all tile entities of a chunk with chunk.getTileEntities(), and a list of all loaded chunks of a world with world.getLoadedChunks().
    You can now easily loop though all tile entities of all loaded chunks of a world and test whether they are instanceof Chest to get all loaded chests in the world.
    Please note that if you want to e.g. empty all chests of a world on command, you would have to also listen to ChunkLoadEvent and clear chests when they are loaded, but you'll have to make sure that you only clear the chests of a chunk once.
     
  4. Offline

    NerdsWBNerds

    Thanks you so much!
    I have one question, how do you get the chest from the TileEntities?
    I have Block block and I'm setting it equal to tile.getBlock();
    Then I have Chest chest and I'm setting it equal to (Chest) block;
    It says I can't cast CraftBlock to Chest.
     
  5. Offline

    Njol

    Cast the tile entity to chest after you test whether it's a chest of course...
     
  6. Offline

    NerdsWBNerds

    Thanks, and one last question, how can I tell if a chest is a double chest?
     
  7. Offline

    Njol

    Try this: if (chest.getInventory() instanceof DoubleChestInventory)
     
Thread Status:
Not open for further replies.

Share This Page