Stops velocity for player going through end portal to end

https://github.com/BentoBoxWorld/BentoBox/issues/1118
This commit is contained in:
tastybento 2020-01-10 15:41:59 -08:00
parent 028bda4f3a
commit 8b27003cfb
2 changed files with 62 additions and 53 deletions

View File

@ -12,6 +12,7 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityPortalEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;
@ -121,7 +122,10 @@ public class PortalTeleportationListener implements Listener {
return true;
}
// Else other worlds teleport to the nether
// Else other worlds teleport to the end
// Set player's velocity to zero
e.getPlayer().setVelocity(new Vector(0,0,0));
// Teleport
new SafeSpotTeleport.Builder(plugin)
.entity(e.getPlayer())
.location(to)

View File

@ -3,7 +3,10 @@ package world.bentobox.bentobox.listeners;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Optional;
@ -24,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.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@ -48,43 +52,46 @@ import world.bentobox.bentobox.util.Util;
@PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class })
public class PortalTeleportationListenerTest {
@Mock
private BentoBox plugin;
@Mock
private IslandsManager im;
private PlayersManager pm;
@Mock
private IslandWorldManager iwm;
@Mock
private World world;
@Mock
private World nether;
@Mock
private World end;
@Mock
private Player p;
@Before
public void setUp() throws Exception {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// island world mgr
iwm = mock(IslandWorldManager.class);
world = mock(World.class);
when(world.getEnvironment()).thenReturn(Environment.NORMAL);
nether = mock(World.class);
when(nether.getEnvironment()).thenReturn(Environment.NETHER);
end = mock(World.class);
when(end.getEnvironment()).thenReturn(Environment.THE_END);
Location endSpawn = mock(Location.class);
when(endSpawn.getWorld()).thenReturn(end);
when(end.getSpawnLocation()).thenReturn(endSpawn);
when(iwm.getEndWorld(Mockito.any())).thenReturn(end);
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true);
when(iwm.getIslandWorld(Mockito.any())).thenReturn(world);
when(iwm.getNetherWorld(Mockito.any())).thenReturn(nether);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.getEndWorld(any())).thenReturn(end);
when(iwm.isEndGenerate(any())).thenReturn(true);
when(iwm.getIslandWorld(any())).thenReturn(world);
when(iwm.getNetherWorld(any())).thenReturn(nether);
when(iwm.isNetherGenerate(any())).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getNetherSpawnRadius(Mockito.any())).thenReturn(100);
when(iwm.getNetherSpawnRadius(any())).thenReturn(100);
when(plugin.getIWM()).thenReturn(iwm);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
when(Util.getWorld(any())).thenReturn(world);
// Settings
Settings s = mock(Settings.class);
@ -97,7 +104,6 @@ public class PortalTeleportationListenerTest {
when(nether.getSpawnLocation()).thenReturn(netherSpawn);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
User user = mock(User.class);
when(user.isOp()).thenReturn(false);
@ -112,12 +118,11 @@ public class PortalTeleportationListenerTest {
User.setPlugin(plugin);
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true);
when(im.getOwner(Mockito.any(), Mockito.any())).thenReturn(uuid);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
when(im.isOwner(any(), any())).thenReturn(true);
when(im.getOwner(any(), any())).thenReturn(uuid);
Optional<Island> optionalIsland = Optional.empty();
when(im.getIslandAt(Mockito.any())).thenReturn(optionalIsland);
when(im.getIslandAt(any())).thenReturn(optionalIsland);
when(plugin.getIslands()).thenReturn(im);
when(plugin.getPlayers()).thenReturn(pm);
@ -129,14 +134,14 @@ public class PortalTeleportationListenerTest {
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
when(lm.get(any(), any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
// Normally in world
Util.setPlugin(plugin);
// Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
when(iwm.getAddon(any())).thenReturn(Optional.empty());
}
@ -177,7 +182,7 @@ public class PortalTeleportationListenerTest {
when(from.getWorld()).thenReturn(world);
when(from.toVector()).thenReturn(new Vector(1,2,3));
// No end world
when(iwm.isEndGenerate(Mockito.any())).thenReturn(false);
when(iwm.isEndGenerate(any())).thenReturn(false);
PortalTeleportationListener np = new PortalTeleportationListener(plugin);
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, TeleportCause.END_PORTAL);
np.onEndIslandPortal(e);
@ -227,17 +232,17 @@ public class PortalTeleportationListenerTest {
// Player has no island
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(false);
// Right cause, end exists, right world
PlayerPortalEvent e = new PlayerPortalEvent(player, from, null, TeleportCause.END_PORTAL);
when(iwm.isEndGenerate(world)).thenReturn(true);
np.onEndIslandPortal(e);
assertFalse(e.isCancelled());
// Give player an island
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(any(), any(UUID.class))).thenReturn(true);
np.onEndIslandPortal(e);
assertTrue(e.isCancelled());
Mockito.verify(im).homeTeleport(Mockito.any(), Mockito.eq(player));
verify(im).homeTeleport(any(), eq(player));
}
/**
@ -245,7 +250,7 @@ public class PortalTeleportationListenerTest {
*/
@Test
public void testOnEndIslandPortalNonBentoBoxWorld() {
when(iwm.inWorld(Mockito.any(World.class))).thenReturn(false);
when(iwm.inWorld(any(World.class))).thenReturn(false);
PortalTeleportationListener np = new PortalTeleportationListener(plugin);
Location from = mock(Location.class);
// Teleport from nether to world
@ -254,7 +259,7 @@ public class PortalTeleportationListenerTest {
assertFalse(np.onEndIslandPortal(e));
// Verify
assertFalse(e.isCancelled());
Mockito.verify(iwm, Mockito.never()).isEndGenerate(Mockito.any());
verify(iwm, never()).isEndGenerate(any());
}
/**
@ -314,15 +319,15 @@ public class PortalTeleportationListenerTest {
when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, TeleportCause.NETHER_PORTAL);
// Nether islands active
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(any())).thenReturn(true);
when(iwm.isNetherGenerate(any())).thenReturn(true);
assertTrue(np.onNetherPortal(e));
// Verify
assertTrue(e.isCancelled());
// If nether islands, then to = from but in nether
Mockito.verify(from).toVector();
verify(from).toVector();
// Do not go to spawn
Mockito.verify(nether, Mockito.never()).getSpawnLocation();
verify(nether, never()).getSpawnLocation();
}
/**
@ -337,24 +342,24 @@ public class PortalTeleportationListenerTest {
when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, TeleportCause.NETHER_PORTAL);
// Nether islands active
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(any())).thenReturn(true);
when(iwm.isNetherGenerate(any())).thenReturn(true);
Island island = mock(Island.class);
Location spawnLoc = mock(Location.class);
when(island.getSpawnPoint(Mockito.any())).thenReturn(spawnLoc);
when(island.getSpawnPoint(any())).thenReturn(spawnLoc);
Optional<Island> optionalIsland = Optional.of(island);
// Island exists at location
when(im.getIslandAt(Mockito.any())).thenReturn(optionalIsland);
when(im.getIslandAt(any())).thenReturn(optionalIsland);
assertTrue(np.onNetherPortal(e));
// Verify
assertTrue(e.isCancelled());
// If nether islands, then to = from but in nether
Mockito.verify(from).toVector();
verify(from).toVector();
// Do not go to spawn
Mockito.verify(nether, Mockito.never()).getSpawnLocation();
verify(nether, never()).getSpawnLocation();
}
/**
@ -369,23 +374,23 @@ public class PortalTeleportationListenerTest {
when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, TeleportCause.NETHER_PORTAL);
// Nether islands active
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(any())).thenReturn(true);
when(iwm.isNetherGenerate(any())).thenReturn(true);
Island island = mock(Island.class);
when(island.getSpawnPoint(Mockito.any())).thenReturn(null);
when(island.getSpawnPoint(any())).thenReturn(null);
Optional<Island> optionalIsland = Optional.of(island);
// Island exists at location
when(im.getIslandAt(Mockito.any())).thenReturn(optionalIsland);
when(im.getIslandAt(any())).thenReturn(optionalIsland);
assertTrue(np.onNetherPortal(e));
// Verify
assertTrue(e.isCancelled());
// If nether islands, then to = from but in nether
Mockito.verify(from).toVector();
verify(from).toVector();
// Do not go to spawn
Mockito.verify(nether, Mockito.never()).getSpawnLocation();
verify(nether, never()).getSpawnLocation();
}
/**
@ -400,8 +405,8 @@ public class PortalTeleportationListenerTest {
when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, TeleportCause.NETHER_PORTAL);
// Nether islands inactive
when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(any())).thenReturn(false);
when(iwm.isNetherGenerate(any())).thenReturn(true);
assertFalse(np.onNetherPortal(e));
// Verify
assertFalse(e.isCancelled());
@ -423,8 +428,8 @@ public class PortalTeleportationListenerTest {
PlayerPortalEvent e = new PlayerPortalEvent(p, from, null, TeleportCause.NETHER_PORTAL);
// Nether islands inactive
when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(any())).thenReturn(false);
when(iwm.isNetherGenerate(any())).thenReturn(true);
// Player should be teleported to their island
assertFalse(np.onNetherPortal(e));
@ -444,14 +449,14 @@ public class PortalTeleportationListenerTest {
when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, TeleportCause.NETHER_PORTAL);
// Nether islands active
when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.isNetherIslands(any())).thenReturn(true);
when(iwm.isNetherGenerate(any())).thenReturn(true);
assertTrue(np.onNetherPortal(e));
// Verify
assertTrue(e.isCancelled());
// If regular nether, then to = island location
Mockito.verify(from).toVector();
Mockito.verify(im, Mockito.never()).getIslandLocation(Mockito.any(), Mockito.any());
verify(from).toVector();
verify(im, never()).getIslandLocation(any(), any());
}
/**
@ -486,7 +491,7 @@ public class PortalTeleportationListenerTest {
*/
@Test
public void testOnNetherPortalNonBentoBoxWorld() {
when(iwm.inWorld(Mockito.any(World.class))).thenReturn(false);
when(iwm.inWorld(any(World.class))).thenReturn(false);
PortalTeleportationListener np = new PortalTeleportationListener(plugin);
Location from = mock(Location.class);
// Teleport from nether to world
@ -495,7 +500,7 @@ public class PortalTeleportationListenerTest {
assertFalse(np.onNetherPortal(e));
// Verify
assertFalse(e.isCancelled());
Mockito.verify(iwm, Mockito.never()).isNetherGenerate(Mockito.any());
verify(iwm, never()).isNetherGenerate(any());
}