So I've been looking for a way to get a cuboid's blocks, like say I have 2 block selections and I try to get all the blocks in between those two sections, I've looked for many ways to do this but haven't found any to this point and was wondering if someone can link me to a thread with the answer or give me some math that finds it or something like that, I'd be very grateful if someone could help me out. For anyone wondering no I'm not trying to recreate worldedit or anything like that, it's basically just schematic building and stuff like that. Thanks for anyone who helps in advance. Sorry if this thread is redundant and asking for something simple or something that has already been answered as I did look for it for about 1 hour or so.
No, I'm not using WorldEditAPI, it would cause a need for an unneeded dependency, I'm sure there's a simple 10-20 line answer and I'd rather not have a huge plugin required for a 10-20 line answer, thanks for the suggestion though. I'm just going brain dead right now and not knowing how to do the math for it x.x
Nested for loops are beautiful things. This is pseudoish. Code (Text): for (int minX = blah; minX <= maxX; minX++) for (int minY = blah; minY <= maxY; minY++) for (int minZ = blah; minZ <= maxZ; minZ++) world.getBlockAt(minX, minY, minZ); // Here's your block
How would I get maxX and minX, what if the 2 positions have the same X or the same Y, then it won't work right? or?
If they're equal, it won't matter which one is which. That'll test from min to max inclusively. Post your code
@Msrules123 Code (Java): World world = pos1.getWorld(); Block min = new Location(pos1.getWorld(), Math.min(pos1.getX(), pos2.getX()), Math.min(pos1.getY(), pos2.getY()), Math.min(pos1.getZ(), pos2.getZ())).getBlock(); Block max = new Location(pos1.getWorld(), Math.max(pos1.getX(), pos2.getX()), Math.max(pos1.getY(), pos2.getY()), Math.max(pos1.getZ(), pos2.getZ())).getBlock(); int maxX = max.getX(); int maxY = max.getY(); int maxZ = max.getZ(); for(int x = min.getX(); x < maxX; x++) for(int y = min.getX(); y < maxY; y++) for(int z = min.getX(); z < maxZ; z++) world.getBlockAt(x, y, z).setType(Material.OBSIDIAN); This is my current code I'm using. I've also used it to where the blocks are just he positions set, the positions btw are just the 2 blocks I've setup.
Code (Java): int maxX = max.getX(); int maxY = max.getY(); int maxZ = max.getZ(); for(int x = min.getX(); x < maxX; x++) for(int y = min.getY(); y < maxY; y++) for(int z = min.getZ(); z < maxZ; z++) world.getBlockAt(x, y, z).setType(Material.OBSIDIAN); Where? doesn't the min need to be that value?
Code (Java): public static final void create(final Block pos1, final Block pos2) { final World world = pos1.getWorld(); final Block min = new Location(pos1.getWorld(), Math.min(pos1.getX(), pos2.getX()), Math.min(pos1.getY(), pos2.getY()), Math.min(pos1.getZ(), pos2.getZ())).getBlock(); final Block max = new Location(pos1.getWorld(), Math.max(pos1.getX(), pos2.getX()), Math.max(pos1.getY(), pos2.getY()), Math.max(pos1.getZ(), pos2.getZ())).getBlock(); final int maxX = max.getX(); final int maxY = max.getY(); final int maxZ = max.getZ(); for(int minX = min.getX(); minX < maxX; minX++) for(int minY = min.getY(); minY < maxY; minY++) for(int minZ = min.getZ(); minZ < maxZ; minZ++) world.getBlockAt(minX, minY, minZ).setType(Material.OBSIDIAN); } This is my current code, I've tried it in game and it doesn't set anything to obsidian.
You can convert the location of the opposite sides of the cuboid to a Vector and then get the minimum vector and maximum vector using Vector#getMaximum and Vector#getMinimum Then do as @Msrules123 said.
Yeah was just about to thank you for writing that in your pseudo code xD, solved with final code as: Code (Java): public static final void create(final Block pos1, final Block pos2) { final World world = pos1.getWorld(); final Block min = new Location(pos1.getWorld(), Math.min(pos1.getX(), pos2.getX()), Math.min(pos1.getY(), pos2.getY()), Math.min(pos1.getZ(), pos2.getZ())).getBlock(); final Block max = new Location(pos1.getWorld(), Math.max(pos1.getX(), pos2.getX()), Math.max(pos1.getY(), pos2.getY()), Math.max(pos1.getZ(), pos2.getZ())).getBlock(); final int maxX = max.getX(); final int maxY = max.getY(); final int maxZ = max.getZ(); for (int minX = min.getX(); minX <= maxX; minX++) for (int minY = min.getY(); minY <= maxY; minY++) for (int minZ = min.getZ(); minZ <= maxZ; minZ++) world.getBlockAt(minX, minY, minZ).setType(Material.OBSIDIAN); } Thanks everyone for their support! I appreciate it.