get blocks between two locations

Discussion in 'Plugin Development' started by superjesse07, Apr 28, 2014.

Thread Status:
Not open for further replies.
  1. i found this code on bukkit
    Code:java
    1. public static List<Block> blocksFromTwoPoints(Location loc1, Location loc2, World w)
    2. {
    3. List<Block> blocks = new ArrayList<Block>();
    4.  
    5. int minx = Math.min(loc1.getBlockX(), loc2.getBlockX()),
    6. miny = Math.min(loc1.getBlockY(), loc2.getBlockY()),
    7. minz = Math.min(loc1.getBlockZ(), loc2.getBlockZ()),
    8. maxx = Math.max(loc1.getBlockX(), loc2.getBlockX()),
    9. maxy = Math.max(loc1.getBlockY(), loc2.getBlockY()),
    10. maxz = Math.max(loc1.getBlockZ(), loc2.getBlockZ());
    11. for (int x = minx; x<=maxx;x++) {
    12. for (int y = miny; y<=maxy;y++) {
    13. for (int z = minz; z<=maxz;z++) {
    14. Block b = w.getBlockAt(x, y, z);
    15.  
    16. blocks.add(b);
    17. }
    18. }
    19. }
    20.  
    21. return blocks;
    22. }

    but when i use it it makes a line of blocks to north
    these are my location's
    point1:
    x: 886
    y: 64
    z: 297
    point2:
    x: 885
    y: 64
    z: 297
    this is the loop where i set the blocks
    Code:java
    1. for (Block b : blocksFromTwoPoints(point1, point2,p.getWorld())) {
    2. b.setType(Material.IRON_BLOCK);
    3. }


    bumb

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

    jeussa

    Hello superjesse07.

    I usually do the following when I need to select an area:

    Code:java
    1. public static List<Block> select(Location loc1, Location loc2, World w){
    2.  
    3. //First of all, we create the list:
    4. List<Block> blocks = new ArrayList<Block>();
    5.  
    6. //Next we will name each coordinate
    7. int x1 = loc1.getBlockX();
    8. int y1 = loc1.getBlockY();
    9. int z1 = loc1.getBlockZ();
    10.  
    11. int x2 = loc2.getBlockX();
    12. int y2 = loc2.getBlockY();
    13. int z2 = loc2.getBlockZ();
    14.  
    15. //Then we create the following integers
    16. int xMin, yMin, zMin;
    17. int xMax, yMax, zMax;
    18. int x, y, z;
    19.  
    20. //Now we need to make sure xMin is always lower then xMax
    21. if(x1 > x2){ //If x1 is a higher number then x2
    22. xMin = x2;
    23. xMax = x1;
    24. }else{
    25. xMin = x1;
    26. xMax = x2;
    27. }
    28.  
    29. //Same with Y
    30. if(y1 > y2){
    31. yMin = y2;
    32. yMax = y1;
    33. }else{
    34. yMin = y1;
    35. yMax = y2;
    36. }
    37.  
    38. //And Z
    39. if(z1 > z2){
    40. zMin = z2;
    41. zMax = z1;
    42. }else{
    43. zMin = z1;
    44. zMax = z2;
    45. }
    46.  
    47. //Now it's time for the loop
    48. for(x = xMin; x <= xMax; x ++){
    49. for(y = yMin; y <= yMax; y ++){
    50. for(z = zMin; z <= zMax; z ++){
    51. Block b = new Location(w, x, y, z).getBlock();
    52. blocks.add(b);
    53. }
    54. }
    55. }
    56.  
    57. //And last but not least, we return with the list
    58. return blocks;
    59. }


    Goodluck with your plugin!
     
    Orange Tabby likes this.
  3. jeussa thanks for you're code wen i tried it it still maked the long iron block line but now i have found the problem
    Code:java
    1. final Location point1 = new Location(p.getWorld(), main.getConfig().getDouble(i + ".point1.x"), main.getConfig().getDouble(i + ".point1.y"), main.getConfig().getDouble(i + ".point1.z"));
    2. final Location point2 = new Location(p.getWorld(), main.getConfig().getDouble(i + ".point2.x"), main.getConfig().getDouble(i + ".point2.y"), main.getConfig().getDouble(i + ".point1.2"));

    the second one is wrong i need to learn that you don't copy thing's wen you're coding :mad:
    i feel so stupid right now
     
Thread Status:
Not open for further replies.

Share This Page