Solved Nearby Blocks

Discussion in 'Plugin Development' started by Coopah, Jan 2, 2016.

Thread Status:
Not open for further replies.
  1. Offline


    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.
  2. Offline


    • 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!
  3. Offline


    I did this, but had no luck?
    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) {
  4. Offline


    1. You are getting the type before you know it is actually a block. What if it's air? Then it would throw an NPE.
    2. 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.
    3. 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.
    4. 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.
    5. Did you debug? What part of this does not work?
  5. Offline


    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.
Thread Status:
Not open for further replies.

Share This Page