2019-01-19 16:52:04 +01:00
|
|
|
package world.bentobox.greenhouses.greenhouse;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
2019-11-23 05:58:32 +01:00
|
|
|
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;
|
|
|
|
|
2020-08-23 00:52:18 +02:00
|
|
|
import world.bentobox.greenhouses.Greenhouses;
|
|
|
|
|
2019-01-19 16:52:04 +01:00
|
|
|
/**
|
|
|
|
* Contains the parameters of a greenhouse roof
|
|
|
|
* @author tastybento
|
|
|
|
*
|
|
|
|
*/
|
2019-11-01 05:51:24 +01:00
|
|
|
public class Roof extends MinMaxXZ {
|
2020-08-23 00:52:18 +02:00
|
|
|
private static final List<Material> ROOF_BLOCKS;
|
2019-11-23 05:58:32 +01:00
|
|
|
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("TRAPDOOR") // All trapdoors
|
|
|
|
|| m.name().contains("GLASS") // All glass blocks
|
2020-08-23 00:52:18 +02:00
|
|
|
|| m.equals(Material.HOPPER)) // Hoppers
|
2019-11-01 05:36:05 +01:00
|
|
|
.collect(Collectors.toList());
|
2019-11-23 05:58:32 +01:00
|
|
|
ROOF_BLOCKS = Collections.unmodifiableList(r);
|
2019-11-01 05:36:05 +01:00
|
|
|
}
|
2019-11-23 05:58:32 +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;
|
2019-11-12 20:38:17 +01:00
|
|
|
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;
|
2019-11-23 06:23:42 +01:00
|
|
|
|
|
|
|
// 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();
|
2019-11-23 06:23:42 +01:00
|
|
|
for (int y = roofY; y < world.getMaxHeight(); y++) {
|
2020-08-23 00:52:18 +02:00
|
|
|
if (roofBlocks(world.getBlockAt(loc.getBlockX(),y,loc.getBlockZ()).getType())) {
|
2019-11-23 06:23:42 +01:00
|
|
|
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
|
2020-10-11 17:08:17 +02:00
|
|
|
spiralSearch(loc, roofY);
|
2019-11-12 20:38:17 +01:00
|
|
|
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!
|
2019-11-12 20:38:17 +01:00
|
|
|
return true;
|
2019-01-19 16:52:04 +01:00
|
|
|
}
|
|
|
|
|
2020-10-11 17:08:17 +02:00
|
|
|
private void spiralSearch(Location loc, int roofY) {
|
|
|
|
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))) {
|
|
|
|
checkVertically(loc, x, roofY, z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkVertically(Location loc, int x, int roofY, int z) {
|
|
|
|
World world = loc.getWorld();
|
|
|
|
Block b = world.getBlockAt(x, roofY, z);
|
|
|
|
if (!Walls.wallBlocks(b.getType())) {
|
|
|
|
// Look up
|
|
|
|
for (int y = roofY; y < world.getMaxHeight() && !roofFound; y++) {
|
|
|
|
if (roofBlocks(world.getBlockAt(x,y,z).getType())) {
|
|
|
|
roofFound = true;
|
|
|
|
loc = new Location(world,x,y,z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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;
|
2019-11-23 05:58:32 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-23 00:52:18 +02:00
|
|
|
while (roofBlocks(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;
|
|
|
|
}
|
|
|
|
|
2020-08-23 00:52:18 +02:00
|
|
|
while (roofBlocks(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;
|
|
|
|
}
|
|
|
|
|
2020-08-23 00:52:18 +02:00
|
|
|
while (roofBlocks(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
|
|
|
|
2020-08-23 00:52:18 +02:00
|
|
|
/**
|
|
|
|
* Check if material is a roof material
|
|
|
|
* @param m - material
|
|
|
|
* @return true if roof material
|
|
|
|
*/
|
|
|
|
public static boolean roofBlocks(Material m) {
|
|
|
|
return ROOF_BLOCKS.contains(m) || (m.equals(Material.GLOWSTONE) && Greenhouses.getInstance().getSettings().isAllowGlowstone());
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|