Added NonNull/Nullable annotations to IslandsManager and IslandCache

This commit is contained in:
Florian CUNY 2019-01-18 11:33:29 +01:00
parent d86b605057
commit d57f9544f3
4 changed files with 61 additions and 94 deletions

View File

@ -23,6 +23,8 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.events.IslandBaseEvent;
import world.bentobox.bentobox.api.events.island.IslandEvent;
@ -51,17 +53,20 @@ public class IslandsManager {
/**
* One island can be spawn, this is the one - otherwise, this value is null
*/
private Map<World, Island> spawn;
@NonNull
private Map<@NonNull World, @Nullable Island> spawn;
@NonNull
private Database<Island> handler;
/**
* The last locations where an island were put.
* This is not stored persistently and resets when the server starts
*/
private Map<World,Location> last;
private Map<World, Location> last;
// Island Cache
@NonNull
private IslandCache islandCache;
/**
@ -83,17 +88,14 @@ public class IslandsManager {
* @param i - the range to scan for a location less than 0 means the full island.
* @return - safe location, or null if none can be found
*/
public Location bigScan(Location l, int i) {
if (l == null) {
return null;
}
@Nullable
public Location bigScan(@NonNull Location l, int i) {
final int height;
final int depth;
if (i > 0) {
height = i;
depth = i;
} else {
Optional<Island> island = getIslandAt(l);
if (!island.isPresent()) {
return null;
@ -159,12 +161,11 @@ public class IslandsManager {
* warps and boat exits Unsafe is any liquid or air and also if there's no
* space
*
* @param l
* - Location to be checked
* @param l Location to be checked, not null.
* @return true if safe, otherwise false
*/
public boolean isSafeLocation(Location l) {
if (l == null || l.getWorld() == null) {
public boolean isSafeLocation(@NonNull Location l) {
if (l.getWorld() == null) {
return false;
}
Block ground = l.getBlock().getRelative(BlockFace.DOWN);
@ -209,20 +210,22 @@ public class IslandsManager {
/**
* Create an island with no owner at location
* @param location - the location - location
* @return Island
* @param location the location, not null
* @return Island or null if the island could not be created for some reason
*/
@Nullable
public Island createIsland(Location location){
return createIsland(location, null);
}
/**
* Create an island with owner. Note this does not create the schematic. It just creates the island data object.
* @param location - the location - location
* @param owner - the island owner UUID
* @param location the location, not null
* @param owner the island owner UUID, may be null
* @return Island or null if the island could not be created for some reason
*/
public Island createIsland(Location location, UUID owner){
@Nullable
public Island createIsland(@NonNull Location location, @Nullable UUID owner){
Island island = new Island(location, owner, plugin.getIWM().getIslandProtectionRange(location.getWorld()));
while (handler.objectExists(island.getUniqueId())) {
// This should never happen, so although this is a potential infinite loop I'm going to leave it here because
@ -237,14 +240,11 @@ public class IslandsManager {
}
/**
* Deletes island. If island is null, it does nothing
* @param island - island
* @param removeBlocks - if the island blocks should be removed or not
* Deletes island.
* @param island island to delete, not null
* @param removeBlocks whether the island blocks should be removed or not
*/
public void deleteIsland(Island island, boolean removeBlocks) {
if (island == null) {
return;
}
public void deleteIsland(@NonNull Island island, boolean removeBlocks) {
// Fire event
IslandBaseEvent event = IslandEvent.builder().island(island).reason(Reason.DELETE).build();
if (event.isCancelled()) {
@ -274,19 +274,21 @@ public class IslandsManager {
}
/**
* Gets the island for this player. If they are in a team, the team island is returned.
* @param world - world to check
* @param user - user
* Gets the island for this player.
* If they are in a team, the team island is returned.
* @param world world to check
* @param user user
* @return Island or null
*/
@Nullable
public Island getIsland(World world, User user){
return islandCache.get(world, user.getUniqueId());
}
/**
* Gets the island for this player. If they are in a team, the team island is returned.
* @param world - world to check
* @param uuid - user's uuid
* @param world world to check
* @param uuid user's uuid
* @return Island or null
*/
public Island getIsland(World world, UUID uuid){

View File

@ -11,16 +11,24 @@ import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*/
public class IslandCache {
private Map<Location, Island> islandsByLocation;
@NonNull
private Map<@NonNull Location, @NonNull Island> islandsByLocation;
/**
* Every player who is associated with an island is in this map.
*/
private Map<World, Map<UUID, Island>> islandsByUUID;
private Map<World, IslandGrid> grids;
@NonNull
private Map<@NonNull World, @NonNull Map<@NonNull UUID, @NonNull Island>> islandsByUUID;
@NonNull
private Map<@NonNull World, @NonNull IslandGrid> grids;
public IslandCache() {
islandsByLocation = new HashMap<>();
@ -30,7 +38,7 @@ public class IslandCache {
/**
* Adds an island to the grid
* @param island - island to add
* @param island island to add, not null
* @return true if successfully added, false if not
*/
public boolean addIsland(Island island) {
@ -144,6 +152,7 @@ public class IslandCache {
* @param uuid the player's UUID
* @return island owner's UUID, the player UUID if they are not in a team, or null if there is no island
*/
@Nullable
public UUID getOwner(World world, UUID uuid) {
islandsByUUID.putIfAbsent(Util.getWorld(world), new HashMap<>());
Island island = islandsByUUID.get(Util.getWorld(world)).get(uuid);
@ -199,7 +208,7 @@ public class IslandCache {
/**
* Gets the number of islands in the cache for this world
* @param world
* @param world world to get the number of islands in
* @return the number of islands
*/
public int size(World world) {
@ -207,7 +216,8 @@ public class IslandCache {
}
/**
* Sets an island owner. Clears out any other owner
* Sets an island owner.
* Clears out any other owner.
* @param island - island
* @param newOwnerUUID - new owner
*/
@ -217,5 +227,4 @@ public class IslandCache {
islandsByUUID.get(Util.getWorld(island.getWorld())).put(newOwnerUUID, island);
islandsByLocation.put(island.getCenter(), island);
}
}

View File

@ -198,15 +198,6 @@ public class IslandsManagerTest {
assertTrue(manager.isSafeLocation(location));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#isSafeLocation(org.bukkit.Location)}.
*/
@Test
public void testIsSafeLocationNull() {
IslandsManager manager = new IslandsManager(plugin);
assertFalse(manager.isSafeLocation(null));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#isSafeLocation(org.bukkit.Location)}.
*/
@ -363,19 +354,11 @@ public class IslandsManagerTest {
assertTrue("Wall sign 2", manager.isSafeLocation(location));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#IslandsManager(world.bentobox.bentobox.BentoBox)}.
*/
@Test
public void testIslandsManager() {
assertNotNull(new IslandsManager(plugin));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#bigScan(org.bukkit.Location, int)}.
*/
@Test
public void testBigScan() throws Exception {
public void testBigScan() {
Settings settings = mock(Settings.class);
when(plugin.getSettings()).thenReturn(settings);
@ -407,13 +390,8 @@ public class IslandsManagerTest {
when(ground.getState()).thenReturn(blockState);
// Negative value = full island scan
// Null location should get a null response
assertNull(manager.bigScan(null, -1));
// No island here yet
assertNull(manager.bigScan(location, -1));
// Try null location, > 0 scan value
assertNull(manager.bigScan(null, 10));
}
/**
@ -440,16 +418,6 @@ public class IslandsManagerTest {
assertEquals(owner, island.getOwner());
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#deleteIsland(world.bentobox.bentobox.database.objects.Island, boolean)}.
*/
@Test
public void testDeleteIslandIslandBooleanNull() {
IslandsManager im = new IslandsManager(plugin);
im.deleteIsland((Island)null, true);
Mockito.verify(pim, Mockito.never()).callEvent(Mockito.any());
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#deleteIsland(world.bentobox.bentobox.database.objects.Island, boolean)}.
*/
@ -521,16 +489,13 @@ public class IslandsManagerTest {
when(iwm.inWorld(any(Location.class))).thenReturn(false);
assertEquals(Optional.empty(), im.getIslandAt(new Location(world, 100000, 120, -100000)));
assertEquals(Optional.empty(), im.getIslandAt(location));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getIslandAt(org.bukkit.Location)}.
* @throws Exception
*/
@Test
public void testGetIslandAtLocationNether() throws Exception {
public void testGetIslandAtLocationNether() {
when(world.getEnvironment()).thenReturn(World.Environment.NETHER);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
@ -544,10 +509,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getIslandAt(org.bukkit.Location)}.
* @throws Exception
*/
@Test
public void testGetIslandAtLocationNetherNoNether() throws Exception {
public void testGetIslandAtLocationNetherNoNether() {
when(world.getEnvironment()).thenReturn(World.Environment.NETHER);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(false);
@ -560,10 +524,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getIslandAt(org.bukkit.Location)}.
* @throws Exception
*/
@Test
public void testGetIslandAtLocationNetherNoNetherIslands() throws Exception {
public void testGetIslandAtLocationNetherNoNetherIslands() {
when(world.getEnvironment()).thenReturn(World.Environment.NETHER);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
@ -594,10 +557,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getIslandAt(org.bukkit.Location)}.
* @throws Exception
*/
@Test
public void testGetIslandAtLocationEndNoEnd() throws Exception {
public void testGetIslandAtLocationEndNoEnd() {
when(world.getEnvironment()).thenReturn(World.Environment.THE_END);
when(iwm.isEndGenerate(Mockito.any())).thenReturn(false);
@ -610,10 +572,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getIslandAt(org.bukkit.Location)}.
* @throws Exception
*/
@Test
public void testGetIslandAtLocationEndNoEndIslands() throws Exception {
public void testGetIslandAtLocationEndNoEndIslands() {
when(world.getEnvironment()).thenReturn(World.Environment.THE_END);
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true);
when(iwm.isEndIslands(Mockito.any())).thenReturn(false);
@ -650,10 +611,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getMembers(World, UUID)}.
* @throws Exception
*/
@Test
public void testGetMembers() throws Exception {
public void testGetMembers() {
// Mock island cache
Set<UUID> members = new HashSet<>();
members.add(UUID.randomUUID());
@ -667,10 +627,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#getProtectedIslandAt(org.bukkit.Location)}.
* @throws Exception
*/
@Test
public void testGetProtectedIslandAt() throws Exception {
public void testGetProtectedIslandAt() {
// Mock island cache
Island is = mock(Island.class);
@ -777,10 +736,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#isOwner(World, UUID)}.
* @throws Exception
*/
@Test
public void testIsOwner() throws Exception {
public void testIsOwner() {
// Mock island cache
Island is = mock(Island.class);
@ -818,10 +776,9 @@ public class IslandsManagerTest {
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#locationIsOnIsland(org.bukkit.entity.Player, org.bukkit.Location)}.
* @throws Exception
*/
@Test
public void testLocationIsOnIsland() throws Exception {
public void testLocationIsOnIsland() {
// Mock island cache
Island is = mock(Island.class);
@ -967,7 +924,7 @@ public class IslandsManagerTest {
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#save(boolean)}.
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#save(Island)}.
*/
@Test
public void testSave() {
@ -1144,5 +1101,4 @@ public class IslandsManagerTest {
Mockito.verify(wither, Mockito.never()).remove();
Mockito.verify(creeper).remove();
}
}

View File

@ -35,12 +35,12 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest(Util.class)
public class IslandCacheTest {
BentoBox plugin;
private BentoBox plugin;
private static World world;
Island island;
UUID owner = UUID.randomUUID();
Location location;
private Island island;
private UUID owner = UUID.randomUUID();
private Location location;
@Before
public void setUp() throws Exception {