Small optimization around utility sameworld check

May improve https://github.com/BentoBoxWorld/BentoBox/issues/676
This commit is contained in:
tastybento 2019-05-26 17:19:31 -07:00
parent 8ac0f08285
commit 7882a77b14
4 changed files with 57 additions and 4 deletions

View File

@ -26,9 +26,22 @@ import world.bentobox.bentobox.lists.Flags;
*/ */
public class LockAndBanListener extends FlagListener { public class LockAndBanListener extends FlagListener {
/**
* Result of checking the island for locked state or player bans
*
*/
private enum CheckResult { private enum CheckResult {
/**
* player is banned from island
*/
BANNED, BANNED,
/**
* Island is locked
*/
LOCKED, LOCKED,
/**
* Island is open for teleporting
*/
OPEN OPEN
} }

View File

@ -630,6 +630,9 @@ public class IslandsManager {
.build(); .build();
return; return;
} }
if (!home.getChunk().isLoaded()) {
home.getChunk().load();
}
player.teleport(home); player.teleport(home);
if (number == 1) { if (number == 1) {
user.sendMessage("commands.island.go.teleport"); user.sendMessage("commands.island.go.teleport");

View File

@ -201,9 +201,18 @@ public class Util {
* @return true if the same * @return true if the same
*/ */
public static boolean sameWorld(World world, World world2) { public static boolean sameWorld(World world, World world2) {
String worldName = world.getName().replaceAll(NETHER, "").replaceAll(THE_END, ""); return stripName(world).equals(stripName(world2));
String world2Name = world2.getName().replaceAll(NETHER, "").replaceAll(THE_END, ""); }
return worldName.equalsIgnoreCase(world2Name);
private static String stripName(World world) {
switch (world.getEnvironment()) {
case NETHER:
return world.getName().substring(0, world.getName().length() - NETHER.length());
case THE_END:
return world.getName().substring(0, world.getName().length() - THE_END.length());
default:
return world.getName();
}
} }
/** /**

View File

@ -4,6 +4,7 @@
package world.bentobox.bentobox.util; package world.bentobox.bentobox.util;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -170,7 +171,34 @@ public class UtilTest {
*/ */
@Test @Test
public void testSameWorld() { public void testSameWorld() {
//fail("Not yet implemented"); // TODO World world = mock(World.class);
World world2 = mock(World.class);
World world3 = mock(World.class);
World world4 = mock(World.class);
when(world.getName()).thenReturn("world");
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(world2.getName()).thenReturn("world_nether");
when(world2.getEnvironment()).thenReturn(World.Environment.NETHER);
when(world3.getName()).thenReturn("world_the_end");
when(world3.getEnvironment()).thenReturn(World.Environment.THE_END);
when(world4.getName()).thenReturn("hfhhfhf_nether");
when(world4.getEnvironment()).thenReturn(World.Environment.NETHER);
assertTrue(Util.sameWorld(world, world));
assertTrue(Util.sameWorld(world2, world2));
assertTrue(Util.sameWorld(world3, world3));
assertTrue(Util.sameWorld(world, world2));
assertTrue(Util.sameWorld(world, world3));
assertTrue(Util.sameWorld(world2, world));
assertTrue(Util.sameWorld(world2, world3));
assertTrue(Util.sameWorld(world3, world2));
assertTrue(Util.sameWorld(world3, world));
assertFalse(Util.sameWorld(world4, world));
assertFalse(Util.sameWorld(world4, world2));
assertFalse(Util.sameWorld(world4, world3));
assertFalse(Util.sameWorld(world, world4));
assertFalse(Util.sameWorld(world2, world4));
assertFalse(Util.sameWorld(world3, world4));
} }
/** /**