Get island by its UUID API added

https://github.com/BentoBoxWorld/BentoBox/issues/341
This commit is contained in:
tastybento 2019-02-11 19:01:44 -08:00
parent 9dfb0573d9
commit 179a71b548
3 changed files with 40 additions and 1 deletions

View File

@ -932,4 +932,12 @@ public class IslandsManager {
handler.saveObject(island); handler.saveObject(island);
} }
/**
* Try to get an island by its unique id
* @param uniqueId - unique id string
* @return optional island
*/
public Optional<Island> getIslandById(String uniqueId) {
return Optional.ofNullable(islandCache.getIslandById(uniqueId));
}
} }

View File

@ -22,6 +22,8 @@ import world.bentobox.bentobox.util.Util;
public class IslandCache { public class IslandCache {
@NonNull @NonNull
private Map<@NonNull Location, @NonNull Island> islandsByLocation; 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. * Every player who is associated with an island is in this map.
*/ */
@ -32,6 +34,7 @@ public class IslandCache {
public IslandCache() { public IslandCache() {
islandsByLocation = new HashMap<>(); islandsByLocation = new HashMap<>();
islandsById = new HashMap<>();
islandsByUUID = new HashMap<>(); islandsByUUID = new HashMap<>();
grids = new HashMap<>(); grids = new HashMap<>();
} }
@ -52,6 +55,7 @@ public class IslandCache {
} }
if (addToGrid(island)) { if (addToGrid(island)) {
islandsByLocation.put(island.getCenter(), island); islandsByLocation.put(island.getCenter(), island);
islandsById.put(island.getUniqueId(), island);
// Make world // Make world
islandsByUUID.putIfAbsent(island.getWorld(), new HashMap<>()); islandsByUUID.putIfAbsent(island.getWorld(), new HashMap<>());
// Only add islands to this map if they are owned // Only add islands to this map if they are owned
@ -86,6 +90,7 @@ public class IslandCache {
public void clear() { public void clear() {
islandsByLocation.clear(); islandsByLocation.clear();
islandsById.clear();
islandsByUUID.clear(); islandsByUUID.clear();
} }
@ -98,6 +103,7 @@ public class IslandCache {
if (!islandsByLocation.remove(island.getCenter(), island) || !islandsByUUID.containsKey(island.getWorld())) { if (!islandsByLocation.remove(island.getCenter(), island) || !islandsByUUID.containsKey(island.getWorld())) {
return false; return false;
} }
islandsById.remove(island.getUniqueId());
islandsByUUID.get(island.getWorld()).entrySet().removeIf(en -> en.getValue().equals(island)); islandsByUUID.get(island.getWorld()).entrySet().removeIf(en -> en.getValue().equals(island));
// Remove from grid // Remove from grid
grids.putIfAbsent(island.getWorld(), new IslandGrid()); grids.putIfAbsent(island.getWorld(), new IslandGrid());
@ -244,5 +250,16 @@ public class IslandCache {
islandsByUUID.putIfAbsent(Util.getWorld(island.getWorld()), new HashMap<>()); islandsByUUID.putIfAbsent(Util.getWorld(island.getWorld()), new HashMap<>());
islandsByUUID.get(Util.getWorld(island.getWorld())).put(newOwnerUUID, island); islandsByUUID.get(Util.getWorld(island.getWorld())).put(newOwnerUUID, island);
islandsByLocation.put(island.getCenter(), 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);
} }
} }

View File

@ -1101,4 +1101,18 @@ public class IslandsManagerTest {
Mockito.verify(wither, Mockito.never()).remove(); Mockito.verify(wither, Mockito.never()).remove();
Mockito.verify(creeper).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());
}
} }