Merge pull request #2499 from BentoBoxWorld/2496_null_to_location

Protect against null to locations. #2496
This commit is contained in:
tastybento 2024-09-07 18:01:05 -07:00 committed by GitHub
commit 6896a4579c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.events.island.IslandEvent;
import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.api.flags.FlagListener;
@ -36,15 +37,16 @@ public class EnterExitListener extends FlagListener {
handleEnterExit(User.getInstance(e.getPlayer()), e.getFrom(), e.getTo(), e); handleEnterExit(User.getInstance(e.getPlayer()), e.getFrom(), e.getTo(), e);
} }
private void handleEnterExit(@NonNull User user, @NonNull Location from, @NonNull Location to, @NonNull PlayerMoveEvent e) { private void handleEnterExit(@NonNull User user, @NonNull Location from, @Nullable Location to,
@NonNull PlayerMoveEvent e) {
// Only process if there is a change in X or Z coords // Only process if there is a change in X or Z coords
if (from.getWorld() != null && from.getWorld().equals(to.getWorld()) if (from.getWorld() != null && to != null && from.getWorld().equals(to.getWorld())
&& from.toVector().multiply(XZ).equals(to.toVector().multiply(XZ))) { && from.toVector().multiply(XZ).equals(to.toVector().multiply(XZ))) {
return; return;
} }
Optional<Island> islandFrom = getIslands().getProtectedIslandAt(from); Optional<Island> islandFrom = getIslands().getProtectedIslandAt(from);
Optional<Island> islandTo = getIslands().getProtectedIslandAt(to); Optional<Island> islandTo = to == null ? Optional.empty() : getIslands().getProtectedIslandAt(to);
/* /*
* Options: * Options:

View File

@ -242,6 +242,19 @@ public class EnterExitListenerTest {
verify(pim, never()).callEvent(any(IslandExitEvent.class)); verify(pim, never()).callEvent(any(IslandExitEvent.class));
} }
/**
* Test method for {@link EnterExitListener#onMove(org.bukkit.event.player.PlayerMoveEvent)}.
*/
@Test
public void testOnMoveOutsideIslandToNull() {
PlayerMoveEvent e = new PlayerMoveEvent(user.getPlayer(), outside, null);
listener.onMove(e);
// Moving outside the island should result in no messages to the user
verify(notifier, never()).notify(any(), any());
verify(pim, never()).callEvent(any(IslandEnterEvent.class));
verify(pim, never()).callEvent(any(IslandExitEvent.class));
}
/** /**
* Test method for {@link EnterExitListener#onMove(org.bukkit.event.player.PlayerMoveEvent)}. * Test method for {@link EnterExitListener#onMove(org.bukkit.event.player.PlayerMoveEvent)}.
*/ */
@ -294,6 +307,23 @@ public class EnterExitListenerTest {
verify(notifier).notify(any(User.class), eq("protection.flags.ENTER_EXIT_MESSAGES.now-leaving")); verify(notifier).notify(any(User.class), eq("protection.flags.ENTER_EXIT_MESSAGES.now-leaving"));
} }
/**
* Test method for {@link EnterExitListener#onMove(org.bukkit.event.player.PlayerMoveEvent)}.
*/
@Test
public void testExitingIslandEmptyIslandNameToNull() {
when(island.getName()).thenReturn("");
PlayerMoveEvent e = new PlayerMoveEvent(user.getPlayer(), inside, null);
listener.onMove(e);
// Moving into the island should show a message
verify(lm).get(any(), eq("protection.flags.ENTER_EXIT_MESSAGES.now-leaving"));
// The island owner needs to be checked
verify(island).isOwned();
verify(pim).callEvent(any(IslandExitEvent.class));
verify(pim, never()).callEvent(any(IslandEnterEvent.class));
verify(notifier).notify(any(User.class), eq("protection.flags.ENTER_EXIT_MESSAGES.now-leaving"));
}
/** /**
* Test method for {@link EnterExitListener#onMove(org.bukkit.event.player.PlayerMoveEvent)}. * Test method for {@link EnterExitListener#onMove(org.bukkit.event.player.PlayerMoveEvent)}.
*/ */
@ -354,6 +384,18 @@ public class EnterExitListenerTest {
verify(pim).callEvent(any(IslandExitEvent.class)); verify(pim).callEvent(any(IslandExitEvent.class));
} }
/**
* Test method for {@link EnterExitListener#onTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.
*/
@Test
public void testExitIslandTeleportToNull() {
PlayerTeleportEvent e = new PlayerTeleportEvent(user.getPlayer(), inside, null, TeleportCause.PLUGIN);
listener.onTeleport(e);
verify(notifier).notify(any(User.class), eq("protection.flags.ENTER_EXIT_MESSAGES.now-leaving"));
verify(pim, never()).callEvent(any(IslandEnterEvent.class));
verify(pim).callEvent(any(IslandExitEvent.class));
}
/** /**
* Test method for {@link EnterExitListener#onTeleport(org.bukkit.event.player.PlayerTeleportEvent)}. * Test method for {@link EnterExitListener#onTeleport(org.bukkit.event.player.PlayerTeleportEvent)}.