Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ public void onPlayerPortal(EntityPortalEnterEvent event)
// Check again if still in portal
if (this.inPortal.contains(uuid))
{
// Create new PlayerPortalEvent
// Create new PlayerPortalEvent and post it through the event bus so that
// protection flag listeners (e.g. PortalListener) can inspect and cancel it
// before BentoBox processes the actual teleportation.
PlayerPortalEvent en = new PlayerPortalEvent((Player) entity,
event.getLocation(),
null,
Expand All @@ -102,15 +104,17 @@ public void onPlayerPortal(EntityPortalEnterEvent event)
false,
0);

this.portalProcess(en, World.Environment.NETHER);
Bukkit.getPluginManager().callEvent(en);
}
}, 40);
return;
}
// End portals are instant transfer
if (!Bukkit.getAllowEnd() && (type.equals(Material.END_PORTAL) || type.equals(Material.END_GATEWAY)))
{
// Create new PlayerPortalEvent
// Create new PlayerPortalEvent and post it through the event bus so that
// protection flag listeners (e.g. PortalListener) can inspect and cancel it
// before BentoBox processes the actual teleportation.
PlayerPortalEvent en = new PlayerPortalEvent((Player) entity,
event.getLocation(),
null,
Expand All @@ -119,7 +123,7 @@ public void onPlayerPortal(EntityPortalEnterEvent event)
false,
0);

this.portalProcess(en, World.Environment.THE_END);
Bukkit.getPluginManager().callEvent(en);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -24,6 +25,7 @@
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.Event;
import org.bukkit.event.entity.EntityPortalEnterEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPortalEvent;
Expand All @@ -34,6 +36,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;

import world.bentobox.bentobox.CommonTestSetup;
Expand Down Expand Up @@ -301,6 +304,33 @@ public void testOnPlayerPortalNetherPortalDisabled() {
verify(Bukkit.getScheduler(), times(1)).runTaskLater(eq(plugin), any(Runnable.class), eq(40L));
}

/**
* Test that when nether is disabled on the server, the scheduled task fires a PlayerPortalEvent
* through the Bukkit event bus (so PortalListener flag checks run) instead of calling
* portalProcess() directly.
*/
@Test
public void testOnPlayerPortalNetherPortalDisabledCallsEvent() {
when(Bukkit.getAllowNether()).thenReturn(false);
when(Util.getWorld(location.getWorld())).thenReturn(world);
when(plugin.getIWM().inWorld(world)).thenReturn(true);
when(block.getType()).thenReturn(Material.NETHER_PORTAL);

EntityPortalEnterEvent e = new EntityPortalEnterEvent(mockPlayer, location);
ptl.onPlayerPortal(e);

// Capture and execute the scheduled Runnable
ArgumentCaptor<Runnable> runnableCaptor = ArgumentCaptor.forClass(Runnable.class);
verify(Bukkit.getScheduler()).runTaskLater(eq(plugin), runnableCaptor.capture(), eq(40L));
runnableCaptor.getValue().run();

// Verify callEvent was called with a PlayerPortalEvent with NETHER_PORTAL cause
ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
verify(pim).callEvent(eventCaptor.capture());
assertInstanceOf(PlayerPortalEvent.class, eventCaptor.getValue());
assertEquals(TeleportCause.NETHER_PORTAL, ((PlayerPortalEvent) eventCaptor.getValue()).getCause());
}

@Test
public void testOnPlayerPortalEndPortalDisabled() {
// Mock configuration for End disabled
Expand All @@ -320,6 +350,12 @@ public void testOnPlayerPortalEndPortalDisabled() {

// Verify the event behavior indirectly by confirming the origin world was stored
assertEquals(location.getWorld(), ptl.getTeleportOrigin().get(mockPlayer.getUniqueId()));

// Verify callEvent was called with a PlayerPortalEvent with END_PORTAL cause
ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
verify(pim).callEvent(eventCaptor.capture());
assertInstanceOf(PlayerPortalEvent.class, eventCaptor.getValue());
assertEquals(TeleportCause.END_PORTAL, ((PlayerPortalEvent) eventCaptor.getValue()).getCause());
}

@Test
Expand All @@ -341,6 +377,12 @@ public void testOnPlayerPortalEndGatewayDisabled() {

// Verify the event behavior indirectly by confirming the origin world was stored
assertEquals(location.getWorld(), ptl.getTeleportOrigin().get(mockPlayer.getUniqueId()));

// Verify callEvent was called with a PlayerPortalEvent with END_GATEWAY cause
ArgumentCaptor<Event> eventCaptor = ArgumentCaptor.forClass(Event.class);
verify(pim).callEvent(eventCaptor.capture());
assertInstanceOf(PlayerPortalEvent.class, eventCaptor.getValue());
assertEquals(TeleportCause.END_GATEWAY, ((PlayerPortalEvent) eventCaptor.getValue()).getCause());
}

@Test
Expand Down