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

@ -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<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 {
@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);
}
}

View File

@ -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());
}
}