Skip to content

Commit

Permalink
Added GreenhouseFinder test class
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Oct 4, 2020
1 parent a96a926 commit 2419e40
Show file tree
Hide file tree
Showing 2 changed files with 593 additions and 81 deletions.
299 changes: 218 additions & 81 deletions src/main/java/world/bentobox/greenhouses/managers/GreenhouseFinder.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package world.bentobox.greenhouses.managers;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;

import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.greenhouse.Roof;
Expand All @@ -15,9 +18,29 @@

public class GreenhouseFinder {

private Greenhouse gh;
private Greenhouse gh = new Greenhouse();
private final Set<Location> redGlass = new HashSet<>();
// 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;

class CounterCheck {
int doorCount;
int hopperCount;
boolean airHole;
boolean otherBlock;
}

/**
* Find out if there is a greenhouse here
Expand All @@ -41,105 +64,48 @@ public Set<GreenhouseResult> find(Location location) {
// Set the original biome
gh.setOriginalBiome(location.getBlock().getBiome());

// Now check again to see if the floor really is the floor and the walls follow the rules
World world = roof.getLocation().getWorld();
int minX = walls.getMinX();
int minZ = walls.getMinZ();
int maxX = walls.getMaxX();
int maxZ = walls.getMaxZ();

// Counts
int wallDoors = 0;
// Hoppers
int ghHopper = 0;
// Air
boolean airHoles = false;
// Other blocks
boolean otherBlocks = false;
// Ceiling issue
boolean inCeiling = false;
// The y height where other blocks were found
// If this is the bottom layer, the player has most likely uneven walls
int otherBlockLayer = -1;
int wallBlockCount;
// 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;
}

Set<GreenhouseResult> checkGreenhouse(Greenhouse gh2, Roof roof, Walls walls) {
Set<GreenhouseResult> result = new HashSet<>();
World world = roof.getLocation().getWorld();
int y;
for (y = world.getMaxHeight() - 1; y >= walls.getFloor(); y--) {
int doorCount = 0;
int hopperCount = 0;
boolean airHole = false;
boolean otherBlock = false;
CounterCheck cc = new CounterCheck();
wallBlockCount = 0;
for (int x = minX; x <= maxX; x++) {
for (int z = minZ; z <= maxZ; z++) {
Location thisBlock = new Location(world, x, y, z);
Material blockType = world.getBlockAt(x, y, z).getType();
// Checking above greenhouse - no blocks allowed
if (y > roof.getHeight()) {
// We are above the greenhouse
if ((world.getEnvironment().equals(Environment.NORMAL) || world.getEnvironment().equals(Environment.THE_END))
&& blockType != Material.AIR) {
result.add(GreenhouseResult.FAIL_BLOCKS_ABOVE);
redGlass.add(thisBlock);
}
} else {
// Check just the walls
if (y == roof.getHeight() || x == minX || x == maxX || z == minZ || z== maxZ) {
if ((y != roof.getHeight() && !Walls.wallBlocks(blockType))
|| (y == roof.getHeight() && !Roof.roofBlocks(blockType))) {
//logger(2,"DEBUG: bad block found at " + x + "," + y+ "," + z + " " + blockType);
if (blockType == Material.AIR) {
airHole = true;
if (y == roof.getHeight()) {
inCeiling = true;
}
} else {
otherBlock = true;
}
redGlass.add(thisBlock);
} else {
wallBlockCount++;
// A string comparison is used to capture 1.8+ door types without stopping pre-1.8
// servers from working
if (blockType.toString().contains("DOOR")) {
doorCount++;
// If we already have 8 doors add these blocks to the red list
if (wallDoors == 8) {
redGlass.add(thisBlock);
}
}
if (blockType.equals(Material.HOPPER)) {
hopperCount++;
if (ghHopper > 0) {
// Problem! Add extra hoppers to the red glass list
redGlass.add(thisBlock);
} else {
// This is the first hopper
gh.setRoofHopperLocation(thisBlock);
}
}
}
}
}
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)));
}
}
if (wallBlockCount == 0 && y < roof.getHeight()) {
// This is the floor
break;
} else {
wallDoors += doorCount;
ghHopper += hopperCount;
if (airHole) {
wallDoors += cc.doorCount;
ghHopper += cc.hopperCount;
if (cc.airHole) {
airHoles = true;
}
if (otherBlock) {
if (cc.otherBlock) {
otherBlocks = true;
if (otherBlockLayer < 0) {
otherBlockLayer = y;
}
}
}
}

result.addAll(checkErrors(roof, y));
return result;
}

Collection<GreenhouseResult> checkErrors(Roof roof, int y) {
Set<GreenhouseResult> result = new HashSet<>();
// Check that the player is vertically in the greenhouse
if (roof.getLocation().getBlockY() <= y) {
result.add(GreenhouseResult.FAIL_BELOW);
Expand All @@ -166,10 +132,83 @@ public Set<GreenhouseResult> find(Location location) {
if (ghHopper > 1) {
result.add(GreenhouseResult.FAIL_TOO_MANY_HOPPERS);
}
return result;
}

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);
}
return result;
}

/**
* 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());
}
}
}

/**
* @return the greenhouse
*/
Expand All @@ -184,4 +223,102 @@ public Set<Location> getRedGlass() {
return redGlass;
}

/**
* @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;
}

}

0 comments on commit 2419e40

Please sign in to comment.