2019-01-22 00:44:01 +01:00
|
|
|
package world.bentobox.greenhouses.managers;
|
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
import java.util.Collection;
|
2019-01-22 00:44:01 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.Material;
|
2020-10-05 01:32:34 +02:00
|
|
|
import org.bukkit.Tag;
|
2019-01-22 00:44:01 +01:00
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.World.Environment;
|
2020-10-05 01:32:34 +02:00
|
|
|
import org.bukkit.block.Block;
|
2019-01-22 00:44:01 +01:00
|
|
|
|
|
|
|
import world.bentobox.greenhouses.data.Greenhouse;
|
|
|
|
import world.bentobox.greenhouses.greenhouse.Roof;
|
|
|
|
import world.bentobox.greenhouses.greenhouse.Walls;
|
|
|
|
import world.bentobox.greenhouses.managers.GreenhouseManager.GreenhouseResult;
|
|
|
|
|
|
|
|
public class GreenhouseFinder {
|
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
private Greenhouse gh = new Greenhouse();
|
2019-01-26 17:38:13 +01:00
|
|
|
private final Set<Location> redGlass = new HashSet<>();
|
2020-10-05 01:32:34 +02:00
|
|
|
// Counts
|
|
|
|
private int wallDoors = 0;
|
|
|
|
// Hoppers
|
|
|
|
private int ghHopper = 0;
|
|
|
|
// Air
|
|
|
|
private boolean airHoles = false;
|
|
|
|
// Other blocks
|
|
|
|
private boolean otherBlocks = false;
|
|
|
|
// Ceiling issue
|
|
|
|
private boolean inCeiling = false;
|
|
|
|
// The y height where other blocks were found
|
|
|
|
// If this is the bottom layer, the player has most likely uneven walls
|
|
|
|
private int otherBlockLayer = -1;
|
|
|
|
private int wallBlockCount;
|
2019-01-22 00:44:01 +01:00
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
class CounterCheck {
|
|
|
|
int doorCount;
|
|
|
|
int hopperCount;
|
|
|
|
boolean airHole;
|
|
|
|
boolean otherBlock;
|
|
|
|
}
|
2019-01-22 00:44:01 +01:00
|
|
|
|
2020-08-23 00:52:18 +02:00
|
|
|
/**
|
|
|
|
* Find out if there is a greenhouse here
|
|
|
|
* @param location - start location
|
|
|
|
* @return GreenhouseResult class
|
|
|
|
*/
|
2019-01-22 00:44:01 +01:00
|
|
|
public Set<GreenhouseResult> find(Location location) {
|
|
|
|
Set<GreenhouseResult> result = new HashSet<>();
|
|
|
|
redGlass.clear();
|
|
|
|
|
|
|
|
// Find the roof
|
|
|
|
Roof roof = new Roof(location);
|
|
|
|
if (!roof.isRoofFound()) {
|
|
|
|
result.add(GreenhouseResult.FAIL_NO_ROOF);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
// Find the walls
|
2020-10-11 18:03:13 +02:00
|
|
|
Walls walls = new Walls().findWalls(roof);
|
2019-01-22 00:44:01 +01:00
|
|
|
// Make the initial greenhouse
|
2019-01-23 05:54:43 +01:00
|
|
|
gh = new Greenhouse(location.getWorld(), walls, roof.getHeight());
|
2019-01-22 00:44:01 +01:00
|
|
|
// Set the original biome
|
|
|
|
gh.setOriginalBiome(location.getBlock().getBiome());
|
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
// Now check to see if the floor really is the floor and the walls follow the rules
|
|
|
|
result.addAll(checkGreenhouse(gh, roof, walls));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-01-22 00:44:01 +01:00
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
Set<GreenhouseResult> checkGreenhouse(Greenhouse gh2, Roof roof, Walls walls) {
|
|
|
|
Set<GreenhouseResult> result = new HashSet<>();
|
|
|
|
World world = roof.getLocation().getWorld();
|
2019-01-26 17:38:13 +01:00
|
|
|
int y;
|
2019-01-22 00:44:01 +01:00
|
|
|
for (y = world.getMaxHeight() - 1; y >= walls.getFloor(); y--) {
|
2020-10-05 01:32:34 +02:00
|
|
|
CounterCheck cc = new CounterCheck();
|
2019-01-22 00:44:01 +01:00
|
|
|
wallBlockCount = 0;
|
2020-10-05 01:32:34 +02:00
|
|
|
for (int x = walls.getMinX(); x <= walls.getMaxX(); x++) {
|
|
|
|
for (int z = walls.getMinZ(); z <= walls.getMaxZ(); z++) {
|
|
|
|
result.addAll(checkBlock(cc, roof, walls, world.getBlockAt(x, y, z)));
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wallBlockCount == 0 && y < roof.getHeight()) {
|
|
|
|
// This is the floor
|
|
|
|
break;
|
|
|
|
} else {
|
2020-10-05 01:32:34 +02:00
|
|
|
wallDoors += cc.doorCount;
|
|
|
|
ghHopper += cc.hopperCount;
|
|
|
|
if (cc.airHole) {
|
2019-01-22 00:44:01 +01:00
|
|
|
airHoles = true;
|
|
|
|
}
|
2020-10-05 01:32:34 +02:00
|
|
|
if (cc.otherBlock) {
|
2019-01-22 00:44:01 +01:00
|
|
|
otherBlocks = true;
|
|
|
|
if (otherBlockLayer < 0) {
|
|
|
|
otherBlockLayer = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 01:32:34 +02:00
|
|
|
|
|
|
|
result.addAll(checkErrors(roof, y));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Collection<GreenhouseResult> checkErrors(Roof roof, int y) {
|
|
|
|
Set<GreenhouseResult> result = new HashSet<>();
|
2019-01-22 00:44:01 +01:00
|
|
|
// Check that the player is vertically in the greenhouse
|
|
|
|
if (roof.getLocation().getBlockY() <= y) {
|
|
|
|
result.add(GreenhouseResult.FAIL_BELOW);
|
|
|
|
}
|
|
|
|
// Show errors
|
2019-11-01 05:36:05 +01:00
|
|
|
if (airHoles && !inCeiling) {
|
2019-01-22 00:44:01 +01:00
|
|
|
result.add(GreenhouseResult.FAIL_HOLE_IN_WALL);
|
2019-11-01 05:36:05 +01:00
|
|
|
} else if (airHoles && inCeiling) {
|
2019-01-22 00:44:01 +01:00
|
|
|
result.add(GreenhouseResult.FAIL_HOLE_IN_ROOF);
|
|
|
|
}
|
|
|
|
if (otherBlocks && otherBlockLayer == y + 1) {
|
|
|
|
// Walls must be even all the way around
|
|
|
|
result.add(GreenhouseResult.FAIL_UNEVEN_WALLS);
|
|
|
|
} else if (otherBlocks && otherBlockLayer == roof.getHeight()) {
|
|
|
|
// Roof blocks must be glass, glowstone, doors or a hopper.
|
|
|
|
result.add(GreenhouseResult.FAIL_BAD_ROOF_BLOCKS);
|
|
|
|
} else if (otherBlocks) {
|
|
|
|
// "Wall blocks must be glass, glowstone, doors or a hopper.
|
|
|
|
result.add(GreenhouseResult.FAIL_BAD_WALL_BLOCKS);
|
|
|
|
}
|
|
|
|
if (wallDoors > 8) {
|
|
|
|
result.add(GreenhouseResult.FAIL_TOO_MANY_DOORS);
|
|
|
|
}
|
|
|
|
if (ghHopper > 1) {
|
|
|
|
result.add(GreenhouseResult.FAIL_TOO_MANY_HOPPERS);
|
|
|
|
}
|
2020-10-05 01:32:34 +02:00
|
|
|
return result;
|
|
|
|
}
|
2019-01-22 00:44:01 +01:00
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
Set<GreenhouseResult> checkBlock(CounterCheck cc, Roof roof, Walls walls, Block block) {
|
|
|
|
Set<GreenhouseResult> result = new HashSet<>();
|
|
|
|
World world = block.getWorld();
|
|
|
|
// Checking above greenhouse - no blocks allowed
|
|
|
|
if (block.getY() > roof.getHeight()) {
|
|
|
|
// We are above the greenhouse
|
|
|
|
if (!world.getEnvironment().equals(Environment.NETHER) && !block.isEmpty()) {
|
|
|
|
result.add(GreenhouseResult.FAIL_BLOCKS_ABOVE);
|
|
|
|
redGlass.add(block.getLocation());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Check just the walls
|
|
|
|
checkWalls(block, roof, walls, cc);
|
|
|
|
}
|
2019-01-22 00:44:01 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
/**
|
|
|
|
* Check a wall block
|
|
|
|
* @param block - block
|
|
|
|
* @param roof - roof object
|
|
|
|
* @param walls - wall object
|
|
|
|
* @param cc - count
|
|
|
|
* @return true if block was in the wall
|
|
|
|
*/
|
|
|
|
boolean checkWalls(Block block, Roof roof, Walls walls, CounterCheck cc) {
|
|
|
|
int x = block.getX();
|
|
|
|
int y = block.getY();
|
|
|
|
int z = block.getZ();
|
|
|
|
// Check wall blocks only
|
|
|
|
if (y == roof.getHeight() || x == walls.getMinX() || x == walls.getMaxX() || z == walls.getMinZ() || z== walls.getMaxZ()) {
|
|
|
|
// Check for non-wall blocks or non-roof blocks at the top of walls
|
|
|
|
if ((y != roof.getHeight() && !Walls.wallBlocks(block.getType())) || (y == roof.getHeight() && !Roof.roofBlocks(block.getType()))) {
|
|
|
|
if (block.isEmpty()) {
|
|
|
|
cc.airHole = true;
|
|
|
|
if (y == roof.getHeight()) {
|
|
|
|
inCeiling = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cc.otherBlock = true;
|
|
|
|
}
|
|
|
|
redGlass.add(block.getLocation());
|
|
|
|
} else {
|
|
|
|
// Normal wall blocks
|
|
|
|
wallBlockCount++;
|
|
|
|
checkDoorsHoppers(cc, block);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkDoorsHoppers(CounterCheck cc, Block block) {
|
|
|
|
// Count doors
|
|
|
|
if (Tag.DOORS.isTagged(block.getType())) {
|
|
|
|
cc.doorCount++;
|
|
|
|
// If we already have 8 doors add these blocks to the red list
|
|
|
|
if (wallDoors == 8) {
|
|
|
|
redGlass.add(block.getLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Count hoppers
|
|
|
|
if (block.getType().equals(Material.HOPPER)) {
|
|
|
|
cc.hopperCount++;
|
|
|
|
if (ghHopper > 0) {
|
|
|
|
// Problem! Add extra hoppers to the red glass list
|
|
|
|
redGlass.add(block.getLocation());
|
|
|
|
} else {
|
|
|
|
// This is the first hopper
|
|
|
|
gh.setRoofHopperLocation(block.getLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 00:44:01 +01:00
|
|
|
/**
|
2019-01-23 05:54:43 +01:00
|
|
|
* @return the greenhouse
|
2019-01-22 00:44:01 +01:00
|
|
|
*/
|
|
|
|
public Greenhouse getGh() {
|
|
|
|
return gh;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the redGlass
|
|
|
|
*/
|
|
|
|
public Set<Location> getRedGlass() {
|
|
|
|
return redGlass;
|
|
|
|
}
|
|
|
|
|
2020-10-05 01:32:34 +02:00
|
|
|
/**
|
|
|
|
* @return the wallDoors
|
|
|
|
*/
|
|
|
|
int getWallDoors() {
|
|
|
|
return wallDoors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param wallDoors the wallDoors to set
|
|
|
|
*/
|
|
|
|
void setWallDoors(int wallDoors) {
|
|
|
|
this.wallDoors = wallDoors;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the ghHopper
|
|
|
|
*/
|
|
|
|
int getGhHopper() {
|
|
|
|
return ghHopper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the airHoles
|
|
|
|
*/
|
|
|
|
boolean isAirHoles() {
|
|
|
|
return airHoles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the otherBlocks
|
|
|
|
*/
|
|
|
|
boolean isOtherBlocks() {
|
|
|
|
return otherBlocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the inCeiling
|
|
|
|
*/
|
|
|
|
boolean isInCeiling() {
|
|
|
|
return inCeiling;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the otherBlockLayer
|
|
|
|
*/
|
|
|
|
int getOtherBlockLayer() {
|
|
|
|
return otherBlockLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the wallBlockCount
|
|
|
|
*/
|
|
|
|
int getWallBlockCount() {
|
|
|
|
return wallBlockCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ghHopper the ghHopper to set
|
|
|
|
*/
|
|
|
|
void setGhHopper(int ghHopper) {
|
|
|
|
this.ghHopper = ghHopper;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param airHoles the airHoles to set
|
|
|
|
*/
|
|
|
|
void setAirHoles(boolean airHoles) {
|
|
|
|
this.airHoles = airHoles;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param otherBlocks the otherBlocks to set
|
|
|
|
*/
|
|
|
|
void setOtherBlocks(boolean otherBlocks) {
|
|
|
|
this.otherBlocks = otherBlocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param inCeiling the inCeiling to set
|
|
|
|
*/
|
|
|
|
void setInCeiling(boolean inCeiling) {
|
|
|
|
this.inCeiling = inCeiling;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param otherBlockLayer the otherBlockLayer to set
|
|
|
|
*/
|
|
|
|
void setOtherBlockLayer(int otherBlockLayer) {
|
|
|
|
this.otherBlockLayer = otherBlockLayer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param wallBlockCount the wallBlockCount to set
|
|
|
|
*/
|
|
|
|
void setWallBlockCount(int wallBlockCount) {
|
|
|
|
this.wallBlockCount = wallBlockCount;
|
|
|
|
}
|
|
|
|
|
2019-01-22 00:44:01 +01:00
|
|
|
}
|