From c6e2a9bd6617fda08278c451e718490719041353 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 27 Mar 2021 09:23:24 -0700 Subject: [PATCH] Admin teleport now goes to island spawn locations if they exist --- .../commands/admin/AdminTeleportCommand.java | 20 +++++- .../admin/AdminTeleportCommandTest.java | 61 ++++++++++++++++--- 2 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java index 7d404b9b5..42f695cf6 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminTeleportCommand.java @@ -6,12 +6,14 @@ import java.util.Optional; import java.util.UUID; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Player; import org.eclipse.jdt.annotation.Nullable; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.teleport.SafeSpotTeleport; @@ -78,12 +80,14 @@ public class AdminTeleportCommand extends CompositeCommand { @Override public boolean execute(User user, String label, List args) { - Location warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getWorld()); + World world = getWorld(); if (getLabel().equals("tpnether")) { - warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getPlugin().getIWM().getNetherWorld(getWorld())); + world = getPlugin().getIWM().getNetherWorld(getWorld()); } else if (getLabel().equals("tpend")) { - warpSpot = getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(getPlugin().getIWM().getEndWorld(getWorld())); + world = getPlugin().getIWM().getEndWorld(getWorld()); } + Location warpSpot = getSpot(world); + // Otherwise, ask the admin to go to a safe spot String failureMessage = user.getTranslation("commands.admin.tp.manual", "[location]", warpSpot.getBlockX() + " " + warpSpot.getBlockY() + " " + warpSpot.getBlockZ()); @@ -103,6 +107,16 @@ public class AdminTeleportCommand extends CompositeCommand { return true; } + private Location getSpot(World world) { + Island island = getIslands().getIsland(world, targetUUID); + if (island != null && island.getSpawnPoint(world.getEnvironment()) != null) { + // Return the defined spawn point + return island.getSpawnPoint(world.getEnvironment()); + } + // Return the default island protection center + return getIslands().getIslandLocation(getWorld(), targetUUID).toVector().toLocation(world); + } + @Override public Optional> tabComplete(User user, String alias, List args) { String lastArg = !args.isEmpty() ? args.get(args.size()-1) : ""; 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 0c940e0b6..819614191 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 @@ -18,6 +18,8 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.World.Environment; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.util.Vector; @@ -25,6 +27,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -49,13 +52,29 @@ import world.bentobox.bentobox.util.Util; @PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class}) public class AdminTeleportCommandTest { + @Mock private CompositeCommand ac; + @Mock private User user; + @Mock private IslandsManager im; + @Mock private PlayersManager pm; private UUID notUUID; + @Mock private Player p; + @Mock private IslandWorldManager iwm; + @Mock + private Island island; + @Mock + private Location spawnPoint; + @Mock + private World world; + @Mock + private World netherWorld; + @Mock + private World endWorld; /** @@ -73,9 +92,6 @@ public class AdminTeleportCommandTest { when(plugin.getCommandsManager()).thenReturn(cm); // Player - p = mock(Player.class); - // Sometimes use Mockito.withSettings().verboseLogging() - user = mock(User.class); when(user.isOp()).thenReturn(false); UUID uuid = UUID.randomUUID(); notUUID = UUID.randomUUID(); @@ -89,17 +105,22 @@ public class AdminTeleportCommandTest { User.setPlugin(plugin); // Parent command has no aliases - ac = mock(CompositeCommand.class); when(ac.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ac.getTopLabel()).thenReturn("bskyblock"); + when(ac.getWorld()).thenReturn(world); + + // World + when(world.getEnvironment()).thenReturn(Environment.NORMAL); + when(netherWorld.getEnvironment()).thenReturn(Environment.NETHER); + when(endWorld.getEnvironment()).thenReturn(Environment.THE_END); + // Island World Manager - iwm = mock(IslandWorldManager.class); when(plugin.getIWM()).thenReturn(iwm); - + when(iwm.getNetherWorld(any())).thenReturn(netherWorld); + when(iwm.getEndWorld(any())).thenReturn(endWorld); // Player has island to begin with - im = mock(IslandsManager.class); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.hasIsland(any(), any(User.class))).thenReturn(true); when(im.isOwner(any(),any())).thenReturn(true); @@ -107,7 +128,6 @@ public class AdminTeleportCommandTest { when(plugin.getIslands()).thenReturn(im); // Has team - pm = mock(PlayersManager.class); when(im.inTeam(any(), eq(uuid))).thenReturn(true); when(plugin.getPlayers()).thenReturn(pm); @@ -137,8 +157,10 @@ public class AdminTeleportCommandTest { when(im.getIslandLocation(any(), any())).thenReturn(location); // We do no actually want to teleport in this test, so return no island Optional nothing = Optional.empty(); - when(im.getIslandAt(any())).thenReturn(nothing ); + when(im.getIslandAt(any())).thenReturn(nothing); + // Return an island for spawn checking + when(im.getIsland(any(), any(UUID.class))).thenReturn(island); // Util PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); when(Util.getUUID(anyString())).thenCallRealMethod(); @@ -189,11 +211,29 @@ public class AdminTeleportCommandTest { verify(user).sendMessage(eq("general.errors.player-has-no-island")); } + /** + * Test for {@link AdminTeleportCommand#execute(User, String, java.util.List)} + */ @Test public void testExecuteUserStringListOfStringKnownTargetHasIsland() { when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); + assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); + assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); + verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0")); + } + + /** + * Test for {@link AdminTeleportCommand#execute(User, String, java.util.List)} + */ + @Test + public void testExecuteUserStringListOfStringKnownTargetHasIslandSpawnPoint() { + when(island.getSpawnPoint(any())).thenReturn(spawnPoint); + when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); + when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); + AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); + assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); verify(user).getTranslation(eq("commands.admin.tp.manual"), eq("[location]"), eq("0 0 0")); } @@ -204,6 +244,7 @@ public class AdminTeleportCommandTest { when(im.hasIsland(any(), any(UUID.class))).thenReturn(false); when(im.inTeam(any(), any(UUID.class))).thenReturn(true); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tp"); + assertTrue(atc.canExecute(user, "tp", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tp", Collections.singletonList("tastybento"))); verify(iwm, Mockito.never()).getNetherWorld(any()); verify(iwm, Mockito.never()).getEndWorld(any()); @@ -215,6 +256,7 @@ public class AdminTeleportCommandTest { when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpnether"); + assertTrue(atc.canExecute(user, "tpnether", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tpnether", Collections.singletonList("tastybento"))); verify(iwm).getNetherWorld(any()); verify(iwm, Mockito.never()).getEndWorld(any()); @@ -226,6 +268,7 @@ public class AdminTeleportCommandTest { when(pm.getUUID(eq("tastybento"))).thenReturn(notUUID); when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); AdminTeleportCommand atc = new AdminTeleportCommand(ac,"tpend"); + assertTrue(atc.canExecute(user, "tpend", Collections.singletonList("tastybento"))); assertTrue(atc.execute(user, "tpend", Collections.singletonList("tastybento"))); verify(iwm, Mockito.never()).getNetherWorld(any()); verify(iwm).getEndWorld(any());