From bd2cc1b63e187c1ffffd61e390372ae4186f707b Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Thu, 16 Aug 2018 11:15:36 +0200 Subject: [PATCH] Updated EnterExitListener to fire IslandEnterEvent and IslandExitEvent --- .../api/events/island/IslandEvent.java | 18 +++--- .../listeners/flags/EnterExitListener.java | 55 +++++++++++++++---- 2 files changed, 55 insertions(+), 18 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/events/island/IslandEvent.java b/src/main/java/world/bentobox/bentobox/api/events/island/IslandEvent.java index 9e2d9767c..83555ffe4 100644 --- a/src/main/java/world/bentobox/bentobox/api/events/island/IslandEvent.java +++ b/src/main/java/world/bentobox/bentobox/api/events/island/IslandEvent.java @@ -39,7 +39,8 @@ public class IslandEvent { } /** - * Fired when an island is going to be created. May be canceled. + * Fired when an island is going to be created. + * May be cancelled. * */ public static class IslandCreateEvent extends IslandBaseEvent { @@ -59,7 +60,8 @@ public class IslandEvent { } } /** - * Fired when an island is going to be deleted. May be canceled. + * Fired when an island is going to be deleted. + * May be cancelled. * */ public static class IslandDeleteEvent extends IslandBaseEvent { @@ -79,8 +81,8 @@ public class IslandEvent { } } /** - * Fired when an a player enters an island - * + * Fired when an a player enters an island. + * Cancellation has no effect. */ public static class IslandEnterEvent extends IslandBaseEvent { private IslandEnterEvent(Island island, UUID player, boolean admin, Location location) { @@ -89,8 +91,8 @@ public class IslandEvent { } } /** - * Fired when a player exits and island - * + * Fired when a player exits an island. + * Cancellation has no effect. */ public static class IslandExitEvent extends IslandBaseEvent { private IslandExitEvent(Island island, UUID player, boolean admin, Location location) { @@ -119,8 +121,8 @@ public class IslandEvent { } } /** - * Fired when an island is going to be reset. May be canceled. - * + * Fired when an island is going to be reset. + * May be cancelled. */ public static class IslandResetEvent extends IslandBaseEvent { private IslandResetEvent(Island island, UUID player, boolean admin, Location location) { diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/EnterExitListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/EnterExitListener.java index 79fa1f887..6d3e1003d 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/EnterExitListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/EnterExitListener.java @@ -10,6 +10,8 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.util.Vector; +import world.bentobox.bentobox.api.events.IslandBaseEvent; +import world.bentobox.bentobox.api.events.island.IslandEvent; import world.bentobox.bentobox.api.flags.AbstractFlagListener; import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; @@ -34,8 +36,8 @@ public class EnterExitListener extends AbstractFlagListener { return; } - Optional from = this.getIslands().getProtectedIslandAt(e.getFrom()); - Optional to = this.getIslands().getProtectedIslandAt(e.getTo()); + Optional from = getIslands().getProtectedIslandAt(e.getFrom()); + Optional to = getIslands().getProtectedIslandAt(e.getTo()); /* * Options: @@ -51,14 +53,47 @@ public class EnterExitListener extends AbstractFlagListener { } User user = User.getInstance(e.getPlayer()); - // Send message if island is owned by someone - from.filter(i -> i.getOwner() != null).ifPresent(i -> user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, (i.getName() != null) ? i.getName() : - user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", TextVariables.NAME, getPlugin().getPlayers().getName(i.getOwner())))); - to.filter(i -> i.getOwner() != null).ifPresent(i -> 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 - from.filter(i -> i.getOwner() == null && (i.getName() != null)).ifPresent(i -> user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", TextVariables.NAME, i.getName())); - to.filter(i -> i.getOwner() == null && (i.getName() != null)).ifPresent(i -> user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-entering", TextVariables.NAME, i.getName())); + from.ifPresent(i -> { + // Fire the IslandExitEvent + new IslandEvent.IslandEventBuilder() + .island(i) + .involvedPlayer(user.getUniqueId()) + .reason(IslandEvent.Reason.EXIT) + .admin(false) + .location(user.getLocation()) + .build(); + + // Send message if island is owned by someone + if (i.getOwner() != null) { + user.notify("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", 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-leaving", TextVariables.NAME, i.getName()); + } + }); + + to.ifPresent(i -> { + // Fire the IslandEnterEvent + new IslandEvent.IslandEventBuilder() + .island(i) + .involvedPlayer(user.getUniqueId()) + .reason(IslandEvent.Reason.ENTER) + .admin(false) + .location(user.getLocation()) + .build(); + + // Send message if island is owned by someone + 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()); + } + }); } }