From 10392d0d6575462a8877ad1c7f6650082fab28af Mon Sep 17 00:00:00 2001 From: ljacqu Date: Fri, 15 Jul 2016 18:49:01 +0200 Subject: [PATCH] #856 Handle null Location on PlayerCache object - Location may null when read from file --- .../authme/util/TeleportationService.java | 10 +++++++--- .../authme/util/TeleportationServiceTest.java | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/fr/xephi/authme/util/TeleportationService.java b/src/main/java/fr/xephi/authme/util/TeleportationService.java index edd0468d..4359cc62 100644 --- a/src/main/java/fr/xephi/authme/util/TeleportationService.java +++ b/src/main/java/fr/xephi/authme/util/TeleportationService.java @@ -108,22 +108,26 @@ public class TeleportationService implements Reloadable { return; } + // #856: If PlayerData comes from a persisted file, the Location might be null + String worldName = (limbo.getLocation() != null) + ? limbo.getLocation().getWorld().getName() + : null; + // The world in PlayerData is from where the player comes, before any teleportation by AuthMe - String worldName = limbo.getLocation().getWorld().getName(); if (mustForceSpawnAfterLogin(worldName)) { teleportToSpawn(player, true); } else if (settings.getProperty(TELEPORT_UNAUTHED_TO_SPAWN)) { if (settings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION) && auth.getQuitLocY() != 0) { Location location = buildLocationFromAuth(player, auth); teleportBackFromSpawn(player, location); - } else { + } else if (limbo.getLocation() != null) { teleportBackFromSpawn(player, limbo.getLocation()); } } } private boolean mustForceSpawnAfterLogin(String worldName) { - return settings.getProperty(RestrictionSettings.FORCE_SPAWN_LOCATION_AFTER_LOGIN) + return worldName != null && settings.getProperty(RestrictionSettings.FORCE_SPAWN_LOCATION_AFTER_LOGIN) && spawnOnLoginWorlds.contains(worldName); } diff --git a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java index dba15782..82b57b5d 100644 --- a/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java +++ b/src/test/java/fr/xephi/authme/util/TeleportationServiceTest.java @@ -32,6 +32,7 @@ import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyZeroInteractions; @@ -422,6 +423,25 @@ public class TeleportationServiceTest { verify(player).teleport(location); } + @Test + public void shouldNotTeleportForNullLocationInLimboPlayer() { + // given + given(settings.getProperty(RestrictionSettings.SAVE_QUIT_LOCATION)).willReturn(false); + given(settings.getProperty(RestrictionSettings.TELEPORT_UNAUTHED_TO_SPAWN)).willReturn(true); + given(settings.getProperty(RestrictionSettings.FORCE_SPAWN_LOCATION_AFTER_LOGIN)).willReturn(false); + + PlayerAuth auth = PlayerAuth.builder().name("bobby").build(); + Player player = mock(Player.class); + PlayerData limbo = mock(PlayerData.class); + + // when + teleportationService.teleportOnLogin(player, auth, limbo); + + // then + verifyZeroInteractions(player); + verify(limbo, times(2)).getLocation(); + } + private static void assertCorrectLocation(Location location, PlayerAuth auth, World world) { assertThat(location.getX(), equalTo(auth.getQuitLocX())); assertThat(location.getY(), equalTo(auth.getQuitLocY()));