Get and change all chests in a world

Discussion in 'Plugin Development' started by Robin Bi, May 18, 2014.

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

    Robin Bi

    Hi!

    Yes, i know this thread, but it's from 2012 and apparently doesn't work anymore.
    So i wanna get all chests in a world so i can easily change their contents. This would be pretty useful for some minigames and it seems to be possible because all those crazy dazy SurvivalGames-plugins do change chest's inventories.


    Looking forward to your answers :)
     
  2. Offline

    ZeusAllMighty11

    How does it not work anymore? Chests are still tile entities.
     
    Azubuso likes this.
  3. Offline

    Robin Bi

    ZeusAllMighty11 I guess
    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4. if (cmd.getName().equalsIgnoreCase("chests")) {
    5. if (!(sender instanceof Player)) {
    6. sender.sendMessage(ChatColor.RED + "This command cannot be entered in console.");
    7. return true;
    8. }
    9.  
    10. Player p = (Player) sender;
    11. World pWorld = p.getWorld();
    12.  
    13. for (TileEntity e : pWorld.getLoadedChunks().getTileEntities()) {
    14. if (e instanceof Chest) {
    15. Bukkit.broadcastMessage("Chest found.");
    16. }
    17. }
    18.  
    19. }
    20. return true;
    21. }
    22. }
    should work then, but
    Code:java
    1. pWorld.getLoadedChunks().getTileEntities()
    is not there... Do i have made anything wrong?

    I know 50 minutes isn't too much, but it's kinda urgent, so... bump!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
  4. Offline

    Code0

    What you've posted works... Robin Bi
     
  5. Offline

    Robin Bi

    Code0 Well.. thanks for your asnwer, but it throws an error i don't really get...
    [​IMG]
     
  6. Offline

    NathanWolf

    You need two loops, one loop over the chunks and an inner loop over tile entities.

    I wish there was a way to iterate overall chunks, not just loaded ones.
     
Thread Status:
Not open for further replies.

Share This Page