Solved HELP NEEDED - All blocks between 2 points

Discussion in 'Plugin Development' started by Tyler Christensen, Aug 27, 2014.

Thread Status:
Not open for further replies.
  1. So this is the first time im attempting something like this... i would like to know how to get all the blocks between 2 specified locations in the config, i have everything setup for this, but not sure how i would go about doing so? I need this so i can store the block state of every block in a region and add it to a list. Thanks in advanced!
     
  2. Offline

    fireblast709

  3. I usually use this code to get all the blocks from a location:
    Code:java
    1. //Example list
    2. List<Block> blocks = new ArrayList<Block>();
    3.  
    4. for(int x=x1;x1 < x2;x++); //x1 is the first x, x2 the second one
    5. for(int y=y1;y1 < y2;y++); //y1 is the first y, y2 is the second one
    6. for(int z=z1;z1 < z2;z++); //z1 is the first z, z2 is the second one
    7. World w = Bukkit.getWorld("yourworldnamehere");
    8. blocks.add(w.getBlockAt(w, x, y, z));
    9.  
    10. //Example usage
    11. for(Block b : blocks) {
    12. b.setType(Material.STONE);
    13.  
     
  4. fireblast709 and megamichiel i get a null pointer exception on the line it adds it to the list. here is my code(i made sure it was printing all the numbers correctly as well as the world so it has something to do with the way im storing it and/or it isnt getting the block correctly, and yes, i need it to be the BlockState can anyone help me?



    Code:java
    1. public static Location Block1 = de(Main.B1);
    2. public static Location Block2 = de(Main.B2);
    3. public static List<BlockState> blocks = new ArrayList<BlockState>();
    4.  
    5. public static void saveMap() {
    6. Location loc1 = new Location(Block1.getWorld(), Block1.getX(),
    7. Block1.getY(), Block1.getZ());
    8. Location loc2 = new Location(Block2.getWorld(), Block2.getX(),
    9. Block2.getY(), Block2.getZ());
    10.  
    11. int x1 = loc1.getBlockX();
    12. int x2 = loc2.getBlockX();
    13. int y1 = loc1.getBlockY();
    14. int y2 = loc2.getBlockY();
    15. int z1 = loc1.getBlockZ();
    16. int z2 = loc2.getBlockZ();
    17. int topBlockX = (x1 < x2 ? x2 : x1);
    18. int bottomBlockX = (x1 > x2 ? x2 : x1);
    19. int topBlockY = (y1 < y2 ? y2 : y1);
    20. int bottomBlockY = (y1 > y2 ? y2 : y1);
    21. int topBlockZ = (z1 < z2 ? z2 : z1);
    22. int bottomBlockZ = (z1 > z2 ? z2 : z1);
    23.  
    24. for (int x = bottomBlockX; x <= topBlockX; x++) {
    25. for (int z = bottomBlockZ; z <= topBlockZ; z++) {
    26. for (int y = bottomBlockY; y <= topBlockY; y++) {
    27. World w = loc1.getWorld();
    28. Block l = w.getBlockAt(x, y, z);
    29. BlockState a = l.getState();
    30. BlockState block = (BlockState) a;
    31. blocks.add(block);
    32. }
    33. }
    34. }
    35. }
    36.  
     
  5. I'd start off by removing all the static's off that code...
     
  6. bendem I've eliminated as much as possible but sill getting a null pointer, here is my class:


    Code:java
    1. public class MapUtilities {
    2. static Location Block1 = de(Main.B1);
    3. static Location Block2 = de(Main.B2);
    4. static List<BlockState> blocks = new ArrayList<BlockState>();
    5.  
    6. public static void saveMap() {
    7. Location loc1 = new Location(Block1.getWorld(), Block1.getX(),Block1.getY(), Block1.getZ());
    8. Location loc2 = new Location(Block2.getWorld(), Block2.getX(),Block2.getY(), Block2.getZ());
    9. int topBlockX = (loc1.getBlockX() < loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
    10. int bottomBlockX = (loc1.getBlockX() > loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
    11. int topBlockY = (loc1.getBlockY() < loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
    12. int bottomBlockY = (loc1.getBlockY() > loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
    13. int topBlockZ = (loc1.getBlockZ() < loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
    14. int bottomBlockZ = (loc1.getBlockZ() > loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
    15.  
    16. for (int x = bottomBlockX; x <= topBlockX; x++){
    17. for (int z = bottomBlockZ; z <= topBlockZ; z++){
    18. for (int y = bottomBlockY; y <= topBlockY; y++){
    19. blocks.add(loc1.getWorld().getBlockAt(x, y, z).getState());
    20. }
    21. }
    22. }
    23. }
    24.  
    25.  
    26. public static Location de(String s){
    27. String[] data = s.split(":");
    28. Location l = new Location(Bukkit.getWorld(data[0]), Double.parseDouble(data[1]), Double.parseDouble(data[2]), Double.parseDouble(data[3]));
    29. return l;
    30. }
    31. }


    Any Suggestions anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  7. Tyler Christensen

    try changing topBlockX to loc1.getBlockX() and topBlockY to loc1.getBlockY() and topBlockZ to loc1.getBlockZ() and do the same for the bottomBlock with loc2
     
  8. megamichiel I would but the only problem with that is in the topBlockX methods is its figuring out weather or not loc1.getBlockX() is greater than loc2.getBlockX() there fore, if i were to replace topBlockX and BottomBlockX() with what you are saying, i would have to label the coordinates in a certian way. Does that make sense?
     
  9. Offline

    hintss

  10. The Server had to be completly loaded in order to mess with any chunks/blocks and worlds and all that xD
     
Thread Status:
Not open for further replies.

Share This Page