Skip to content

Commit

Permalink
Limits roof glass search to a radius of 3 instead of 100
Browse files Browse the repository at this point in the history
Search was taking too long if there was no glass and timing out the
server.
#31
  • Loading branch information
tastybento committed Nov 12, 2019
1 parent 0845706 commit c8008e3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
public class Roof extends MinMaxXZ {
private final Location location;
private final int height;
private int height;
private boolean roofFound;

/**
Expand All @@ -40,6 +40,11 @@ public static List<Material> getRoofBlocks() {
*/
public Roof(Location loc) {
this.location = loc;
roofFound = findRoof(loc);
}


private boolean findRoof(Location loc) {
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
Expand All @@ -49,7 +54,7 @@ public Roof(Location loc) {
int roofY = loc.getBlockY();
// 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 < 100; radius++) {
for (int radius = 0; radius < 3; radius++) {
for (int x = loc.getBlockX() - radius; x <= loc.getBlockX() + radius; x++) {
for (int z = loc.getBlockZ() - radius; z <= loc.getBlockZ() + radius; z++) {
if (!((x > loc.getBlockX() - radius && x < loc.getBlockX() + radius)
Expand Down Expand Up @@ -78,7 +83,7 @@ public Roof(Location loc) {
break;
}
}

if (!roofFound) return false;
// Record the height
this.height = loc.getBlockY();
// Now we have a roof block, find how far we can go NSWE
Expand Down Expand Up @@ -106,9 +111,9 @@ public Roof(Location loc) {
// Repeat until nothing changes
} while (minx != minX || maxx != maxX || minz != minZ || maxz != maxZ);
// That's as much as we can do!
return true;
}


/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ public void setUp() throws Exception {
// Test
roof = new Roof(location);
}

@Test
public void testNoGlass() {
when(block.getType()).thenReturn(Material.AIR);
roof = new Roof(location);

}

/**
* Test method for {@link world.bentobox.greenhouses.greenhouse.Roof#getMinX()}.
Expand Down

0 comments on commit c8008e3

Please sign in to comment.