Getting all nearby blocks

Discussion in 'Plugin Development' started by DogeDebugger, May 7, 2015.

Not open for further replies.
1. Offline

DogeDebugger

So i want to get all nearby blocks in a 25x25 (x and z only) radius from a location, and then pick out 10 of them to do something.

Picking them out won't be too hard, but how do i actually get all nearby blocks? Method or something?

#1
2. Offline

#2
3. Offline

DogeDebugger

I know, but loop through what...

#3
4. Offline

mythbusterma

@DogeDebugger

25 x 25 doesn't specify a radius (a property of a circle), it specifies a rectangle.

Which one do you mean?

#4
5. Offline

DogeDebugger

Rectangle, sorry
@mythbusterma

#5
6. Offline

Konato_K

@DogeDebugger With the location (center), make a new location with -25 in each direction and another one with 25 in each direction, then just loop through these numbers?

#6
7. Offline

DogeDebugger

Like this?
Code:
```  Location loc1 = new Location(w, loc.getX() + 25, loc.getY(), loc.getZ() + 25);
Location loc2 = new Location(w, loc.getX() - 25, loc.getY(), loc.getZ() - 25);
```
@Konato_K

#7
8. Offline

Konato_K

@DogeDebugger Actually, thinking that you need to loop in it, I'd go with something like this
Code:
```//Get one corner
int minX = center.getBlockX() - 12;
int minY = center.getBlockY() - 12;
int minZ = center.getBlockZ() - 12;
//Get the other corner
int maxX = center.getBlockX() + 12;
int maxY = center.getBlockY() + 12;
int maxZ = center.getBlockZ() + 12;
//Then you loop here and do your stuff
```
Edit: The number should be 12, using 25 will get you a 51x51 cuboid.

#8
9. Offline

DogeDebugger

@Konato_K
So i have this:
Code:
```int minX = loc.getBlockX() - 25;
int minZ = loc.getBlockZ() - 25;
int maxX = loc.getBlockX() + 25;
int maxZ = loc.getBlockZ() + 25;
Location min = new Location(w, minX, loc.getBlockY(), minZ);
Location max = new Location(w, maxX, loc.getBlockY(), maxZ);```
Now how may i get all locations between the 2 locations i just defined?

After that I can proceed xD

#9
10. Offline

Konato_K

@DogeDebugger No no, just loop with the numbers and call World#getBlockAt(int, int, int)

#10
11. Offline

DogeDebugger

Code:
```for (int x = min.getBlockX(); x <= max.getBlockX(); x++) {
for (int y = min.getBlockY(); y <= max.getBlockY(); y++) {
for (int z = min.getBlockZ(); z <= max.getBlockZ(); z++) {
Block blk = min.getWorld().getBlockAt(
new Location(min.getWorld(), x, y, z));
if(blk.getType().equals(Material.GOLD_BLOCK)) {
Location loc1 = blk.getLocation();
p.sendMessage(ChatColor.GOLD + "" + loc1.getX() + ", " + loc1.getY() + ", " + loc1.getZ() + "");
Location l = loc1.add(0, 30, 0);
ParticleEffect.VILLAGER_HAPPY.display(l, 30, 0, 0, 0,
10, 10);
}
}
}
}
}
}
return false;
};```

Ok, so i looped through the numbers and successfully got the blocks between them (thanks btw ^-^).

This is supposed to see if any of the blocks are gold, and if they are, they spawn a 30 block beam of VILLAGER_HAPPY on them, and sends the block's locations to me. It all works except for the actual spawning of the beam, incorrect code to get it to work i guess?

#11
12. Offline

Konato_K

@DogeDebugger Particles usually have a limit of render in the client (particles can't render too far), maybe this is the cause?

Otherwise maybe the parameters that are passed to the ParticleEffect are wrong? I don't know.

#12
13. Offline

DogeDebugger

Okay, I'll do some more research on that. I'm open for suggestions guys!

#13