Solved How to get all blocks around two locations

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

  1. This amazing image explains me.
    awd.png
     
    • Like Like x 2
  2. im not sure what your asking ....
     
  3. Do you mean all the blocks inside the two points, going by that image? (like a worldedit selection?)
     
    • Like Like x 1
  4. Yeah! inside the two points
     
  5. get the world edit plugin .//wand then right click one block left click the other
     
    • Funny Funny x 2
  6. 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 Useful x 1
  7. 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++)
            blocks.add(world.getBlockAt(x, y, z));
      return blocks;
    }
     
     
    • Like Like x 1
    • Useful Useful x 1
  8. I don't want use WorldEdit. I want make my code-.
     
  9. crap, I forgot to take negative coordinates into account.
     
    • Friendly Friendly x 1
  10. Thanks you :D
    Thanks you! Now I underestand how to make it.