Reworked world checking.

Worlds are checked by the IslandWord class. Additional worlds can be
added to the list of worlds covered by BSB by Addons.
This commit is contained in:
tastybento 2018-05-18 23:08:18 -07:00
parent 9f6819bd3b
commit df696fb4a0
27 changed files with 162 additions and 230 deletions

View File

@ -1,5 +1,6 @@
package us.tastybento.bskyblock;
import org.bukkit.World;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
@ -106,7 +107,7 @@ public class BSkyBlock extends JavaPlugin {
getServer().getScheduler().runTask(this, () -> {
// Create the world if it does not exist
islandWorldManager = new IslandWorld(instance);
getServer().getScheduler().runTask(instance, () -> {
// Load Flags
@ -123,6 +124,8 @@ public class BSkyBlock extends JavaPlugin {
// Load addons
addonsManager = new AddonsManager(instance);
addonsManager.loadAddons();
// Enable addons
addonsManager.enableAddons();
// Save islands & players data asynchronously every X minutes
@ -302,4 +305,15 @@ public class BSkyBlock extends JavaPlugin {
public void logWarning(String warning) {
getLogger().warning(warning);
}
/**
* Registers a world as a world to be covered by this plugin
* @param world - world
*/
public void registerWorld(World world) {
islandWorldManager.addWorld(world);
}
}

View File

@ -383,7 +383,7 @@ public class Island implements DataObject {
}
public boolean inIslandSpace(Location location) {
if (Util.inWorld(location)) {
if (Util.sameWorld(world, location.getWorld())) {
return inIslandSpace(location.getBlockX(), location.getBlockZ());
}
return false;
@ -441,11 +441,10 @@ public class Island implements DataObject {
* @return true if it is, false if not
*/
public boolean onIsland(Location target) {
if (center != null && center.getWorld() != null) {
if (Util.sameWorld(world, target.getWorld())) {
return target.getBlockX() >= minProtectedX && target.getBlockX() < (minProtectedX + protectionRange * 2)
&& target.getBlockZ() >= minProtectedZ && target.getBlockZ() < (minProtectedZ + protectionRange * 2);
}
return false;
}

View File

@ -15,6 +15,7 @@ import org.bukkit.entity.Player;
import com.google.gson.annotations.Expose;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.util.Util;
/**
* Tracks the following info on the player
@ -74,7 +75,7 @@ public class Players implements DataObject {
*/
public Location getHomeLocation(World world, int number) {
return homeLocations.entrySet().stream()
.filter(en -> sameWorld(en.getKey().getWorld(), world) && en.getValue() == number)
.filter(en -> Util.sameWorld(en.getKey().getWorld(), world) && en.getValue() == number)
.map(en -> en.getKey())
.findFirst()
.orElse(null);
@ -85,22 +86,10 @@ public class Players implements DataObject {
* @return List of home locations
*/
public Map<Location, Integer> getHomeLocations(World world) {
return homeLocations.entrySet().stream().filter(e -> sameWorld(e.getKey().getWorld(),world))
return homeLocations.entrySet().stream().filter(e -> Util.sameWorld(e.getKey().getWorld(),world))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
/**
* Checks is world = world2 irrespective of the world type
* @param world
* @param world2
* @return true if the same
*/
private boolean sameWorld(World world, World world2) {
String worldName = world.getName().replaceAll("_nether", "").replaceAll("_the_end", "");
String world2Name = world2.getName().replaceAll("_nether", "").replaceAll("_the_end", "");
return worldName.equalsIgnoreCase(world2Name);
}
/**
* @return the kickedList
*/
@ -188,7 +177,7 @@ public class Players implements DataObject {
* @param world
*/
public void clearHomeLocations(World world) {
homeLocations.keySet().removeIf(l -> sameWorld(l.getWorld(), world));
homeLocations.keySet().removeIf(l -> Util.sameWorld(l.getWorld(), world));
}
/**

View File

@ -1,5 +1,8 @@
package us.tastybento.bskyblock.generators;
import java.util.HashSet;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
@ -20,12 +23,14 @@ public class IslandWorld {
private World islandWorld;
private World netherWorld;
private World endWorld;
private Set<World> worlds;
/**
* Generates the Skyblock worlds.
*/
public IslandWorld(BSkyBlock plugin) {
this.plugin = plugin;
worlds = new HashSet<>();
if (plugin.getSettings().isUseOwnGenerator()) {
// Do nothing
return;
@ -128,12 +133,17 @@ public class IslandWorld {
}
/**
* Checks if a player is in any of the island worlds
* @param loc - player to check
* Checks if a location is in any of the island worlds
* @param loc - location
* @return true if in a world or false if not
*/
public boolean inWorld(Location loc) {
return loc.getWorld() !=null && (loc.getWorld().equals(islandWorld) || loc.getWorld().equals(netherWorld) || loc.getWorld().equals(endWorld));
}
public void addWorld(World world) {
worlds.add(world);
}
}

View File

@ -24,7 +24,6 @@ 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 {
@ -107,7 +106,7 @@ public class NetherPortals implements Listener {
if (!e.getCause().equals(TeleportCause.END_PORTAL) || !plugin.getSettings().isEndGenerate()) {
return;
}
if (!Util.inWorld(e.getFrom())) {
if (!plugin.getIslandWorldManager().inWorld(e.getFrom())) {
return;
}
// If entering a portal in the end, teleport home if you have one, else do nothing
@ -137,7 +136,7 @@ public class NetherPortals implements Listener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onEntityPortal(EntityPortalEvent e) {
if (Util.inWorld(e.getFrom())) {
if (plugin.getIslandWorldManager().inWorld(e.getFrom())) {
// Disable entity portal transfer due to dupe glitching
e.setCancelled(true);
}
@ -150,7 +149,7 @@ public class NetherPortals implements Listener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onExplosion(EntityExplodeEvent e) {
if (!Util.inWorld(e.getLocation())) {
if (!plugin.getIslandWorldManager().inWorld(e.getLocation())) {
return false;
}
if ((e.getLocation().getWorld().equals(nether) && plugin.getSettings().isNetherIslands())
@ -179,7 +178,7 @@ public class NetherPortals implements Listener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onNetherPortal(PlayerPortalEvent e) {
if (!e.getCause().equals(TeleportCause.NETHER_PORTAL) || !Util.inWorld(e.getFrom())) {
if (!e.getCause().equals(TeleportCause.NETHER_PORTAL) || !plugin.getIslandWorldManager().inWorld(e.getFrom())) {
return false;
}
// If entering a portal in the nether, teleport to portal in overworld if there is one

View File

@ -7,7 +7,6 @@ import java.lang.reflect.Method;
import java.util.Optional;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
@ -18,6 +17,7 @@ import us.tastybento.bskyblock.api.flags.Flag;
import us.tastybento.bskyblock.api.flags.Flag.Type;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.managers.IslandsManager;
/**
@ -104,36 +104,6 @@ public abstract class AbstractFlagListener implements Listener {
}
}
/**
* Check if loc is in the island worlds.
* (If you are here because of an NPE in your test, then you need to do setPlugin(plugin) for your listener)
* @param loc - location
* @return true if the location is in the island worlds
*/
public boolean inWorld(Location loc) {
return loc != null && (loc.getWorld().equals(plugin.getIslandWorldManager().getIslandWorld())
|| loc.getWorld().equals(plugin.getIslandWorldManager().getNetherWorld())
|| loc.getWorld().equals(plugin.getIslandWorldManager().getEndWorld()));
}
/**
* Check if the entity is in the island worlds
* @param entity - the entity
* @return true if in world
*/
public boolean inWorld(Entity entity) {
return inWorld(entity.getLocation());
}
/**
* Check if user is in the island worlds
* @param user - the User - a user
* @return true if in world
*/
public boolean inWorld(User user) {
return inWorld(user.getLocation());
}
/**
* Generic flag checker
* @param e - event
@ -145,8 +115,6 @@ public abstract class AbstractFlagListener implements Listener {
return checkIsland(e, loc, breakBlocks, false);
}
/**
* Check if flag is allowed
* @param e - event
@ -156,7 +124,7 @@ public abstract class AbstractFlagListener implements Listener {
*/
public boolean checkIsland(Event e, Location loc, Flag flag, boolean silent) {
// If this is not an Island World, skip
if (!inWorld(loc)) {
if (!plugin.getIslandWorldManager().inWorld(loc)) {
return true;
}
@ -221,4 +189,12 @@ public abstract class AbstractFlagListener implements Listener {
protected IslandsManager getIslands() {
return plugin.getIslands();
}
/**
* Get the island world manager
* @return Island World Manager
*/
protected IslandWorld getIslandWorldManager() {
return plugin.getIslandWorldManager();
}
}

View File

@ -93,7 +93,7 @@ public class BreakBlocksListener extends AbstractFlagListener {
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onVehicleDamageEvent(VehicleDamageEvent e) {
if (inWorld(e.getVehicle()) && e.getAttacker() instanceof Player) {
if (getIslandWorldManager().inWorld(e.getVehicle().getLocation()) && e.getAttacker() instanceof Player) {
User user = User.getInstance((Player) e.getAttacker());
// Get the island and if present, check the flag, react if required and return
getIslands().getIslandAt(e.getVehicle().getLocation()).ifPresent(x -> {

View File

@ -39,7 +39,7 @@ public class FireListener extends AbstractFlagListener {
*/
public boolean checkFire(Cancellable e, Location l, Flag flag) {
// Check world
if (!inWorld(l)) {
if (!getIslandWorldManager().inWorld(l)) {
return false;
}
// Check if the island exists and if fire is allowed
@ -123,7 +123,7 @@ public class FireListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public boolean onTNTDamage(EntityChangeBlockEvent e) {
// Check world
if (!e.getBlock().getType().equals(Material.TNT) || !inWorld(e.getBlock().getLocation())) {
if (!e.getBlock().getType().equals(Material.TNT) || !getIslandWorldManager().inWorld(e.getBlock().getLocation())) {
return false;
}
// Stop TNT from being damaged if it is being caused by a visitor with a flaming arrow

View File

@ -32,7 +32,7 @@ public class MobSpawnListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onNaturalMobSpawn(CreatureSpawnEvent e) {
// If not in the right world, return
if (!inWorld(e.getEntity())) {
if (!getIslandWorldManager().inWorld(e.getEntity().getLocation())) {
return false;
}
// Deal with natural spawning

View File

@ -21,7 +21,6 @@ import org.bukkit.event.entity.ExplosionPrimeEvent;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.Util;
/**
* This class manages flying mobs. If they exist the spawned island's limits they will be removed.
@ -71,7 +70,7 @@ public class FlyingMobEvents implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onMobSpawn(CreatureSpawnEvent e) {
// Only cover withers in the island world
if (!Util.inWorld(e.getEntity()) || !(e.getEntityType().equals(EntityType.WITHER)
if (!plugin.getIslandWorldManager().inWorld(e.getEntity().getLocation()) || !(e.getEntityType().equals(EntityType.WITHER)
|| e.getEntityType().equals(EntityType.BLAZE)
|| e.getEntityType().equals(EntityType.GHAST))) {
return;
@ -88,7 +87,7 @@ public class FlyingMobEvents implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onMobExplosion(EntityExplodeEvent e) {
// Only cover in the island world
if (e.getEntity() == null || !Util.inWorld(e.getEntity())) {
if (e.getEntity() == null || !plugin.getIslandWorldManager().inWorld(e.getEntity().getLocation())) {
return false;
}
if (mobSpawnInfo.containsKey(e.getEntity()) && !mobSpawnInfo.get(e.getEntity()).inIslandSpace(e.getLocation())) {
@ -106,7 +105,7 @@ public class FlyingMobEvents implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onWitherExplode(ExplosionPrimeEvent e) {
// Only cover withers in the island world
if (!Util.inWorld(e.getEntity()) || e.getEntity() == null) {
if (!plugin.getIslandWorldManager().inWorld(e.getEntity().getLocation()) || e.getEntity() == null) {
return false;
}
// The wither or wither skulls can both blow up
@ -140,7 +139,7 @@ public class FlyingMobEvents implements Listener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onWitherChangeBlocks(EntityChangeBlockEvent e) {
// Only cover withers in the island world
if (e.getEntityType() != EntityType.WITHER || !Util.inWorld(e.getEntity()) ) {
if (e.getEntityType() != EntityType.WITHER || !plugin.getIslandWorldManager().inWorld(e.getEntity().getLocation()) ) {
return;
}
if (mobSpawnInfo.containsKey(e.getEntity())) {

View File

@ -5,6 +5,7 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
@ -47,28 +48,27 @@ public class AddonsManager {
/**
* Loads all the addons from the addons folder
*/
public void enableAddons() {
public void loadAddons() {
plugin.log("Loading addons...");
File f = new File(plugin.getDataFolder(), "addons");
if (f.exists()) {
if (f.isDirectory()) {
for (File file : f.listFiles()) {
if (!file.isDirectory()) {
try {
loadAddon(file);
} catch (InvalidAddonFormatException | InvalidAddonInheritException | InvalidDescriptionException e) {
plugin.logError("Could not load addon " + file.getName() + " : " + e.getMessage());
}
}
}
}
} else {
try {
f.mkdir();
} catch (SecurityException e) {
plugin.logError("Cannot create folder 'addons' (Permission ?)");
}
if (!f.exists()) {
f.mkdirs();
}
Arrays.asList(f.listFiles()).stream().filter(x -> !x.isDirectory() && x.getName().endsWith(".jar")).forEach(t -> {
plugin.log("Loading " + t.getName());
try {
loadAddon(t);
} catch (Exception e) {
plugin.logError("Could not load addon " + t.getName() + " : " + e.getMessage());
}
});
addons.forEach(Addon::onLoad);
}
/**
* Enables all the addons
*/
public void enableAddons() {
addons.forEach(addon -> {
addon.onEnable();
Bukkit.getPluginManager().callEvent(AddonEvent.builder().addon(addon).reason(AddonEvent.Reason.ENABLE).build());

View File

@ -17,7 +17,6 @@ import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.util.Util;
public class IslandCache {
private BiMap<Location, Island> islandsByLocation;
@ -124,10 +123,6 @@ public class IslandCache {
if (location == null) {
return null;
}
// World check
if (!Util.inWorld(location)) {
return null;
}
return grids.getOrDefault(location.getWorld(), defaultGrid).getIslandAt(location.getBlockX(), location.getBlockZ());
}

View File

@ -12,8 +12,6 @@ import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.util.Vector;
@ -32,7 +30,7 @@ public class Util {
private static final DecimalFormat df = new DecimalFormat("#.###");
private static String serverVersion = null;
private static BSkyBlock plugin = BSkyBlock.getInstance();
public static void setPlugin(BSkyBlock p) {
plugin = p;
}
@ -142,46 +140,6 @@ public class Util {
return fin;
}
/**
* Determines if a location is in the island world or not or
* in the new nether if it is activated
* @param loc - location
* @return true if in the island world
*/
public static boolean inWorld(Location loc) {
if (loc != null) {
if (loc.getWorld().equals(plugin.getIslandWorldManager().getIslandWorld())) {
return true;
}
if (plugin.getSettings().isNetherIslands() && loc.getWorld().equals(plugin.getIslandWorldManager().getNetherWorld())) {
return true;
}
if (plugin.getSettings().isEndIslands() && loc.getWorld().equals(plugin.getIslandWorldManager().getEndWorld())) {
return true;
}
}
return false;
}
/**
* Determines if an entity is in the island world or not or
* in the new nether if it is activated
* @param entity
* @return true if in world
*/
public static boolean inWorld(Entity entity) {
return inWorld(entity.getLocation());
}
/**
* Determines if a block is in the island world or not
* @param block
* @return true if in the island world
*/
public static boolean inWorld(Block block) {
return inWorld(block.getLocation());
}
/**
* Return a list of online players this player can see, i.e. are not invisible
* @param user - the User - if null, all player names on the server are shown
@ -256,4 +214,18 @@ public class Util {
return location.getBlockX() + "," + location.getBlockY() + "," + location.getBlockZ();
}
/**
* Checks is world = world2 irrespective of the world type
* @param world
* @param world2
* @return true if the same
*/
public static boolean sameWorld(World world, World world2) {
String worldName = world.getName().replaceAll("_nether", "").replaceAll("_the_end", "");
String world2Name = world2.getName().replaceAll("_nether", "").replaceAll("_the_end", "");
return worldName.equalsIgnoreCase(world2Name);
}
}

View File

@ -146,6 +146,7 @@ public class TestBSkyBlock {
Mockito.when(iwm.getIslandWorld()).thenReturn(world);
Mockito.when(iwm.getNetherWorld()).thenReturn(world);
Mockito.when(iwm.getEndWorld()).thenReturn(world);
when(iwm.inWorld(any())).thenReturn(true);
// Islands

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.commands.admin;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.commands.admin.teams;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

View File

@ -31,7 +31,6 @@ import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.commands.admin.teams.AdminTeamKickCommand;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.managers.CommandsManager;

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.commands.admin.teams;
import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

View File

@ -3,7 +3,9 @@
*/
package us.tastybento.bskyblock.database.objects;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.UUID;

View File

@ -6,6 +6,7 @@ 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.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -88,6 +89,7 @@ public class NetherPortalsTest {
when(iwm.getEndWorld()).thenReturn(end);
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getNetherWorld()).thenReturn(nether);
when(iwm.inWorld(any())).thenReturn(true);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
// Settings
@ -136,10 +138,6 @@ public class NetherPortalsTest {
// 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);
}
/**
@ -287,7 +285,7 @@ public class NetherPortalsTest {
// Right cause, end exists, wrong world
when(loc.getWorld()).thenReturn(mock(World.class));
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
when(iwm.inWorld(any())).thenReturn(false);
PlayerPortalEvent e = new PlayerPortalEvent(null, loc, null, null, TeleportCause.END_PORTAL);
when(s.isEndGenerate()).thenReturn(true);
np.onEndIslandPortal(e);
@ -330,12 +328,12 @@ public class NetherPortalsTest {
Location from = mock(Location.class);
when(from.getWorld()).thenReturn(mock(World.class));
// Not in world
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
when(iwm.inWorld(any())).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);
when(iwm.inWorld(any())).thenReturn(true);
e = new EntityPortalEvent(ent, from, null, null);
np.onEntityPortal(e);
assertTrue(e.isCancelled());
@ -362,7 +360,7 @@ public class NetherPortalsTest {
Location from = mock(Location.class);
when(from.getWorld()).thenReturn(mock(World.class));
// Not in world
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
when(iwm.inWorld(any())).thenReturn(false);
EntityExplodeEvent e = new EntityExplodeEvent(en, from, affectedBlocks, 0);
@ -387,8 +385,6 @@ public class NetherPortalsTest {
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));
}
@ -408,9 +404,7 @@ public class NetherPortalsTest {
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);
Location from = mock(Location.class);
// In world, in nether, nether islands
when(from.getWorld()).thenReturn(nether);
@ -438,9 +432,7 @@ public class NetherPortalsTest {
List<Block> affectedBlocks = new ArrayList<>();
affectedBlocks.add(block);
Location from = mock(Location.class);
// In world
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
Location from = mock(Location.class);
// In world, in nether, nether islands
when(from.getWorld()).thenReturn(nether);
when(s.isNetherIslands()).thenReturn(false);
@ -464,8 +456,6 @@ public class NetherPortalsTest {
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);
@ -495,13 +485,6 @@ public class NetherPortalsTest {
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);
@ -538,7 +521,7 @@ public class NetherPortalsTest {
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);
when(iwm.inWorld(any())).thenReturn(false);
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
assertFalse(np.onNetherPortal(e));
}

View File

@ -18,7 +18,6 @@ import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Slime;
import org.bukkit.entity.WitherSkeleton;
@ -61,6 +60,7 @@ public class FireListenerTest {
private static Zombie zombie;
private static Slime slime;
private static Cow cow;
private static IslandWorld iwm;
@BeforeClass
public static void setUp() {
@ -98,16 +98,14 @@ public class FireListenerTest {
// Worlds
IslandWorld iwm = mock(IslandWorld.class);
iwm = mock(IslandWorld.class);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getNetherWorld()).thenReturn(world);
when(iwm.getEndWorld()).thenReturn(world);
MobSpawnListener listener = mock(MobSpawnListener.class);
when(listener.inWorld(any(Location.class))).thenReturn(true);
when(listener.inWorld(any(Entity.class))).thenReturn(true);
when(iwm.inWorld(any())).thenReturn(true);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
// Monsters and animals
zombie = mock(Zombie.class);
when(zombie.getLocation()).thenReturn(location);
@ -349,7 +347,7 @@ public class FireListenerTest {
// Obsidian is not TNT
assertFalse(listener.onTNTPrimed(e));
// Out of world
when(block.getLocation()).thenReturn(null);
when(iwm.inWorld(any())).thenReturn(false);
assertFalse(listener.onTNTPrimed(e));
// Now set to TNT
@ -357,7 +355,7 @@ public class FireListenerTest {
assertFalse(listener.onTNTPrimed(e));
// Back in world
when(block.getLocation()).thenReturn(location);
when(iwm.inWorld(any())).thenReturn(true);
// Disallow fire
when(island.isAllowed(Mockito.any())).thenReturn(false);

View File

@ -14,7 +14,6 @@ import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.entity.Cow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Zombie;
@ -23,6 +22,7 @@ import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.inventory.ItemFactory;
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;
@ -50,9 +50,11 @@ public class MobSpawnListenerTest {
private static Zombie zombie;
private static Slime slime;
private static Cow cow;
private static IslandWorld iwm;
private static World world;
@BeforeClass
public static void setUp() {
public static void setUpBeforeClass() {
// Set up plugin
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
@ -61,7 +63,7 @@ public class MobSpawnListenerTest {
when(plugin.getIslands()).thenReturn(im);
Server server = mock(Server.class);
World world = mock(World.class);
world = mock(World.class);
when(server.getLogger()).thenReturn(Logger.getAnonymousLogger());
when(server.getWorld("world")).thenReturn(world);
when(server.getVersion()).thenReturn("BSB_Mocking");
@ -88,18 +90,6 @@ public class MobSpawnListenerTest {
flagsManager = new FlagsManager(plugin);
when(plugin.getFlagsManager()).thenReturn(flagsManager);
// Worlds
IslandWorld iwm = mock(IslandWorld.class);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getNetherWorld()).thenReturn(world);
when(iwm.getEndWorld()).thenReturn(world);
MobSpawnListener listener = mock(MobSpawnListener.class);
when(listener.inWorld(any(Location.class))).thenReturn(true);
when(listener.inWorld(any(Entity.class))).thenReturn(true);
// Monsters and animals
zombie = mock(Zombie.class);
when(zombie.getLocation()).thenReturn(location);
@ -109,10 +99,24 @@ public class MobSpawnListenerTest {
when(cow.getLocation()).thenReturn(location);
}
@Before
public void setUp() {
// Worlds
iwm = mock(IslandWorld.class);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getNetherWorld()).thenReturn(world);
when(iwm.getEndWorld()).thenReturn(world);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
}
@Test
public void testNotInWorld() {
when(iwm.inWorld(any(Location.class))).thenReturn(false);
IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
Island island = mock(Island.class);

View File

@ -1,6 +1,7 @@
package us.tastybento.bskyblock.listeners.flags;
import static org.junit.Assert.*;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

View File

@ -6,6 +6,7 @@ package us.tastybento.bskyblock.listeners.protection;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -44,6 +45,7 @@ import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.database.objects.Island;
import us.tastybento.bskyblock.generators.IslandWorld;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
@ -65,6 +67,7 @@ public class FlyingMobEventsTest {
private PlayersManager pm;
private UUID notUUID;
private BukkitScheduler sch;
private IslandWorld iwm;
/**
* @throws java.lang.Exception
@ -115,10 +118,11 @@ public class FlyingMobEventsTest {
// 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);
// Worlds
iwm = mock(IslandWorld.class);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
when(iwm.inWorld(any())).thenReturn(true);
}
/**
@ -140,7 +144,7 @@ public class FlyingMobEventsTest {
LivingEntity le = mock(LivingEntity.class);
CreatureSpawnEvent e = new CreatureSpawnEvent(le, SpawnReason.BUILD_WITHER);
// Not in world
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(false);
when(iwm.inWorld(any())).thenReturn(false);
fme.onMobSpawn(e);
Mockito.verify(im, Mockito.never()).getIslandAt(Mockito.any(Location.class));
}
@ -198,12 +202,12 @@ public class FlyingMobEventsTest {
// Not in world
Entity ent = mock(Entity.class);
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(false);
when(iwm.inWorld(any())).thenReturn(false);
e = new EntityExplodeEvent(ent, null, null, 0);
assertFalse(fme.onMobExplosion(e));
// Unknown entity (not in the list)
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
when(iwm.inWorld(any())).thenReturn(true);
assertFalse(fme.onMobExplosion(e));
}
@ -233,8 +237,6 @@ public class FlyingMobEventsTest {
affectedBlocks.add(block);
// Create event
EntityExplodeEvent e = new EntityExplodeEvent(le, mock(Location.class), affectedBlocks, 0);
// In world
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
// Nothing blocked
assertFalse(fme.onMobExplosion(e));
assertFalse(e.isCancelled());
@ -267,8 +269,6 @@ public class FlyingMobEventsTest {
affectedBlocks.add(block);
// Create event
EntityExplodeEvent e = new EntityExplodeEvent(le, mock(Location.class), affectedBlocks, 0);
// In world
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
// Blocked
assertTrue(fme.onMobExplosion(e));
assertTrue(e.isCancelled());
@ -296,8 +296,6 @@ public class FlyingMobEventsTest {
// Make the wither explode
// Create event
ExplosionPrimeEvent e = new ExplosionPrimeEvent(le, 0, false);
// In world
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
// Blocked
assertTrue(fme.onWitherExplode(e));
assertTrue(e.isCancelled());
@ -328,8 +326,6 @@ public class FlyingMobEventsTest {
// Create event
ExplosionPrimeEvent e = new ExplosionPrimeEvent(skull, 0, false);
// In world
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
// Blocked
assertTrue(fme.onWitherExplode(e));
assertTrue(e.isCancelled());
@ -364,8 +360,6 @@ public class FlyingMobEventsTest {
*/
@SuppressWarnings("deprecation")
EntityChangeBlockEvent e = new EntityChangeBlockEvent(wither, mock(Block.class), Material.AIR, (byte) 0);
// In world
when(Util.inWorld(Mockito.any(Entity.class))).thenReturn(true);
// Blocked
fme.onWitherChangeBlocks(e);
assertTrue(e.isCancelled());

View File

@ -66,6 +66,7 @@ public class IslandsManagerTest {
private Block space2;
private Location location;
private BlockState blockState;
private IslandWorld iwm;
/**
* @throws java.lang.Exception
@ -148,6 +149,14 @@ public class IslandsManagerTest {
}
});
// Worlds
iwm = mock(IslandWorld.class);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getNetherWorld()).thenReturn(world);
when(iwm.getEndWorld()).thenReturn(world);
when(iwm.inWorld(any())).thenReturn(true);
}
@ -482,9 +491,6 @@ public class IslandsManagerTest {
when(ic.getIslandAt(Mockito.any(Location.class))).thenReturn(is);
PowerMockito.whenNew(IslandCache.class).withAnyArguments().thenReturn(ic);
PowerMockito.mockStatic(Util.class);
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
IslandsManager im = new IslandsManager(plugin);
// In world, correct island
Optional<Island> oi = Optional.ofNullable(is);
@ -495,7 +501,7 @@ public class IslandsManagerTest {
assertEquals(Optional.empty(), im.getIslandAt(new Location(world, 100000, 120, -100000)));
// not in world
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(false);
when(iwm.inWorld(any())).thenReturn(true);
assertEquals(Optional.empty(), im.getIslandAt(new Location(world, 100000, 120, -100000)));
assertEquals(Optional.empty(), im.getIslandAt(location));
@ -557,9 +563,6 @@ public class IslandsManagerTest {
PowerMockito.whenNew(IslandCache.class).withAnyArguments().thenReturn(ic);
// In world
PowerMockito.mockStatic(Util.class);
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
IslandsManager im = new IslandsManager(plugin);
Optional<Island> oi = Optional.ofNullable(is);
// In world, correct island
@ -654,9 +657,6 @@ public class IslandsManagerTest {
PowerMockito.whenNew(IslandCache.class).withAnyArguments().thenReturn(ic);
// In world
PowerMockito.mockStatic(Util.class);
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
IslandsManager im = new IslandsManager(plugin);
assertFalse(im.isIsland(null));
im.createIsland(location);
@ -770,9 +770,6 @@ public class IslandsManagerTest {
PowerMockito.whenNew(IslandCache.class).withAnyArguments().thenReturn(ic);
// In world
PowerMockito.mockStatic(Util.class);
when(Util.inWorld(Mockito.any(Location.class))).thenReturn(true);
when(is.onIsland(Mockito.any())).thenReturn(true);
Builder<UUID> members = new ImmutableSet.Builder<>();

View File

@ -8,6 +8,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -18,8 +19,6 @@ import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.util.Vector;
@ -84,6 +83,7 @@ public class PlayersManagerTest {
when(iwm.getEndWorld()).thenReturn(end);
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getNetherWorld()).thenReturn(nether);
when(iwm.inWorld(any())).thenReturn(true);
when(plugin.getIslandWorldManager()).thenReturn(iwm);
// Settings
@ -131,10 +131,6 @@ public class PlayersManagerTest {
// 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);
// Mock database
db = mock(BSBDatabase.class);

View File

@ -6,6 +6,7 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -51,6 +52,7 @@ public class IslandCacheTest {
when(iwm.getIslandWorld()).thenReturn(world);
when(iwm.getNetherWorld()).thenReturn(world);
when(iwm.getEndWorld()).thenReturn(world);
when(iwm.inWorld(any())).thenReturn(true);
// Mock up IslandsManager
IslandsManager im = mock(IslandsManager.class);
@ -175,8 +177,7 @@ public class IslandCacheTest {
// New cache
IslandCache ic = new IslandCache();
ic.addIsland(island);
// Check islands is in world
assertTrue(Util.inWorld(island.getCenter()));
// Check exact match for location
assertEquals(island, ic.getIslandAt(island.getCenter()));