Solved Get blocks between coordinates

Discussion in 'Spigot Plugin Development' started by Minimigamer, Feb 1, 2020.

  1. hey guys im trying to make a method wich takes 2 coordinates iterates through the Layers bewteen the coordinates, iterates through the rows in each layer, iterates through each block in each row and if the block isnt air it add the block to a blockList

    My code:

    Code (Text):

        public static List<Block> getBlocks() {
            Location loc1 = Main.loc1;
            Location loc2 = Main.loc2;
            Location loc3 = loc1;
            List<Block> blockList = null;

            if (loc1.getBlockY() > loc2.getBlockY()) {
                for (int i = loc1.getBlockY(); i != loc2.getBlockY(); i--) {
                    for(int a = loc1.getBlockX(); a != loc2.getBlockX(); a--) {
                        for(int z = loc1.getBlockZ(); z != loc2.getBlockZ(); z++) {
                            loc3 = new Location(loc1.getWorld(), a, i, z);
                            if(loc3.getBlock().getType() != Material.AIR) {
                                blockList.add(loc3.getBlock());
                            }
                        }
                    }
                }
            }
            return blockList;
        }
     
  2. That is great! Might we know what your problem is as well?
     
  3. This basically means means null.add(..) so your code probably throws a NullPointerException
     
    • Like Like x 1
  4. You need to check if block != null since not only Material.AIR equals NULL
     
    • Like Like x 1
  5. First, Material.AIR is not null.
    Secondly, I meant that the List is null.
     
    • Like Like x 1
  6. Im not even answering to you, Im completing your comment lol. Btw, yes Material.AIR returns null and so does Material.BARRIER when you check for a block.

    But yes, the list is null and hasnt been initialized
     
    • Funny Funny x 2
    • Like Like x 1
  7. It works now

    so now my code looks like this:
    Code (Text):

    public class Methods {
        public static void set() {
            List<Block> blockList = getBlocks();
            for(Block b : blockList) {
                b.setType(Material.DIAMOND_BLOCK);
            }
        }

        public static List<Block> getBlocks() {
            Location loc1 = Main.loc1;
            Location loc2 = Main.loc2;
            Location loc3 = loc1;
            List<Block> blockList = new ArrayList<Block>();

            if (loc1.getBlockY() > loc2.getBlockY()) {
                for (int i = loc1.getBlockY(); i != loc2.getBlockY(); i--) {
                    for(int a = loc1.getBlockX(); a != loc2.getBlockX(); a--) {
                        for(int z = loc1.getBlockZ(); z != loc2.getBlockZ(); z++) {
                            loc3 = new Location(loc1.getWorld(), a, i, z);
                            Bukkit.broadcastMessage("" + loc3.getBlockX() + " "+ loc3.getBlockY() + " "+ loc3.getBlockZ() + " ");
                            if(loc3.getBlock() != null) {
                                blockList.add(loc3.getBlock());
                            }
                        }
                    }
                }
            }
            return blockList;
        }
    }
     
    #12 Minimigamer, Feb 4, 2020
    Last edited: Feb 4, 2020
  8. and it works now?