Take blocks around from a location

Discussion in 'Spigot Plugin Development' started by JuanDouCore_, Dec 1, 2018.

  1. How about, well, what I need to do is that from setting a specific location I need to grab its blocks around. I can not think of a way to get hold of each location around the main location.
    Any ideas that you give me?
    Here I leave an image as an example, where red is the main location and blue are the locations that I need to grab from the red one.

    [​IMG]
    [​IMG]
     
  2. Code (Java):
            IntStream.rangeClosed(-1, 1)
                    .forEach(x -> IntStream.rangeClosed(-1, 1).forEach(z -> {
                     
                        Location origin = block.getLocation();
                        Location around = origin.add(new Location(origin.getWorld(), x, 0, z));
                     
                     
                    }));
    Keep in mind to check if around is equal to origin
     
  3. Had I understood, then around would be each Location that is obtained from that around. It would be convenient then to go putting in a list each Location that is obtained?
     
  4. As long as x & z are not 0 at the same time, add them in a collection so as to retrieve them later. If x & z are 0, then the location will be the original one.
     

  5. Did you mean to do this?
    Forgive my ignorance


    Code (Java):
            List<Location> locs = new ArrayList();
            IntStream.rangeClosed(-1, 1)
                    .forEach(x -> IntStream.rangeClosed(-1, 1).forEach(z -> {

                        Location origin = block.getLocation();
                        Location around = origin.add(new Location(origin.getWorld(), x, 0, z));
                       
                        if(x!=0&&z!=0) {
                            locs.add(around);
                        }

                    }));
     
    • A Set<Location> would be prefered unless you care about order and need the #get function that it List offers.
    • new ArrayList() should be new ArrayList<>()
    I'd probably change the code to:
    Code (Java):
    final Set<Location> locs = Sets.newHashSet();
           IntStream.rangeClosed(-1, 1).forEach(x -> IntStream.rangeClosed(-1, 1).forEach(z -> {
               if (x == 0 && z == 0) return;
               Location origin = block.getLocation().clone();
               Location around = origin.add(new Location(origin.getWorld(), x, 0, z));
               locs.add(around);
           }));
    No difference really but seems nicer to me. Pure preference.
     
  6. You can simply use Block#getRelative
    Code (Java):
    private static final BlockFace[] FACES = new BlockFace[] { BlockFace.NORTH_WEST, BlockFace.NORTH, BlockFace.NORTH_EAST, BlockFace.EAST, BlockFace.SOUTH_EAST, BlockFace.SOUTH, BlockFace.SOUTH_WEST, BlockFace.WEST }

    public static Set<Block> getBlocksAround(Location location) {
        Set<Block> blocks = new HashSet<>();
        Block base = location.getWorld().getBlockAt(location);
        for (BlockFace face : FACES) {
            blocks.add(base.getRelative(face));
        }
        return blocks;
    }
     
    • Agree Agree x 1