diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index d3d29b88e..a48795859 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -736,7 +736,7 @@ public class IslandsManager { /** * Checks if an online player is in the protected area of an island he owns or he is part of. * - * @param world the World to check. Typically this is the user's world. Does not check nether or end worlds. If null the method will always return {@code false}. + * @param world the World to check. Typically this is the user's world. Does not check nether or end worlds. If null the method will always return {@code false}. * @param user the User to check, if null or if this is not a Player the method will always return {@code false}. * * @return {@code true} if this User is located within the protected area of an island he owns or he is part of, @@ -932,4 +932,12 @@ public class IslandsManager { handler.saveObject(island); } + /** + * Try to get an island by its unique id + * @param uniqueId - unique id string + * @return optional island + */ + public Optional getIslandById(String uniqueId) { + return Optional.ofNullable(islandCache.getIslandById(uniqueId)); + } } diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java index 8280608ad..7201c738e 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java @@ -22,6 +22,8 @@ import world.bentobox.bentobox.util.Util; public class IslandCache { @NonNull private Map<@NonNull Location, @NonNull Island> islandsByLocation; + @NonNull + private Map<@NonNull String, @NonNull Island> islandsById; /** * Every player who is associated with an island is in this map. */ @@ -32,6 +34,7 @@ public class IslandCache { public IslandCache() { islandsByLocation = new HashMap<>(); + islandsById = new HashMap<>(); islandsByUUID = new HashMap<>(); grids = new HashMap<>(); } @@ -52,6 +55,7 @@ public class IslandCache { } if (addToGrid(island)) { islandsByLocation.put(island.getCenter(), island); + islandsById.put(island.getUniqueId(), island); // Make world islandsByUUID.putIfAbsent(island.getWorld(), new HashMap<>()); // Only add islands to this map if they are owned @@ -86,6 +90,7 @@ public class IslandCache { public void clear() { islandsByLocation.clear(); + islandsById.clear(); islandsByUUID.clear(); } @@ -98,6 +103,7 @@ public class IslandCache { if (!islandsByLocation.remove(island.getCenter(), island) || !islandsByUUID.containsKey(island.getWorld())) { return false; } + islandsById.remove(island.getUniqueId()); islandsByUUID.get(island.getWorld()).entrySet().removeIf(en -> en.getValue().equals(island)); // Remove from grid grids.putIfAbsent(island.getWorld(), new IslandGrid()); @@ -244,5 +250,16 @@ public class IslandCache { islandsByUUID.putIfAbsent(Util.getWorld(island.getWorld()), new HashMap<>()); islandsByUUID.get(Util.getWorld(island.getWorld())).put(newOwnerUUID, island); islandsByLocation.put(island.getCenter(), island); + islandsById.put(island.getUniqueId(), island); + } + + /** + * Get the island by unique id + * @param uniqueId + * @return island or null if none found + * @since 1.3.0 + */ + public Island getIslandById(String uniqueId) { + return islandsById.get(uniqueId); } } diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index fba2d53f9..162fcf8e9 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -1101,4 +1101,18 @@ public class IslandsManagerTest { Mockito.verify(wither, Mockito.never()).remove(); Mockito.verify(creeper).remove(); } + + /** + * Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getIslandById(String)}. + */ + @Test + public void testGetIslandByIdString() { + Island island = mock(Island.class); + String uuid = UUID.randomUUID().toString(); + when(islandCache.getIslandById(Mockito.anyString())).thenReturn(island); + // Test + IslandsManager im = new IslandsManager(plugin); + im.setIslandCache(islandCache); + assertEquals(island, im.getIslandById(uuid).get()); + } }