Merge pull request #238 from BentoBoxWorld/enter-exit-listener

Updated EnterExitListener to fire IslandEnterEvent and IslandExitEvent
This commit is contained in:
tastybento 2018-08-16 11:03:55 -07:00 committed by GitHub
commit 5740ad7e0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 22 deletions

View File

@ -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) {

View File

@ -10,6 +10,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.util.Vector;
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 +35,8 @@ public class EnterExitListener extends AbstractFlagListener {
return;
}
Optional<Island> from = this.getIslands().getProtectedIslandAt(e.getFrom());
Optional<Island> to = this.getIslands().getProtectedIslandAt(e.getTo());
Optional<Island> from = getIslands().getProtectedIslandAt(e.getFrom());
Optional<Island> to = getIslands().getProtectedIslandAt(e.getTo());
/*
* Options:
@ -51,14 +52,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());
}
});
}
}

View File

@ -14,9 +14,11 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.Vector;
import org.junit.Before;
import org.junit.Test;
@ -71,6 +73,13 @@ public class EnterExitListenerTest {
// World
World world = mock(World.class);
// Server
Server server = mock(Server.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
PluginManager pim = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pim);
// Settings
Settings s = mock(Settings.class);
when(plugin.getSettings()).thenReturn(s);
@ -195,7 +204,7 @@ public class EnterExitListenerTest {
// Moving into the island should show a message
Mockito.verify(lm).get(Mockito.any(), Mockito.eq("protection.flags.ENTER_EXIT_MESSAGES.now-entering"));
// The island owner needs to be checked
Mockito.verify(island, Mockito.times(2)).getOwner();
Mockito.verify(island).getOwner();
}
/**
@ -209,7 +218,7 @@ public class EnterExitListenerTest {
// Moving into the island should show a message
Mockito.verify(lm).get(Mockito.any(), Mockito.eq("protection.flags.ENTER_EXIT_MESSAGES.now-entering"));
// No owner check
Mockito.verify(island, Mockito.times(2)).getOwner();
Mockito.verify(island).getOwner();
Mockito.verify(island, Mockito.times(2)).getName();
}
@ -224,7 +233,7 @@ public class EnterExitListenerTest {
// Moving into the island should show a message
Mockito.verify(lm).get(Mockito.any(), Mockito.eq("protection.flags.ENTER_EXIT_MESSAGES.now-leaving"));
// The island owner needs to be checked
Mockito.verify(island, Mockito.times(2)).getOwner();
Mockito.verify(island).getOwner();
}
/**
@ -238,7 +247,7 @@ public class EnterExitListenerTest {
// Moving into the island should show a message
Mockito.verify(lm).get(Mockito.any(), Mockito.eq("protection.flags.ENTER_EXIT_MESSAGES.now-leaving"));
// No owner check
Mockito.verify(island, Mockito.times(2)).getOwner();
Mockito.verify(island).getOwner();
Mockito.verify(island, Mockito.times(2)).getName();
}