Any idea how to get all nearby blocks in a radius of... say 20? Thanks I've mucked around with nested loops, but can't seem to figure it out.
For loop for the X: start at x - 20. If the x is less than x+ 20, add 1 to the X Copy for Y Copy for Z If the distance for all the values together are less than or equal to 20. You got a block within a radius of 20!
@Zombie_Striker I did this, but had no luck? Code: Location playerLoc = player.getLocation(); double pX = playerLoc.getX(); double pY = playerLoc.getY(); double pZ = playerLoc.getZ(); for (int x = -(20); x <= 20; x ++) { for (int y = -(20); y <= 20; y ++) { for (int z = -(20); z <= 20; z ++) { Block b = player.getWorld().getBlockAt((int)pX+x, (int)pY+y, (int)pZ+z); if (b.getType() == Material.MOB_SPAWNER) { p.sendMessage("Success!"); } } } }
@Coopah You are getting the type before you know it is actually a block. What if it's air? Then it would throw an NPE. Instead of doing the math once when you create the for loop, you do the math for each and every value when retrieving the block. It seems you only use the playerLoc variable to create px,py,and pz. Unless it does more than that in your code, you might as well not have that variable. A main problem is that you have greater than or equal in your code. This will be it will look 20 blocks in one direction and 21 in another. Did you debug? What part of this does not work?
Are you 100% positive about that? Why would the block be null? How would you ever set an air block to a solid block if it was like that? @Coopah Use Bukkit's Vector class and this method OR use Location's distanceSquared method. Add that to your if statement in the for-loop.