[SOLVED] Get All Blocks

Discussion in 'Spigot Plugin Development' started by Tauchet, May 3, 2015.

  1. Get all blocks of a world and add the location of which he is stone
     
  2. gigosaurus

    Supporter

    What have you tried so far?
     
  3. What are you talking about?
     
  4. Have you tried anything yourself first?
     
  5. This is the code that I have so far.

    World w = Bukkit.getWorld("world");
    for (Chunk c : w.getLoadedChunks()) {
    int cx = c.getX() << 4;
    int cz = c.getZ() << 4;
    for (int x = cx; x < cx + 16; x++) {
    for (int z = cz; z < cz + 16; z++) {
    for (int y = 0; y < 128; y++) {
    if (w.getBlockAt(x, y, z).getType() == Material.CHEST) {
    Chests.add(w.getBlockAt(x, y, z).getLocation());
    }
    }
    }
    }​

    }

    when I start the server gives me an error
     
  6. Ok now do us a favour, could you reword what you're trying to do?
     
  7. I want to do that all blocks of a World that are stone you save in a list
     
  8. gigosaurus

    Supporter

    So just change the Material.CHEST in your code to Material.STONE, and add to a list for the stone?
     
  9. Loop through all the chunks blocks, if a block is Material.STONE, increment the size of an int, or add the location to an arraylist.
     
  10. the material Chest is in example in reality is a material Stone
     
  11. gigosaurus

    Supporter

    So you've already solved your own question? Or are you trying to get ALL the chunks, including the ones not loaded?
    You might want to change the y < 128 to y < 256 though.
     
  12. The error is

    [​IMG]
     
  13. Do you know basic Java? I think you could achieve this pretty easily.

    Otherwise, if you do and you're just a bit confused:

    Code (Text):

    int amount = 0;
    ArrayList<Location> stones = new ArrayList<Location>;

    World w = Bukkit.getWorld("world");
    for (Chunk c : w.getLoadedChunks()) {
    int cx = c.getX() << 4;
    int cz = c.getZ() << 4;
    for (int x = cx; x < cx + 16; x++) {
    for (int z = cz; z < cz + 16; z++) {
    for (int y = 0; y < 128; y++) {
    if (w.getBlockAt(x, y, z).getType() == Material.STONE) {
    stone.add(w.getLocation());
    amount++;
    }
    }
    }
    It's a rough copy but it should work
     
  14. Thanks to all the solution was not executed a message that was Main.getPlugin().getLogger().info("Load " + Chests.size() + " block(s)");
    the change for System.out.Print("") and no longer fails.
     
  15. There were some errors so I fixed it!
    Code (Text):

    World w = Bukkit.getWorld("world");
            for (Chunk c : w.getLoadedChunks()) {
            int cx = c.getX() << 4;
            int cz = c.getZ() << 4;
            for (int x = cx; x < cx + 16; x++) {
            for (int z = cz; z < cz + 16; z++) {
            for (int y = 0; y < 128; y++) {
            if (w.getBlockAt(x, y, z).getType() == Material.STONE) {
            JoinEvent.blocks.add(w.getBlockAt(x,y,z));
            }
            }
            }
            }
            }
     
     
    • Funny Funny x 1
    • Useful Useful x 1
  16. 2015. We're in 2019. Why are you bumping this up
     
    • Agree Agree x 1
  17. I searched a method like this and found it! So I tought I'll update it!
     
    • Like Like x 1
    • Agree Agree x 1