Skip to content

Instantly share code, notes, and snippets.

@jonahseguin
Created March 5, 2015 18:19
Show Gist options
  • Select an option

  • Save jonahseguin/c51a9562b1067b9c3892 to your computer and use it in GitHub Desktop.

Select an option

Save jonahseguin/c51a9562b1067b9c3892 to your computer and use it in GitHub Desktop.
A useful class for Minecraft cuboid locations
package com.shawckz.spawnprot;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
/**
* Created by 360 on 05/03/2015.
* @author Shawckz
*/
public class Cuboid {
private Location p1;
private Location p2;
public Cuboid(Location p1, Location p2) {
this.p1 = p1;
this.p2 = p2;
}
/**
* Returns a list of locations for each block that is on the wall of the cuboid (the perimeter)
* @return
*/
public List<Location> getPerimeter() {
List<Location> locations = new ArrayList<>();
double minX = p1.getX() < p2.getX() ? p1.getX() : p2.getX();
double minZ = p1.getZ() < p2.getZ() ? p1.getZ() : p2.getZ();
double maxX = p1.getX() > p2.getX() ? p1.getX() : p2.getX();
double maxZ = p1.getZ() > p2.getZ() ? p1.getZ() : p2.getZ();
double minY = p1.getY() < p2.getY() ? p1.getY() : p2.getY();
double maxY = p1.getY() > p2.getY() ? p1.getY() : p2.getY();
double x = minX;
for (double z = minZ; z <= maxZ; z++) {
for (double y = minY; y <= maxY; y++) {
locations.add(new Location(p1.getWorld(), x, y, z));
}
}
x = maxX;
for (double z = minZ; z <= maxZ; z++) {
for (double y = minY; y <= maxY; y++) {
locations.add(new Location(p1.getWorld(), x, y, z));
}
}
double z = minZ;
for (x = minX; x <= maxX; x++) {
for (double y = minY; y <= maxY; y++) {
locations.add(new Location(p1.getWorld(), x, y, z));
}
}
z = maxZ;
for (x = minX; x <= maxX; x++) {
for (double y = minY; y <= maxY; y++){
locations.add(new Location(p1.getWorld(), x, y, z));
}
}
return locations;
}
/**
* Returns if the specified location is within the 2 positions (cuboid)
* @param loc
* @return
*/
public boolean withinCuboid(Location loc) {
double minX = p1.getX() < p2.getX() ? p1.getX() : p2.getX();
double minZ = p1.getZ() < p2.getZ() ? p1.getZ() : p2.getZ();
double maxX = p1.getX() > p2.getX() ? p1.getX() : p2.getX();
double maxZ = p1.getZ() > p2.getZ() ? p1.getZ() : p2.getZ();
if (loc.getBlockX() > minX) {
if (loc.getBlockX() < maxX) {
if (loc.getBlockZ() > minZ) {
if (loc.getBlockZ() < maxZ) {
return true;
}
}
}
}
return false;
}
public Location getP2() {
return p2;
}
public void setP2(Location p2) {
this.p2 = p2;
}
public Location getP1() {
return p1;
}
public void setP1(Location p1) {
this.p1 = p1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment