Greenhouses/src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java

190 lines
6.6 KiB
Java
Raw Normal View History

2019-01-19 16:52:04 +01:00
package world.bentobox.greenhouses.greenhouse;
import java.util.Arrays;
import java.util.Collections;
2019-01-19 16:52:04 +01:00
import java.util.List;
2019-01-22 00:44:01 +01:00
import java.util.stream.Collectors;
2019-01-19 16:52:04 +01:00
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.util.Vector;
/**
* Contains the parameters of a greenhouse roof
* @author tastybento
*
*/
2019-11-01 05:51:24 +01:00
public class Roof extends MinMaxXZ {
public static final List<Material> ROOF_BLOCKS;
static {
List<Material> r = Arrays.stream(Material.values())
2019-11-01 05:36:05 +01:00
.filter(Material::isBlock) // Blocks only, no items
.filter(m -> !m.name().contains("DOOR")) // No doors
.filter(m -> m.name().contains("TRAPDOOR") // All trapdoors
|| m.name().contains("GLASS") // All glass blocks
|| m.equals(Material.HOPPER) // Hoppers
|| m.equals(Material.GLOWSTONE)) // Glowstone
.collect(Collectors.toList());
ROOF_BLOCKS = Collections.unmodifiableList(r);
2019-11-01 05:36:05 +01:00
}
private final Location location;
private int height;
private boolean roofFound;
2019-11-01 05:36:05 +01:00
2019-01-19 16:52:04 +01:00
/**
* Finds a roof from a starting location under the roof and characterizes it
2019-01-26 17:38:13 +01:00
* @param loc - starting location
2019-01-19 16:52:04 +01:00
*/
2019-01-22 00:44:01 +01:00
public Roof(Location loc) {
this.location = loc;
roofFound = findRoof(loc);
}
private boolean findRoof(Location loc) {
2019-01-19 16:52:04 +01:00
World world = loc.getWorld();
// This section tries to find a roof block
// Try just going up - this covers every case except if the player is standing under a hole
roofFound = false;
// This does a ever-growing check around the player to find a wall block. It is possible for the player
2019-01-26 17:38:13 +01:00
// to be outside the greenhouse in this situation, so a check is done later to make sure the player is inside
2019-01-19 16:52:04 +01:00
int roofY = loc.getBlockY();
for (int y = roofY; y < world.getMaxHeight(); y++) {
if (ROOF_BLOCKS.contains(world.getBlockAt(loc.getBlockX(),y,loc.getBlockZ()).getType())) {
roofFound = true;
loc = new Location(world,loc.getBlockX(),y,loc.getBlockZ());
break;
}
}
2019-01-19 16:52:04 +01:00
// If the roof was not found start going around in circles until something is found
// Expand in ever increasing squares around location until a wall block is found
for (int radius = 0; radius < 3 && !roofFound; radius++) {
for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius && !roofFound; x++) {
for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius && !roofFound; z++) {
if (!((x > loc.getBlockX() - radius && x < loc.getBlockX() + radius) && (z > loc.getBlockZ() - radius && z < loc.getBlockZ() + radius))) {
2019-10-14 20:39:50 +02:00
Block b = world.getBlockAt(x, roofY, z);
if (!Walls.WALL_BLOCKS.contains(b.getType())) {
2019-01-19 16:52:04 +01:00
// Look up
for (int y = roofY; y < world.getMaxHeight() && !roofFound; y++) {
if (ROOF_BLOCKS.contains(world.getBlockAt(x,y,z).getType())) {
2019-01-19 16:52:04 +01:00
roofFound = true;
loc = new Location(world,x,y,z);
}
}
}
}
}
}
}
if (!roofFound) return false;
2019-01-19 16:52:04 +01:00
// Record the height
this.height = loc.getBlockY();
// Now we have a roof block, find how far we can go NSWE
minX = loc.getBlockX();
maxX = loc.getBlockX();
minZ = loc.getBlockZ();
maxZ = loc.getBlockZ();
2019-10-14 20:39:50 +02:00
expandCoords(world, loc.toVector());
2019-01-26 17:38:13 +01:00
int minx;
int maxx;
int minz;
int maxz;
2019-01-19 16:52:04 +01:00
// Now we have some idea of the mins and maxes, check each block and see if it goes further
do {
minx = minX;
maxx = maxX;
minz = minZ;
maxz = maxZ;
for (int x = minx; x <= maxx; x++) {
for (int z = minz; z <= maxz; z++) {
// This will push out the coords if possible
2019-10-14 20:39:50 +02:00
expandCoords(world, new Vector(x, loc.getBlockY(), z));
2019-01-19 16:52:04 +01:00
}
}
// Repeat until nothing changes
} while (minx != minX || maxx != maxX || minz != minZ || maxz != maxZ);
// That's as much as we can do!
return true;
2019-01-19 16:52:04 +01:00
}
/**
* This takes any location and tries to go as far as possible in NWSE directions finding contiguous roof blocks
* up to 100 in any direction
2019-01-26 17:38:13 +01:00
* @param height - location to start search
2019-01-19 16:52:04 +01:00
*/
2019-10-14 20:39:50 +02:00
private void expandCoords(World world, Vector height) {
Location maxx = height.toLocation(world);
Location minx = height.toLocation(world);
Location maxz = height.toLocation(world);
Location minz = height.toLocation(world);
2019-01-19 16:52:04 +01:00
int limit = 0;
while (ROOF_BLOCKS
2019-10-14 20:39:50 +02:00
.contains(world.getBlockAt(maxx).getType()) && limit < 100) {
2019-01-19 16:52:04 +01:00
limit++;
maxx.add(new Vector(1,0,0));
}
if (maxx.getBlockX()-1 > maxX) {
maxX = maxx.getBlockX()-1;
}
while (ROOF_BLOCKS.contains(world.getBlockAt(minx).getType()) && limit < 200) {
2019-01-19 16:52:04 +01:00
limit++;
minx.subtract(new Vector(1,0,0));
}
if (minx.getBlockX() + 1 < minX) {
minX = minx.getBlockX() + 1;
}
while (ROOF_BLOCKS.contains(world.getBlockAt(maxz).getType()) && limit < 300) {
2019-01-19 16:52:04 +01:00
limit++;
maxz.add(new Vector(0,0,1));
2019-01-22 00:44:01 +01:00
}
2019-01-19 16:52:04 +01:00
if (maxz.getBlockZ() - 1 > maxZ) {
maxZ = maxz.getBlockZ() - 1;
}
while (ROOF_BLOCKS.contains(world.getBlockAt(minz).getType()) && limit < 400) {
2019-01-19 16:52:04 +01:00
limit++;
minz.subtract(new Vector(0,0,1));
}
if (minz.getBlockZ() + 1 < minZ) {
minZ = minz.getBlockZ() + 1;
}
}
2019-11-01 05:36:05 +01:00
2019-01-19 16:52:04 +01:00
/**
* @return the roofFound
*/
public boolean isRoofFound() {
return roofFound;
}
/**
* @return the height
*/
public int getHeight() {
return height;
}
/**
2019-01-22 00:44:01 +01:00
* @return the location
2019-01-19 16:52:04 +01:00
*/
2019-01-22 00:44:01 +01:00
public Location getLocation() {
return location;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Roof [location=" + location + ", minX=" + minX + ", maxX=" + maxX + ", minZ=" + minZ + ", maxZ=" + maxZ
+ ", height=" + height + ", roofFound=" + roofFound + "]";
2019-01-19 16:52:04 +01:00
}
}