From caed56f16e7c5b7095fc6f8caba212caac49b603 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 26 Nov 2020 17:25:17 -0800 Subject: [PATCH] Load initial tp location async Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1584 --- .../bentobox/bentobox/util/teleport/SafeSpotTeleport.java | 8 ++++++-- .../api/commands/admin/AdminTeleportCommandTest.java | 7 ++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java b/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java index ac844d4bd..5f69bf60b 100644 --- a/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java +++ b/src/main/java/world/bentobox/bentobox/util/teleport/SafeSpotTeleport.java @@ -66,6 +66,10 @@ public class SafeSpotTeleport { this.result = builder.getResult(); // If there is no portal scan required, try the desired location immediately + Util.getChunkAtAsync(location).thenRun(()-> tryTogo(builder.getFailureMessage())); + } + + private void tryTogo(String failureMessage) { if (plugin.getIslands().isSafeLocation(location)) { if (portal) { // If the desired location is safe, then that's where you'll go if there's no portal @@ -86,7 +90,7 @@ public class SafeSpotTeleport { notChecking = true; // Start a recurring task until done or cancelled - task = Bukkit.getScheduler().runTaskTimer(plugin, () -> gatherChunks(builder.getFailureMessage()), 0L, SPEED); + task = Bukkit.getScheduler().runTaskTimer(plugin, () -> gatherChunks(failureMessage), 0L, SPEED); } private void gatherChunks(String failureMessage) { @@ -368,7 +372,7 @@ public class SafeSpotTeleport { build(); return result; } - + /** * Try to teleport the player * @return SafeSpotTeleport diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java index 9f1edc4e5..0c940e0b6 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommandTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -45,7 +46,7 @@ import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) -@PrepareForTest({Bukkit.class, BentoBox.class, User.class}) +@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class}) public class AdminTeleportCommandTest { private CompositeCommand ac; @@ -137,6 +138,10 @@ public class AdminTeleportCommandTest { // We do no actually want to teleport in this test, so return no island Optional nothing = Optional.empty(); when(im.getIslandAt(any())).thenReturn(nothing ); + + // Util + PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); + when(Util.getUUID(anyString())).thenCallRealMethod(); } @After