Move getOnlinePlayers() from Utils to BukkitService; delete Wrapper
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -19,9 +25,12 @@ public class BukkitService {
|
||||
public static final int TICKS_PER_MINUTE = 60 * TICKS_PER_SECOND;
|
||||
|
||||
private final AuthMe authMe;
|
||||
private final boolean getOnlinePlayersIsCollection;
|
||||
private Method getOnlinePlayers;
|
||||
|
||||
public BukkitService(AuthMe authMe) {
|
||||
this.authMe = authMe;
|
||||
getOnlinePlayersIsCollection = initializeOnlinePlayersIsCollectionField();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,6 +70,20 @@ public class BukkitService {
|
||||
return Bukkit.getScheduler().runTask(authMe, task);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a task that will run after the specified number of server
|
||||
* ticks.
|
||||
*
|
||||
* @param task the task to be run
|
||||
* @param delay the ticks to wait before running the task
|
||||
* @return a BukkitTask that contains the id number
|
||||
* @throws IllegalArgumentException if plugin is null
|
||||
* @throws IllegalArgumentException if task is null
|
||||
*/
|
||||
public BukkitTask runTaskLater(Runnable task, long delay) {
|
||||
return Bukkit.getScheduler().runTaskLater(authMe, task, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* <b>Asynchronous tasks should never access any API in Bukkit. Great care
|
||||
* should be taken to assure the thread-safety of asynchronous tasks.</b>
|
||||
@@ -102,8 +125,60 @@ public class BukkitService {
|
||||
* @return a set containing banned players
|
||||
*/
|
||||
public Set<OfflinePlayer> getBannedPlayers() {
|
||||
Bukkit.getBannedPlayers();
|
||||
return authMe.getServer().getBannedPlayers();
|
||||
return Bukkit.getBannedPlayers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe way to retrieve the list of online players from the server. Depending on the
|
||||
* implementation of the server, either an array of {@link Player} instances is being returned,
|
||||
* or a Collection. Always use this wrapper to retrieve online players instead of {@link
|
||||
* Bukkit#getOnlinePlayers()} directly.
|
||||
*
|
||||
* @return collection of online players
|
||||
*
|
||||
* @see <a href="https://www.spigotmc.org/threads/solved-cant-use-new-getonlineplayers.33061/">SpigotMC
|
||||
* forum</a>
|
||||
* @see <a href="http://stackoverflow.com/questions/32130851/player-changed-from-array-to-collection">StackOverflow</a>
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Collection<? extends Player> getOnlinePlayers() {
|
||||
if (getOnlinePlayersIsCollection) {
|
||||
return Bukkit.getOnlinePlayers();
|
||||
}
|
||||
try {
|
||||
// The lookup of a method via Reflections is rather expensive, so we keep a reference to it
|
||||
if (getOnlinePlayers == null) {
|
||||
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
}
|
||||
Object obj = getOnlinePlayers.invoke(null);
|
||||
if (obj instanceof Collection<?>) {
|
||||
return (Collection<? extends Player>) obj;
|
||||
} else if (obj instanceof Player[]) {
|
||||
return Arrays.asList((Player[]) obj);
|
||||
} else {
|
||||
String type = (obj != null) ? obj.getClass().getName() : "null";
|
||||
ConsoleLogger.showError("Unknown list of online players of type " + type);
|
||||
}
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
ConsoleLogger.logException("Could not retrieve list of online players:", e);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method run upon initialization to verify whether or not the Bukkit implementation
|
||||
* returns the online players as a Collection.
|
||||
*
|
||||
* @see #getOnlinePlayers()
|
||||
*/
|
||||
private static boolean initializeOnlinePlayersIsCollectionField() {
|
||||
try {
|
||||
Method method = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
return method.getReturnType() == Collection.class;
|
||||
} catch (NoSuchMethodException e) {
|
||||
ConsoleLogger.showError("Error verifying if getOnlinePlayers is a collection! Method doesn't exist");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import com.maxmind.geoip.LookupService;
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.settings.Settings;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
@@ -37,7 +37,8 @@ public class GeoLiteAPI {
|
||||
if (lookupService != null) {
|
||||
return true;
|
||||
}
|
||||
final File data = new File(Settings.PLUGIN_FOLDER, "GeoIP.dat");
|
||||
final File pluginFolder = AuthMe.getInstance().getDataFolder();
|
||||
final File data = new File(pluginFolder, "GeoIP.dat");
|
||||
boolean dataIsOld = (System.currentTimeMillis() - data.lastModified()) > TimeUnit.DAYS.toMillis(30);
|
||||
if (dataIsOld && !data.delete()) {
|
||||
ConsoleLogger.showError("Failed to delete GeoLiteAPI database");
|
||||
|
||||
@@ -14,31 +14,16 @@ import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Utility class for various operations used in the codebase.
|
||||
*/
|
||||
public final class Utils {
|
||||
|
||||
private static AuthMe plugin;
|
||||
private static Wrapper wrapper;
|
||||
|
||||
private static boolean getOnlinePlayersIsCollection = false;
|
||||
private static Method getOnlinePlayers;
|
||||
|
||||
static {
|
||||
wrapper = Wrapper.getInstance();
|
||||
plugin = wrapper.getAuthMe();
|
||||
initializeOnlinePlayersIsCollectionField();
|
||||
}
|
||||
private static AuthMe plugin = AuthMe.getInstance();
|
||||
|
||||
private Utils() {
|
||||
// Utility class
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,12 +152,12 @@ public final class Utils {
|
||||
final World world = theWorld;
|
||||
final Location loc = new Location(world, x, y, z);
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(wrapper.getAuthMe(), new Runnable() {
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(pl, loc);
|
||||
wrapper.getServer().getPluginManager().callEvent(tpEvent);
|
||||
plugin.getServer().getPluginManager().callEvent(tpEvent);
|
||||
if (!tpEvent.isCancelled()) {
|
||||
pl.teleport(tpEvent.getTo());
|
||||
}
|
||||
@@ -180,58 +165,6 @@ public final class Utils {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe way to retrieve the list of online players from the server. Depending on the
|
||||
* implementation of the server, either an array of {@link Player} instances is being returned,
|
||||
* or a Collection. Always use this wrapper to retrieve online players instead of {@link
|
||||
* Bukkit#getOnlinePlayers()} directly.
|
||||
*
|
||||
* @return collection of online players
|
||||
*
|
||||
* @see <a href="https://www.spigotmc.org/threads/solved-cant-use-new-getonlineplayers.33061/">SpigotMC
|
||||
* forum</a>
|
||||
* @see <a href="http://stackoverflow.com/questions/32130851/player-changed-from-array-to-collection">StackOverflow</a>
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Collection<? extends Player> getOnlinePlayers() {
|
||||
if (getOnlinePlayersIsCollection) {
|
||||
return Bukkit.getOnlinePlayers();
|
||||
}
|
||||
try {
|
||||
// The lookup of a method via Reflections is rather expensive, so we keep a reference to it
|
||||
if (getOnlinePlayers == null) {
|
||||
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
}
|
||||
Object obj = getOnlinePlayers.invoke(null);
|
||||
if (obj instanceof Collection<?>) {
|
||||
return (Collection<? extends Player>) obj;
|
||||
} else if (obj instanceof Player[]) {
|
||||
return Arrays.asList((Player[]) obj);
|
||||
} else {
|
||||
String type = (obj != null) ? obj.getClass().getName() : "null";
|
||||
ConsoleLogger.showError("Unknown list of online players of type " + type);
|
||||
}
|
||||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
|
||||
ConsoleLogger.logException("Could not retrieve list of online players:", e);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Method run when the Utils class is loaded to verify whether or not the Bukkit implementation
|
||||
* returns the online players as a Collection.
|
||||
*
|
||||
* @see Utils#getOnlinePlayers()
|
||||
*/
|
||||
private static void initializeOnlinePlayersIsCollectionField() {
|
||||
try {
|
||||
Method method = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
getOnlinePlayersIsCollection = method.getReturnType() == Collection.class;
|
||||
} catch (NoSuchMethodException e) {
|
||||
ConsoleLogger.showError("Error verifying if getOnlinePlayers is a collection! Method doesn't exist");
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isNPC(Player player) {
|
||||
return player.hasMetadata("NPC") || plugin.getPluginHooks().isNpcInCombatTagPlus(player);
|
||||
}
|
||||
@@ -240,7 +173,7 @@ public final class Utils {
|
||||
if (Settings.isTeleportToSpawnEnabled && !Settings.noTeleport) {
|
||||
Location spawn = plugin.getSpawnLocation(player);
|
||||
AuthMeTeleportEvent tpEvent = new AuthMeTeleportEvent(player, spawn);
|
||||
wrapper.getServer().getPluginManager().callEvent(tpEvent);
|
||||
plugin.getServer().getPluginManager().callEvent(tpEvent);
|
||||
if (!tpEvent.isCancelled()) {
|
||||
player.teleport(tpEvent.getTo());
|
||||
}
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
package fr.xephi.authme.util;
|
||||
|
||||
import fr.xephi.authme.AuthMe;
|
||||
import fr.xephi.authme.cache.auth.PlayerCache;
|
||||
import fr.xephi.authme.output.Messages;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Wrapper for the retrieval of common singletons used throughout the application.
|
||||
* This class simply delegates the calls.
|
||||
*/
|
||||
public class Wrapper {
|
||||
|
||||
private static Wrapper singleton;
|
||||
|
||||
/**
|
||||
* Package-private constructor for testing purposes to inject a mock instance.
|
||||
*/
|
||||
Wrapper() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Package-private setter of the singleton field used for tests to inject a mock instance.
|
||||
*
|
||||
* @param wrapper The wrapper to use as singleton
|
||||
*/
|
||||
static void setSingleton(Wrapper wrapper) {
|
||||
Wrapper.singleton = wrapper;
|
||||
}
|
||||
|
||||
public static Wrapper getInstance() {
|
||||
if (singleton == null) {
|
||||
singleton = new Wrapper();
|
||||
}
|
||||
return singleton;
|
||||
}
|
||||
|
||||
public AuthMe getAuthMe() {
|
||||
return AuthMe.getInstance();
|
||||
}
|
||||
|
||||
public Server getServer() {
|
||||
return getAuthMe().getServer();
|
||||
}
|
||||
|
||||
public Messages getMessages() {
|
||||
return getAuthMe().getMessages();
|
||||
}
|
||||
|
||||
public PlayerCache getPlayerCache() {
|
||||
return PlayerCache.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the folder containing plugin data via the AuthMe instance.
|
||||
*
|
||||
* @return The plugin data folder
|
||||
* @see AuthMe#getDataFolder()
|
||||
*/
|
||||
public File getDataFolder() {
|
||||
return getAuthMe().getDataFolder();
|
||||
}
|
||||
|
||||
public BukkitScheduler getScheduler() {
|
||||
return Bukkit.getScheduler();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user