mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-23 19:25:12 +01:00
Fixes island center startx/z correction.
https://github.com/BentoBoxWorld/BentoBox/issues/1169
This commit is contained in:
parent
2a4522d7c3
commit
ab89e4835e
@ -896,22 +896,25 @@ 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
|
||||
* @return true if coordinate is altered
|
||||
* @since 1.3.0
|
||||
*/
|
||||
private void fixIslandCenter(Island island) {
|
||||
boolean fixIslandCenter(Island island) {
|
||||
World world = island.getWorld();
|
||||
if (world == null || island.getCenter() == null || !plugin.getIWM().inWorld(world)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
int distance = plugin.getIWM().getIslandDistance(island.getWorld()) * 2;
|
||||
long x = ((long) island.getCenter().getBlockX()) - plugin.getIWM().getIslandXOffset(world);
|
||||
long z = ((long) island.getCenter().getBlockZ()) - plugin.getIWM().getIslandZOffset(world);
|
||||
long x = ((long) island.getCenter().getBlockX()) - plugin.getIWM().getIslandXOffset(world) - plugin.getIWM().getIslandStartX(world);
|
||||
long z = ((long) island.getCenter().getBlockZ()) - plugin.getIWM().getIslandZOffset(world) - plugin.getIWM().getIslandStartZ(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);
|
||||
x = Math.round((double) x / distance) * distance + plugin.getIWM().getIslandXOffset(world) + plugin.getIWM().getIslandStartX(world);
|
||||
z = Math.round((double) z / distance) * distance + plugin.getIWM().getIslandZOffset(world) + plugin.getIWM().getIslandStartZ(world);
|
||||
island.setCenter(new Location(world, x, island.getCenter().getBlockY(), z));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -55,6 +55,7 @@ import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.Answer;
|
||||
@ -1111,4 +1112,207 @@ public class IslandsManagerTest {
|
||||
assertEquals(island, im.getIslandById(uuid).get());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#fixIslandCenter(Island)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFixIslandCenter() {
|
||||
Island island = mock(Island.class);
|
||||
when(island.getWorld()).thenReturn(world);
|
||||
// Island center
|
||||
when(location.getBlockX()).thenReturn(0);
|
||||
when(location.getBlockY()).thenReturn(120);
|
||||
when(location.getBlockZ()).thenReturn(0);
|
||||
when(island.getCenter()).thenReturn(location);
|
||||
// Start x,z
|
||||
when(iwm.getIslandStartX(eq(world))).thenReturn(0);
|
||||
when(iwm.getIslandStartZ(eq(world))).thenReturn(0);
|
||||
// Offset x,z
|
||||
when(iwm.getIslandXOffset(eq(world))).thenReturn(0);
|
||||
when(iwm.getIslandZOffset(eq(world))).thenReturn(0);
|
||||
// World
|
||||
when(iwm.inWorld(eq(world))).thenReturn(true);
|
||||
// Island distance
|
||||
when(iwm.getIslandDistance(eq(world))).thenReturn(100);
|
||||
// Test
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
assertFalse(im.fixIslandCenter(island));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#fixIslandCenter(Island)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFixIslandCenterOff() {
|
||||
Island island = mock(Island.class);
|
||||
when(island.getWorld()).thenReturn(world);
|
||||
// Island center
|
||||
when(location.getBlockX()).thenReturn(10);
|
||||
when(location.getBlockY()).thenReturn(120);
|
||||
when(location.getBlockZ()).thenReturn(-10);
|
||||
when(island.getCenter()).thenReturn(location);
|
||||
// Start x,z
|
||||
when(iwm.getIslandStartX(eq(world))).thenReturn(0);
|
||||
when(iwm.getIslandStartZ(eq(world))).thenReturn(0);
|
||||
// Offset x,z
|
||||
when(iwm.getIslandXOffset(eq(world))).thenReturn(0);
|
||||
when(iwm.getIslandZOffset(eq(world))).thenReturn(0);
|
||||
// World
|
||||
when(iwm.inWorld(eq(world))).thenReturn(true);
|
||||
// Island distance
|
||||
when(iwm.getIslandDistance(eq(world))).thenReturn(100);
|
||||
// Test
|
||||
ArgumentCaptor<Location> captor = ArgumentCaptor.forClass(Location.class);
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
assertTrue(im.fixIslandCenter(island));
|
||||
// Verify location
|
||||
verify(island).setCenter(captor.capture());
|
||||
assertEquals(world, captor.getValue().getWorld());
|
||||
assertEquals(0, captor.getValue().getBlockX());
|
||||
assertEquals(120, captor.getValue().getBlockY());
|
||||
assertEquals(0, captor.getValue().getBlockZ());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#fixIslandCenter(Island)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFixIslandCenterOffStart() {
|
||||
Island island = mock(Island.class);
|
||||
when(island.getWorld()).thenReturn(world);
|
||||
// Island center
|
||||
when(location.getBlockX()).thenReturn(100010);
|
||||
when(location.getBlockY()).thenReturn(120);
|
||||
when(location.getBlockZ()).thenReturn(8755);
|
||||
when(island.getCenter()).thenReturn(location);
|
||||
// Start x,z
|
||||
when(iwm.getIslandStartX(eq(world))).thenReturn(100000);
|
||||
when(iwm.getIslandStartZ(eq(world))).thenReturn(8765);
|
||||
// Offset x,z
|
||||
when(iwm.getIslandXOffset(eq(world))).thenReturn(0);
|
||||
when(iwm.getIslandZOffset(eq(world))).thenReturn(0);
|
||||
// World
|
||||
when(iwm.inWorld(eq(world))).thenReturn(true);
|
||||
// Island distance
|
||||
when(iwm.getIslandDistance(eq(world))).thenReturn(100);
|
||||
// Test
|
||||
ArgumentCaptor<Location> captor = ArgumentCaptor.forClass(Location.class);
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
assertTrue(im.fixIslandCenter(island));
|
||||
// Verify location
|
||||
verify(island).setCenter(captor.capture());
|
||||
assertEquals(world, captor.getValue().getWorld());
|
||||
assertEquals(100000, captor.getValue().getBlockX());
|
||||
assertEquals(120, captor.getValue().getBlockY());
|
||||
assertEquals(8765, captor.getValue().getBlockZ());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#fixIslandCenter(Island)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFixIslandCenterStartOnGrid() {
|
||||
Island island = mock(Island.class);
|
||||
when(island.getWorld()).thenReturn(world);
|
||||
// Island center
|
||||
when(location.getBlockX()).thenReturn(10000);
|
||||
when(location.getBlockY()).thenReturn(120);
|
||||
when(location.getBlockZ()).thenReturn(8765);
|
||||
when(island.getCenter()).thenReturn(location);
|
||||
// Start x,z
|
||||
when(iwm.getIslandStartX(eq(world))).thenReturn(100000);
|
||||
when(iwm.getIslandStartZ(eq(world))).thenReturn(8765);
|
||||
// Offset x,z
|
||||
when(iwm.getIslandXOffset(eq(world))).thenReturn(0);
|
||||
when(iwm.getIslandZOffset(eq(world))).thenReturn(0);
|
||||
// World
|
||||
when(iwm.inWorld(eq(world))).thenReturn(true);
|
||||
// Island distance
|
||||
when(iwm.getIslandDistance(eq(world))).thenReturn(100);
|
||||
// Test
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
assertFalse(im.fixIslandCenter(island));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#fixIslandCenter(Island)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFixIslandCenterStartOnGridOffset() {
|
||||
Island island = mock(Island.class);
|
||||
when(island.getWorld()).thenReturn(world);
|
||||
// Island center
|
||||
when(location.getBlockX()).thenReturn(10050);
|
||||
when(location.getBlockY()).thenReturn(120);
|
||||
when(location.getBlockZ()).thenReturn(8815);
|
||||
when(island.getCenter()).thenReturn(location);
|
||||
// Start x,z
|
||||
when(iwm.getIslandStartX(eq(world))).thenReturn(100000);
|
||||
when(iwm.getIslandStartZ(eq(world))).thenReturn(8765);
|
||||
// Offset x,z
|
||||
when(iwm.getIslandXOffset(eq(world))).thenReturn(50);
|
||||
when(iwm.getIslandZOffset(eq(world))).thenReturn(50);
|
||||
// World
|
||||
when(iwm.inWorld(eq(world))).thenReturn(true);
|
||||
// Island distance
|
||||
when(iwm.getIslandDistance(eq(world))).thenReturn(100);
|
||||
// Test
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
assertFalse(im.fixIslandCenter(island));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#fixIslandCenter(Island)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFixIslandCenterOffStartOffOffset() {
|
||||
Island island = mock(Island.class);
|
||||
when(island.getWorld()).thenReturn(world);
|
||||
// Island center
|
||||
when(location.getBlockX()).thenReturn(100060);
|
||||
when(location.getBlockY()).thenReturn(120);
|
||||
when(location.getBlockZ()).thenReturn(8815);
|
||||
when(island.getCenter()).thenReturn(location);
|
||||
// Start x,z
|
||||
when(iwm.getIslandStartX(eq(world))).thenReturn(100000);
|
||||
when(iwm.getIslandStartZ(eq(world))).thenReturn(8765);
|
||||
// Offset x,z
|
||||
when(iwm.getIslandXOffset(eq(world))).thenReturn(50);
|
||||
when(iwm.getIslandZOffset(eq(world))).thenReturn(50);
|
||||
// World
|
||||
when(iwm.inWorld(eq(world))).thenReturn(true);
|
||||
// Island distance
|
||||
when(iwm.getIslandDistance(eq(world))).thenReturn(100);
|
||||
// Test
|
||||
ArgumentCaptor<Location> captor = ArgumentCaptor.forClass(Location.class);
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
assertTrue(im.fixIslandCenter(island));
|
||||
// Verify location
|
||||
verify(island).setCenter(captor.capture());
|
||||
assertEquals(world, captor.getValue().getWorld());
|
||||
assertEquals(100050, captor.getValue().getBlockX());
|
||||
assertEquals(120, captor.getValue().getBlockY());
|
||||
assertEquals(8815, captor.getValue().getBlockZ());
|
||||
|
||||
}
|
||||
/**
|
||||
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#fixIslandCenter(Island)}.
|
||||
*/
|
||||
@Test
|
||||
public void testFixIslandCenterNulls() {
|
||||
Island island = mock(Island.class);
|
||||
when(island.getWorld()).thenReturn(null);
|
||||
// Test
|
||||
IslandsManager im = new IslandsManager(plugin);
|
||||
assertFalse(im.fixIslandCenter(island));
|
||||
when(island.getWorld()).thenReturn(world);
|
||||
when(island.getCenter()).thenReturn(null);
|
||||
assertFalse(im.fixIslandCenter(island));
|
||||
when(island.getCenter()).thenReturn(location);
|
||||
when(iwm.inWorld(eq(world))).thenReturn(false);
|
||||
assertFalse(im.fixIslandCenter(island));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user