Block listed within cuboid x, y coodinates

Discussion in 'Plugin Development' started by Quackster, Oct 2, 2012.

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

    Quackster

    Hello...

    I've search everywhere and I really need a function that finds all blocks within a first x, y coordinate and a second x, y coodinate...

    If so, can someone tell me, pretty please?
     
  2. Offline

    andf54

    Its not hard to make, just do 3 loops:

    z1 = 0;
    z2 = world.getMaxHeight();

    for(int x = x1; x <= x2; x++){
    for(int y = y1; y <= y2; y++){
    for(int z = z1; z <= z2; z++){
    blocks.add(world.getBlockAt(x, y, z));
    }
    }
    }
     
  3. Offline

    Quackster

    Thanks for the response, but I've figured it out myself!

    Code:
        public static List<Block> blocksFromTwoPoints(Location loc1, Location loc2)
        {
            List<Block> blocks = new ArrayList<Block>();
     
            int topBlockX = (loc1.getBlockX() < loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
            int bottomBlockX = (loc1.getBlockX() > loc2.getBlockX() ? loc2.getBlockX() : loc1.getBlockX());
     
            int topBlockY = (loc1.getBlockY() < loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
            int bottomBlockY = (loc1.getBlockY() > loc2.getBlockY() ? loc2.getBlockY() : loc1.getBlockY());
     
            int topBlockZ = (loc1.getBlockZ() < loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
            int bottomBlockZ = (loc1.getBlockZ() > loc2.getBlockZ() ? loc2.getBlockZ() : loc1.getBlockZ());
     
            for(int x = bottomBlockX; x <= topBlockX; x++)
            {
                for(int z = bottomBlockZ; z <= topBlockZ; z++)
                {
                    for(int y = bottomBlockY; y <= topBlockY; y++)
                    {
                        Block block = loc1.getWorld().getBlockAt(x, y, z);
                       
                        blocks.add(block);
                    }
                }
            }
           
            return blocks;
        }
     
    Vanidor1424, Paxination and Neodork like this.
Thread Status:
Not open for further replies.

Share This Page