Skip to content

Instantly share code, notes, and snippets.

@hamza-cskn
Created September 26, 2021 08:49
Show Gist options
  • Save hamza-cskn/39188fd868e03571b71fbbc8cfc7cbac to your computer and use it in GitHub Desktop.
Save hamza-cskn/39188fd868e03571b71fbbc8cfc7cbac to your computer and use it in GitHub Desktop.
Creates a cuboid that is includes all locations of a locations list.
//to get main cuboid class: https://www.spigotmc.org/threads/region-cuboid.329859/
public static Cuboid convertCuboid(final List<Location> locations) {
int firstPosX = Integer.MIN_VALUE;
int firstPosY = Integer.MIN_VALUE;
int firstPosZ = Integer.MIN_VALUE;
int secondPosX = Integer.MAX_VALUE;
int secondPosY = Integer.MAX_VALUE;
int secondPosZ = Integer.MAX_VALUE;
World world = null;
for (final Location loc : locations) {
if (loc == null || loc.getWorld() == null) continue;
if (world == null) world = loc.getWorld();
if (world != loc.getWorld())
throw new IllegalStateException("world of location can not be different! (expected: " + world.getName() + ", but found: " + loc.getWorld().getName() + "");
final int x = loc.getBlockX();
final int z = loc.getBlockZ();
final int y = loc.getBlockY();
if (firstPosX < x) firstPosX = x;
if (firstPosY < y) firstPosY = y;
if (firstPosZ < z) firstPosZ = z;
if (secondPosX > x) secondPosX = x;
if (secondPosY > y) secondPosY = y;
if (secondPosZ > z) secondPosZ = z;
}
if (world == null) return null;
return new Cuboid(new Location(world, firstPosX, firstPosY, firstPosZ), new Location(world, secondPosX, secondPosY, secondPosZ));
}
@hamza-cskn
Copy link
Author

hamza-cskn commented Mar 11, 2022

image

Red: locations which is you'll give
Light Blue: cuboid which is method'll give
Dark Blue: space

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment