Removed code smells.

This commit is contained in:
tastybento 2018-06-23 18:38:48 -07:00
parent bb4c199487
commit 045855f6ad
5 changed files with 180 additions and 167 deletions

View File

@ -403,7 +403,7 @@ protection:
&aConfigure invincible visitor
&asettings.
name: "Invincible Visitors"
visitors-protected: "&cVisitors protected"
hint: "&cVisitors protected"
ISLAND_RESPAWN:
description: |
&aPlayers respawn
@ -478,8 +478,7 @@ protection:
&cEnable/Disable PVP
&con island.
name: "Overworld PVP"
pvp-not-allowed: "&cPVP is not allowed"
hint: "No PVP allowed in this world"
hint: "&cPVP is not allowed"
REDSTONE:
description: "Toggle use"
name: "Redstone items"

View File

@ -53,11 +53,17 @@ public class Clipboard {
NORTH,
UP
}
private static final String ATTACHED = "attached";
private static final String BLOCK = "blocks";
private static final String FACING = "facing";
private static final String POWERED = "powered";
private static final String LOAD_ERROR = "Could not load schems file - does not exist : ";
private YamlConfiguration blockConfig = new YamlConfiguration();
private Location pos1;
private Location pos2;
@ -185,7 +191,7 @@ public class Clipboard {
// Block state
if (s.getBoolean(ATTACHED) && m.toString().contains("TORCH")) {
TorchDir d = TorchDir.valueOf(s.getString("facing"));
TorchDir d = TorchDir.valueOf(s.getString(FACING));
Block rel = block.getRelative(BlockFace.DOWN);
Material rm = rel.getType();
@ -203,7 +209,7 @@ public class Clipboard {
block.setData((byte)d.ordinal());
}
return;
}
}
block.setType(m);
@ -217,21 +223,21 @@ public class Clipboard {
MaterialData md = bs.getData();
if (md instanceof Openable) {
Openable open = (Openable)md;
open.setOpen(s.getBoolean("open"));
open.setOpen(s.getBoolean("open"));
}
if (md instanceof Directional) {
Directional facing = (Directional)md;
facing.setFacingDirection(BlockFace.valueOf(s.getString("facing")));
facing.setFacingDirection(BlockFace.valueOf(s.getString(FACING)));
}
if (md instanceof Lever) {
Lever r = (Lever)md;
r.setPowered(s.getBoolean("powered"));
r.setPowered(s.getBoolean(POWERED));
}
if (md instanceof Button) {
Button r = (Button)md;
r.setPowered(s.getBoolean("powered"));
r.setPowered(s.getBoolean(POWERED));
}
// Block data
if (bs instanceof Sign) {
@ -249,7 +255,7 @@ public class Clipboard {
int i = 0;
ConfigurationSection pat = s.getConfigurationSection("pattern");
if (pat != null) {
for (String pattern : pat.getKeys(false)) {
for (String pattern : pat.getKeys(false)) {
banner.setPattern(i, new Pattern(DyeColor.valueOf(pat.getString(pattern))
, PatternType.valueOf(pattern)));
i++;
@ -293,15 +299,15 @@ public class Clipboard {
MaterialData md = bs.getData();
if (md instanceof Openable) {
Openable open = (Openable)md;
s.set("open", open.isOpen());
s.set("open", open.isOpen());
}
if (md instanceof Directional) {
Directional facing = (Directional)md;
s.set("facing", facing.getFacing().name());
s.set(FACING, facing.getFacing().name());
}
if (md instanceof Attachable) {
Attachable facing = (Attachable)md;
s.set("facing", facing.getFacing().name());
s.set(FACING, facing.getFacing().name());
s.set("attached-face", facing.getAttachedFace().name());
s.set(ATTACHED, true);
}
@ -311,11 +317,11 @@ public class Clipboard {
}
if (block.getType().equals(Material.CARPET)) {
DyeColor c = DyeColor.getByWoolData(block.getData());
s.set("color", c.name());
s.set("color", c.name());
}
if (md instanceof Redstone) {
Redstone r = (Redstone)md;
blockConfig.set("powered", r.isPowered());
blockConfig.set(POWERED, r.isPowered());
}
// Block data
@ -407,20 +413,20 @@ public class Clipboard {
* Load a file to clipboard
* @param filename - filename in schems folder
* @return
* @throws IOException
* @throws InvalidConfigurationException
* @throws IOException
* @throws InvalidConfigurationException
*/
public void load(String fileName) throws IOException, InvalidConfigurationException {
File zipFile = new File(schemFolder, fileName + ".schem");
File zipFile = new File(schemFolder, fileName + ".schem");
if (!zipFile.exists()) {
plugin.logError("Could not load schems file - does not exist : " + zipFile.getName());
throw new IOException("Could not load schems file - does not exist : " + zipFile.getName());
plugin.logError(LOAD_ERROR + zipFile.getName());
throw new IOException(LOAD_ERROR + zipFile.getName());
}
unzip(zipFile.getAbsolutePath());
File file = new File(schemFolder, fileName);
if (!file.exists()) {
plugin.logError("Could not load schems file - does not exist : " + file.getName());
throw new IOException("Could not load schems file - does not exist : " + file.getName());
plugin.logError(LOAD_ERROR + file.getName());
throw new IOException(LOAD_ERROR + file.getName());
}
blockConfig = new YamlConfiguration();
blockConfig.load(file);
@ -442,11 +448,11 @@ public class Clipboard {
} catch (IOException e1) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file: " + fileName + " " + e1.getMessage());
return false;
return false;
} catch (InvalidConfigurationException e1) {
user.sendMessage("commands.admin.schem.could-not-load");
plugin.logError("Could not load schems file - YAML error : " + fileName + " " + e1.getMessage());
return false;
return false;
}
user.sendMessage("general.success");
return true;
@ -459,7 +465,7 @@ public class Clipboard {
* @return - true if successful, false if error
*/
public boolean save(User user, String string) {
File file = new File(schemFolder, string);
File file = new File(schemFolder, string);
try {
getBlockConfig().save(file);
} catch (IOException e) {

View File

@ -1,5 +1,5 @@
/**
*
*
*/
package us.tastybento.bskyblock.listeners.flags;
@ -20,23 +20,24 @@ import us.tastybento.bskyblock.lists.Flags;
*
*/
public class EnterExitListener extends AbstractFlagListener {
private static final Vector XZ = new Vector(1,0,1);
private static final String NAME = "[name]";
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onMove(PlayerMoveEvent e) {
// Only process if Enter Exit flags are active, we are in the right world and there is a change in X or Z coords
if (!getIWM().inWorld(e.getFrom())
|| e.getFrom().toVector().multiply(XZ).equals(e.getTo().toVector().multiply(XZ))
|| e.getFrom().toVector().multiply(XZ).equals(e.getTo().toVector().multiply(XZ))
|| !Flags.ENTER_EXIT_MESSAGES.isSetForWorld(e.getFrom().getWorld())) {
return;
}
Optional<Island> from = this.getIslands().getProtectedIslandAt(e.getFrom());
Optional<Island> to = this.getIslands().getProtectedIslandAt(e.getTo());
/*
* Options:
*
*
* from = empty, to = island - entering
* from = island1, to = island2 - leaving 1, entering 2
* from = island, to = empty - leaving
@ -48,12 +49,12 @@ 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.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", "[name]", !i.getName().isEmpty() ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", "[name]", getPlugin().getPlayers().getName(i.getOwner()))));
to.filter(i -> i.getOwner() != null).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", "[name]", !i.getName().isEmpty() ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", "[name]", getPlugin().getPlayers().getName(i.getOwner()))));
from.filter(i -> i.getOwner() != null).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", NAME, !i.getName().isEmpty() ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", NAME, getPlugin().getPlayers().getName(i.getOwner()))));
to.filter(i -> i.getOwner() != null).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", NAME, !i.getName().isEmpty() ? i.getName() :
user.getTranslation("protection.flags.ENTER_EXIT_MESSAGES.island", NAME, getPlugin().getPlayers().getName(i.getOwner()))));
// Send message if island is unowned, but has a name
from.filter(i -> i.getOwner() == null && !i.getName().isEmpty()).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", "[name]", i.getName()));
to.filter(i -> i.getOwner() == null && !i.getName().isEmpty()).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", "[name]", i.getName()));
from.filter(i -> i.getOwner() == null && !i.getName().isEmpty()).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-leaving", NAME, i.getName()));
to.filter(i -> i.getOwner() == null && !i.getName().isEmpty()).ifPresent(i -> user.sendMessage("protection.flags.ENTER_EXIT_MESSAGES.now-entering", NAME, i.getName()));
}
}

View File

@ -45,9 +45,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("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
User.getInstance(e.getDamager()).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
} else if (e.getDamager() instanceof Projectile && ((Projectile)e.getDamager()).getShooter() instanceof Player) {
User.getInstance((Player)((Projectile)e.getDamager()).getShooter()).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
User.getInstance((Player)((Projectile)e.getDamager()).getShooter()).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
e.setCancelled(true);
} else {
@ -68,7 +68,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("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
user.sendMessage(Flags.PVP_OVERWORLD.getHintReference());
e.setCancelled(true);
}
} else if (damager instanceof Projectile) {
@ -79,7 +79,7 @@ public class PVPListener extends AbstractFlagListener {
if (!setUser(user).checkIsland((Event)e, damager.getLocation(), flag)) {
damager.setFireTicks(0);
damager.remove();
user.sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
user.sendMessage(Flags.PVP_OVERWORLD.getHintReference());
e.setCancelled(true);
}
}
@ -91,11 +91,11 @@ public class PVPListener extends AbstractFlagListener {
if (e.getCaught() instanceof Player && getPlugin().getIWM().inWorld(e.getCaught().getLocation())) {
// Protect visitors
if (protectedVisitor((Player)e.getCaught())) {
User.getInstance(e.getPlayer()).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
User.getInstance(e.getPlayer()).sendMessage(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("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
User.getInstance(e.getPlayer()).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
e.setCancelled(true);
}
}
@ -113,17 +113,17 @@ public class PVPListener extends AbstractFlagListener {
e.setCancelled(e.getAffectedEntities().stream().anyMatch(le -> blockPVP(user, le, e, getFlag(e.getEntity().getWorld()))));
}
}
private boolean blockPVP(User user, LivingEntity le, Event e, Flag flag) {
if (le instanceof Player) {
// Protect visitors
if (protectedVisitor(le)) {
user.sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
user.sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
return true;
}
// Check if PVP is allowed or not
if (!checkIsland(e, le.getLocation(), flag)) {
user.sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
user.sendMessage(Flags.PVP_OVERWORLD.getHintReference());
return true;
}
}
@ -155,8 +155,14 @@ public class PVPListener extends AbstractFlagListener {
}
private Flag getFlag(World w) {
return w.getEnvironment().equals(World.Environment.NORMAL) ? Flags.PVP_OVERWORLD
: w.getEnvironment().equals(World.Environment.NETHER) ? Flags.PVP_NETHER : Flags.PVP_END;
switch (w.getEnvironment()) {
case NETHER:
return Flags.PVP_NETHER;
case THE_END:
return Flags.PVP_END;
default:
return Flags.PVP_OVERWORLD;
}
}
}

View File

@ -1,5 +1,5 @@
/**
*
*
*/
package us.tastybento.bskyblock.listeners.flags;
@ -67,6 +67,7 @@ import us.tastybento.bskyblock.api.panels.Panel;
import us.tastybento.bskyblock.api.panels.PanelItem;
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.FlagsManager;
import us.tastybento.bskyblock.managers.IslandWorldManager;
import us.tastybento.bskyblock.managers.IslandsManager;
@ -94,7 +95,7 @@ public class PVPListenerTest {
private Zombie zombie;
private Creeper creeper;
private World world;
/**
* @throws java.lang.Exception
*/
@ -113,34 +114,34 @@ public class PVPListenerTest {
panel = mock(Panel.class);
when(panel.getInventory()).thenReturn(mock(Inventory.class));
// Sometimes use Mockito.withSettings().verboseLogging()
player = mock(Player.class);
UUID uuid = UUID.randomUUID();
when(player.getUniqueId()).thenReturn(uuid);
world = mock(World.class);
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(player.getWorld()).thenReturn(world);
loc = mock(Location.class);
when(loc.getWorld()).thenReturn(world);
when(player.getLocation()).thenReturn(loc);
when(player.getLocation()).thenReturn(loc);
User.getInstance(player);
// Sometimes use Mockito.withSettings().verboseLogging()
player2 = mock(Player.class);
UUID uuid2 = UUID.randomUUID();
when(player2.getUniqueId()).thenReturn(uuid2);
when(player2.getWorld()).thenReturn(world);
when(player2.getLocation()).thenReturn(loc);
when(player2.getLocation()).thenReturn(loc);
User.getInstance(player2);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
FlagsManager fm = mock(FlagsManager.class);
flag = mock(Flag.class);
when(flag.isSetForWorld(Mockito.any())).thenReturn(false);
@ -149,7 +150,7 @@ public class PVPListenerTest {
when(flag.toPanelItem(Mockito.any(), Mockito.any())).thenReturn(item);
when(fm.getFlagByID(Mockito.anyString())).thenReturn(flag);
when(plugin.getFlagsManager()).thenReturn(fm);
im = mock(IslandsManager.class);
// Default is that player in on their island
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(true);
@ -163,8 +164,8 @@ public class PVPListenerTest {
// Settings
s = mock(Settings.class);
when(plugin.getSettings()).thenReturn(s);
// Locales - this returns the string that was requested for translation
// Locales - this returns the string that was requested for translation
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), any())).thenAnswer(new Answer<String>() {
@ -173,7 +174,7 @@ public class PVPListenerTest {
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArgumentAt(1, String.class);
}});
// Create some entities
zombie = mock(Zombie.class);
when(zombie.getWorld()).thenReturn(world);
@ -186,7 +187,7 @@ public class PVPListenerTest {
BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
@ -201,14 +202,14 @@ public class PVPListenerTest {
public void testOnEntityDamageNotPlayer() {
Entity damager = mock(Zombie.class);
Entity damagee = mock(Creeper.class);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
}
/**
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@Test
@ -219,29 +220,29 @@ public class PVPListenerTest {
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(damager.getWorld()).thenReturn(world);
when(damagee.getWorld()).thenReturn(world);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
// Different attack type
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK,
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
assertFalse(e.isCancelled());
// Wrong world
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
when(iwm.inWorld(Mockito.any())).thenReturn(false);
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
assertFalse(e.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@ -253,27 +254,27 @@ public class PVPListenerTest {
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(damager.getWorld()).thenReturn(world);
when(damagee.getWorld()).thenReturn(world);
// Protect visitors
List<String> visitorProtectionList = new ArrayList<>();
visitorProtectionList.add("ENTITY_ATTACK");
when(iwm.getIvSettings(world)).thenReturn(visitorProtectionList);
// This player is on their island, i.e., not a visitor
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
// Wrong world
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
when(iwm.inWorld(Mockito.any())).thenReturn(false);
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
assertFalse(e.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@ -285,28 +286,28 @@ public class PVPListenerTest {
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(damager.getWorld()).thenReturn(world);
when(damagee.getWorld()).thenReturn(world);
// Protect visitors
List<String> visitorProtectionList = new ArrayList<>();
visitorProtectionList.add("ENTITY_ATTACK");
when(iwm.getIvSettings(world)).thenReturn(visitorProtectionList);
// This player is a visitor
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
assertTrue(e.isCancelled());
assertTrue(e.isCancelled());
// Wrong world
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
when(iwm.inWorld(Mockito.any())).thenReturn(false);
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
assertFalse(e.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@ -318,7 +319,7 @@ public class PVPListenerTest {
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(damager.getWorld()).thenReturn(world);
when(damagee.getWorld()).thenReturn(world);
// Protect visitors
List<String> visitorProtectionList = new ArrayList<>();
visitorProtectionList.add("ENTITY_ATTACK");
@ -326,17 +327,17 @@ public class PVPListenerTest {
// This player is a visitor
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
// Damage is not entity attack
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.THORNS,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.THORNS,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
assertFalse(e.isCancelled());
// Wrong world
when(iwm.inWorld(Mockito.any())).thenReturn(false);
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
assertFalse(e.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@ -348,50 +349,50 @@ public class PVPListenerTest {
when(world.getEnvironment()).thenReturn(World.Environment.NORMAL);
when(damager.getWorld()).thenReturn(world);
when(damagee.getWorld()).thenReturn(world);
// This player is a visitor
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
// Wrong world
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
e = new EntityDamageByEntityEvent(damager, damagee, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
when(iwm.inWorld(Mockito.any())).thenReturn(false);
new PVPListener().onEntityDamage(e);
assertFalse(e.isCancelled());
assertFalse(e.isCancelled());
}
// PVP TESTS
/*
* PVP Tests
*
*
* Variables:
* PVP on/off -> Direct hit / Projectile
* Visitor protection on/off -> protection type correct/incorrect
*
*
*/
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@Test
public void testOnEntityDamagePVPNotAllowed() {
// No visitor protection
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(player, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(player, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
// PVP should be banned
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
// Enable visitor protection
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
@ -399,9 +400,9 @@ public class PVPListenerTest {
new PVPListener().onEntityDamage(e);
// visitor should be protected
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@ -409,14 +410,14 @@ public class PVPListenerTest {
public void testOnEntityDamageOnPVPAllowed() {
// PVP is allowed
when(island.isAllowed(Mockito.any())).thenReturn(true);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(player, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(player, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
// PVP should be allowed
assertFalse(e.isCancelled());
Mockito.verify(player, Mockito.never()).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player, Mockito.never()).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
// Enable visitor protection
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
@ -424,10 +425,10 @@ public class PVPListenerTest {
new PVPListener().onEntityDamage(e);
// visitor should be protected
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@ -436,14 +437,14 @@ public class PVPListenerTest {
Projectile p = mock(Projectile.class);
when(p.getShooter()).thenReturn(player);
when(p.getLocation()).thenReturn(loc);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(p, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(p, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
// PVP should be banned
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
// Visitor protection
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
@ -452,10 +453,10 @@ public class PVPListenerTest {
// visitor should be protected
assertTrue(e.isCancelled());
// PVP trumps visitor protection
Mockito.verify(player).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onEntityDamage(org.bukkit.event.entity.EntityDamageByEntityEvent)}.
*/
@ -466,14 +467,14 @@ public class PVPListenerTest {
when(p.getLocation()).thenReturn(loc);
// PVP is allowed
when(island.isAllowed(Mockito.any())).thenReturn(true);
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(p, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(p, player2, EntityDamageEvent.DamageCause.ENTITY_ATTACK,
new EnumMap<DamageModifier, Double>(ImmutableMap.of(DamageModifier.BASE, 0D)),
new EnumMap<DamageModifier, Function<? super Double, Double>>(ImmutableMap.of(DamageModifier.BASE, Functions.constant(-0.0))));
new PVPListener().onEntityDamage(e);
// PVP should be allowed
assertFalse(e.isCancelled());
Mockito.verify(player, Mockito.never()).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player, Mockito.never()).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
// Enable visitor protection
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
@ -481,11 +482,11 @@ public class PVPListenerTest {
new PVPListener().onEntityDamage(e);
// visitor should be protected
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onFishing(org.bukkit.event.player.PlayerFishEvent)}.
*/
@ -498,39 +499,39 @@ public class PVPListenerTest {
PlayerFishEvent pfe = new PlayerFishEvent(player, caught, hook, null);
new PVPListener().onFishing(pfe);
assertFalse(pfe.isCancelled());
// Catch a player
pfe = new PlayerFishEvent(player, player2, hook, null);
new PVPListener().onFishing(pfe);
// PVP should be banned
assertTrue(pfe.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
// Hook should be removed
Mockito.verify(hook).remove();
// Wrong world
when(iwm.inWorld(Mockito.any())).thenReturn(false);
pfe = new PlayerFishEvent(player, player2, hook, null);
new PVPListener().onFishing(pfe);
assertFalse(pfe.isCancelled());
// Correct world
when(iwm.inWorld(Mockito.any())).thenReturn(true);
// Allow PVP
when(island.isAllowed(Mockito.any())).thenReturn(true);
pfe = new PlayerFishEvent(player, player2, hook, null);
new PVPListener().onFishing(pfe);
assertFalse(pfe.isCancelled());
// Wrong world
when(iwm.inWorld(Mockito.any())).thenReturn(false);
pfe = new PlayerFishEvent(player, player2, hook, null);
new PVPListener().onFishing(pfe);
assertFalse(pfe.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onFishing(org.bukkit.event.player.PlayerFishEvent)}.
*/
@ -540,10 +541,10 @@ public class PVPListenerTest {
Fish hook = mock(Fish.class);
// Catch a player
PlayerFishEvent pfe = new PlayerFishEvent(player, player2, hook, null);
// Allow PVP
when(island.isAllowed(Mockito.any())).thenReturn(true);
// Protect visitors
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
@ -551,9 +552,9 @@ public class PVPListenerTest {
new PVPListener().onFishing(pfe);
// visitor should be protected
assertTrue(pfe.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onFishing(org.bukkit.event.player.PlayerFishEvent)}.
*/
@ -563,10 +564,10 @@ public class PVPListenerTest {
Fish hook = mock(Fish.class);
// Catch a player
PlayerFishEvent pfe = new PlayerFishEvent(player, player2, hook, null);
// Disallow PVP
when(island.isAllowed(Mockito.any())).thenReturn(false);
// Protect visitors
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
@ -574,7 +575,7 @@ public class PVPListenerTest {
new PVPListener().onFishing(pfe);
// visitor should be protected
assertTrue(pfe.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
/**
@ -589,7 +590,7 @@ public class PVPListenerTest {
new PVPListener().onSplashPotionSplash(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onSplashPotionSplash(org.bukkit.event.entity.PotionSplashEvent)}.
*/
@ -606,7 +607,7 @@ public class PVPListenerTest {
new PVPListener().onSplashPotionSplash(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onSplashPotionSplash(org.bukkit.event.entity.PotionSplashEvent)}.
*/
@ -614,7 +615,7 @@ public class PVPListenerTest {
public void testOnSplashPotionSplash() {
// Disallow PVP
when(island.isAllowed(Mockito.any())).thenReturn(false);
ThrownPotion tp = mock(ThrownPotion.class);
when(tp.getShooter()).thenReturn(player);
when(tp.getWorld()).thenReturn(world);
@ -626,15 +627,15 @@ public class PVPListenerTest {
PotionSplashEvent e = new PotionSplashEvent(tp, map);
new PVPListener().onSplashPotionSplash(e);
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
// Wrong world
when(iwm.inWorld(Mockito.any())).thenReturn(false);
e = new PotionSplashEvent(tp, map);
new PVPListener().onSplashPotionSplash(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onSplashPotionSplash(org.bukkit.event.entity.PotionSplashEvent)}.
*/
@ -642,7 +643,7 @@ public class PVPListenerTest {
public void testOnSplashPotionSplashAllowPVP() {
// Disallow PVP
when(island.isAllowed(Mockito.any())).thenReturn(true);
ThrownPotion tp = mock(ThrownPotion.class);
when(tp.getShooter()).thenReturn(player);
when(tp.getWorld()).thenReturn(world);
@ -654,10 +655,10 @@ public class PVPListenerTest {
PotionSplashEvent e = new PotionSplashEvent(tp, map);
new PVPListener().onSplashPotionSplash(e);
assertFalse(e.isCancelled());
Mockito.verify(player, Mockito.never()).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player, Mockito.never()).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onSplashPotionSplash(org.bukkit.event.entity.PotionSplashEvent)}.
*/
@ -665,7 +666,7 @@ public class PVPListenerTest {
public void testOnSplashPotionSplashAllowPVPProtectVisitors() {
// Allow PVP
when(island.isAllowed(Mockito.any())).thenReturn(true);
ThrownPotion tp = mock(ThrownPotion.class);
when(tp.getShooter()).thenReturn(player);
when(tp.getWorld()).thenReturn(world);
@ -682,8 +683,8 @@ public class PVPListenerTest {
new PVPListener().onSplashPotionSplash(e);
// visitor should be protected
assertTrue(e.isCancelled());
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
// Wrong world
when(iwm.inWorld(Mockito.any())).thenReturn(false);
e = new PotionSplashEvent(tp, map);
@ -709,7 +710,7 @@ public class PVPListenerTest {
Mockito.verify(tp, Mockito.times(2)).getShooter();
PowerMockito.verifyStatic(Bukkit.class);
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onLingeringPotionSplash(org.bukkit.event.entity.LingeringPotionSplashEvent)}.
*/
@ -753,14 +754,14 @@ public class PVPListenerTest {
listener.onLingeringPotionDamage(ae);
assertEquals(3, ae.getAffectedEntities().size());
assertFalse(ae.getAffectedEntities().contains(player2));
Mockito.verify(player).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player).sendMessage(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("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
}
/**
@ -788,15 +789,15 @@ public class PVPListenerTest {
AreaEffectCloudApplyEvent ae = new AreaEffectCloudApplyEvent(cloud, list);
listener.onLingeringPotionDamage(ae);
assertEquals(4, ae.getAffectedEntities().size());
Mockito.verify(player, Mockito.never()).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player, Mockito.never()).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
// Wrong world
when(iwm.inWorld(Mockito.any())).thenReturn(false);
listener.onLingeringPotionSplash(e);
assertEquals(4, ae.getAffectedEntities().size());
Mockito.verify(player, Mockito.never()).sendMessage("protection.flags.PVP_OVERWORLD.pvp-not-allowed");
Mockito.verify(player, Mockito.never()).sendMessage(Flags.PVP_OVERWORLD.getHintReference());
}
/**
* Test method for {@link us.tastybento.bskyblock.listeners.flags.PVPListener#onLingeringPotionDamage(org.bukkit.event.entity.AreaEffectCloudApplyEvent)}.
*/
@ -822,19 +823,19 @@ public class PVPListenerTest {
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
when(iwm.getIvSettings(Mockito.any())).thenReturn(Arrays.asList("ENTITY_ATTACK"));
// See who it affects
AreaEffectCloudApplyEvent ae = new AreaEffectCloudApplyEvent(cloud, list);
listener.onLingeringPotionDamage(ae);
assertEquals(3, ae.getAffectedEntities().size());
assertFalse(ae.getAffectedEntities().contains(player2));
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(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("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
/**
@ -862,18 +863,18 @@ public class PVPListenerTest {
// This player is a visitor and any damage is not allowed
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
when(iwm.getIvSettings(Mockito.any())).thenReturn(Arrays.asList("ENTITY_ATTACK"));
// See who it affects
AreaEffectCloudApplyEvent ae = new AreaEffectCloudApplyEvent(cloud, list);
listener.onLingeringPotionDamage(ae);
assertEquals(3, ae.getAffectedEntities().size());
assertFalse(ae.getAffectedEntities().contains(player2));
Mockito.verify(player).sendMessage("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(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("protection.flags.INVINCIBLE_VISITORS.visitors-protected");
Mockito.verify(player).sendMessage(Flags.INVINCIBLE_VISITORS.getHintReference());
}
}