Added global TNT on/off flag

Protects against visitors priming TNT too.
Test class is just "happy path".

Fixed bugs with other flags around fire extinguish.
This commit is contained in:
tastybento 2018-07-16 19:00:12 -07:00
parent 1f682092db
commit 5572875257
9 changed files with 478 additions and 260 deletions

View File

@ -439,7 +439,6 @@ public abstract class CompositeCommand extends Command implements PluginIdentifi
@Override
public List<String> tabComplete(final CommandSender sender, final String alias, final String[] args) {
Arrays.stream(args).forEach(plugin::log);// DEBUG
List<String> options = new ArrayList<>();
// Get command object based on args entered so far
CompositeCommand cmd = getCommandFromArgs(args);

View File

@ -3,8 +3,6 @@ package us.tastybento.bskyblock.listeners.flags;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -12,13 +10,11 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.util.BlockIterator;
import us.tastybento.bskyblock.api.flags.AbstractFlagListener;
import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.lists.Flags;
/**
@ -88,10 +84,6 @@ public class FireListener extends AbstractFlagListener {
BlockIterator iter = new BlockIterator(e.getPlayer(), 10);
while (iter.hasNext()) {
Block lastBlock = iter.next();
lastBlock = iter.next();
if (lastBlock.equals(e.getClickedBlock())) {
break;
}
if (lastBlock.getType().equals(Material.FIRE)) {
checkIsland(e, lastBlock.getLocation(), Flags.FIRE_EXTINGUISH);
}
@ -101,44 +93,4 @@ public class FireListener extends AbstractFlagListener {
}
}
/**
* Protect TNT.
* Note that allowing TNT to explode is governed by the Break Blocks flag.
* @param e - event
* @return true if cancelled
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onTNTPrimed(EntityChangeBlockEvent e) {
return e.getBlock().getType().equals(Material.TNT) && checkFire(e, e.getBlock().getLocation(), Flags.FIRE);
}
/**
* Protect TNT from being set light by a fire arrow
* @param e - event
* @return true if cancelled
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public boolean onTNTDamage(EntityChangeBlockEvent e) {
// Check world
if (!e.getBlock().getType().equals(Material.TNT) || !getIWM().inWorld(e.getBlock().getLocation())) {
return false;
}
// Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow
if (e.getEntity() instanceof Projectile) {
Projectile projectile = (Projectile) e.getEntity();
// Find out who fired it
if (projectile.getShooter() instanceof Player && projectile.getFireTicks() > 0) {
Player shooter = (Player)projectile.getShooter();
setUser(User.getInstance(shooter));
if (!setUser(User.getInstance(shooter)).checkIsland(e, e.getBlock().getLocation(), Flags.BREAK_BLOCKS)) {
// Remove the arrow
projectile.remove();
e.setCancelled(true);
return true;
}
}
}
return false;
}
}

View File

@ -30,7 +30,7 @@ public class ItemDropPickUpListener extends AbstractFlagListener {
public void onVisitorPickup(EntityPickupItemEvent e) {
if (e.getEntity() instanceof Player) {
// Disallow, but don't tell the player an error
setUser(User.getInstance(e.getEntity())).checkIsland(e, e.getItem().getLocation(), Flags.ITEM_PICKUP, true);
setUser(User.getInstance(e.getEntity())).checkIsland(e, e.getItem().getLocation(), Flags.ITEM_PICKUP, false);
}
}
}

View File

@ -27,6 +27,10 @@ public class PlaceBlocksListener extends AbstractFlagListener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onBlockPlace(final BlockPlaceEvent e) {
getPlugin().logDebug("Place blocks " + e.getBlock().getType());
if (e.getBlock().getType().equals(Material.FIRE)) {
return;
}
checkIsland(e, e.getBlock().getLocation(), Flags.PLACE_BLOCKS);
}

View File

@ -0,0 +1,87 @@
/**
*
*/
package us.tastybento.bskyblock.listeners.flags;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import us.tastybento.bskyblock.api.flags.AbstractFlagListener;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.lists.Flags;
/**
* Protects islands from visitors blowing things up
* @author tastybento
*
*/
public class TNTListener extends AbstractFlagListener {
/**
* Protect TNT from being set light by a fire arrow
* @param e - event
* @return true if cancelled
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTNTDamage(EntityChangeBlockEvent e) {
// Check world
if (!e.getBlock().getType().equals(Material.TNT) || !getIWM().inWorld(e.getBlock().getLocation())) {
return;
}
// Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow
if (e.getEntity() instanceof Projectile) {
Projectile projectile = (Projectile) e.getEntity();
// Find out who fired it
if (projectile.getShooter() instanceof Player && projectile.getFireTicks() > 0) {
Player shooter = (Player)projectile.getShooter();
setUser(User.getInstance(shooter));
if (!setUser(User.getInstance(shooter)).checkIsland(e, e.getBlock().getLocation(), Flags.BREAK_BLOCKS)) {
// Remove the arrow
projectile.remove();
e.setCancelled(true);
return;
}
}
}
return;
}
/**
* Protect against priming of TNT unless break blocks is allowed
* @param e
*/
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onTNTPriming(PlayerInteractEvent e) {
if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)
&& e.getClickedBlock().getType().equals(Material.TNT)
&& e.getMaterial() != null
&& e.getMaterial().equals(Material.FLINT_AND_STEEL)) {
checkIsland(e, e.getClickedBlock().getLocation(), Flags.BREAK_BLOCKS);
}
}
/**
* Prevent TNT damage from explosion
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onExplosion(final EntityExplodeEvent e) {
if (e.getEntity() != null && e.getEntityType().equals(EntityType.PRIMED_TNT)) {
// Remove any blocks from the explosion list if they are inside a protected area
if (e.blockList().removeIf(b -> getIslands().getProtectedIslandAt(b.getLocation()).map(i -> !i.isAllowed(Flags.TNT)).orElse(false))) {
// If any were removed, then prevent damage too
e.setCancelled(true);
}
}
}
}

View File

@ -36,6 +36,7 @@ import us.tastybento.bskyblock.listeners.flags.PlaceBlocksListener;
import us.tastybento.bskyblock.listeners.flags.PortalListener;
import us.tastybento.bskyblock.listeners.flags.RemoveMobsListener;
import us.tastybento.bskyblock.listeners.flags.ShearingListener;
import us.tastybento.bskyblock.listeners.flags.TNTListener;
import us.tastybento.bskyblock.listeners.flags.TeleportationListener;
import us.tastybento.bskyblock.managers.RanksManager;
@ -188,6 +189,15 @@ public class Flags {
public static final Flag CLEAN_SUPER_FLAT = new FlagBuilder().id("CLEAN_SUPER_FLAT").icon(Material.BEDROCK).type(Type.WORLD_SETTING)
.listener(new CleanSuperFlatListener()).allowedByDefault(false).build();
public static final Flag CHEST_DAMAGE = new FlagBuilder().id("CHEST_DAMAGE").icon(Material.TRAPPED_CHEST).type(Type.WORLD_SETTING)
.allowedByDefault(false).build();
public static final Flag CREEPER_DAMAGE = new FlagBuilder().id("CREEPER_DAMAGE").icon(Material.GREEN_SHULKER_BOX).type(Type.WORLD_SETTING)
.allowedByDefault(true).build();
public static final Flag CREEPER_GRIEFING = new FlagBuilder().id("CREEPER_GRIEFING").icon(Material.FIREWORK).type(Type.WORLD_SETTING)
.allowedByDefault(false).build();
public static final Flag TNT = new FlagBuilder().id("TNT").icon(Material.TNT).listener(new TNTListener()).allowedByDefault(false).type(Type.WORLD_SETTING).build();
/**
* @return List of all the flags in this class
*/

View File

@ -18,16 +18,12 @@ import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.WitherSkeleton;
import org.bukkit.entity.Zombie;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockIgniteEvent;
import org.bukkit.event.block.BlockSpreadEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.PluginManager;
@ -45,7 +41,6 @@ import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.configuration.WorldSettings;
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;
@ -106,7 +101,7 @@ public class FireListenerTest {
when(iwm.getBSBEndWorld()).thenReturn(world);
when(iwm.inWorld(any())).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Monsters and animals
Zombie zombie = mock(Zombie.class);
when(zombie.getLocation()).thenReturn(location);
@ -124,26 +119,26 @@ public class FireListenerTest {
//User user = mock(User.class);
///user.setPlugin(plugin);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), any())).thenReturn("mock translation");
// Player name
PlayersManager pm = mock(PlayersManager.class);
when(pm.getName(Mockito.any())).thenReturn("tastybento");
when(plugin.getPlayers()).thenReturn(pm);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
}
@Before
public void setUp() {
PowerMockito.mockStatic(Util.class);
@ -157,7 +152,7 @@ public class FireListenerTest {
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.of(island));
// Block on fire
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
@ -166,7 +161,7 @@ public class FireListenerTest {
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
// Disallow fire
when(island.isAllowed(Mockito.any())).thenReturn(false);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
@ -174,7 +169,7 @@ public class FireListenerTest {
assertTrue(listener.checkFire(e, location, Flags.FIRE));
Flags.FIRE.setDefaultSetting(true);
assertTrue(listener.checkFire(e, location, Flags.FIRE));
// Allow fire
when(island.isAllowed(Mockito.any())).thenReturn(true);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
@ -182,7 +177,7 @@ public class FireListenerTest {
assertFalse(listener.checkFire(e, location, Flags.FIRE));
Flags.FIRE.setDefaultSetting(true);
assertFalse(listener.checkFire(e, location, Flags.FIRE));
// Check with no island
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty());
// Fire is not allowed, so should be cancelled
@ -200,7 +195,7 @@ public class FireListenerTest {
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.of(island));
// Block on fire
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
@ -209,7 +204,7 @@ public class FireListenerTest {
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
// Disallow fire
when(island.isAllowed(Mockito.any())).thenReturn(false);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
@ -217,7 +212,7 @@ public class FireListenerTest {
assertTrue(listener.onBlockBurn(e));
Flags.FIRE.setDefaultSetting(true);
assertTrue(listener.onBlockBurn(e));
// Allow fire
when(island.isAllowed(Mockito.any())).thenReturn(true);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
@ -225,7 +220,7 @@ public class FireListenerTest {
assertFalse(listener.onBlockBurn(e));
Flags.FIRE.setDefaultSetting(true);
assertFalse(listener.onBlockBurn(e));
// Check with no island
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty());
// Fire is not allowed, so should be cancelled
@ -243,21 +238,21 @@ public class FireListenerTest {
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.of(island));
// Block on fire spread
Block block = mock(Block.class);
Block fire = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(fire.getLocation()).thenReturn(location);
when(fire.getType()).thenReturn(Material.FIRE);
BlockSpreadEvent e = new BlockSpreadEvent(block, fire, null);
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
// Disallow fire
when(island.isAllowed(Mockito.any())).thenReturn(false);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
@ -265,7 +260,7 @@ public class FireListenerTest {
assertTrue(listener.onBlockSpread(e));
Flags.FIRE_SPREAD.setDefaultSetting(true);
assertTrue(listener.onBlockSpread(e));
// Allow fire spread
when(island.isAllowed(Mockito.any())).thenReturn(true);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
@ -273,7 +268,7 @@ public class FireListenerTest {
assertFalse(listener.onBlockSpread(e));
Flags.FIRE_SPREAD.setDefaultSetting(true);
assertFalse(listener.onBlockSpread(e));
// Check with no island
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty());
// Fire spread is not allowed, so should be cancelled
@ -291,7 +286,7 @@ public class FireListenerTest {
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.of(island));
// Block on fire spread
Block block = mock(Block.class);
@ -303,13 +298,13 @@ public class FireListenerTest {
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
// Obsidian is okay to ignite
assertFalse(listener.onBlockIgnite(e));
// Now set to something flammable
when(block.getType()).thenReturn(Material.WOOD);
// Disallow fire
when(island.isAllowed(Mockito.any())).thenReturn(false);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
@ -317,7 +312,7 @@ public class FireListenerTest {
assertTrue(listener.onBlockIgnite(e));
Flags.FIRE.setDefaultSetting(true);
assertTrue(listener.onBlockIgnite(e));
// Allow fire spread
when(island.isAllowed(Mockito.any())).thenReturn(true);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
@ -325,7 +320,7 @@ public class FireListenerTest {
assertFalse(listener.onBlockIgnite(e));
Flags.FIRE.setDefaultSetting(true);
assertFalse(listener.onBlockIgnite(e));
// Check with no island
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty());
// Fire spread is not allowed, so should be cancelled
@ -336,162 +331,4 @@ public class FireListenerTest {
assertFalse(listener.onBlockIgnite(e));
}
/*
@Test
public void testOnPlayerInteract() {
fail("Not yet implemented"); // TODO
}
*/
@Test
public void testOnTNTPrimed() {
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.of(island));
// Block on fire spread
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(block.getType()).thenReturn(Material.OBSIDIAN);
EntityChangeBlockEvent e = mock(EntityChangeBlockEvent.class);
when(e.getBlock()).thenReturn(block);
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
// Obsidian is not TNT
assertFalse(listener.onTNTPrimed(e));
// Out of world
when(iwm.inWorld(any())).thenReturn(false);
assertFalse(listener.onTNTPrimed(e));
// Now set to TNT
when(block.getType()).thenReturn(Material.TNT);
assertFalse(listener.onTNTPrimed(e));
// Back in world
when(iwm.inWorld(any())).thenReturn(true);
// Disallow fire
when(island.isAllowed(Mockito.any())).thenReturn(false);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
Flags.FIRE.setDefaultSetting(false);
assertTrue(listener.onTNTPrimed(e));
Flags.FIRE.setDefaultSetting(true);
assertTrue(listener.onTNTPrimed(e));
// Allow fire spread
when(island.isAllowed(Mockito.any())).thenReturn(true);
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
Flags.FIRE.setDefaultSetting(false);
assertFalse(listener.onTNTPrimed(e));
Flags.FIRE.setDefaultSetting(true);
assertFalse(listener.onTNTPrimed(e));
// Check with no island
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.empty());
// Fire spread is not allowed, so should be cancelled
Flags.FIRE.setDefaultSetting(false);
assertTrue(listener.onTNTPrimed(e));
// Fire allowed
Flags.FIRE.setDefaultSetting(true);
assertFalse(listener.onTNTPrimed(e));
}
@Test
public void testOnTNTDamage() {
// Notifier
Notifier notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.of(island));
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(Optional.of(island));
// Block on fire
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(block.getType()).thenReturn(Material.OBSIDIAN);
EntityChangeBlockEvent e = mock(EntityChangeBlockEvent.class);
when(e.getBlock()).thenReturn(block);
// Fire listener - remember to set the plugin for testing!
FireListener listener = new FireListener();
listener.setPlugin(plugin);
// Obsidian is not TNT
assertFalse(listener.onTNTDamage(e));
// Out of world
when(block.getLocation()).thenReturn(null);
assertFalse(listener.onTNTDamage(e));
// Now set to TNT
when(block.getType()).thenReturn(Material.TNT);
assertFalse(listener.onTNTDamage(e));
// Back in world
when(block.getLocation()).thenReturn(location);
// Entity is not a projectile
Player player = mock(Player.class);
when(e.getEntity()).thenReturn(player);
assertFalse(listener.onTNTDamage(e));
// Entity is an arrow
Arrow arrow = mock(Arrow.class);
// Shooter is a skeleton
WitherSkeleton skeleton = mock(WitherSkeleton.class);
when(arrow.getShooter()).thenReturn(skeleton);
// No fire arrow
when(arrow.getFireTicks()).thenReturn(0);
when(e.getEntity()).thenReturn(arrow);
assertFalse(listener.onTNTDamage(e));
// Fire arrow
when(arrow.getFireTicks()).thenReturn(10);
assertFalse(listener.onTNTDamage(e));
// Shooter is a player
when(arrow.getShooter()).thenReturn(player);
// No fire arrow
when(arrow.getFireTicks()).thenReturn(0);
when(e.getEntity()).thenReturn(arrow);
assertFalse(listener.onTNTDamage(e));
// Fire arrow
when(arrow.getFireTicks()).thenReturn(10);
// Break blocks not allowed, general flag should have no effect
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
Flags.BREAK_BLOCKS.setDefaultSetting(false);
assertTrue(listener.onTNTDamage(e));
Flags.BREAK_BLOCKS.setDefaultSetting(true);
assertTrue(listener.onTNTDamage(e));
// Allow BREAK_BLOCKS spread
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
Flags.BREAK_BLOCKS.setDefaultSetting(false);
assertFalse(listener.onTNTDamage(e));
Flags.BREAK_BLOCKS.setDefaultSetting(true);
assertFalse(listener.onTNTDamage(e));
// Check with no island
when(im.getProtectedIslandAt(Matchers.any())).thenReturn(Optional.empty());
// BREAK_BLOCKS spread is not allowed, so should be cancelled
Flags.BREAK_BLOCKS.setDefaultSetting(false);
assertTrue(listener.onTNTDamage(e));
// BREAK_BLOCKS allowed
Flags.BREAK_BLOCKS.setDefaultSetting(true);
assertFalse(listener.onTNTDamage(e));
}
}

View File

@ -0,0 +1,317 @@
package us.tastybento.bskyblock.listeners.flags;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.WitherSkeleton;
import org.bukkit.entity.Zombie;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
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;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.configuration.WorldSettings;
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.FlagsManager;
import us.tastybento.bskyblock.managers.IslandWorldManager;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
import us.tastybento.bskyblock.util.Util;
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BSkyBlock.class, Flags.class, Util.class} )
public class TNTListenerTest {
private static Location location;
private static BSkyBlock plugin;
private static IslandWorldManager iwm;
private static IslandsManager im;
private static Notifier notifier;
@BeforeClass
public static void setUpClass() {
// 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());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
PluginManager pluginManager = mock(PluginManager.class);
when(server.getPluginManager()).thenReturn(pluginManager);
ItemFactory itemFactory = mock(ItemFactory.class);
when(server.getItemFactory()).thenReturn(itemFactory);
Bukkit.setServer(server);
SkullMeta skullMeta = mock(SkullMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(skullMeta);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger());
location = mock(Location.class);
when(location.getWorld()).thenReturn(world);
when(location.getBlockX()).thenReturn(0);
when(location.getBlockY()).thenReturn(0);
when(location.getBlockZ()).thenReturn(0);
PowerMockito.mockStatic(Flags.class);
FlagsManager flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
iwm = mock(IslandWorldManager.class);
when(iwm.getBSBIslandWorld()).thenReturn(world);
when(iwm.getBSBNetherWorld()).thenReturn(world);
when(iwm.getBSBEndWorld()).thenReturn(world);
when(iwm.inWorld(any())).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm);
// Monsters and animals
Zombie zombie = mock(Zombie.class);
when(zombie.getLocation()).thenReturn(location);
Slime slime = mock(Slime.class);
when(slime.getLocation()).thenReturn(location);
Cow cow = mock(Cow.class);
when(cow.getLocation()).thenReturn(location);
// Fake players
Settings settings = mock(Settings.class);
Mockito.when(plugin.getSettings()).thenReturn(settings);
Mockito.when(settings.getFakePlayers()).thenReturn(new HashSet<String>());
// Users
//User user = mock(User.class);
///user.setPlugin(plugin);
User.setPlugin(plugin);
// Locales - final
LocalesManager lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
Answer<String> answer = new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return (String)Arrays.asList(invocation.getArguments()).get(1);
}
};
when(lm.get(any(), any())).thenAnswer(answer);
// Player name
PlayersManager pm = mock(PlayersManager.class);
when(pm.getName(Mockito.any())).thenReturn("tastybento");
when(plugin.getPlayers()).thenReturn(pm);
// World Settings
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> worldFlags = new HashMap<>();
when(ws.getWorldFlags()).thenReturn(worldFlags);
// Island manager
im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
Optional<Island> optional = Optional.of(island);
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(optional);
// Notifier
notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
}
@Before
public void setUp() {
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
}
@Test
public void testOnTNTPriming() {
BlockFace clickedFace = BlockFace.DOWN;
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.TNT);
when(clickedBlock.getLocation()).thenReturn(location);
ItemStack item = new ItemStack(Material.FLINT_AND_STEEL);
Action action = Action.RIGHT_CLICK_BLOCK;
Player player = mock(Player.class);
PlayerInteractEvent e = new PlayerInteractEvent(player , action, item, clickedBlock, clickedFace);
TNTListener listener = new TNTListener();
listener.setPlugin(plugin);
listener.onTNTPriming(e);
assertTrue(e.isCancelled());
Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq("protection.protected"));
}
@Test
public void testOnExplosion() {
Entity entity = mock(Entity.class);
when(entity.getType()).thenReturn(EntityType.PRIMED_TNT);
List<Block> list = new ArrayList<>();
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
list.add(block);
EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0);
TNTListener listener = new TNTListener();
listener.setPlugin(plugin);
listener.onExplosion(e);
assertTrue(e.isCancelled());
assertTrue(list.isEmpty());
}
@Test
public void testOnTNTDamage() {
// Notifier
Notifier notifier = mock(Notifier.class);
when(plugin.getNotifier()).thenReturn(notifier);
// Island
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);
when(im.getIslandAt(Matchers.any())).thenReturn(Optional.of(island));
when(im.getProtectedIslandAt(Mockito.any())).thenReturn(Optional.of(island));
// Block on fire
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
when(block.getType()).thenReturn(Material.OBSIDIAN);
EntityChangeBlockEvent e = mock(EntityChangeBlockEvent.class);
when(e.getBlock()).thenReturn(block);
// TNT listener
TNTListener listener = new TNTListener();
listener.setPlugin(plugin);
// Obsidian is not TNT
listener.onTNTDamage(e);
assertFalse(e.isCancelled());
// Out of world
when(block.getLocation()).thenReturn(null);
listener.onTNTDamage(e);
assertFalse(e.isCancelled());
// Now set to TNT
when(block.getType()).thenReturn(Material.TNT);
listener.onTNTDamage(e);
assertFalse(e.isCancelled());
// Back in world
when(block.getLocation()).thenReturn(location);
// Entity is not a projectile
Player player = mock(Player.class);
when(e.getEntity()).thenReturn(player);
listener.onTNTDamage(e);
assertFalse(e.isCancelled());
// Entity is an arrow
Arrow arrow = mock(Arrow.class);
// Shooter is a skeleton
WitherSkeleton skeleton = mock(WitherSkeleton.class);
when(arrow.getShooter()).thenReturn(skeleton);
// No fire arrow
when(arrow.getFireTicks()).thenReturn(0);
when(e.getEntity()).thenReturn(arrow);
listener.onTNTDamage(e);
assertFalse(e.isCancelled());
// Fire arrow
when(arrow.getFireTicks()).thenReturn(10);
listener.onTNTDamage(e);
assertFalse(e.isCancelled());
// Shooter is a player
when(arrow.getShooter()).thenReturn(player);
// No fire arrow
when(arrow.getFireTicks()).thenReturn(0);
when(e.getEntity()).thenReturn(arrow);
listener.onTNTDamage(e);
assertFalse(e.isCancelled());
// Fire arrow
when(arrow.getFireTicks()).thenReturn(10);
/*
// Break blocks not allowed, general flag should have no effect
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(false);
Flags.BREAK_BLOCKS.setDefaultSetting(false);
assertTrue(listener.onTNTDamage(e));
Flags.BREAK_BLOCKS.setDefaultSetting(true);
assertTrue(listener.onTNTDamage(e));
// Allow BREAK_BLOCKS spread
when(island.isAllowed(Mockito.any(), Mockito.any())).thenReturn(true);
Flags.BREAK_BLOCKS.setDefaultSetting(false);
assertFalse(listener.onTNTDamage(e));
Flags.BREAK_BLOCKS.setDefaultSetting(true);
assertFalse(listener.onTNTDamage(e));
// Check with no island
when(im.getProtectedIslandAt(Matchers.any())).thenReturn(Optional.empty());
// BREAK_BLOCKS spread is not allowed, so should be cancelled
Flags.BREAK_BLOCKS.setDefaultSetting(false);
assertTrue(listener.onTNTDamage(e));
// BREAK_BLOCKS allowed
Flags.BREAK_BLOCKS.setDefaultSetting(true);
assertFalse(listener.onTNTDamage(e));
*/
}
}

View File

@ -56,7 +56,7 @@ public class IslandCacheTest {
when(iwm.getBSBNetherWorld()).thenReturn(world);
when(iwm.getBSBEndWorld()).thenReturn(world);
when(iwm.inWorld(any())).thenReturn(true);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(world);
@ -78,7 +78,7 @@ public class IslandCacheTest {
Builder<UUID> members = new ImmutableSet.Builder<>();
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
when(island.getMemberSet()).thenReturn(members.build());
when(island.getMinX()).thenReturn(-200);
when(island.getMinZ()).thenReturn(-200);
@ -95,7 +95,7 @@ public class IslandCacheTest {
IslandCache ic = new IslandCache();
assertTrue(ic.addIsland(island));
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(location));
}
@ -112,27 +112,27 @@ public class IslandCacheTest {
@Test
public void testClear() {
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(location));
ic.clear();
assertNull(ic.get(world, owner));
assertNull(ic.get(world, owner));
assertNull(ic.get(location));
}
@Test
public void testDeleteIslandFromCache() {
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(location));
boolean result = ic.deleteIslandFromCache(island);
assertTrue(result);
assertNull(ic.get(world, owner));
assertNull(ic.get(world, owner));
assertNull(ic.get(location));
// Test removing an island that is not in the cache
@ -148,7 +148,7 @@ public class IslandCacheTest {
Builder<UUID> members = new ImmutableSet.Builder<>();
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
members.add(UUID.randomUUID());
when(island2.getMemberSet()).thenReturn(members.build());
when(island2.getMinX()).thenReturn(-400);
when(island2.getMinZ()).thenReturn(-400);
@ -159,7 +159,7 @@ public class IslandCacheTest {
@Test
public void testGetLocation() {
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(location));
@ -168,20 +168,20 @@ public class IslandCacheTest {
@Test
public void testGetUUID() {
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check if they are added
assertEquals(island, ic.get(world, owner));
assertEquals(island, ic.get(world, owner));
}
@Test
public void testGetIslandAtLocation() {
public void testGetIslandAtLocation() {
// Set coords to be in island space
when(island.inIslandSpace(Mockito.any(Integer.class), Mockito.any(Integer.class))).thenReturn(true);
// Set plugin
Util.setPlugin(plugin);
// New cache
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check exact match for location
@ -209,7 +209,7 @@ public class IslandCacheTest {
@Test
public void testgetMembers() {
// New cache
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertTrue(ic.getMembers(world, null).isEmpty());
@ -221,7 +221,7 @@ public class IslandCacheTest {
@Test
public void testGetTeamLeader() {
// New cache
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertEquals(owner, ic.getTeamLeader(world, owner));
@ -234,7 +234,7 @@ public class IslandCacheTest {
@Test
public void testHasIsland() {
// New cache
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertTrue(ic.hasIsland(world, owner));
@ -245,7 +245,7 @@ public class IslandCacheTest {
@Test
public void testRemovePlayer() {
// New cache
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertTrue(ic.hasIsland(world, owner));
@ -260,9 +260,21 @@ public class IslandCacheTest {
@Test
public void testSize() {
// New cache
IslandCache ic = new IslandCache();
IslandCache ic = new IslandCache();
ic.addIsland(island);
assertEquals(1, ic.size());
}
@Test
public void testSetOwner() {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
UUID newOwnerUUID = UUID.randomUUID();
ic.setOwner(island, newOwnerUUID);
Mockito.verify(island).setOwner(newOwnerUUID);
assertEquals(island, ic.get(world, newOwnerUUID));
assertEquals(island, ic.get(island.getCenter()));
}
}