Fixed IslandEnterEvent and IslandExitEvent not being called if Flags#ENTER_EXIT_MESSAGES was set to false

This commit is contained in:
Florian CUNY 2019-03-03 17:39:58 +01:00
parent cdaec6c517
commit f1b24a45d3
2 changed files with 76 additions and 33 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 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;
import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.localization.TextVariables;
@ -17,9 +18,8 @@ import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.lists.Flags;
/** /**
* The Enter/Exit messages flag is a global flag and applies everywhere * Handles {@link Flags#ENTER_EXIT_MESSAGES} flag and {@link world.bentobox.bentobox.api.events.island.IslandEvent.IslandExitEvent} and {@link world.bentobox.bentobox.api.events.island.IslandEvent.IslandEnterEvent}.
* @author tastybento * @author tastybento
*
*/ */
public class EnterExitListener extends FlagListener { public class EnterExitListener extends FlagListener {
@ -27,19 +27,17 @@ public class EnterExitListener extends FlagListener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onMove(PlayerMoveEvent e) { public void onMove(PlayerMoveEvent e) {
handleEnterExitNotification(User.getInstance(e.getPlayer()), e.getFrom(), e.getTo()); handleEnterExit(User.getInstance(e.getPlayer()), e.getFrom(), e.getTo());
} }
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTeleport(PlayerTeleportEvent e) { public void onTeleport(PlayerTeleportEvent e) {
handleEnterExitNotification(User.getInstance(e.getPlayer()), e.getFrom(), e.getTo()); handleEnterExit(User.getInstance(e.getPlayer()), e.getFrom(), e.getTo());
} }
private void handleEnterExitNotification(User user, Location from, Location to) { private void handleEnterExit(@NonNull User user, @NonNull Location from, @NonNull Location to) {
// Only process if Enter Exit flags are active, we are in the right world and there is a change in X or Z coords // Only process we are in the right world and there is a change in X or Z coords
if (!getIWM().inWorld(from) if (!getIWM().inWorld(from) || from.toVector().multiply(XZ).equals(to.toVector().multiply(XZ))) {
|| from.toVector().multiply(XZ).equals(to.toVector().multiply(XZ))
|| !Flags.ENTER_EXIT_MESSAGES.isSetForWorld(from.getWorld())) {
return; return;
} }
@ -69,16 +67,7 @@ public class EnterExitListener extends FlagListener {
.location(user.getLocation()) .location(user.getLocation())
.build(); .build();
// Send message if island is owned by someone sendExitNotification(user, i);
if (i.getOwner() != null) {
// Leave messages are always specific to this world
user.notify(i.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, (i.getName() != null) ? i.getName() :
user.getTranslation(i.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner())));
}
// Send message if island is unowned, but has a name
else if (i.getName() != null) {
user.notify(i.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, i.getName());
}
}); });
islandTo.ifPresent(i -> { islandTo.ifPresent(i -> {
@ -91,15 +80,55 @@ public class EnterExitListener extends FlagListener {
.location(user.getLocation()) .location(user.getLocation())
.build(); .build();
// Send message if island is owned by someone sendEnterNotification(user, i);
if (i.getOwner() != null) {
user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, (i.getName() != null) ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner())));
}
// Send message if island is unowned, but has a name
else if (i.getName() != null) {
user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, i.getName());
}
}); });
} }
/**
* Sends a notification to this user telling them they exited this island.
* @param user the User to send the notification to, not null.
* @param island the island the user exited, not null.
* @since 1.4.0
*/
private void sendExitNotification(@NonNull User user, @NonNull Island island) {
// Only process if ENTER_EXIT_MESSAGES is enabled
if (!Flags.ENTER_EXIT_MESSAGES.isSetForWorld(island.getWorld())) {
return;
}
// Send message if island is owned by someone
if (island.getOwner() != null) {
// Leave messages are always specific to this world
user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, (island.getName() != null) ? island.getName() :
user.getTranslation(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(island.getOwner())));
}
// Send message if island is unowned, but has a name
else if (island.getName() != null) {
user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, island.getName());
}
}
/**
* Sends a notification to this user telling them they entered this island.
* @param user the User to send the notification to, not null.
* @param island the island the user entered, not null.
* @since 1.4.0
*/
private void sendEnterNotification(@NonNull User user, @NonNull Island island) {
// Only process if ENTER_EXIT_MESSAGES is enabled
if (!Flags.ENTER_EXIT_MESSAGES.isSetForWorld(island.getWorld())) {
return;
}
// Send message if island is owned by someone
if (island.getOwner() != null) {
// Leave messages are always specific to this world
user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, (island.getName() != null) ? island.getName() :
user.getTranslation(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(island.getOwner())));
}
// Send message if island is unowned, but has a name
else if (island.getName() != null) {
user.notify(island.getWorld(), "protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, island.getName());
}
}
} }

View File

@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.bentobox.listeners.flags.worldsettings; package world.bentobox.bentobox.listeners.flags.worldsettings;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
@ -35,6 +32,7 @@ import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.Notifier;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager; import world.bentobox.bentobox.managers.LocalesManager;
@ -59,19 +57,19 @@ public class EnterExitListenerTest {
private Location inside; private Location inside;
private EnterExitListener listener; private EnterExitListener listener;
private LocalesManager lm; private LocalesManager lm;
private World world;
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// Set up plugin // Set up plugin
BentoBox plugin = mock(BentoBox.class); BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin); Whitebox.setInternalState(BentoBox.class, "instance", plugin);
// World // World
World world = mock(World.class); world = mock(World.class);
// Server // Server
Server server = mock(Server.class); Server server = mock(Server.class);
@ -174,6 +172,8 @@ public class EnterExitListenerTest {
// Addon // Addon
when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty()); when(iwm.getAddon(Mockito.any())).thenReturn(Optional.empty());
// Flags
Flags.ENTER_EXIT_MESSAGES.setSetting(world, true);
} }
/** /**
@ -256,4 +256,18 @@ public class EnterExitListenerTest {
Mockito.verify(island, Mockito.times(2)).getName(); Mockito.verify(island, Mockito.times(2)).getName();
} }
/**
* Asserts that no notifications are sent if {@link world.bentobox.bentobox.lists.Flags#ENTER_EXIT_MESSAGES Flags#ENTER_EXIT_MESSAGES} flag is set to false.
* @since 1.4.0
*/
@Test
public void testNoNotificationIfDisabled() {
// No notifications should be sent
Flags.ENTER_EXIT_MESSAGES.setSetting(world, false);
PlayerMoveEvent e = new PlayerMoveEvent(user.getPlayer(), inside, outside);
listener.onMove(e);
// No messages should be sent
Mockito.verify(user, Mockito.never()).sendMessage(Mockito.anyVararg());
}
} }