mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-13 19:51:27 +01:00
Get island by its UUID API added
https://github.com/BentoBoxWorld/BentoBox/issues/341
This commit is contained in:
parent
9dfb0573d9
commit
179a71b548
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user