# Solved Get blocks between coordinates

Discussion in 'Spigot Plugin Development' started by Minimigamer, Feb 1, 2020.

1. ### Minimigamer

hey guys im trying to make a method wich takes 2 coordinates iterates through the Layers bewteen the coordinates, iterates through the rows in each layer, iterates through each block in each row and if the block isnt air it add the block to a blockList

My code:

Code (Text):

public static List<Block> getBlocks() {
Location loc1 = Main.loc1;
Location loc2 = Main.loc2;
Location loc3 = loc1;
List<Block> blockList = null;

if (loc1.getBlockY() > loc2.getBlockY()) {
for (int i = loc1.getBlockY(); i != loc2.getBlockY(); i--) {
for(int a = loc1.getBlockX(); a != loc2.getBlockX(); a--) {
for(int z = loc1.getBlockZ(); z != loc2.getBlockZ(); z++) {
loc3 = new Location(loc1.getWorld(), a, i, z);
if(loc3.getBlock().getType() != Material.AIR) {
}
}
}
}
}
return blockList;
}

2. ### Schottky

That is great! Might we know what your problem is as well?

3. ### robertlit

This basically means means null.add(..) so your code probably throws a NullPointerException

• Like x 1
4. ### Maxx_Qc

You need to check if block != null since not only Material.AIR equals NULL

• Like x 1
5. ### robertlit

First, Material.AIR is not null.
Secondly, I meant that the List is null.

• Like x 1
6. ### Maxx_Qc

Im not even answering to you, Im completing your comment lol. Btw, yes Material.AIR returns null and so does Material.BARRIER when you check for a block.

But yes, the list is null and hasnt been initialized

• Funny x 2

• Like x 1
12. ### Minimigamer

It works now

so now my code looks like this:
Code (Text):

public class Methods {
public static void set() {
List<Block> blockList = getBlocks();
for(Block b : blockList) {
b.setType(Material.DIAMOND_BLOCK);
}
}

public static List<Block> getBlocks() {
Location loc1 = Main.loc1;
Location loc2 = Main.loc2;
Location loc3 = loc1;
List<Block> blockList = new ArrayList<Block>();

if (loc1.getBlockY() > loc2.getBlockY()) {
for (int i = loc1.getBlockY(); i != loc2.getBlockY(); i--) {
for(int a = loc1.getBlockX(); a != loc2.getBlockX(); a--) {
for(int z = loc1.getBlockZ(); z != loc2.getBlockZ(); z++) {
loc3 = new Location(loc1.getWorld(), a, i, z);
Bukkit.broadcastMessage("" + loc3.getBlockX() + " "+ loc3.getBlockY() + " "+ loc3.getBlockZ() + " ");
if(loc3.getBlock() != null) {
}
}
}
}
}
return blockList;
}
}

#12
Last edited: Feb 4, 2020
13. ### Warren1001

and it works now?

yea