diff --git a/src/main/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategy.java b/src/main/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategy.java index 1837b8d81..8f1ae17e8 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategy.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategy.java @@ -46,12 +46,9 @@ public Location getNextLocation(World world) { // Find a free spot Map result = new EnumMap<>(Result.class); // Check center - last = Util.getClosestIsland(last); Result r = isIsland(last); - while (!r.equals(Result.FREE) && result.getOrDefault(Result.BLOCKS_IN_AREA, 0) < MAX_UNOWNED_ISLANDS) { nextGridLocation(last); - last = Util.getClosestIsland(last); result.put(r, result.getOrDefault(r, 0) + 1); r = isIsland(last); } @@ -73,10 +70,12 @@ public Location getNextLocation(World world) { * Checks if there is an island or blocks at this location * * @param location - the location - * @return true if island found, null if blocks found, false if nothing found + * @return Result enum if island found, null if blocks found, false if nothing found */ protected Result isIsland(Location location) { - + // Quick check + if (plugin.getIslands().getIslandAt(location).isPresent()) return Result.ISLAND_FOUND; + World world = location.getWorld(); // Check 4 corners diff --git a/src/test/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategyTest.java b/src/test/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategyTest.java index de8515105..903aa33cd 100644 --- a/src/test/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategyTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/island/DefaultNewIslandLocationStrategyTest.java @@ -61,6 +61,8 @@ public class DefaultNewIslandLocationStrategyTest { @Mock private Block adjBlock; + private int count; + /** * @throws java.lang.Exception */ @@ -147,6 +149,21 @@ public void testGetNextLocationSuccessSomeIslands() { assertEquals(location,dnils.getNextLocation(world)); verify(im).setLast(location); } + + /** + * Test method for {@link world.bentobox.bentobox.managers.island.DefaultNewIslandLocationStrategy#getNextLocation(org.bukkit.World)}. + */ + @Test + public void testGetNextLocationSuccessSomeIslands10() { + Optional opIsland = Optional.of(new Island()); + Optional emptyIsland = Optional.empty(); + count = 0; + //long time = System.currentTimeMillis(); + when(im.getIslandAt(any())).thenAnswer(i -> count++ > 10 ? emptyIsland :opIsland); + assertEquals(location,dnils.getNextLocation(world)); + //System.out.println(System.currentTimeMillis() - time); + verify(im).setLast(location); + } /** * Test method for {@link world.bentobox.bentobox.managers.island.DefaultNewIslandLocationStrategy#isIsland(org.bukkit.Location)}.