From c8008e34e24a941a71bae58ee91f5b0c09cca7fc Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 12 Nov 2019 11:38:17 -0800 Subject: [PATCH] Limits roof glass search to a radius of 3 instead of 100 Search was taking too long if there was no glass and timing out the server. https://github.com/BentoBoxWorld/Greenhouses/issues/31 --- .../world/bentobox/greenhouses/greenhouse/Roof.java | 13 +++++++++---- .../bentobox/greenhouses/greenhouse/RoofTest.java | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java b/src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java index 873ffda..5358e3b 100644 --- a/src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java +++ b/src/main/java/world/bentobox/greenhouses/greenhouse/Roof.java @@ -17,7 +17,7 @@ import org.bukkit.util.Vector; */ public class Roof extends MinMaxXZ { private final Location location; - private final int height; + private int height; private boolean roofFound; /** @@ -40,6 +40,11 @@ public class Roof extends MinMaxXZ { */ 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 @@ -49,7 +54,7 @@ public class Roof extends MinMaxXZ { 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) @@ -78,7 +83,7 @@ public class Roof extends MinMaxXZ { 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 @@ -106,9 +111,9 @@ public class Roof extends MinMaxXZ { // 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 diff --git a/src/test/java/world/bentobox/greenhouses/greenhouse/RoofTest.java b/src/test/java/world/bentobox/greenhouses/greenhouse/RoofTest.java index c44edfe..1b457de 100644 --- a/src/test/java/world/bentobox/greenhouses/greenhouse/RoofTest.java +++ b/src/test/java/world/bentobox/greenhouses/greenhouse/RoofTest.java @@ -76,6 +76,13 @@ public class RoofTest { // 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()}.