From 97222808acdfbb6f96643b152ed75a4cd928306e Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 2 Sep 2018 08:50:40 +0200 Subject: [PATCH 1/5] Fixed players considered "unknown" until they create an island --- .../world/bentobox/bentobox/listeners/JoinLeaveListener.java | 4 ++-- .../java/world/bentobox/bentobox/managers/PlayersManager.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java index 2368b171f..78c1fddf7 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java @@ -36,9 +36,9 @@ public class JoinLeaveListener implements Listener { return; } UUID playerUUID = user.getUniqueId(); + // Load player + players.addPlayer(playerUUID); if (plugin.getPlayers().isKnown(playerUUID)) { - // Load player - players.addPlayer(playerUUID); // Reset island resets if required plugin.getIWM().getOverWorlds().stream() .filter(w -> event.getPlayer().getLastPlayed() < plugin.getIWM().getResetEpoch(w)) diff --git a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java index 99dd145fb..964342c41 100644 --- a/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/PlayersManager.java @@ -122,7 +122,7 @@ public class PlayersManager { * in the database too. * * @param uniqueID - unique ID - * @return true if player is know, otherwise false + * @return true if player is known, otherwise false */ public boolean isKnown(UUID uniqueID) { return uniqueID != null && (playerCache.containsKey(uniqueID) || handler.objectExists(uniqueID.toString())); From e5cc56b9197b147ccb2638fad11dbc970ade0022 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 2 Sep 2018 11:55:26 +0200 Subject: [PATCH 2/5] Added #askConfirmation(User, String, Runnable) in ConfirmableCommand This can allow the command to give a bit more context about the confirmation request. ConfirmableCommand#askConfirmation(User, Runnable) can still be used. --- .../api/commands/ConfirmableCommand.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/ConfirmableCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/ConfirmableCommand.java index 0687900e5..5f7ffcc49 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/ConfirmableCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/ConfirmableCommand.java @@ -48,11 +48,12 @@ public abstract class ConfirmableCommand extends CompositeCommand { } /** - * Tells user to confirm command by retyping - * @param user - user - * @param confirmed - runnable to be executed if confirmed + * Tells user to confirm command by retyping it. + * @param user User to ask confirmation to. + * @param message Optional message to send to the user to give them a bit more context. It must already be translated. + * @param confirmed Runnable to be executed if successfully confirmed. */ - public void askConfirmation(User user, Runnable confirmed) { + public void askConfirmation(User user, String message, Runnable confirmed) { // Check for pending confirmations if (toBeConfirmed.containsKey(user)) { if (toBeConfirmed.get(user).getTopLabel().equals(getTopLabel()) && toBeConfirmed.get(user).getLabel().equalsIgnoreCase(getLabel())) { @@ -65,6 +66,10 @@ public abstract class ConfirmableCommand extends CompositeCommand { user.sendMessage("commands.confirmation.previous-request-cancelled"); } } + // Send user the context message if it is not empty + if (!message.trim().isEmpty()) { + user.sendRawMessage(message); + } // Tell user that they need to confirm user.sendMessage("commands.confirmation.confirm", "[seconds]", String.valueOf(getSettings().getConfirmationTime())); // Set up a cancellation task @@ -77,6 +82,15 @@ public abstract class ConfirmableCommand extends CompositeCommand { toBeConfirmed.put(user, new Confirmer(getTopLabel(), getLabel(), confirmed, task)); } + /** + * Tells user to confirm command by retyping it. + * @param user User to ask confirmation to. + * @param confirmed Runnable to be executed if successfully confirmed. + */ + public void askConfirmation(User user, Runnable confirmed) { + askConfirmation(user, "", confirmed); + } + private class Confirmer { private final String topLabel; private final String label; From fbf88483f0452d8b80374da16a9cf8c56ffb93a5 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 2 Sep 2018 15:06:46 +0200 Subject: [PATCH 3/5] Added trust and coop cooldown in config instead of using the invite cooldown for /is team coop and /is team trust --- .../world/bentobox/bentobox/Settings.java | 24 +++++++++++++++++++ .../island/team/IslandTeamCoopCommand.java | 2 +- .../island/team/IslandTeamTrustCommand.java | 2 +- .../island/team/IslandTeamUncoopCommand.java | 4 ++-- .../island/team/IslandTeamUntrustCommand.java | 4 ++-- 5 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/Settings.java b/src/main/java/world/bentobox/bentobox/Settings.java index 02e2c69ca..d93a67526 100644 --- a/src/main/java/world/bentobox/bentobox/Settings.java +++ b/src/main/java/world/bentobox/bentobox/Settings.java @@ -107,6 +107,14 @@ public class Settings implements DataObject { @ConfigEntry(path = "island.cooldown.invite") private int inviteCooldown = 60; + @ConfigComment("How long a player must wait until they can coop a player in minutes.") + @ConfigEntry(path = "island.cooldown.coop") + private int coopCooldown = 5; + + @ConfigComment("How long a player must wait until they can trust a player in minutes.") + @ConfigEntry(path = "island.cooldown.trust") + private int trustCooldown = 5; + @ConfigComment("How long a player must wait until they can ban a player") @ConfigComment("after unbanning them. In minutes.") @ConfigEntry(path = "island.cooldown.ban") @@ -286,6 +294,22 @@ public class Settings implements DataObject { this.inviteCooldown = inviteCooldown; } + public int getCoopCooldown() { + return coopCooldown; + } + + public void setCoopCooldown(int coopCooldown) { + this.coopCooldown = coopCooldown; + } + + public int getTrustCooldown() { + return trustCooldown; + } + + public void setTrustCooldown(int trustCooldown) { + this.trustCooldown = trustCooldown; + } + public int getBanCooldown() { return banCooldown; } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java index af17a6ce5..fb9e8992c 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamCoopCommand.java @@ -54,7 +54,7 @@ public class IslandTeamCoopCommand extends CompositeCommand { user.sendMessage("general.errors.unknown-player"); return false; } - return (getSettings().getInviteCooldown() <= 0 || !checkCooldown(user, targetUUID)) && coopCmd(user, targetUUID); + return (getSettings().getCoopCooldown() <= 0 || !checkCooldown(user, targetUUID)) && coopCmd(user, targetUUID); } private boolean coopCmd(User user, UUID targetUUID) { diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java index 78404297d..c893f422e 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamTrustCommand.java @@ -58,7 +58,7 @@ public class IslandTeamTrustCommand extends CompositeCommand { user.sendMessage("general.errors.unknown-player"); return false; } - return (getSettings().getInviteCooldown() <= 0 || !checkCooldown(user, targetUUID)) && trustCmd(user, targetUUID); + return (getSettings().getTrustCooldown() <= 0 || !checkCooldown(user, targetUUID)) && trustCmd(user, targetUUID); } private boolean trustCmd(User user, UUID targetUUID) { diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java index eaccae5d2..6a2a00bcb 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUncoopCommand.java @@ -84,9 +84,9 @@ public class IslandTeamUncoopCommand extends CompositeCommand { user.sendMessage("general.success"); target.sendMessage("commands.island.team.uncoop.you-are-no-longer-a-coop-member", TextVariables.NAME, user.getName()); // Set cooldown - if (getSettings().getInviteCooldown() > 0 && getParent() != null) { + if (getSettings().getCoopCooldown() > 0 && getParent() != null) { getParent().getSubCommand("coop").ifPresent(subCommand -> - subCommand.setCooldown(user.getUniqueId(), targetUUID, getSettings().getInviteCooldown() * 60)); + subCommand.setCooldown(user.getUniqueId(), targetUUID, getSettings().getCoopCooldown() * 60)); } return true; } else { diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java index e9dedb6fa..e1cd64a44 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamUntrustCommand.java @@ -84,9 +84,9 @@ public class IslandTeamUntrustCommand extends CompositeCommand { user.sendMessage("general.success"); target.sendMessage("commands.island.team.untrust.you-are-no-longer-trusted", TextVariables.NAME, user.getName()); // Set cooldown - if (getSettings().getInviteCooldown() > 0 && getParent() != null) { + if (getSettings().getTrustCooldown() > 0 && getParent() != null) { getParent().getSubCommand("trust").ifPresent(subCommand -> - subCommand.setCooldown(user.getUniqueId(), targetUUID, getSettings().getInviteCooldown() * 60)); + subCommand.setCooldown(user.getUniqueId(), targetUUID, getSettings().getTrustCooldown() * 60)); } return true; } else { From ee9c7937ea793162d12513ffced64a7c6e60f77e Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 2 Sep 2018 17:25:15 +0200 Subject: [PATCH 4/5] Use User#notify() instead of #sendMessage() in PVPListener --- .../bentobox/listeners/flags/PVPListener.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/PVPListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/PVPListener.java index 61a75d1cc..b15c6d459 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/PVPListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/PVPListener.java @@ -50,9 +50,9 @@ public class PVPListener extends AbstractFlagListener { // Protect visitors if (e.getCause().equals(DamageCause.ENTITY_ATTACK) && protectedVisitor((Player)e.getEntity())) { if (e.getDamager() instanceof Player) { - User.getInstance(e.getDamager()).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + User.getInstance(e.getDamager()).notify(Flags.INVINCIBLE_VISITORS.getHintReference()); } else if (e.getDamager() instanceof Projectile && ((Projectile)e.getDamager()).getShooter() instanceof Player) { - User.getInstance((Player)((Projectile)e.getDamager()).getShooter()).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + User.getInstance((Player)((Projectile)e.getDamager()).getShooter()).notify(Flags.INVINCIBLE_VISITORS.getHintReference()); } e.setCancelled(true); } else { @@ -73,7 +73,7 @@ public class PVPListener extends AbstractFlagListener { if (damager instanceof Player) { User user = User.getInstance(damager); if (!setUser(user).checkIsland((Event)e, damager.getLocation(), flag)) { - user.sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + user.notify(Flags.PVP_OVERWORLD.getHintReference()); e.setCancelled(true); } } else if (damager instanceof Projectile) { @@ -88,7 +88,7 @@ public class PVPListener extends AbstractFlagListener { if (!setUser(user).checkIsland((Event)e, damager.getLocation(), flag)) { damager.setFireTicks(0); damager.remove(); - user.sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + user.notify(Flags.PVP_OVERWORLD.getHintReference()); e.setCancelled(true); } } @@ -104,11 +104,11 @@ public class PVPListener extends AbstractFlagListener { } // Protect visitors if (protectedVisitor((Player)e.getCaught())) { - User.getInstance(e.getPlayer()).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + User.getInstance(e.getPlayer()).notify(Flags.INVINCIBLE_VISITORS.getHintReference()); e.setCancelled(true); } else if (!checkIsland(e, e.getCaught().getLocation(), getFlag(e.getCaught().getWorld()))) { e.getHook().remove(); - User.getInstance(e.getPlayer()).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + User.getInstance(e.getPlayer()).notify(Flags.PVP_OVERWORLD.getHintReference()); e.setCancelled(true); } } @@ -143,12 +143,12 @@ public class PVPListener extends AbstractFlagListener { if (le instanceof Player) { // Protect visitors if (protectedVisitor(le)) { - user.sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + user.notify(Flags.INVINCIBLE_VISITORS.getHintReference()); return true; } // Check if PVP is allowed or not if (!checkIsland(e, le.getLocation(), flag)) { - user.sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + user.notify(Flags.PVP_OVERWORLD.getHintReference()); return true; } } From 505624b45cb3fce0fc0a04a6bf9aed4cd967d17e Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 2 Sep 2018 20:45:31 -0700 Subject: [PATCH 5/5] Fixes tests. Also fixes a bug. https://github.com/BentoBoxWorld/bentobox/issues/258 --- .../bentobox/database/objects/Island.java | 15 ++--- .../bentobox/managers/IslandsManager.java | 14 ++++- .../bentobox/panels/SettingsPanel.java | 1 - .../listeners/flags/PVPListenerTest.java | 60 ++++++++++++------- .../bentobox/managers/IslandsManagerTest.java | 23 ++++--- 5 files changed, 72 insertions(+), 41 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/database/objects/Island.java b/src/main/java/world/bentobox/bentobox/database/objects/Island.java index 2487db1e8..60833c196 100644 --- a/src/main/java/world/bentobox/bentobox/database/objects/Island.java +++ b/src/main/java/world/bentobox/bentobox/database/objects/Island.java @@ -190,6 +190,7 @@ public class Island implements DataObject { } /** + * Members > MEMBER_RANK * @return the members of the island (owner included) */ public ImmutableSet getMemberSet(){ @@ -605,14 +606,14 @@ public class Island implements DataObject { // Fixes #getLastPlayed() returning 0 when it is the owner's first connection. long lastPlayed = (plugin.getServer().getOfflinePlayer(owner).getLastPlayed() != 0) ? plugin.getServer().getOfflinePlayer(owner).getLastPlayed() : plugin.getServer().getOfflinePlayer(owner).getFirstPlayed(); - user.sendMessage("commands.admin.info.last-login","[date]", new Date(lastPlayed).toString()); + user.sendMessage("commands.admin.info.last-login","[date]", new Date(lastPlayed).toString()); - user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(world, owner))); - String resets = String.valueOf(plugin.getPlayers().getResets(world, owner)); - String total = plugin.getIWM().getResetLimit(world) < 0 ? "Unlimited" : String.valueOf(plugin.getIWM().getResetLimit(world)); - user.sendMessage("commands.admin.info.resets-left", "[number]", resets, "[total]", total); - // Show team members - showMembers(plugin, user, world); + user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(world, owner))); + String resets = String.valueOf(plugin.getPlayers().getResets(world, owner)); + String total = plugin.getIWM().getResetLimit(world) < 0 ? "Unlimited" : String.valueOf(plugin.getIWM().getResetLimit(world)); + user.sendMessage("commands.admin.info.resets-left", "[number]", resets, "[total]", total); + // Show team members + showMembers(plugin, user, world); } Vector location = center.toVector(); user.sendMessage("commands.admin.info.island-location", "[xyz]", Util.xyz(location)); diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index 1e7e29f7f..d9f43da75 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -285,7 +285,9 @@ public class IslandsManager { /** * Returns the island at the location or Optional empty if there is none. - * This includes the full island space, not just the protected area + * This includes the full island space, not just the protected area. + * Use {@link #getProtectedIslandAt(Location)} for only the protected + * island space. * * @param location - the location * @return Optional Island object @@ -334,7 +336,9 @@ public class IslandsManager { } /** - * Returns the island being public at the location or Optional Empty if there is none + * Returns the island at the location or Optional empty if there is none. + * This includes only the protected area. Use {@link #getIslandAt(Location)} + * for the full island space. * * @param location - the location * @return Optional Island object @@ -631,7 +635,11 @@ public class IslandsManager { if (user == null) { return false; } - return Optional.ofNullable(getIsland(world, user)).map(i -> i.onIsland(user.getLocation())).orElse(false); + return getProtectedIslandAt(user.getLocation()) + .map(i -> i.getMembers().entrySet().stream() + .map(en -> en.getKey().equals(user.getUniqueId()) && en.getValue() > RanksManager.VISITOR_RANK) + .findAny().orElse(false)) + .orElse(false); } /** diff --git a/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java b/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java index 6b2a10a91..2db99c127 100644 --- a/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java +++ b/src/main/java/world/bentobox/bentobox/panels/SettingsPanel.java @@ -4,7 +4,6 @@ import java.util.Comparator; import org.bukkit.Material; import org.bukkit.World; -import org.bukkit.inventory.ItemStack; import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.Flag; diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/PVPListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/PVPListenerTest.java index f966792be..5b69b9a74 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/PVPListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/PVPListenerTest.java @@ -64,6 +64,7 @@ import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.flags.Flag; import world.bentobox.bentobox.api.panels.Panel; import world.bentobox.bentobox.api.panels.PanelItem; +import world.bentobox.bentobox.api.user.Notifier; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.lists.Flags; @@ -91,6 +92,7 @@ public class PVPListenerTest { private Zombie zombie; private Creeper creeper; private World world; + private Notifier notifier; /** * @throws java.lang.Exception @@ -100,6 +102,8 @@ public class PVPListenerTest { // Set up plugin BentoBox plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); + // Make sure you set the plung for the User class otherwise it'll use an old object + User.setPlugin(plugin); // Island World Manager iwm = mock(IslandWorldManager.class); when(iwm.inWorld(Mockito.any())).thenReturn(true); @@ -184,6 +188,10 @@ public class PVPListenerTest { when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); Map worldFlags = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(worldFlags); + + // Notifier + notifier = mock(Notifier.class); + when(plugin.getNotifier()).thenReturn(notifier); } /** @@ -199,7 +207,7 @@ public class PVPListenerTest { new PVPListener().onEntityDamage(e); assertFalse(e.isCancelled()); } - + /** * Test method for {@link world.bentobox.bentobox.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}. */ @@ -387,7 +395,6 @@ public class PVPListenerTest { */ @Test public void testOnEntityDamagePVPNotAllowed() { - // No visitor protection EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(player, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK, new EnumMap<>(ImmutableMap.of(DamageModifier.BASE, 0D)), @@ -395,7 +402,18 @@ public class PVPListenerTest { new PVPListener().onEntityDamage(e); // PVP should be banned assertTrue(e.isCancelled()); - Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.PVP_OVERWORLD.getHintReference())); + + } + + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}. + */ + @Test + public void testOnEntityDamagePVPNotAllowedInvVisitor() { + EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(player, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK, + new EnumMap<>(ImmutableMap.of(DamageModifier.BASE, 0D)), + new EnumMap>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0)))); // Enable visitor protection // This player is a visitor and any damage is not allowed @@ -404,7 +422,7 @@ public class PVPListenerTest { new PVPListener().onEntityDamage(e); // visitor should be protected assertTrue(e.isCancelled()); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); } /** @@ -429,7 +447,7 @@ public class PVPListenerTest { new PVPListener().onEntityDamage(e); // visitor should be protected assertTrue(e.isCancelled()); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); } @@ -447,7 +465,7 @@ public class PVPListenerTest { new PVPListener().onEntityDamage(e); // PVP should be banned assertTrue(e.isCancelled()); - Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.PVP_OVERWORLD.getHintReference())); // Visitor protection // This player is a visitor and any damage is not allowed @@ -457,10 +475,10 @@ public class PVPListenerTest { // visitor should be protected assertTrue(e.isCancelled()); // PVP trumps visitor protection - Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.PVP_OVERWORLD.getHintReference())); } - + /** * Test method for {@link world.bentobox.bentobox.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}. */ @@ -502,7 +520,7 @@ public class PVPListenerTest { new PVPListener().onEntityDamage(e); // visitor should be protected assertTrue(e.isCancelled()); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); } @@ -525,7 +543,7 @@ public class PVPListenerTest { // PVP should be banned assertTrue(pfe.isCancelled()); - Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.PVP_OVERWORLD.getHintReference())); // Hook should be removed Mockito.verify(hook).remove(); @@ -571,7 +589,7 @@ public class PVPListenerTest { new PVPListener().onFishing(pfe); // visitor should be protected assertTrue(pfe.isCancelled()); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); } /** @@ -607,7 +625,7 @@ public class PVPListenerTest { new PVPListener().onFishing(pfe); // visitor should be protected assertTrue(pfe.isCancelled()); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); } /** @@ -659,7 +677,7 @@ public class PVPListenerTest { PotionSplashEvent e = new PotionSplashEvent(tp, map); new PVPListener().onSplashPotionSplash(e); assertTrue(e.isCancelled()); - Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.PVP_OVERWORLD.getHintReference())); // Wrong world when(iwm.inWorld(Mockito.any())).thenReturn(false); @@ -667,7 +685,7 @@ public class PVPListenerTest { new PVPListener().onSplashPotionSplash(e); assertFalse(e.isCancelled()); } - + /** * Test method for {@link world.bentobox.bentobox.listeners.flags.PVPListener#onSplashPotionSplash(org.bukkit.event.entity.PotionSplashEvent)}. */ @@ -742,7 +760,7 @@ public class PVPListenerTest { new PVPListener().onSplashPotionSplash(e); // visitor should be protected assertTrue(e.isCancelled()); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); // Wrong world when(iwm.inWorld(Mockito.any())).thenReturn(false); @@ -813,14 +831,14 @@ public class PVPListenerTest { listener.onLingeringPotionDamage(ae); assertEquals(3, ae.getAffectedEntities().size()); assertFalse(ae.getAffectedEntities().contains(player2)); - Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.PVP_OVERWORLD.getHintReference())); // Wrong world when(iwm.inWorld(Mockito.any())).thenReturn(false); listener.onLingeringPotionSplash(e); // No change to results assertEquals(3, ae.getAffectedEntities().size()); assertFalse(ae.getAffectedEntities().contains(player2)); - Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.PVP_OVERWORLD.getHintReference())); } /** @@ -888,13 +906,13 @@ public class PVPListenerTest { listener.onLingeringPotionDamage(ae); assertEquals(3, ae.getAffectedEntities().size()); assertFalse(ae.getAffectedEntities().contains(player2)); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); // Wrong world when(iwm.inWorld(Mockito.any())).thenReturn(false); listener.onLingeringPotionSplash(e); assertEquals(3, ae.getAffectedEntities().size()); assertFalse(ae.getAffectedEntities().contains(player2)); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); } /** @@ -928,12 +946,12 @@ public class PVPListenerTest { listener.onLingeringPotionDamage(ae); assertEquals(3, ae.getAffectedEntities().size()); assertFalse(ae.getAffectedEntities().contains(player2)); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); // Wrong world when(iwm.inWorld(Mockito.any())).thenReturn(false); listener.onLingeringPotionSplash(e); assertEquals(3, ae.getAffectedEntities().size()); assertFalse(ae.getAffectedEntities().contains(player2)); - Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference()); + Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq(Flags.INVINCIBLE_VISITORS.getHintReference())); } } diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index bd20b4e5b..e6ee8da1c 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -78,6 +78,7 @@ public class IslandsManagerTest { private IslandWorldManager iwm; private IslandCache islandCache; private Optional optionalIsland; + private Island is; /** * @throws java.lang.Exception @@ -163,7 +164,7 @@ public class IslandsManagerTest { // Mock island cache islandCache = mock(IslandCache.class); - Island is = mock(Island.class); + is = mock(Island.class); when(islandCache.getIslandAt(Mockito.any(Location.class))).thenReturn(is); optionalIsland = Optional.ofNullable(is); @@ -704,26 +705,30 @@ public class IslandsManagerTest { } /** - * Test method for . + * Test method for user is on island * @throws Exception */ @Test public void testUserIsOnIsland() throws Exception { - // Mock island cache - Island is = mock(Island.class); - - when(islandCache.get(Mockito.any(), Mockito.any(UUID.class))).thenReturn(is); - IslandsManager im = new IslandsManager(plugin); im.setIslandCache(islandCache); + // Null user assertFalse(im.userIsOnIsland(world, null)); - when(is.onIsland(Mockito.any())).thenReturn(false); + + // User is on island is determined by whether the user's location is on + // an island that has them as a memebr (rank > 0) + when(is.onIsland(Mockito.any())).thenReturn(true); + Map members = new HashMap<>(); + when(is.getMembers()).thenReturn(members); assertFalse(im.userIsOnIsland(world, user)); - when(is.onIsland(Mockito.any())).thenReturn(true); + members.put(user.getUniqueId(), RanksManager.MEMBER_RANK); assertTrue(im.userIsOnIsland(world, user)); + + members.put(user.getUniqueId(), RanksManager.BANNED_RANK); + assertFalse(im.userIsOnIsland(world, user)); } /**