Merge branch 'develop' into rank_management

This commit is contained in:
tastybento 2023-11-10 10:53:08 -08:00 committed by GitHub
commit 8c2ff93d35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 145 additions and 122 deletions

View File

@ -7,6 +7,7 @@
package world.bentobox.bentobox.listeners.teleports; package world.bentobox.bentobox.listeners.teleports;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -77,8 +78,29 @@ public class EntityTeleportListener extends AbstractTeleportListener implements
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
// Trigger event processor.
this.portalProcess(event, event.getTo().getWorld().getEnvironment()); // Check which teleportation is happening.
World.Environment source = fromWorld.getEnvironment();
World.Environment destination = event.getTo().getWorld().getEnvironment();
if (World.Environment.NETHER == source && World.Environment.NORMAL == destination ||
World.Environment.NORMAL == source && World.Environment.NETHER == destination)
{
// Nether to overworld or opposite
this.portalProcess(event, World.Environment.NETHER);
}
else if (World.Environment.THE_END == source && World.Environment.NORMAL == destination ||
World.Environment.NORMAL == source && World.Environment.THE_END == destination)
{
// end to overworld or opposite
this.portalProcess(event, World.Environment.THE_END);
}
else
{
// unknown teleportation
this.portalProcess(event, event.getTo().getWorld().getEnvironment());
}
} }
@ -224,32 +246,24 @@ public class EntityTeleportListener extends AbstractTeleportListener implements
} }
this.inTeleport.add(event.getEntity().getUniqueId()); this.inTeleport.add(event.getEntity().getUniqueId());
// Get target world. if (fromWorld.equals(overWorld) && !this.isIslandWorld(overWorld, environment))
World toWorld;
if (environment.equals(World.Environment.NORMAL))
{
toWorld = overWorld;
}
else
{
toWorld = this.getNetherEndWorld(overWorld, environment);
}
if (!overWorld.equals(toWorld) && !this.isIslandWorld(overWorld, environment))
{ {
// This is not island world. Use standard nether or end world teleportation. // This is not island world. Use standard nether or end world teleportation.
this.handleToStandardNetherOrEnd(event, overWorld, toWorld); this.handleToStandardNetherOrEnd(event, overWorld, environment);
return; return;
} }
if (!overWorld.equals(fromWorld) && !this.isIslandWorld(overWorld, environment)) if (!fromWorld.equals(overWorld) && !this.isIslandWorld(overWorld, environment))
{ {
// If entering a portal in the other world, teleport to a portal in overworld if // If entering a portal in the other world, teleport to a portal in overworld if
// there is one // there is one
this.handleFromStandardNetherOrEnd(event, overWorld, toWorld.getEnvironment()); this.handleFromStandardNetherOrEnd(event, overWorld, environment);
return; return;
} }
// To the nether/end or overworld.
World toWorld = !fromWorld.getEnvironment().equals(environment) ?
this.getNetherEndWorld(overWorld, environment) : overWorld;
// Set the destination location // Set the destination location
// If portals cannot be created, then destination is the spawn point, otherwise it's the vector // If portals cannot be created, then destination is the spawn point, otherwise it's the vector
@ -286,7 +300,7 @@ public class EntityTeleportListener extends AbstractTeleportListener implements
// Let the server teleport // Let the server teleport
return; return;
} }
if (environment.equals(World.Environment.THE_END)) if (environment.equals(World.Environment.THE_END))
{ {
// Prevent death from hitting the ground while calculating location. // Prevent death from hitting the ground while calculating location.
@ -324,10 +338,11 @@ public class EntityTeleportListener extends AbstractTeleportListener implements
* Handle teleport to standard nether or end * Handle teleport to standard nether or end
* @param event - EntityPortalEvent * @param event - EntityPortalEvent
* @param overWorld - over world * @param overWorld - over world
* @param toWorld - to world * @param environment - to target environment
*/ */
private void handleToStandardNetherOrEnd(EntityPortalEvent event, World overWorld, World toWorld) private void handleToStandardNetherOrEnd(EntityPortalEvent event, World overWorld, World.Environment environment)
{ {
World toWorld = Objects.requireNonNull(this.getNetherEndWorld(overWorld, environment));
Location spawnPoint = toWorld.getSpawnLocation(); Location spawnPoint = toWorld.getSpawnLocation();
// If going to the nether and nether portals are active then just teleport to approx location // If going to the nether and nether portals are active then just teleport to approx location
@ -345,7 +360,7 @@ public class EntityTeleportListener extends AbstractTeleportListener implements
toWorld.setSpawnLocation(100, 50, 0); toWorld.setSpawnLocation(100, 50, 0);
} }
if (this.isAllowedOnServer(toWorld.getEnvironment())) if (this.isAllowedOnServer(environment))
{ {
// To Standard Nether or end // To Standard Nether or end
event.setTo(spawnPoint); event.setTo(spawnPoint);

View File

@ -301,7 +301,7 @@ public class PlayerTeleportListener extends AbstractTeleportListener implements
// Find the distance from edge of island's protection and set the search radius // Find the distance from edge of island's protection and set the search radius
this.getIsland(event.getTo()).ifPresent(island -> this.getIsland(event.getTo()).ifPresent(island ->
event.setSearchRadius(this.calculateSearchRadius(event.getTo(), island))); event.setSearchRadius(this.calculateSearchRadius(event.getTo(), island)));
// Check if there is an island there or not // Check if there is an island there or not
if (this.isPastingMissingIslands(overWorld) && if (this.isPastingMissingIslands(overWorld) &&
@ -327,7 +327,7 @@ public class PlayerTeleportListener extends AbstractTeleportListener implements
return; return;
} }
if (environment.equals(World.Environment.THE_END)) if (World.Environment.THE_END.equals(environment))
{ {
// Prevent death from hitting the ground while calculating location. // Prevent death from hitting the ground while calculating location.
event.getPlayer().setVelocity(new Vector(0,0,0)); event.getPlayer().setVelocity(new Vector(0,0,0));
@ -374,14 +374,14 @@ public class PlayerTeleportListener extends AbstractTeleportListener implements
Location spawnPoint = toWorld.getSpawnLocation(); Location spawnPoint = toWorld.getSpawnLocation();
// If going to the nether and nether portals are active then just teleport to approx location // If going to the nether and nether portals are active then just teleport to approx location
if (environment.equals(World.Environment.NETHER) && if (World.Environment.NETHER.equals(environment) &&
this.plugin.getIWM().getWorldSettings(overWorld).isMakeNetherPortals()) this.plugin.getIWM().getWorldSettings(overWorld).isMakeNetherPortals())
{ {
spawnPoint = event.getFrom().toVector().toLocation(toWorld); spawnPoint = event.getFrom().toVector().toLocation(toWorld);
} }
// If spawn is set as 0,63,0 in the End then move it to 100, 50 ,0. // If spawn is set as 0,63,0 in the End then move it to 100, 50 ,0.
if (environment.equals(World.Environment.THE_END) && spawnPoint.getBlockX() == 0 && spawnPoint.getBlockZ() == 0) if (World.Environment.THE_END.equals(environment) && spawnPoint.getBlockX() == 0 && spawnPoint.getBlockZ() == 0)
{ {
// Set to the default end spawn // Set to the default end spawn
spawnPoint = new Location(toWorld, 100, 50, 0); spawnPoint = new Location(toWorld, 100, 50, 0);
@ -413,7 +413,7 @@ public class PlayerTeleportListener extends AbstractTeleportListener implements
*/ */
private void handleFromStandardNetherOrEnd(PlayerPortalEvent event, World overWorld, World.Environment environment) private void handleFromStandardNetherOrEnd(PlayerPortalEvent event, World overWorld, World.Environment environment)
{ {
if (environment.equals(World.Environment.NETHER) && if (World.Environment.NETHER.equals(environment) &&
this.plugin.getIWM().getWorldSettings(overWorld).isMakeNetherPortals()) this.plugin.getIWM().getWorldSettings(overWorld).isMakeNetherPortals())
{ {
// Set to location directly to the from location. // Set to location directly to the from location.

View File

@ -34,50 +34,51 @@ import world.bentobox.bentobox.util.Util;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({BentoBox.class, Util.class, Bukkit.class }) @PrepareForTest({ BentoBox.class, Util.class, Bukkit.class })
public class EntityTeleportListenerTest extends AbstractCommonSetup { public class EntityTeleportListenerTest extends AbstractCommonSetup {
private EntityTeleportListener etl;
@Mock
private IslandsManager im;
private EntityTeleportListener etl;
@Mock
private IslandsManager im;
/** /**
*/ */
@Override @Override
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
when(plugin.getIslands()).thenReturn(im);
when(plugin.getIslandsManager()).thenReturn(im);
when(im.getProtectedIslandAt(any())).thenReturn(Optional.of(island));
etl = new EntityTeleportListener(plugin);
}
/** when(plugin.getIslands()).thenReturn(im);
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#EntityTeleportListener(world.bentobox.bentobox.BentoBox)}. when(plugin.getIslandsManager()).thenReturn(im);
*/
@Test
public void testEntityTeleportListener() {
assertNotNull(etl);
}
/** when(im.getProtectedIslandAt(any())).thenReturn(Optional.of(island));
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
*/ etl = new EntityTeleportListener(plugin);
@Test }
public void testOnEntityPortalWrongWorld() {
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); /**
when(Util.getWorld(any())).thenReturn(null); * Test method for
EntityPortalEvent event = new EntityPortalEvent(player, location, location, 10); * {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#EntityTeleportListener(world.bentobox.bentobox.BentoBox)}.
etl.onEntityPortal(event); */
assertFalse(event.isCancelled()); @Test
} public void testEntityTeleportListener() {
assertNotNull(etl);
/** }
/**
* Test method for
* {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
*/
@Test
public void testOnEntityPortalWrongWorld() {
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.getWorld(any())).thenReturn(null);
EntityPortalEvent event = new EntityPortalEvent(player, location, location, 10);
etl.onEntityPortal(event);
assertFalse(event.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
*/ */
@Test @Test
@ -87,44 +88,47 @@ public class EntityTeleportListenerTest extends AbstractCommonSetup {
etl.onEntityPortal(event); etl.onEntityPortal(event);
assertFalse(event.isCancelled()); assertFalse(event.isCancelled());
} }
/** /**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}. * Test method for
*/ * {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
@Test */
public void testOnEntityPortalNullTo() { @Test
EntityPortalEvent event = new EntityPortalEvent(player, location, null, 10); public void testOnEntityPortalNullTo() {
etl.onEntityPortal(event); EntityPortalEvent event = new EntityPortalEvent(player, location, null, 10);
assertFalse(event.isCancelled()); etl.onEntityPortal(event);
} assertFalse(event.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}. /**
*/ * Test method for
@Test * {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
public void testOnEntityPortalTeleportDisabled() { */
EntityPortalEvent event = new EntityPortalEvent(player, location, location, 10); @Test
etl.onEntityPortal(event); public void testOnEntityPortalTeleportDisabled() {
assertTrue(event.isCancelled()); EntityPortalEvent event = new EntityPortalEvent(player, location, location, 10);
} etl.onEntityPortal(event);
assertTrue(event.isCancelled());
/** }
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
*/ /**
@Test * Test method for
public void testOnEntityPortalTeleportEnabled() { * {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); */
when(Util.getWorld(any())).thenReturn(world); @Test
when(world.getEnvironment()).thenReturn(Environment.NORMAL); public void testOnEntityPortalTeleportEnabled() {
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
Flags.ENTITY_PORTAL_TELEPORT.setSetting(world, true); when(Util.getWorld(any())).thenReturn(world);
EntityPortalEvent event = new EntityPortalEvent(player, location, location, 10); when(world.getEnvironment()).thenReturn(Environment.NORMAL);
etl.onEntityPortal(event);
assertFalse(event.isCancelled()); Flags.ENTITY_PORTAL_TELEPORT.setSetting(world, true);
EntityPortalEvent event = new EntityPortalEvent(player, location, location, 10);
} etl.onEntityPortal(event);
assertFalse(event.isCancelled());
/**
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
*/ */
@Test @Test
@ -139,15 +143,15 @@ public class EntityTeleportListenerTest extends AbstractCommonSetup {
PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS);
when(Util.getWorld(any())).thenReturn(world2); when(Util.getWorld(any())).thenReturn(world2);
when(location.getWorld()).thenReturn(null); when(location.getWorld()).thenReturn(world);
Flags.ENTITY_PORTAL_TELEPORT.setSetting(world, true); Flags.ENTITY_PORTAL_TELEPORT.setSetting(world, true);
EntityPortalEvent event = new EntityPortalEvent(player, location, location2, 10); EntityPortalEvent event = new EntityPortalEvent(player, location, location2, 10);
etl.onEntityPortal(event); etl.onEntityPortal(event);
assertTrue(event.isCancelled()); assertTrue(event.isCancelled());
} }
/** /**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
*/ */
@Test @Test
@ -168,12 +172,14 @@ public class EntityTeleportListenerTest extends AbstractCommonSetup {
assertTrue(event.isCancelled()); assertTrue(event.isCancelled());
} }
/** /**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}. * Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
*/ */
@Test @Test
public void testOnEntityPortalTeleportEnabledIsAllowedInConfig() { public void testOnEntityPortalTeleportEnabledIsAllowedInConfig() {
when(world.getEnvironment()).thenReturn(Environment.NORMAL);
when(iwm.isNetherGenerate(any())).thenReturn(true); when(iwm.isNetherGenerate(any())).thenReturn(true);
when(iwm.isNetherIslands(any())).thenReturn(true); when(iwm.isNetherIslands(any())).thenReturn(true);
@ -192,18 +198,20 @@ public class EntityTeleportListenerTest extends AbstractCommonSetup {
} }
/** /**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityEnterPortal(org.bukkit.event.entity.EntityPortalEnterEvent)}. * Test method for
*/ * {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityEnterPortal(org.bukkit.event.entity.EntityPortalEnterEvent)}.
@Test */
public void testOnEntityEnterPortal() { @Test
} public void testOnEntityEnterPortal() {
}
/** /**
* Test method for {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityExitPortal(org.bukkit.event.entity.EntityPortalExitEvent)}. * Test method for
*/ * {@link world.bentobox.bentobox.listeners.teleports.EntityTeleportListener#onEntityExitPortal(org.bukkit.event.entity.EntityPortalExitEvent)}.
@Test */
public void testOnEntityExitPortal() { @Test
} public void testOnEntityExitPortal() {
}
} }