Skip to content

Commit

Permalink
Merge pull request #2499 from BentoBoxWorld/2496_null_to_location
Browse files Browse the repository at this point in the history
Protect against null to locations. #2496
  • Loading branch information
tastybento authored Sep 8, 2024
2 parents a716feb + a9b8613 commit 6896a45
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.util.Vector;
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.flags.FlagListener;
Expand Down Expand Up @@ -36,15 +37,16 @@ public void onTeleport(PlayerTeleportEvent 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
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))) {
return;
}

Optional<Island> islandFrom = getIslands().getProtectedIslandAt(from);
Optional<Island> islandTo = getIslands().getProtectedIslandAt(to);
Optional<Island> islandTo = to == null ? Optional.empty() : getIslands().getProtectedIslandAt(to);

/*
* Options:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,19 @@ public void testOnMoveOutsideIsland() {
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)}.
*/
Expand Down Expand Up @@ -294,6 +307,23 @@ public void testExitingIslandEmptyIslandName() {
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)}.
*/
Expand Down Expand Up @@ -354,6 +384,18 @@ public void testExitIslandTeleport() {
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)}.
Expand Down

0 comments on commit 6896a45

Please sign in to comment.