Added protection against turtle egg crushing.

Added test class for physical interactions.

EWR -> DEN
This commit is contained in:
tastybento 2018-11-30 18:49:58 -05:00
parent c88ea31237
commit 46bb958a6d
4 changed files with 424 additions and 24 deletions

View File

@ -33,24 +33,34 @@ public class PhysicalInteractionListener extends FlagListener {
// Crop trample
checkIsland(e, e.getPlayer().getLocation(), Flags.CROP_TRAMPLE);
break;
case ACACIA_PRESSURE_PLATE:
case BIRCH_PRESSURE_PLATE:
case DARK_OAK_PRESSURE_PLATE:
case HEAVY_WEIGHTED_PRESSURE_PLATE:
case JUNGLE_PRESSURE_PLATE:
case LIGHT_WEIGHTED_PRESSURE_PLATE:
case OAK_PRESSURE_PLATE:
case SPRUCE_PRESSURE_PLATE:
case STONE_PRESSURE_PLATE:
case ACACIA_PRESSURE_PLATE:
case BIRCH_PRESSURE_PLATE:
case DARK_OAK_PRESSURE_PLATE:
case HEAVY_WEIGHTED_PRESSURE_PLATE:
case JUNGLE_PRESSURE_PLATE:
case LIGHT_WEIGHTED_PRESSURE_PLATE:
case OAK_PRESSURE_PLATE:
case SPRUCE_PRESSURE_PLATE:
case STONE_PRESSURE_PLATE:
// Pressure plates
checkIsland(e, e.getPlayer().getLocation(), Flags.PRESSURE_PLATE);
break;
case TURTLE_EGG:
checkIsland(e, e.getPlayer().getLocation(), Flags.TURTLE_EGGS);
break;
default:
break;
}
}
/**
* Protects buttons and plates from being activated by projectiles
* @param e - event
*/
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onProjectileHit(EntityInteractEvent e) {
if (!(e.getEntity() instanceof Projectile)) {
@ -62,24 +72,24 @@ public class PhysicalInteractionListener extends FlagListener {
setUser(User.getInstance((Player)p.getShooter()));
switch(e.getBlock().getType()) {
case ACACIA_BUTTON:
case BIRCH_BUTTON:
case JUNGLE_BUTTON:
case OAK_BUTTON:
case SPRUCE_BUTTON:
case ACACIA_BUTTON:
case BIRCH_BUTTON:
case JUNGLE_BUTTON:
case OAK_BUTTON:
case SPRUCE_BUTTON:
case STONE_BUTTON:
case DARK_OAK_BUTTON:
case DARK_OAK_BUTTON:
checkIsland(e, e.getBlock().getLocation(), Flags.BUTTON);
break;
case ACACIA_PRESSURE_PLATE:
case BIRCH_PRESSURE_PLATE:
case DARK_OAK_PRESSURE_PLATE:
case HEAVY_WEIGHTED_PRESSURE_PLATE:
case JUNGLE_PRESSURE_PLATE:
case LIGHT_WEIGHTED_PRESSURE_PLATE:
case OAK_PRESSURE_PLATE:
case SPRUCE_PRESSURE_PLATE:
case STONE_PRESSURE_PLATE:
case ACACIA_PRESSURE_PLATE:
case BIRCH_PRESSURE_PLATE:
case DARK_OAK_PRESSURE_PLATE:
case HEAVY_WEIGHTED_PRESSURE_PLATE:
case JUNGLE_PRESSURE_PLATE:
case LIGHT_WEIGHTED_PRESSURE_PLATE:
case OAK_PRESSURE_PLATE:
case SPRUCE_PRESSURE_PLATE:
case STONE_PRESSURE_PLATE:
// Pressure plates
checkIsland(e, e.getBlock().getLocation(), Flags.PRESSURE_PLATE);
break;

View File

@ -100,6 +100,7 @@ public class Flags {
// Physical interactions
public static final Flag CROP_TRAMPLE = new FlagBuilder().id("CROP_TRAMPLE").icon(Material.WHEAT).listener(new PhysicalInteractionListener()).build();
public static final Flag PRESSURE_PLATE = new FlagBuilder().id("PRESSURE_PLATE").icon(Material.STONE_PRESSURE_PLATE).build();
public static final Flag TURTLE_EGGS = new FlagBuilder().id("TURTLE_EGGS").icon(Material.TURTLE_EGG).build();
// Egg throwing
public static final Flag EGGS = new FlagBuilder().id("EGGS").icon(Material.EGG).listener(new EggListener()).build();

View File

@ -692,6 +692,10 @@ protection:
description: "Toggle access"
name: "Trap doors"
hint: "No trapdoor use"
TURTLE_EGGS:
description: "Toggle crushing"
name: "Turtle Eggs"
hint: "Turtle eggs cannot be crushed!"
locked: "&cThis island is locked!"
protected: "&cIsland protected: [description]"
spawn-protected: "&cSpawn protected: [description]"

View File

@ -0,0 +1,385 @@
/**
*
*/
package world.bentobox.bentobox.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.Arrays;
import java.util.HashMap;
import java.util.HashSet;
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.Cow;
import org.bukkit.entity.Creeper;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Zombie;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityInteractEvent;
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.bukkit.projectiles.ProjectileSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
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 world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
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;
import world.bentobox.bentobox.managers.FlagsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlayersManager;
import world.bentobox.bentobox.util.Util;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest( {BentoBox.class, Flags.class, Util.class, Bukkit.class} )
public class PhysicalInteractionListenerTest {
private static Location location;
private static BentoBox plugin;
private static Notifier notifier;
@Before
public void setUp() {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.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);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getServer()).thenReturn(server);
//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
IslandWorldManager iwm = mock(IslandWorldManager.class);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).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<>());
// 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 = invocation -> (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
IslandsManager 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);
PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnPlayerInteractNotPhysical() {
Player player = mock(Player.class);
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.STONE);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_AIR, item, clickedBlock, BlockFace.UP);
new PhysicalInteractionListener().onPlayerInteract(e);
assertFalse(e.isCancelled());
User.removePlayer(player);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnPlayerInteractWrongMaterial() {
Player player = mock(Player.class);
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.STONE);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, item, clickedBlock, BlockFace.UP);
new PhysicalInteractionListener().onPlayerInteract(e);
assertFalse(e.isCancelled());
User.removePlayer(player);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnPlayerInteractFarmland() {
Player player = mock(Player.class);
when(player.isOp()).thenReturn(false);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.FARMLAND);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, item, clickedBlock, BlockFace.UP);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onPlayerInteract(e);
assertTrue(e.isCancelled());
Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq("protection.protected"));
User.removePlayer(player);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnPlayerInteractFarmlandOp() {
Player player = mock(Player.class);
when(player.isOp()).thenReturn(true);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.FARMLAND);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, item, clickedBlock, BlockFace.UP);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onPlayerInteract(e);
assertFalse(e.isCancelled());
User.removePlayer(player);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnPlayerInteractFarmlandPermission() {
Player player = mock(Player.class);
when(player.isOp()).thenReturn(false);
when(player.hasPermission(Mockito.anyString())).thenReturn(true);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.FARMLAND);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, item, clickedBlock, BlockFace.UP);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onPlayerInteract(e);
assertFalse(e.isCancelled());
User.removePlayer(player);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnPlayerInteractTurtleEgg() {
Player player = mock(Player.class);
when(player.isOp()).thenReturn(false);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
when(clickedBlock.getType()).thenReturn(Material.TURTLE_EGG);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, item, clickedBlock, BlockFace.UP);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onPlayerInteract(e);
assertTrue(e.isCancelled());
Mockito.verify(notifier).notify(Mockito.any(), Mockito.eq("protection.protected"));
User.removePlayer(player);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onPlayerInteract(org.bukkit.event.player.PlayerInteractEvent)}.
*/
@Test
public void testOnPlayerInteractPressurePlate() {
Player player = mock(Player.class);
when(player.isOp()).thenReturn(false);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
Arrays.stream(Material.values()).filter(m -> m.name().contains("PRESSURE_PLATE")).forEach(p -> {
when(clickedBlock.getType()).thenReturn(p);
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.PHYSICAL, item, clickedBlock, BlockFace.UP);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onPlayerInteract(e);
assertTrue(e.isCancelled());
});
User.removePlayer(player);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onProjectileHit(org.bukkit.event.entity.EntityInteractEvent)}.
*/
@Test
public void testOnProjectileHitNotProjectile() {
Entity entity = mock(Entity.class);
Block block = mock(Block.class);
EntityInteractEvent e = new EntityInteractEvent(entity, block);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onProjectileHit(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onProjectileHit(org.bukkit.event.entity.EntityInteractEvent)}.
*/
@Test
public void testOnProjectileHitProjectileBlockNull() {
Projectile entity = mock(Projectile.class);
ProjectileSource source = (ProjectileSource) mock(Creeper.class);
when(entity.getShooter()).thenReturn(source);
Block block = null;
EntityInteractEvent e = new EntityInteractEvent(entity, block);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onProjectileHit(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onProjectileHit(org.bukkit.event.entity.EntityInteractEvent)}.
*/
@Test
public void testOnProjectileHitProjectile() {
Projectile entity = mock(Projectile.class);
ProjectileSource source = (ProjectileSource) mock(Creeper.class);
when(entity.getShooter()).thenReturn(source);
Block block = mock(Block.class);
EntityInteractEvent e = new EntityInteractEvent(entity, block);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onProjectileHit(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onProjectileHit(org.bukkit.event.entity.EntityInteractEvent)}.
*/
@Test
public void testOnProjectileHitProjectileBlockNullPlayer() {
Projectile entity = mock(Projectile.class);
ProjectileSource source = (ProjectileSource) mock(Player.class);
when(entity.getShooter()).thenReturn(source);
Block block = null;
EntityInteractEvent e = new EntityInteractEvent(entity, block);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onProjectileHit(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.flags.PhysicalInteractionListener#onProjectileHit(org.bukkit.event.entity.EntityInteractEvent)}.
*/
@Test
public void testOnProjectileHitProjectilePlayer() {
Projectile entity = mock(Projectile.class);
Player player = mock(Player.class);
when(player.isOp()).thenReturn(false);
when(player.getLocation()).thenReturn(location);
User.getInstance(player);
ProjectileSource source = (ProjectileSource) player ;
when(entity.getShooter()).thenReturn(source);
Block block = mock(Block.class);
when(block.getLocation()).thenReturn(location);
Arrays.stream(Material.values())
.filter(m -> !m.name().contains("LEGACY"))
.filter(m -> m.name().contains("PRESSURE_PLATE") || m.name().contains("BUTTON")).forEach(p -> {
when(block.getType()).thenReturn(p);
EntityInteractEvent e = new EntityInteractEvent(entity, block);
PhysicalInteractionListener i = new PhysicalInteractionListener();
i.onProjectileHit(e);
assertTrue(p.name() +" failed", e.isCancelled());
});
}
}