Added island lock class and test class

Island lock is combined with island ban in terms of the Listener. It is
LockAndBanListener. It is a flag because after thinking about it, it
made sense to be just like any other island setting. Also, island owners
can now (in theory) lock out others by rank.

Although the test class says everything works, the Settings GUI needs to
enable toggling so that players can lock the island.

Enjoy banning players!
This commit is contained in:
Tastybento 2018-04-30 01:12:32 -07:00
parent 41631e8a18
commit 6e810747f4
16 changed files with 371 additions and 132 deletions

View File

@ -177,8 +177,6 @@ commands:
noone: "&aNo one is banned on this island"
the-following: "&bThe following players are banned:"
names: "&c[line]"
lock:
description: "lock/unlock your island so visitors cannot enter it"
settings:
description: "display island settings"
language:
@ -249,6 +247,10 @@ protection:
ITEM_DROP: "ITEM_DROP"
ITEM_PICKUP: "ITEM_PICKUP"
LEASH: "LEASH"
LOCK:
name: "Lock island"
description: |
Toggle island lock
MILKING: "MILKING"
MOB_SPAWN: "MOB_SPAWN"
MONSTER_SPAWN: "MONSTER_SPAWN"

View File

@ -9,7 +9,6 @@ import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.commands.IslandCommand;
import us.tastybento.bskyblock.database.BSBDbSetup;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.listeners.IslandBanEnforcer;
import us.tastybento.bskyblock.listeners.JoinLeaveListener;
import us.tastybento.bskyblock.listeners.NetherPortals;
import us.tastybento.bskyblock.listeners.ObsidianToLava;
@ -150,8 +149,6 @@ public class BSkyBlock extends JavaPlugin {
manager.registerEvents(new NetherPortals(this), this);
// Obsidian to lava helper
manager.registerEvents(new ObsidianToLava(this), this);
// Island ban enforcer
manager.registerEvents(new IslandBanEnforcer(this), this);
}
@Override

View File

@ -22,13 +22,15 @@ public class Flag implements Comparable<Flag> {
private final Listener listener;
private final Type type;
private boolean defaultSetting;
private final int defaultRank;
Flag(String id, Material icon, Listener listener, boolean defaultSetting, Type type) {
Flag(String id, Material icon, Listener listener, boolean defaultSetting, Type type, int defaultRank) {
this.id = id;
this.icon = icon;
this.listener = listener;
this.defaultSetting = defaultSetting;
this.type = type;
this.defaultRank = defaultRank;
}
public String getID() {
@ -65,6 +67,13 @@ public class Flag implements Comparable<Flag> {
return type;
}
/**
* @return the defaultRank
*/
public int getDefaultRank() {
return defaultRank;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@ -129,4 +138,5 @@ public class Flag implements Comparable<Flag> {
public int compareTo(Flag o) {
return getID().compareTo(o.getID());
}
}

View File

@ -4,6 +4,7 @@ import org.bukkit.Material;
import org.bukkit.event.Listener;
import us.tastybento.bskyblock.api.flags.Flag.Type;
import us.tastybento.bskyblock.managers.RanksManager;
public class FlagBuilder {
@ -12,6 +13,7 @@ public class FlagBuilder {
private Listener listener;
private boolean defaultSetting;
private Type type = Type.PROTECTION;
private int defaultRank = RanksManager.MEMBER_RANK;
public FlagBuilder id(String string) {
id = string;
@ -29,7 +31,7 @@ public class FlagBuilder {
}
public Flag build() {
return new Flag(id, icon, listener, defaultSetting, type);
return new Flag(id, icon, listener, defaultSetting, type, defaultRank);
}
/**
@ -61,4 +63,14 @@ public class FlagBuilder {
id = flag.name();
return this;
}
/**
* Set a default rank for this flag. If not set, the value of RanksManager.MEMBER_RANK will be used
* @param rank
* @return FlagBuilder
*/
public FlagBuilder defaultRank(int rank) {
this.defaultRank = rank;
return this;
}
}

View File

@ -7,6 +7,7 @@ import java.util.UUID;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.Constants;
@ -76,11 +77,14 @@ public class IslandBanCommand extends CompositeCommand {
}
private boolean ban(User user, User targetUser) {
if (getIslands().getIsland(user.getUniqueId()).addToBanList(targetUser.getUniqueId())) {
Island island = getIslands().getIsland(user.getUniqueId());
if (island.addToBanList(targetUser.getUniqueId())) {
user.sendMessage("general.success");
targetUser.sendMessage("commands.island.ban.owner-banned-you", "[owner]", user.getName());
if (targetUser.isOnline() && getPlayers().hasIsland(targetUser.getUniqueId())) {
// If the player is online, has an island and on the banned island, move them home immediately
if (targetUser.isOnline() && getPlayers().hasIsland(targetUser.getUniqueId()) && island.onIsland(targetUser.getLocation())) {
getIslands().homeTeleport(targetUser.getPlayer());
island.getWorld().playSound(targetUser.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 1F, 1F);
}
return true;
}

View File

@ -87,8 +87,6 @@ public class Island implements DataObject {
//// State ////
@Expose
private boolean locked = false;
@Expose
private boolean spawn = false;
@Expose
private boolean purgeProtected = false;
@ -175,8 +173,8 @@ public class Island implements DataObject {
if(flags.containsKey(flag)) {
return flags.get(flag);
} else {
flags.put(flag, RanksManager.MEMBER_RANK);
return RanksManager.MEMBER_RANK;
flags.put(flag, flag.getDefaultRank());
return flag.getDefaultRank();
}
}
@ -195,7 +193,7 @@ public class Island implements DataObject {
}
/**
* @return the members
* @return the members - key is the UUID, value is the RanksManager enum, e.g. RanksManager.MEMBER_RANK
*/
public Map<UUID, Integer> getMembers() {
return members;
@ -426,13 +424,6 @@ public class Island implements DataObject {
return members.containsKey(targetUUID) && members.get(targetUUID).equals(RanksManager.BANNED_RANK);
}
/**
* @return true if island is locked, false if not
*/
public boolean isLocked() {
return locked;
}
/**
* @return spawn
*/
@ -486,7 +477,7 @@ public class Island implements DataObject {
/**
* Set the Island Guard flag rank
* @param flag
* @param value - rank value. If the flag applies to the island, a positive number = true, negative = false
* @param value - Use RanksManager settings, e.g. RanksManager.MEMBER
*/
public void setFlag(Flag flag, int value){
flags.put(flag, value);
@ -513,14 +504,6 @@ public class Island implements DataObject {
this.levelHandicap = levelHandicap;
}
/**
* Locks/Unlocks the island.
* @param locked - the lock state to set
*/
public void setLocked(boolean locked){
this.locked = locked;
}
/**
* @param members the members to set
*/

View File

@ -9,9 +9,7 @@ import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.managers.PlayersManager;
public class JoinLeaveListener implements Listener {
@ -55,15 +53,6 @@ public class JoinLeaveListener implements Listener {
if (plugin.getSettings().isRemoveMobsOnLogin()) {
plugin.getIslands().removeMobs(user.getLocation());
}
// Check if they logged in to a locked island and expel them or if they are banned
Island currentIsland = plugin.getIslands().getIslandAt(user.getLocation()).orElse(null);
if (currentIsland != null && (currentIsland.isLocked() || plugin.getPlayers().isBanned(currentIsland.getOwner(),user.getUniqueId()))) {
if (!currentIsland.getMemberSet().contains(playerUUID) && !user.hasPermission(Constants.PERMPREFIX + "mod.bypassprotect")) {
user.sendMessage("protection.locked");
plugin.getIslands().homeTeleport(user.getPlayer());
}
}
}
}

View File

@ -1,13 +1,11 @@
/**
*
*/
package us.tastybento.bskyblock.listeners;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
package us.tastybento.bskyblock.listeners.flags;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -19,42 +17,37 @@ import org.bukkit.event.vehicle.VehicleMoveEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.lists.Flags;
import us.tastybento.bskyblock.managers.IslandsManager;
/**
* Enforces island bans. Checks for teleporting, entry via flying and due to logging in
* Listener for the lock flag
* Also handles ban protection
*
* @author tastybento
*
*/
public class IslandBanEnforcer implements Listener {
public class LockAndBanListener implements Listener {
private IslandsManager im;
private Set<UUID> inTeleport;
private enum CheckResult {
BANNED,
LOCKED,
OPEN
}
/**
* Enforces island bans
* Enforces island bans and locks
* @param plugin
*/
public IslandBanEnforcer(BSkyBlock plugin) {
this.im = plugin.getIslands();
inTeleport = new HashSet<>();
public LockAndBanListener() {
this.im = BSkyBlock.getInstance().getIslands();
}
// Teleport check
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerTeleport(PlayerTeleportEvent e) {
// Ignore players who are being ejected
if (inTeleport.contains(e.getPlayer().getUniqueId())) {
// Remove them
inTeleport.remove(e.getPlayer().getUniqueId());
return;
}
e.setCancelled(checkAndNotify(e.getPlayer(), e.getTo()));
// Check from - just in case the player is inside the island
if (check(e.getPlayer(), e.getFrom())) {
eject(e.getPlayer());
}
e.setCancelled(checkAndNotify(e.getPlayer(), e.getTo()).equals(CheckResult.OPEN) ? false : true);
}
// Movement check
@ -64,10 +57,14 @@ public class IslandBanEnforcer implements Listener {
if (e.getFrom().getBlockX() - e.getTo().getBlockX() == 0 && e.getFrom().getBlockZ() - e.getTo().getBlockZ() == 0) {
return;
}
e.setCancelled(checkAndNotify(e.getPlayer(), e.getTo()));
e.setCancelled(checkAndNotify(e.getPlayer(), e.getTo()).equals(CheckResult.OPEN) ? false : true);
if (e.isCancelled()) {
e.getFrom().getWorld().playSound(e.getFrom(), Sound.BLOCK_ANVIL_HIT, 1F, 1F);
}
// Check from - just in case the player is inside the island
if (check(e.getPlayer(), e.getFrom())) {
eject(e.getPlayer());
if (!check(e.getPlayer(), e.getFrom()).equals(CheckResult.OPEN)) {
// Has to be done 1 tick later otherwise it doesn't happen for some reason...
Bukkit.getScheduler().runTask(BSkyBlock.getInstance(), () -> eject(e.getPlayer()));
}
}
@ -80,9 +77,10 @@ public class IslandBanEnforcer implements Listener {
}
// For each Player in the vehicle
e.getVehicle().getPassengers().stream().filter(en -> en instanceof Player).map(en -> (Player)en).forEach(p -> {
if (checkAndNotify(p, e.getTo())) {
if (!checkAndNotify(p, e.getTo()).equals(CheckResult.OPEN)) {
p.leaveVehicle();
p.teleport(e.getFrom());
e.getFrom().getWorld().playSound(e.getFrom(), Sound.BLOCK_ANVIL_HIT, 1F, 1F);
eject(p);
}
});
@ -91,20 +89,24 @@ public class IslandBanEnforcer implements Listener {
// Login check
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onPlayerLogin(PlayerJoinEvent e) {
if (checkAndNotify(e.getPlayer(), e.getPlayer().getLocation())) {
if (!checkAndNotify(e.getPlayer(), e.getPlayer().getLocation()).equals(CheckResult.OPEN)) {
eject(e.getPlayer());
}
}
/**
* Check if a player is banned from this location
* Check if a player is banned or the island is locked
* @param player - player
* @param loc - location to check
* @return true if banned
* @return CheckResult LOCKED, BANNED or OPEN. If an island is locked, that will take priority over banned
*/
private boolean check(Player player, Location loc) {
// See if player is banned
return im.getProtectedIslandAt(loc).map(is -> is.isBanned(player.getUniqueId())).orElse(false);
private CheckResult check(Player player, Location loc) {
// See if the island is locked to non-members or player is banned
return im.getProtectedIslandAt(loc)
.map(is -> !is.isAllowed(User.getInstance(player), Flags.LOCK) ? CheckResult.LOCKED
: is.isBanned(player.getUniqueId()) ? CheckResult.BANNED
: CheckResult.OPEN)
.orElse(CheckResult.OPEN);
}
/**
@ -113,12 +115,19 @@ public class IslandBanEnforcer implements Listener {
* @param loc - location to check
* @return true if banned
*/
private boolean checkAndNotify(Player player, Location loc) {
if (check(player, loc)) {
private CheckResult checkAndNotify(Player player, Location loc) {
CheckResult r = check(player,loc);
switch (r) {
case BANNED:
User.getInstance(player).notify("commands.island.ban.you-are-banned");
return true;
break;
case LOCKED:
User.getInstance(player).notify("protection.locked");
break;
default:
break;
}
return false;
return r;
}
/**
@ -127,7 +136,6 @@ public class IslandBanEnforcer implements Listener {
*/
private void eject(Player player) {
// Teleport player to their home
inTeleport.add(player.getUniqueId());
if (im.hasIsland(player.getUniqueId())) {
im.homeTeleport(player);
} // else, TODO: teleport somewhere else?

View File

@ -21,12 +21,14 @@ import us.tastybento.bskyblock.listeners.flags.HurtingListener;
import us.tastybento.bskyblock.listeners.flags.InventoryListener;
import us.tastybento.bskyblock.listeners.flags.ItemDropPickUpListener;
import us.tastybento.bskyblock.listeners.flags.LeashListener;
import us.tastybento.bskyblock.listeners.flags.LockAndBanListener;
import us.tastybento.bskyblock.listeners.flags.PVPListener;
import us.tastybento.bskyblock.listeners.flags.PhysicalInteractionListener;
import us.tastybento.bskyblock.listeners.flags.PlaceBlocksListener;
import us.tastybento.bskyblock.listeners.flags.PortalListener;
import us.tastybento.bskyblock.listeners.flags.ShearingListener;
import us.tastybento.bskyblock.listeners.flags.TeleportationListener;
import us.tastybento.bskyblock.managers.RanksManager;
public class Flags {
@ -116,6 +118,9 @@ public class Flags {
public static final Flag ITEM_DROP = new FlagBuilder().id("ITEM_DROP").icon(Material.BEETROOT_SOUP).allowedByDefault(true).listener(new ItemDropPickUpListener()).build();
public static final Flag ITEM_PICKUP = new FlagBuilder().id("ITEM_PICKUP").icon(Material.BEETROOT_SEEDS).build();
// Island lock
public static final Flag LOCK = new FlagBuilder().id("LOCK").icon(Material.TRIPWIRE_HOOK).type(Type.PROTECTION).allowedByDefault(true).defaultRank(RanksManager.VISITOR_RANK).listener(new LockAndBanListener()).build();
/*
* Settings flags (not protection flags)
*/

View File

@ -25,6 +25,7 @@ import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.BSBDatabase;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.lists.Flags;
import us.tastybento.bskyblock.managers.island.IslandCache;
import us.tastybento.bskyblock.util.DeleteIslandChunks;
import us.tastybento.bskyblock.util.Util;
@ -246,7 +247,7 @@ public class IslandsManager {
}
// Set the owner of the island to no one.
island.setOwner(null);
island.setLocked(false);
island.setFlag(Flags.LOCK, RanksManager.VISITOR_RANK);
if (removeBlocks) {
// Remove players from island
removePlayersFromIsland(island);

View File

@ -46,6 +46,7 @@ import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Constants;
@ -66,9 +67,7 @@ import us.tastybento.bskyblock.managers.RanksManager;
import us.tastybento.bskyblock.util.Util;
@RunWith(PowerMockRunner.class)
//@SuppressStaticInitializationFor("us.tastybento.BSkyBlock")
//@PrepareForTest( { Bukkit.class })
@PrepareForTest( { Flags.class })
@PrepareForTest({ BSkyBlock.class, Flags.class})
public class TestBSkyBlock {
private static final UUID MEMBER_UUID = UUID.randomUUID();
private static final UUID OWNER_UUID = UUID.randomUUID();
@ -85,6 +84,10 @@ public class TestBSkyBlock {
@BeforeClass
public static void setUp() {
// Set up plugin
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
Server server = mock(Server.class);
World world = mock(World.class);
Mockito.when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
@ -108,7 +111,6 @@ public class TestBSkyBlock {
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
//when(Bukkit.getServer()).thenReturn(server);
sender = mock(CommandSender.class);
player = mock(Player.class);
@ -117,8 +119,6 @@ public class TestBSkyBlock {
Mockito.when(player.hasPermission(Constants.PERMPREFIX + "default.permission")).thenReturn(true);
//Mockito.when(plugin.getServer()).thenReturn(server);
location = mock(Location.class);
Mockito.when(location.getWorld()).thenReturn(world);
Mockito.when(location.getBlockX()).thenReturn(0);
@ -148,9 +148,6 @@ public class TestBSkyBlock {
Mockito.when(iwm.getNetherWorld()).thenReturn(world);
Mockito.when(iwm.getEndWorld()).thenReturn(world);
// User
//User user = Mockito.mock(User.class);
//Mockito.when(user.getName()).thenReturn("tastybento");
// Islands
IslandsManager im = mock(IslandsManager.class);

View File

@ -129,7 +129,6 @@ public class MySQLDatabaseHandlerTest {
}
island.setFlags(flags);
island.setLevelHandicap(10);
island.setLocked(true);
Map<UUID, Integer> members = new HashMap<>();
for (int i = 0; i < 10; i++) {
members.put(UUID.randomUUID(), i);

View File

@ -38,6 +38,7 @@ import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
@ -51,7 +52,7 @@ import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
@RunWith(PowerMockRunner.class)
@PrepareForTest( { Flags.class} )
@PrepareForTest( {BSkyBlock.class, Flags.class} )
public class FireListenerTest {
private static Location location;
@ -63,6 +64,10 @@ public class FireListenerTest {
@BeforeClass
public static void setUp() {
// Set up plugin
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
Server server = mock(Server.class);
World world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
@ -88,7 +93,6 @@ public class FireListenerTest {
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
plugin = Mockito.mock(BSkyBlock.class);
flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);

View File

@ -1,7 +1,4 @@
/**
*
*/
package us.tastybento.bskyblock.listeners;
package us.tastybento.bskyblock.listeners.flags;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -30,6 +27,8 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@ -40,17 +39,14 @@ import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.Notifier;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.lists.Flags;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class })
public class IslandBanEnforcerTest {
public class LockAndBanListenerTest {
private static final Integer PROTECTION_RANGE = 200;
private static final Integer X = 600;
@ -64,12 +60,13 @@ public class IslandBanEnforcerTest {
private PlayersManager pm;
private Island island;
private World world;
private IslandBanEnforcer ibe;
private LockAndBanListener listener;
private Location loc;
private Location outside;
private Location inside;
private Notifier notifier;
private Location inside2;
private BukkitScheduler sch;
/**
* @throws java.lang.Exception
@ -113,7 +110,7 @@ public class IslandBanEnforcerTest {
when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
@ -126,7 +123,6 @@ public class IslandBanEnforcerTest {
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
// Island Banned list initialization
island = mock(Island.class);
when(island.getBanned()).thenReturn(new HashSet<>());
@ -138,11 +134,13 @@ public class IslandBanEnforcerTest {
when(loc.getBlockZ()).thenReturn(Z);
when(island.getCenter()).thenReturn(loc);
when(island.getProtectionRange()).thenReturn(PROTECTION_RANGE);
// Island is not locked by default
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
when(im.getIsland(Mockito.any(UUID.class))).thenReturn(island);
ibe = new IslandBanEnforcer(plugin);
// Create the listener object
listener = new LockAndBanListener();
// Common from to's
outside = mock(Location.class);
@ -178,7 +176,7 @@ public class IslandBanEnforcerTest {
// Simulate a teleport into an island
PlayerTeleportEvent e = new PlayerTeleportEvent(player, outside, inside);
// Pass to event listener
ibe.onPlayerTeleport(e);
listener.onPlayerTeleport(e);
// Should not be cancelled
assertFalse(e.isCancelled());
// User should see no message from this class
@ -197,7 +195,7 @@ public class IslandBanEnforcerTest {
// Simulate a teleport into an island
PlayerTeleportEvent e = new PlayerTeleportEvent(player, outside, inside);
// Pass to event listener
ibe.onPlayerTeleport(e);
listener.onPlayerTeleport(e);
// Should be cancelled
assertTrue(e.isCancelled());
// Player should see a message
@ -218,7 +216,7 @@ public class IslandBanEnforcerTest {
when(island.isBanned(Mockito.eq(uuid))).thenReturn(true);
// Log them in
ibe.onPlayerLogin(new PlayerJoinEvent(player, "join message"));
listener.onPlayerLogin(new PlayerJoinEvent(player, "join message"));
// User should see a message
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
// User should be teleported somewhere
@ -226,7 +224,7 @@ public class IslandBanEnforcerTest {
// Call teleport event
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, outside);
// Pass to event listener
ibe.onPlayerTeleport(e);
listener.onPlayerTeleport(e);
// Should not be cancelled
assertFalse(e.isCancelled());
}
@ -245,7 +243,7 @@ public class IslandBanEnforcerTest {
when(to.getBlockY()).thenReturn(55);
when(to.getBlockZ()).thenReturn(Z);
PlayerMoveEvent e = new PlayerMoveEvent(user.getPlayer(), from, to);
ibe.onPlayerMove(e);
listener.onPlayerMove(e);
assertFalse(e.isCancelled());
// Confirm no check is done on the island
Mockito.verify(im, Mockito.never()).getProtectedIslandAt(Mockito.any());
@ -272,7 +270,7 @@ public class IslandBanEnforcerTest {
passengers.add(player2);
when(vehicle.getPassengers()).thenReturn(passengers);
// Move vehicle
ibe.onVehicleMove(new VehicleMoveEvent(vehicle, from, to));
listener.onVehicleMove(new VehicleMoveEvent(vehicle, from, to));
// Confirm no check is done on the island
Mockito.verify(im, Mockito.never()).getProtectedIslandAt(Mockito.any());
}
@ -292,7 +290,7 @@ public class IslandBanEnforcerTest {
// Move player
PlayerMoveEvent e = new PlayerMoveEvent(player, outside, inside);
ibe.onPlayerMove(e);
listener.onPlayerMove(e);
assertTrue(e.isCancelled());
// Player should see a message
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
@ -314,16 +312,16 @@ public class IslandBanEnforcerTest {
when(island.isBanned(Mockito.eq(uuid))).thenReturn(true);
// Move player
PlayerMoveEvent e = new PlayerMoveEvent(player, inside, inside2);
ibe.onPlayerMove(e);
listener.onPlayerMove(e);
assertTrue(e.isCancelled());
// Player should see a message
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
// User should be teleported somewhere
Mockito.verify(im).homeTeleport(Mockito.eq(player));
Mockito.verify(sch).runTask(Mockito.any(), Mockito.any(Runnable.class));
// Call teleport event
PlayerTeleportEvent ev = new PlayerTeleportEvent(player, inside, outside);
// Pass to event listener
ibe.onPlayerTeleport(ev);
listener.onPlayerTeleport(ev);
// Should not be cancelled
assertFalse(ev.isCancelled());
}
@ -350,7 +348,7 @@ public class IslandBanEnforcerTest {
passengers.add(player2);
when(vehicle.getPassengers()).thenReturn(passengers);
// Move vehicle
ibe.onVehicleMove(new VehicleMoveEvent(vehicle, outside, inside));
listener.onVehicleMove(new VehicleMoveEvent(vehicle, outside, inside));
// Player should see a message and nothing should be sent to Player 2
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
// User should be teleported somewhere
@ -360,8 +358,224 @@ public class IslandBanEnforcerTest {
// Call teleport event
PlayerTeleportEvent ev = new PlayerTeleportEvent(player, inside, outside);
// Pass to event listener
ibe.onPlayerTeleport(ev);
listener.onPlayerTeleport(ev);
// Should not be cancelled
assertFalse(ev.isCancelled());
}
/*
* Island lock tests
*/
@Test
public void testTeleportToLockedIsland() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Lock island for player
when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false);
// Simulate a teleport into an island
PlayerTeleportEvent e = new PlayerTeleportEvent(player, outside, inside);
// Pass to event listener
listener.onPlayerTeleport(e);
// Should be cancelled
assertTrue(e.isCancelled());
// Player should see a message
Mockito.verify(notifier).notify(Mockito.any(), Mockito.any());
}
@Test
public void testTeleportToLockedIslandAsMember() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Simulate a teleport into an island
PlayerTeleportEvent e = new PlayerTeleportEvent(player, outside, inside);
// Pass to event listener
listener.onPlayerTeleport(e);
// Should not be not cancelled
assertFalse(e.isCancelled());
// Player should not see a message
Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.any());
}
@Test
public void testLoginToLockedIsland() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Give player an island
when(im.hasIsland(uuid)).thenReturn(true);
// Place the player on the island
when(player.getLocation()).thenReturn(inside);
// Lock island for player
when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false);
// Log them in
listener.onPlayerLogin(new PlayerJoinEvent(player, "join message"));
// User should see a message
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
// User should be teleported somewhere
Mockito.verify(im).homeTeleport(Mockito.eq(player));
// Call teleport event
PlayerTeleportEvent e = new PlayerTeleportEvent(player, inside, outside);
// Pass to event listener
listener.onPlayerTeleport(e);
// Should not be cancelled
assertFalse(e.isCancelled());
}
@Test
public void testLoginToLockedIslandAsMember() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Give player an island
when(im.hasIsland(uuid)).thenReturn(true);
// Place the player on the island
when(player.getLocation()).thenReturn(inside);
// Log them in
listener.onPlayerLogin(new PlayerJoinEvent(player, "join message"));
// User should not see a message
Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString());
// User should not be teleported somewhere
Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.eq(player));
}
@Test
public void testPlayerMoveIntoLockedIsland() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Give player an island
when(im.hasIsland(uuid)).thenReturn(true);
// Place the player just outside island
when(player.getLocation()).thenReturn(outside);
// Lock island for player
when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false);
// Move player
PlayerMoveEvent e = new PlayerMoveEvent(player, outside, inside);
listener.onPlayerMove(e);
assertTrue(e.isCancelled());
// Player should see a message
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
// User should NOT be teleported somewhere
Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.eq(player));
}
@Test
public void testPlayerMoveIntoLockedIslandAsMember() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Give player an island
when(im.hasIsland(uuid)).thenReturn(true);
// Place the player just outside island
when(player.getLocation()).thenReturn(outside);
// Move player
PlayerMoveEvent e = new PlayerMoveEvent(player, outside, inside);
listener.onPlayerMove(e);
// Should not be cancelled
assertFalse(e.isCancelled());
// Player should not see a message
Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString());
// User should NOT be teleported somewhere
Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.eq(player));
}
@Test
public void testPlayerMoveInsideLockedIsland() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Give player an island
when(im.hasIsland(uuid)).thenReturn(true);
// Place the player inside island
when(player.getLocation()).thenReturn(inside);
// Lock island for player
when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false);
// Move player
PlayerMoveEvent e = new PlayerMoveEvent(player, inside, inside2);
listener.onPlayerMove(e);
assertTrue(e.isCancelled());
// Player should see a message
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
// User should be teleported somewhere
Mockito.verify(sch).runTask(Mockito.any(), Mockito.any(Runnable.class));
// Call teleport event
PlayerTeleportEvent ev = new PlayerTeleportEvent(player, inside, outside);
// Pass to event listener
listener.onPlayerTeleport(ev);
// Should not be cancelled
assertFalse(ev.isCancelled());
}
@Test
public void testPlayerMoveInsideLockedIslandAsMember() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Give player an island
when(im.hasIsland(uuid)).thenReturn(true);
// Place the player inside island
when(player.getLocation()).thenReturn(inside);
// Move player
PlayerMoveEvent e = new PlayerMoveEvent(player, inside, inside2);
listener.onPlayerMove(e);
assertFalse(e.isCancelled());
// Player should not see a message
Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString());
// User should not be teleported somewhere
Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.eq(player));
}
@Test
public void testVehicleMoveIntoLockedIsland() {
// Make player
Player player = mock(Player.class);
when(player.getUniqueId()).thenReturn(uuid);
// Give player an island
when(im.hasIsland(uuid)).thenReturn(true);
Player player2 = mock(Player.class);
UUID uuid2 = UUID.randomUUID();
when(player2.getUniqueId()).thenReturn(uuid2);
// Player 1 is not a member, player 2 is an island member
when(island.isAllowed(Mockito.any(User.class), Mockito.any())).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgumentAt(0, User.class).getUniqueId().equals(uuid2) ? true : false;
}
});
// Create vehicle and put two players in it. One is a member, the other is not
Vehicle vehicle = mock(Vehicle.class);
List<Entity> passengers = new ArrayList<>();
passengers.add(player);
passengers.add(player2);
when(vehicle.getPassengers()).thenReturn(passengers);
// Move vehicle
listener.onVehicleMove(new VehicleMoveEvent(vehicle, outside, inside));
// Player should see a message and nothing should be sent to Player 2
Mockito.verify(notifier).notify(Mockito.any(), Mockito.anyString());
// User should be teleported somewhere
Mockito.verify(im).homeTeleport(Mockito.eq(player));
// Player 2 should not be teleported
Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.eq(player2));
// Call teleport event
PlayerTeleportEvent ev = new PlayerTeleportEvent(player, inside, outside);
// Pass to event listener
listener.onPlayerTeleport(ev);
// Should not be cancelled
assertFalse(ev.isCancelled());
}
}

View File

@ -31,6 +31,7 @@ import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.objects.Island;
@ -40,7 +41,7 @@ import us.tastybento.bskyblock.managers.FlagsManager;
import us.tastybento.bskyblock.managers.IslandsManager;
@RunWith(PowerMockRunner.class)
@PrepareForTest( { Flags.class })
@PrepareForTest( {BSkyBlock.class, Flags.class })
public class MobSpawnListenerTest {
private static Location location;
@ -52,6 +53,13 @@ public class MobSpawnListenerTest {
@BeforeClass
public static void setUp() {
// Set up plugin
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Server server = mock(Server.class);
World world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
@ -77,7 +85,6 @@ public class MobSpawnListenerTest {
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
plugin = Mockito.mock(BSkyBlock.class);
flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);

View File

@ -27,6 +27,7 @@ import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.api.flags.Flag;
@ -35,12 +36,22 @@ import us.tastybento.bskyblock.listeners.flags.BreakBlocksListener;
import us.tastybento.bskyblock.lists.Flags;
@RunWith(PowerMockRunner.class)
@PrepareForTest( { Flags.class} )
@PrepareForTest( {BSkyBlock.class, Flags.class} )
public class FlagsManagerTest {
private static BSkyBlock plugin;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
// Set up plugin
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Server server = mock(Server.class);
World world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
@ -65,13 +76,11 @@ public class FlagsManagerTest {
@Test
public void testFlagsManager() {
BSkyBlock plugin = mock(BSkyBlock.class);
assertNotNull(new FlagsManager(plugin));
}
@Test
public void testRegisterFlag() {
BSkyBlock plugin = mock(BSkyBlock.class);
FlagsManager fm = new FlagsManager(plugin);
// Try to register every single flag - it should fail every time
Flags.values().forEach(dupe -> assertFalse(fm.registerFlag(dupe)));
@ -90,14 +99,12 @@ public class FlagsManagerTest {
@Test
public void testGetFlags() {
BSkyBlock plugin = mock(BSkyBlock.class);
FlagsManager fm = new FlagsManager(plugin);
assertThat(fm.getFlags(), is(Flags.values()));
}
@Test
public void testGetFlagByID() {
BSkyBlock plugin = mock(BSkyBlock.class);
FlagsManager fm = new FlagsManager(plugin);
// Test in forward and reverse order so that any duplicates are caught
Flags.values().stream().sorted().forEach(flag -> assertEquals(flag, fm.getFlagByID(flag.getID())));