# Solved How to get all blocks around two locations

Discussion in 'Spigot Plugin Development' started by --TOMAS--, Oct 27, 2016.

1. ### --TOMAS--

This amazing image explains me.

• Like x 2
2. ### Twisted_Gaming

im not sure what your asking ....

3. ### KingDragonRider

Do you mean all the blocks inside the two points, going by that image? (like a worldedit selection?)

• Like x 1
4. ### --TOMAS--

Yeah! inside the two points

5. ### Twisted_Gaming

get the world edit plugin .//wand then right click one block left click the other

• Funny x 2

7. ### finnbon

You need the Location object of the first, and the second location. Get the difference between the x, y and z, then make nested for-loops with those differences and get the locations!
Example with pseudo code:
Code (Java):

List<Block> list = new ArrayList<>();
x-difference = second location.x - first location.x
y-difference = second location.y - first location.y
z-difference = second location.z - first location.z

for x starts at 0, is less or equal to x-difference {
for y starts at 0, is less or equal to y-difference {
for z starts at 0, is less or equal to z-difference {
block = clone first location, then add x, y & z, then get block.
add block to list.
}
}
}

• Useful x 1
8. ### KingDragonRider

Ninja'd by two people by only a few seconds. But I'm going to post it here anyway so you have multiple options. I wasn't sure how to explain it so I've created a quick method for you:
Code (Text):

public List<Block> getBlocks(Location pos1, Location pos2)
{
if(pos1.getWorld() != pos2.getWorld())
return null;
World world = pos1.getWorld();
List<Block> blocks = new ArrayList<>();
int x1 = pos1.getBlockX();
int y1 = pos1.getBlockY();
int z1 = pos1.getBlockZ();

int x2 = pos2.getBlockX();
int y2 = pos2.getBlockY();
int z2 = pos2.getBlockZ();

int lowestX = Math.min(x1, x2);
int lowestY = Math.min(y1, y2);
int lowestZ = Math.min(z1, z2);

int highestX = lowestX == x1 ? x2 : x1;
int highestY = lowestX == y1 ? y2 : y1;
int highestZ = lowestX == z1 ? z2 : z1;

for(int x = lowestX; x <= highestX; x++)
for(int y = lowestY; x <= highestY; y++)
for(int z = lowestZ; x <= highestZ; z++)
return blocks;
}

• Like x 1
• Useful x 1
9. ### --TOMAS--

I don't want use WorldEdit. I want make my code-.

10. ### finnbon

crap, I forgot to take negative coordinates into account.

• Friendly x 1
11. ### --TOMAS--

Thanks you
Thanks you! Now I underestand how to make it.

12. ### --TOMAS--

And thanks you for the API. But, I think I will make my own code.