Fixes island coords that are not on the grid.

https://github.com/BentoBoxWorld/BentoBox/issues/549
This commit is contained in:
tastybento 2019-02-20 21:42:43 -08:00
parent 30e3dbf269
commit 160ef002e4
2 changed files with 98 additions and 0 deletions

View File

@ -713,6 +713,8 @@ public class IslandsManager {
// Add to quarantine cache
quarantineCache.computeIfAbsent(island.getOwner(), k -> new ArrayList<>()).add(island);
} else {
// Fix island center if it is off
fixIslandCenter(island);
if (!islandCache.addIsland(island)) {
// Quarantine the offending island
toQuarantine.add(island);
@ -731,6 +733,27 @@ public class IslandsManager {
}
}
/**
* Island coordinates should always be a multiple of the island distance x 2. If they are not, this method
* realigns the grid coordinates.
* @param island - island
*/
public void fixIslandCenter(Island island) {
World world = island.getWorld();
if (world == null || island.getCenter() == null || !plugin.getIWM().inWorld(world)) {
return;
}
int distance = island.getRange() * 2;
long x = island.getCenter().getBlockX() - plugin.getIWM().getIslandXOffset(world);
long z = island.getCenter().getBlockZ() - plugin.getIWM().getIslandZOffset(world);
if (x % distance != 0 || z % distance != 0) {
// Island is off grid
x = Math.round((double) x / distance) * distance + plugin.getIWM().getIslandXOffset(world);
z = Math.round((double) z / distance) * distance + plugin.getIWM().getIslandZOffset(world);
}
island.setCenter(new Location(world, x, island.getCenter().getBlockY(), z));
}
/**
* Checks if a specific location is within the protected range of an island
* that the player is a member of (owner or member)

View File

@ -1115,4 +1115,79 @@ public class IslandsManagerTest {
im.setIslandCache(islandCache);
assertEquals(island, im.getIslandById(uuid).get());
}
/**
* Test method for {@link IslandsManager#fixIslandCenter(Island)}.
*/
@Test
public void testFixIslandCenter() {
// Setup
when(iwm.inWorld(Mockito.any(World.class))).thenReturn(true);
when(iwm.getIslandXOffset(Mockito.any())).thenReturn(0);
when(iwm.getIslandZOffset(Mockito.any())).thenReturn(0);
Island island = mock(Island.class);
Location center = mock(Location.class);
when(center.getWorld()).thenReturn(world);
when(center.getBlockX()).thenReturn(129);
when(center.getBlockY()).thenReturn(120);
when(center.getBlockZ()).thenReturn(127);
when(island.getCenter()).thenReturn(center);
when(island.getRange()).thenReturn(64);
when(island.getWorld()).thenReturn(world);
// Test
IslandsManager im = new IslandsManager(plugin);
im.fixIslandCenter(island);
Location loc = new Location(world, 128, 120, 128);
Mockito.verify(island).setCenter(Mockito.eq(loc));
}
/**
* Test method for {@link IslandsManager#fixIslandCenter(Island)}.
*/
@Test
public void testFixIslandCenterOffsets() {
// Setup
when(iwm.inWorld(Mockito.any(World.class))).thenReturn(true);
when(iwm.getIslandXOffset(Mockito.any())).thenReturn(10);
when(iwm.getIslandZOffset(Mockito.any())).thenReturn(10);
Island island = mock(Island.class);
Location center = mock(Location.class);
when(center.getWorld()).thenReturn(world);
when(center.getBlockX()).thenReturn(1295);
when(center.getBlockY()).thenReturn(120);
when(center.getBlockZ()).thenReturn(1295);
when(island.getCenter()).thenReturn(center);
when(island.getRange()).thenReturn(64);
when(island.getWorld()).thenReturn(world);
// Test
IslandsManager im = new IslandsManager(plugin);
im.fixIslandCenter(island);
Location loc = new Location(world, 1290, 120, 1290);
Mockito.verify(island).setCenter(Mockito.eq(loc));
}
/**
* Test method for {@link IslandsManager#fixIslandCenter(Island)}.
*/
@Test
public void testFixIslandCenterOffsetsNegatives() {
// Setup
when(iwm.inWorld(Mockito.any(World.class))).thenReturn(true);
when(iwm.getIslandXOffset(Mockito.any())).thenReturn(0);
when(iwm.getIslandZOffset(Mockito.any())).thenReturn(0);
Island island = mock(Island.class);
Location center = mock(Location.class);
when(center.getWorld()).thenReturn(world);
when(center.getBlockX()).thenReturn(-1295);
when(center.getBlockY()).thenReturn(120);
when(center.getBlockZ()).thenReturn(-1287);
when(island.getCenter()).thenReturn(center);
when(island.getRange()).thenReturn(64);
when(island.getWorld()).thenReturn(world);
// Test
IslandsManager im = new IslandsManager(plugin);
im.fixIslandCenter(island);
Location loc = new Location(world, -1280, 120, -1280);
Mockito.verify(island).setCenter(Mockito.eq(loc));
}
}