mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-05 18:40:06 +01:00
Added NetherPortalsTest class
This commit is contained in:
parent
060549b5ec
commit
dd0770cf8c
@ -24,6 +24,7 @@ import org.bukkit.util.Vector;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
import us.tastybento.bskyblock.util.teleport.SafeTeleportBuilder;
|
||||
|
||||
public class NetherPortals implements Listener {
|
||||
@ -53,15 +54,6 @@ public class NetherPortals implements Listener {
|
||||
return (spawn.distanceSquared(p) < (plugin.getSettings().getNetherSpawnRadius() * plugin.getSettings().getNetherSpawnRadius())) ? false : true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the player is in the island worlds
|
||||
* @param location - the location
|
||||
* @return true if the player is
|
||||
*/
|
||||
private boolean inWorlds(Location location) {
|
||||
return (location.getWorld().equals(world) || location.getWorld().equals(nether) || location.getWorld().equals(theEnd)) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the player is in the standard nether or standard end or op, do nothing.
|
||||
* Used to protect the standard spawn for nether or end
|
||||
@ -115,11 +107,11 @@ public class NetherPortals implements Listener {
|
||||
if (!e.getCause().equals(TeleportCause.END_PORTAL) || !plugin.getSettings().isEndGenerate()) {
|
||||
return;
|
||||
}
|
||||
if (!inWorlds(e.getFrom())) {
|
||||
if (!Util.inWorld(e.getFrom())) {
|
||||
return;
|
||||
}
|
||||
// If entering a portal in the end, teleport home if you have one, else do nothing
|
||||
if (e.getFrom().getWorld().equals(theEnd)) {
|
||||
if (e.getFrom().getWorld().equals(theEnd)) {
|
||||
if (plugin.getIslands().hasIsland(e.getPlayer().getUniqueId())) {
|
||||
e.setCancelled(true);
|
||||
plugin.getIslands().homeTeleport(e.getPlayer());
|
||||
@ -145,7 +137,7 @@ public class NetherPortals implements Listener {
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onEntityPortal(EntityPortalEvent e) {
|
||||
if (inWorlds(e.getFrom())) {
|
||||
if (Util.inWorld(e.getFrom())) {
|
||||
// Disable entity portal transfer due to dupe glitching
|
||||
e.setCancelled(true);
|
||||
}
|
||||
@ -157,19 +149,19 @@ public class NetherPortals implements Listener {
|
||||
* @param e - event
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onExplosion(EntityExplodeEvent e) {
|
||||
if (!inWorlds(e.getLocation())) {
|
||||
return;
|
||||
public boolean onExplosion(EntityExplodeEvent e) {
|
||||
if (!Util.inWorld(e.getLocation())) {
|
||||
return false;
|
||||
}
|
||||
if ((e.getLocation().getWorld().equals(nether) && plugin.getSettings().isNetherIslands())
|
||||
|| (e.getLocation().getWorld().equals(theEnd) && plugin.getSettings().isEndIslands())) {
|
||||
// Not used in island worlds
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
// Find out what is exploding
|
||||
Entity expl = e.getEntity();
|
||||
if (expl == null) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
Iterator<Block> it = e.blockList().iterator();
|
||||
while (it.hasNext()) {
|
||||
@ -178,6 +170,7 @@ public class NetherPortals implements Listener {
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -185,16 +178,13 @@ public class NetherPortals implements Listener {
|
||||
* @param e - event
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onNetherPortal(PlayerPortalEvent e) {
|
||||
if (!e.getCause().equals(TeleportCause.NETHER_PORTAL)) {
|
||||
return;
|
||||
}
|
||||
if (!inWorlds(e.getFrom())) {
|
||||
return;
|
||||
public boolean onNetherPortal(PlayerPortalEvent e) {
|
||||
if (!e.getCause().equals(TeleportCause.NETHER_PORTAL) || !Util.inWorld(e.getFrom())) {
|
||||
return false;
|
||||
}
|
||||
// If entering a portal in the nether, teleport to portal in overworld if there is one
|
||||
if (e.getFrom().getWorld().equals(nether)) {
|
||||
// If this is island nether, then go to the same vector, otherwise try spawn
|
||||
// If this is from the island nether, then go to the same vector, otherwise try island home location
|
||||
Location to = plugin.getSettings().isNetherIslands() ? e.getFrom().toVector().toLocation(world) : plugin.getIslands().getIslandLocation(e.getPlayer().getUniqueId());
|
||||
e.setCancelled(true);
|
||||
// Else other worlds teleport to the nether
|
||||
@ -203,7 +193,7 @@ public class NetherPortals implements Listener {
|
||||
.location(to)
|
||||
.portal()
|
||||
.build();
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
// If this is island nether, then go to the same vector, otherwise try spawn
|
||||
Location to = plugin.getSettings().isNetherIslands() ? e.getFrom().toVector().toLocation(nether) : nether.getSpawnLocation();
|
||||
@ -214,7 +204,7 @@ public class NetherPortals implements Listener {
|
||||
.location(to)
|
||||
.portal()
|
||||
.build();
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -239,9 +229,9 @@ public class NetherPortals implements Listener {
|
||||
* @param e - event
|
||||
*/
|
||||
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
|
||||
public void onTreeGrow(StructureGrowEvent e) {
|
||||
public boolean onTreeGrow(StructureGrowEvent e) {
|
||||
if (!plugin.getSettings().isNetherTrees() || !e.getWorld().equals(nether)) {
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
for (BlockState b : e.getBlocks()) {
|
||||
if (b.getType() == Material.LOG || b.getType() == Material.LOG_2) {
|
||||
@ -250,5 +240,6 @@ public class NetherPortals implements Listener {
|
||||
b.setType(Material.GLOWSTONE);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,696 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package us.tastybento.bskyblock.listeners;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.TreeType;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.block.BlockPlaceEvent;
|
||||
import org.bukkit.event.entity.EntityExplodeEvent;
|
||||
import org.bukkit.event.entity.EntityPortalEvent;
|
||||
import org.bukkit.event.player.PlayerBucketEmptyEvent;
|
||||
import org.bukkit.event.player.PlayerPortalEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
||||
import org.bukkit.event.world.StructureGrowEvent;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.Settings;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.generators.IslandWorld;
|
||||
import us.tastybento.bskyblock.managers.IslandsManager;
|
||||
import us.tastybento.bskyblock.managers.LocalesManager;
|
||||
import us.tastybento.bskyblock.managers.PlayersManager;
|
||||
import us.tastybento.bskyblock.util.Util;
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class, Util.class })
|
||||
public class NetherPortalsTest {
|
||||
|
||||
private BSkyBlock plugin;
|
||||
private UUID uuid;
|
||||
private User user;
|
||||
private Settings s;
|
||||
private IslandsManager im;
|
||||
private PlayersManager pm;
|
||||
private UUID notUUID;
|
||||
private BukkitScheduler sch;
|
||||
private IslandWorld iwm;
|
||||
private World world;
|
||||
private World nether;
|
||||
private World end;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
// Set up plugin
|
||||
plugin = mock(BSkyBlock.class);
|
||||
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
|
||||
|
||||
// island world mgr
|
||||
iwm = mock(IslandWorld.class);
|
||||
world = mock(World.class);
|
||||
nether = mock(World.class);
|
||||
end = mock(World.class);
|
||||
when(iwm.getEndWorld()).thenReturn(end);
|
||||
when(iwm.getIslandWorld()).thenReturn(world);
|
||||
when(iwm.getNetherWorld()).thenReturn(nether);
|
||||
when(plugin.getIslandWorldManager()).thenReturn(iwm);
|
||||
|
||||
// Settings
|
||||
s = mock(Settings.class);
|
||||
when(plugin.getSettings()).thenReturn(s);
|
||||
|
||||
// Set up spawn
|
||||
Location netherSpawn = mock(Location.class);
|
||||
when(netherSpawn.toVector()).thenReturn(new Vector(0,0,0));
|
||||
when(nether.getSpawnLocation()).thenReturn(netherSpawn);
|
||||
when(s.getNetherSpawnRadius()).thenReturn(100);
|
||||
|
||||
// Player
|
||||
Player p = mock(Player.class);
|
||||
// Sometimes use Mockito.withSettings().verboseLogging()
|
||||
user = mock(User.class);
|
||||
when(user.isOp()).thenReturn(false);
|
||||
uuid = UUID.randomUUID();
|
||||
notUUID = UUID.randomUUID();
|
||||
while(notUUID.equals(uuid)) {
|
||||
notUUID = UUID.randomUUID();
|
||||
}
|
||||
when(user.getUniqueId()).thenReturn(uuid);
|
||||
when(user.getPlayer()).thenReturn(p);
|
||||
when(user.getName()).thenReturn("tastybento");
|
||||
User.setPlugin(plugin);
|
||||
|
||||
// Player has island to begin with
|
||||
im = mock(IslandsManager.class);
|
||||
when(im.hasIsland(Mockito.any())).thenReturn(true);
|
||||
when(im.isOwner(Mockito.any())).thenReturn(true);
|
||||
when(im.getTeamLeader(Mockito.any())).thenReturn(uuid);
|
||||
when(plugin.getIslands()).thenReturn(im);
|
||||
|
||||
when(plugin.getPlayers()).thenReturn(pm);
|
||||
|
||||
// Server & Scheduler
|
||||
sch = mock(BukkitScheduler.class);
|
||||
PowerMockito.mockStatic(Bukkit.class);
|
||||
when(Bukkit.getScheduler()).thenReturn(sch);
|
||||
|
||||
// Locales
|
||||
LocalesManager lm = mock(LocalesManager.class);
|
||||
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
|
||||
when(plugin.getLocalesManager()).thenReturn(lm);
|
||||
|
||||
// Normally in world
|
||||
Util.setPlugin(plugin);
|
||||
PowerMockito.mockStatic(Util.class);
|
||||
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
|
||||
when(Util.inWorld(Mockito.any(Block.class))).thenReturn(true);
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#NetherPortals(us.tastybento.bskyblock.BSkyBlock)}.
|
||||
*/
|
||||
@Test
|
||||
public void testNetherPortals() {
|
||||
assertNotNull(new NetherPortals(plugin));
|
||||
Mockito.verify(iwm).getIslandWorld();
|
||||
Mockito.verify(iwm).getNetherWorld();
|
||||
Mockito.verify(iwm).getEndWorld();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onBlockBreak(org.bukkit.event.block.BlockBreakEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnBlockBreakNoAction() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Block block = mock(Block.class);
|
||||
Player player = mock(Player.class);
|
||||
// Ops can do anything
|
||||
when(player.isOp()).thenReturn(true);
|
||||
BlockBreakEvent e = new BlockBreakEvent(block, player);
|
||||
np.onBlockBreak(e);
|
||||
assertFalse(e.isCancelled());
|
||||
// not op, but not in right world
|
||||
when(player.isOp()).thenReturn(false);
|
||||
when(player.getWorld()).thenReturn(mock(World.class));
|
||||
np.onBlockBreak(e);
|
||||
assertFalse(e.isCancelled());
|
||||
// not op, island world
|
||||
when(player.getWorld()).thenReturn(world);
|
||||
np.onBlockBreak(e);
|
||||
assertFalse(e.isCancelled());
|
||||
// Nether, but not standard nether
|
||||
when(player.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(true);
|
||||
np.onBlockBreak(e);
|
||||
assertFalse(e.isCancelled());
|
||||
// End, but not standard end
|
||||
when(player.getWorld()).thenReturn(nether);
|
||||
when(s.isEndIslands()).thenReturn(true);
|
||||
np.onBlockBreak(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onBlockBreak(org.bukkit.event.block.BlockBreakEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnBlockBreakActionAwayFromSpawn() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Block block = mock(Block.class);
|
||||
Location far = mock(Location.class);
|
||||
when(far.toVector()).thenReturn(new Vector(10000, 56, 2000));
|
||||
when(far.getWorld()).thenReturn(nether);
|
||||
when(block.getLocation()).thenReturn(far);
|
||||
Player player = mock(Player.class);
|
||||
// Standard nether
|
||||
when(player.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
|
||||
BlockBreakEvent e = new BlockBreakEvent(block, player);
|
||||
np.onBlockBreak(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onBlockBreak(org.bukkit.event.block.BlockBreakEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnBlockBreakActionAtSpawn() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Block block = mock(Block.class);
|
||||
Location near = mock(Location.class);
|
||||
when(near.toVector()).thenReturn(new Vector(0, 56, 0));
|
||||
when(near.getWorld()).thenReturn(nether);
|
||||
when(block.getLocation()).thenReturn(near);
|
||||
Player player = mock(Player.class);
|
||||
// Standard nether
|
||||
when(player.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
|
||||
BlockBreakEvent e = new BlockBreakEvent(block, player);
|
||||
np.onBlockBreak(e);
|
||||
Mockito.verify(block).getLocation();
|
||||
assertTrue(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onBucketEmpty(org.bukkit.event.player.PlayerBucketEmptyEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnBucketEmpty() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Block block = mock(Block.class);
|
||||
Location near = mock(Location.class);
|
||||
when(near.toVector()).thenReturn(new Vector(0, 56, 0));
|
||||
when(near.getWorld()).thenReturn(nether);
|
||||
when(block.getLocation()).thenReturn(near);
|
||||
Player player = mock(Player.class);
|
||||
// Standard nether
|
||||
when(player.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
|
||||
PlayerBucketEmptyEvent e = new PlayerBucketEmptyEvent(player, block, null, null, null);
|
||||
np.onBucketEmpty(e);
|
||||
Mockito.verify(block).getLocation();
|
||||
assertTrue(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onEndIslandPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEndIslandPortalNotEnd() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Wrong cause
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, null, null, null, TeleportCause.CHORUS_FRUIT);
|
||||
np.onEndIslandPortal(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onEndIslandPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEndIslandPortalNoEnd() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Right cause, no end
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, null, null, null, TeleportCause.END_PORTAL);
|
||||
when(s.isEndGenerate()).thenReturn(false);
|
||||
np.onEndIslandPortal(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onEndIslandPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEndIslandPortalWrongWorld() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location loc = mock(Location.class);
|
||||
|
||||
// Right cause, end exists, wrong world
|
||||
when(loc.getWorld()).thenReturn(mock(World.class));
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, loc, null, null, TeleportCause.END_PORTAL);
|
||||
when(s.isEndGenerate()).thenReturn(true);
|
||||
np.onEndIslandPortal(e);
|
||||
assertFalse(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onEndIslandPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEndIslandPortalHome() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location from = mock(Location.class);
|
||||
// Teleport from end
|
||||
when(from.getWorld()).thenReturn(end);
|
||||
|
||||
// Player has no island
|
||||
Player player = mock(Player.class);
|
||||
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
when(im.hasIsland(Mockito.any())).thenReturn(false);
|
||||
// Right cause, end exists, right world
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(player, from, null, null, TeleportCause.END_PORTAL);
|
||||
when(s.isEndGenerate()).thenReturn(true);
|
||||
np.onEndIslandPortal(e);
|
||||
assertFalse(e.isCancelled());
|
||||
// Give player an island
|
||||
when(im.hasIsland(Mockito.any())).thenReturn(true);
|
||||
np.onEndIslandPortal(e);
|
||||
assertTrue(e.isCancelled());
|
||||
Mockito.verify(im).homeTeleport(Mockito.eq(player));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onEntityPortal(org.bukkit.event.entity.EntityPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnEntityPortal() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Entity ent = mock(Entity.class);
|
||||
Location from = mock(Location.class);
|
||||
when(from.getWorld()).thenReturn(mock(World.class));
|
||||
// Not in world
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
|
||||
EntityPortalEvent e = new EntityPortalEvent(ent, from, null, null);
|
||||
np.onEntityPortal(e);
|
||||
assertFalse(e.isCancelled());
|
||||
// In world
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
e = new EntityPortalEvent(ent, from, null, null);
|
||||
np.onEntityPortal(e);
|
||||
assertTrue(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnExplosionNotInWorld() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Null entity
|
||||
Entity en = null;
|
||||
// Entity, Location, list of Blocks, yield
|
||||
Block block = mock(Block.class);
|
||||
Location blockLoc = mock(Location.class);
|
||||
when(blockLoc.toVector()).thenReturn(new Vector(10000,0,10000));
|
||||
when(block.getLocation()).thenReturn(blockLoc);
|
||||
when(blockLoc.getWorld()).thenReturn(nether);
|
||||
// One block will be blown up by the wither
|
||||
List<Block> affectedBlocks = new ArrayList<>();
|
||||
affectedBlocks.add(block);
|
||||
|
||||
Location from = mock(Location.class);
|
||||
when(from.getWorld()).thenReturn(mock(World.class));
|
||||
// Not in world
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
|
||||
|
||||
EntityExplodeEvent e = new EntityExplodeEvent(en, from, affectedBlocks, 0);
|
||||
|
||||
assertFalse(np.onExplosion(e));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testOnExplosionInWorldNotNetherOrEnd() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Null entity
|
||||
Entity en = null;
|
||||
// Entity, Location, list of Blocks, yield
|
||||
Block block = mock(Block.class);
|
||||
Location blockLoc = mock(Location.class);
|
||||
when(blockLoc.toVector()).thenReturn(new Vector(10000,0,10000));
|
||||
when(block.getLocation()).thenReturn(blockLoc);
|
||||
when(blockLoc.getWorld()).thenReturn(nether);
|
||||
// One block will be blown up by the wither
|
||||
List<Block> affectedBlocks = new ArrayList<>();
|
||||
affectedBlocks.add(block);
|
||||
|
||||
Location from = mock(Location.class);
|
||||
when(from.getWorld()).thenReturn(mock(World.class));
|
||||
// In world, not nether or end
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
EntityExplodeEvent e = new EntityExplodeEvent(en, from, affectedBlocks, 0);
|
||||
assertFalse(np.onExplosion(e));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnExplosionIslands() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Null entity
|
||||
Entity en = null;
|
||||
// Entity, Location, list of Blocks, yield
|
||||
Block block = mock(Block.class);
|
||||
Location blockLoc = mock(Location.class);
|
||||
when(blockLoc.toVector()).thenReturn(new Vector(10000,0,10000));
|
||||
when(block.getLocation()).thenReturn(blockLoc);
|
||||
when(blockLoc.getWorld()).thenReturn(nether);
|
||||
// One block will be blown up by the wither
|
||||
List<Block> affectedBlocks = new ArrayList<>();
|
||||
affectedBlocks.add(block);
|
||||
|
||||
Location from = mock(Location.class);
|
||||
// In world, not nether or end
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
|
||||
// In world, in nether, nether islands
|
||||
when(from.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(true);
|
||||
EntityExplodeEvent e = new EntityExplodeEvent(en, from, affectedBlocks, 0);
|
||||
assertFalse(np.onExplosion(e));
|
||||
|
||||
// In world, in end, end islands
|
||||
when(from.getWorld()).thenReturn(end);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
when(s.isEndIslands()).thenReturn(true);
|
||||
assertFalse(np.onExplosion(e));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnExplosionNullEntity() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Entity, Location, list of Blocks, yield
|
||||
Block block = mock(Block.class);
|
||||
Location blockLoc = mock(Location.class);
|
||||
when(blockLoc.toVector()).thenReturn(new Vector(10000,0,10000));
|
||||
when(block.getLocation()).thenReturn(blockLoc);
|
||||
when(blockLoc.getWorld()).thenReturn(nether);
|
||||
// One block will be blown up by the wither
|
||||
List<Block> affectedBlocks = new ArrayList<>();
|
||||
affectedBlocks.add(block);
|
||||
|
||||
Location from = mock(Location.class);
|
||||
// In world
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
// In world, in nether, nether islands
|
||||
when(from.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
EntityExplodeEvent e = new EntityExplodeEvent(null, from, affectedBlocks, 0);
|
||||
assertFalse(np.onExplosion(e));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnExplosionAwayFromSpawn() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Null entity
|
||||
Entity en = mock(Entity.class);
|
||||
// Entity, Location, list of Blocks, yield
|
||||
Block block = mock(Block.class);
|
||||
Location blockLoc = mock(Location.class);
|
||||
when(blockLoc.toVector()).thenReturn(new Vector(10000,0,10000));
|
||||
when(block.getLocation()).thenReturn(blockLoc);
|
||||
when(blockLoc.getWorld()).thenReturn(nether);
|
||||
// One block will be blown up by the wither
|
||||
List<Block> affectedBlocks = new ArrayList<>();
|
||||
affectedBlocks.add(block);
|
||||
|
||||
Location from = mock(Location.class);
|
||||
// In world, not nether or end
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
|
||||
// In world, in nether, standard nether, null entity
|
||||
when(from.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
|
||||
EntityExplodeEvent e = new EntityExplodeEvent(en, from, affectedBlocks, 0);
|
||||
// Real entity, away from spawn
|
||||
assertTrue(np.onExplosion(e));
|
||||
// Block should still exist because it is away from spawn
|
||||
assertFalse(e.blockList().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnExplosion() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
// Null entity
|
||||
Entity en = null;
|
||||
// Entity, Location, list of Blocks, yield
|
||||
Block block = mock(Block.class);
|
||||
Location blockLoc = mock(Location.class);
|
||||
when(blockLoc.toVector()).thenReturn(new Vector(10000,0,10000));
|
||||
when(block.getLocation()).thenReturn(blockLoc);
|
||||
when(blockLoc.getWorld()).thenReturn(nether);
|
||||
// One block will be blown up by the wither
|
||||
List<Block> affectedBlocks = new ArrayList<>();
|
||||
affectedBlocks.add(block);
|
||||
|
||||
Location from = mock(Location.class);
|
||||
when(from.getWorld()).thenReturn(mock(World.class));
|
||||
// Not in world
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
|
||||
|
||||
|
||||
|
||||
// In world, not nether or end
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
|
||||
// In world, in nether, standard nether, null entity
|
||||
when(from.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
|
||||
|
||||
// Real entity, next to spawn
|
||||
en = mock(Entity.class);
|
||||
when(blockLoc.toVector()).thenReturn(new Vector(0,0,0));
|
||||
EntityExplodeEvent e = new EntityExplodeEvent(en, from, affectedBlocks, 0);
|
||||
// Block exists before
|
||||
assertFalse(e.blockList().isEmpty());
|
||||
assertTrue(np.onExplosion(e));
|
||||
// Block removed
|
||||
assertTrue(e.blockList().isEmpty());
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onNetherPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnNetherPortalNotPortal() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, null, null, null, TeleportCause.COMMAND);
|
||||
assertFalse(np.onNetherPortal(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onNetherPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnNetherPortalWrongWorld() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location from = mock(Location.class);
|
||||
when(from.getWorld()).thenReturn(mock(World.class));
|
||||
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
|
||||
assertFalse(np.onNetherPortal(e));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onNetherPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnNetherPortalFromWorldToNetherIsland() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location from = mock(Location.class);
|
||||
// Teleport from world to nether
|
||||
when(from.getWorld()).thenReturn(world);
|
||||
when(from.toVector()).thenReturn(new Vector(1,2,3));
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
|
||||
// Nether islands active
|
||||
when(s.isNetherIslands()).thenReturn(true);
|
||||
assertTrue(np.onNetherPortal(e));
|
||||
// Verify
|
||||
assertTrue(e.isCancelled());
|
||||
// If nether islands, then to = from but in nether
|
||||
Mockito.verify(from).toVector();
|
||||
// Do not go to spawn
|
||||
Mockito.verify(nether, Mockito.never()).getSpawnLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onNetherPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnNetherPortalFromWorldToNetherStandard() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location from = mock(Location.class);
|
||||
// Teleport from world to nether
|
||||
when(from.getWorld()).thenReturn(world);
|
||||
when(from.toVector()).thenReturn(new Vector(1,2,3));
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
|
||||
// Nether islands inactive
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
assertTrue(np.onNetherPortal(e));
|
||||
// Verify
|
||||
assertTrue(e.isCancelled());
|
||||
// If regular nether, then to = spawn point of nether
|
||||
Mockito.verify(from, Mockito.never()).toVector();
|
||||
Mockito.verify(nether).getSpawnLocation();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onNetherPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnNetherPortalFromNetherStandard() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location from = mock(Location.class);
|
||||
// Teleport from nether to world
|
||||
when(from.getWorld()).thenReturn(nether);
|
||||
when(from.toVector()).thenReturn(new Vector(1,2,3));
|
||||
Player player = mock(Player.class);
|
||||
when(player.getUniqueId()).thenReturn(UUID.randomUUID());
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(player, from, null, null, TeleportCause.NETHER_PORTAL);
|
||||
// Nether islands inactive
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
assertTrue(np.onNetherPortal(e));
|
||||
// Verify
|
||||
assertTrue(e.isCancelled());
|
||||
// If regular nether, then to = island location
|
||||
Mockito.verify(from, Mockito.never()).toVector();
|
||||
Mockito.verify(im).getIslandLocation(Mockito.any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onNetherPortal(org.bukkit.event.player.PlayerPortalEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnNetherPortalFromNetherIsland() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location from = mock(Location.class);
|
||||
// Teleport from nether to world
|
||||
when(from.getWorld()).thenReturn(nether);
|
||||
when(from.toVector()).thenReturn(new Vector(1,2,3));
|
||||
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
|
||||
// Nether islands active
|
||||
when(s.isNetherIslands()).thenReturn(true);
|
||||
assertTrue(np.onNetherPortal(e));
|
||||
// Verify
|
||||
assertTrue(e.isCancelled());
|
||||
// If regular nether, then to = island location
|
||||
Mockito.verify(from).toVector();
|
||||
Mockito.verify(im, Mockito.never()).getIslandLocation(Mockito.any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onPlayerBlockPlace(org.bukkit.event.block.BlockPlaceEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnPlayerBlockPlace() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Block block = mock(Block.class);
|
||||
Location near = mock(Location.class);
|
||||
when(near.toVector()).thenReturn(new Vector(0, 56, 0));
|
||||
when(near.getWorld()).thenReturn(nether);
|
||||
when(block.getLocation()).thenReturn(near);
|
||||
Player player = mock(Player.class);
|
||||
// Standard nether
|
||||
when(player.getWorld()).thenReturn(nether);
|
||||
when(s.isNetherIslands()).thenReturn(false);
|
||||
|
||||
BlockPlaceEvent e = new BlockPlaceEvent(block, null, block, null, player, false, null);
|
||||
np.onPlayerBlockPlace(e);
|
||||
Mockito.verify(block).getLocation();
|
||||
assertTrue(e.isCancelled());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test method for {@link us.tastybento.bskyblock.listeners.NetherPortals#onTreeGrow(org.bukkit.event.world.StructureGrowEvent)}.
|
||||
*/
|
||||
@Test
|
||||
public void testOnTreeGrow() {
|
||||
NetherPortals np = new NetherPortals(plugin);
|
||||
Location loc = mock(Location.class);
|
||||
// Wrong world to start
|
||||
when(loc.getWorld()).thenReturn(world);
|
||||
BlockState log = mock(BlockState.class);
|
||||
when(log.getType()).thenReturn(Material.LOG);
|
||||
BlockState log2 = mock(BlockState.class);
|
||||
when(log2.getType()).thenReturn(Material.LOG_2);
|
||||
BlockState leaves = mock(BlockState.class);
|
||||
when(leaves.getType()).thenReturn(Material.LEAVES);
|
||||
BlockState leaves2 = mock(BlockState.class);
|
||||
when(leaves2.getType()).thenReturn(Material.LEAVES_2);
|
||||
List<BlockState> blocks = new ArrayList<>();
|
||||
blocks.add(log);
|
||||
blocks.add(log2);
|
||||
blocks.add(leaves);
|
||||
blocks.add(leaves2);
|
||||
StructureGrowEvent e = new StructureGrowEvent(loc, TreeType.ACACIA, false, null, blocks);
|
||||
// No nether trees
|
||||
when(s.isNetherTrees()).thenReturn(false);
|
||||
assertFalse(np.onTreeGrow(e));
|
||||
// nether trees, wrong world
|
||||
e = new StructureGrowEvent(loc, TreeType.ACACIA, false, null, blocks);
|
||||
when(s.isNetherTrees()).thenReturn(true);
|
||||
assertFalse(np.onTreeGrow(e));
|
||||
// Make the world nether
|
||||
when(loc.getWorld()).thenReturn(nether);
|
||||
e = new StructureGrowEvent(loc, TreeType.ACACIA, false, null, blocks);
|
||||
assertTrue(np.onTreeGrow(e));
|
||||
Mockito.verify(log).setType(Material.GRAVEL);
|
||||
Mockito.verify(log2).setType(Material.GRAVEL);
|
||||
Mockito.verify(leaves).setType(Material.GLOWSTONE);
|
||||
Mockito.verify(leaves2).setType(Material.GLOWSTONE);
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user