Stuff from the common floobits workspace
Author: AuthMe-Team <AuthMeTeam@123NoEmail.com>
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
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.*;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
public class GeoLiteAPI {
|
||||
|
||||
private static final String GEOIP_URL = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry" +
|
||||
"/GeoIP.dat.gz";
|
||||
private static final AuthMe plugin = AuthMe.getInstance();
|
||||
private static LookupService lookupService;
|
||||
|
||||
/**
|
||||
* Download (if absent) the GeoIpLite data file and then try to load it.
|
||||
*
|
||||
* @return Boolean True if the data is available, false if not.
|
||||
*/
|
||||
public static boolean isDataAvailable() {
|
||||
if (lookupService != null) {
|
||||
return true;
|
||||
}
|
||||
final File data = new File(Settings.PLUGIN_FOLDER, "GeoIP.dat");
|
||||
if (data.exists()) {
|
||||
try {
|
||||
lookupService = new LookupService(data);
|
||||
plugin.getLogger().info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, " +
|
||||
"available at http://www.maxmind.com");
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Ok, let's try to download the data file!
|
||||
plugin.getGameServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
URL downloadUrl = new URL(GEOIP_URL);
|
||||
URLConnection conn = downloadUrl.openConnection();
|
||||
conn.setConnectTimeout(10000);
|
||||
conn.connect();
|
||||
InputStream input = conn.getInputStream();
|
||||
if (conn.getURL().toString().endsWith(".gz")) {
|
||||
input = new GZIPInputStream(input);
|
||||
}
|
||||
OutputStream output = new FileOutputStream(data);
|
||||
byte[] buffer = new byte[2048];
|
||||
int length = input.read(buffer);
|
||||
while (length >= 0) {
|
||||
output.write(buffer, 0, length);
|
||||
length = input.read(buffer);
|
||||
}
|
||||
output.close();
|
||||
input.close();
|
||||
} catch (IOException e) {
|
||||
ConsoleLogger.writeStackTrace(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the country code of the given IP address.
|
||||
*
|
||||
* @param ip Ip address
|
||||
* @return String
|
||||
*/
|
||||
public static String getCountryCode(String ip) {
|
||||
if (isDataAvailable()) {
|
||||
return lookupService.getCountry(ip).getCode();
|
||||
}
|
||||
return "--";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the country name of the given IP address.
|
||||
*
|
||||
* @param ip Ip address
|
||||
* @return String
|
||||
*/
|
||||
public static String getCountryName(String ip) {
|
||||
if (isDataAvailable()) {
|
||||
return lookupService.getCountry(ip).getName();
|
||||
}
|
||||
return "N/A";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -7,9 +7,13 @@ import java.text.DecimalFormat;
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
public class Profiler {
|
||||
|
||||
/** Defines the past time in milliseconds. */
|
||||
/**
|
||||
* Defines the past time in milliseconds.
|
||||
*/
|
||||
private long time = 0;
|
||||
/** Defines the time in milliseconds the profiler last started at. */
|
||||
/**
|
||||
* Defines the time in milliseconds the profiler last started at.
|
||||
*/
|
||||
private long start = -1;
|
||||
|
||||
/**
|
||||
@@ -26,19 +30,19 @@ public class Profiler {
|
||||
*/
|
||||
public Profiler(boolean start) {
|
||||
// Should the timer be started
|
||||
if(start)
|
||||
if (start)
|
||||
start();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the profiler.
|
||||
*
|
||||
|
||||
* @return True if the profiler was started, false otherwise possibly due to an error.
|
||||
* True will also be returned if the profiler was started already. */
|
||||
* True will also be returned if the profiler was started already.
|
||||
*/
|
||||
public boolean start() {
|
||||
// Make sure the timer isn't started already
|
||||
if(isActive())
|
||||
if (isActive())
|
||||
return true;
|
||||
|
||||
// Set the start time
|
||||
@@ -49,11 +53,11 @@ public class Profiler {
|
||||
/**
|
||||
* This will start the profiler if it's not active, or will stop the profiler if it's currently active.
|
||||
*
|
||||
|
||||
* @return True if the profiler has been started, false if the profiler has been stopped. */
|
||||
* @return True if the profiler has been started, false if the profiler has been stopped.
|
||||
*/
|
||||
public boolean pause() {
|
||||
// Toggle the profiler state
|
||||
if(isStarted())
|
||||
if (isStarted())
|
||||
stop();
|
||||
else
|
||||
start();
|
||||
@@ -65,12 +69,12 @@ public class Profiler {
|
||||
/**
|
||||
* Stop the profiler if it's active.
|
||||
*
|
||||
|
||||
* @return True will be returned if the profiler was stopped while it was active. False will be returned if the
|
||||
* profiler was stopped already. */
|
||||
* profiler was stopped already.
|
||||
*/
|
||||
public boolean stop() {
|
||||
// Make sure the profiler is active
|
||||
if(!isActive())
|
||||
if (!isActive())
|
||||
return false;
|
||||
|
||||
// Stop the profiler, calculate the passed time
|
||||
@@ -82,8 +86,8 @@ public class Profiler {
|
||||
/**
|
||||
* Check whether the profiler has been started. The profiler doesn't need to be active right now.
|
||||
*
|
||||
|
||||
* @return True if the profiler was started, false otherwise. */
|
||||
* @return True if the profiler was started, false otherwise.
|
||||
*/
|
||||
public boolean isStarted() {
|
||||
return isActive() || this.time > 0;
|
||||
}
|
||||
@@ -91,8 +95,8 @@ public class Profiler {
|
||||
/**
|
||||
* Check whether the profiler is currently active.
|
||||
*
|
||||
|
||||
* @return True if the profiler is active, false otherwise. */
|
||||
* @return True if the profiler is active, false otherwise.
|
||||
*/
|
||||
public boolean isActive() {
|
||||
return this.start >= 0;
|
||||
}
|
||||
@@ -100,11 +104,11 @@ public class Profiler {
|
||||
/**
|
||||
* Get the passed time in milliseconds.
|
||||
*
|
||||
|
||||
* @return The passed time in milliseconds. */
|
||||
* @return The passed time in milliseconds.
|
||||
*/
|
||||
public long getTime() {
|
||||
// Check whether the profiler is currently active
|
||||
if(isActive())
|
||||
if (isActive())
|
||||
return this.time + (System.currentTimeMillis() - this.start);
|
||||
return this.time;
|
||||
}
|
||||
@@ -112,18 +116,18 @@ public class Profiler {
|
||||
/**
|
||||
* Get the passed time in a formatted string.
|
||||
*
|
||||
|
||||
* @return The passed time in a formatted string. */
|
||||
* @return The passed time in a formatted string.
|
||||
*/
|
||||
public String getTimeFormatted() {
|
||||
// Get the passed time
|
||||
long time = getTime();
|
||||
|
||||
// Return the time if it's less than one millisecond
|
||||
if(time <= 0)
|
||||
if (time <= 0)
|
||||
return "<1 ms";
|
||||
|
||||
// Return the time in milliseconds
|
||||
if(time < 1000)
|
||||
if (time < 1000)
|
||||
return time + " ms";
|
||||
|
||||
// Convert the time into seconds with a single decimal
|
||||
|
||||
@@ -12,17 +12,18 @@ import java.io.StringWriter;
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
public static final String newline = System.getProperty("line.separator");
|
||||
|
||||
/**
|
||||
* Get the difference of two strings.
|
||||
*
|
||||
* @param first First string
|
||||
* @param first First string
|
||||
* @param second Second string
|
||||
*
|
||||
* @return The difference value
|
||||
*/
|
||||
public static double getDifference(String first, String second) {
|
||||
// Make sure the strings are valid.
|
||||
if(first == null || second == null)
|
||||
if (first == null || second == null)
|
||||
return 1.0;
|
||||
|
||||
// Create a string similarity service instance, to allow comparison
|
||||
@@ -35,21 +36,20 @@ public class StringUtils {
|
||||
/**
|
||||
* Returns whether the given string contains any of the provided elements.
|
||||
*
|
||||
* @param str the string to analyze
|
||||
* @param str the string to analyze
|
||||
* @param pieces the items to check the string for
|
||||
*
|
||||
* @return true if the string contains at least one of the items
|
||||
*/
|
||||
public static boolean containsAny(String str, String... pieces) {
|
||||
if (str == null) {
|
||||
return false;
|
||||
}
|
||||
for (String piece : pieces) {
|
||||
if (piece != null && str.contains(piece)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
if (str == null) {
|
||||
return false;
|
||||
}
|
||||
for (String piece : pieces) {
|
||||
if (piece != null && str.contains(piece)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +57,6 @@ public class StringUtils {
|
||||
* is trimmed, so this method also considers a string with whitespace as empty.
|
||||
*
|
||||
* @param str the string to verify
|
||||
*
|
||||
* @return true if the string is empty, false otherwise
|
||||
*/
|
||||
public static boolean isEmpty(String str) {
|
||||
@@ -68,8 +67,7 @@ public class StringUtils {
|
||||
* Joins a list of elements into a single string with the specified delimiter.
|
||||
*
|
||||
* @param delimiter the delimiter to use
|
||||
* @param elements the elements to join
|
||||
*
|
||||
* @param elements the elements to join
|
||||
* @return a new String that is composed of the elements separated by the delimiter
|
||||
*/
|
||||
public static String join(String delimiter, Iterable<String> elements) {
|
||||
@@ -91,7 +89,6 @@ public class StringUtils {
|
||||
* Get a full stack trace of an exception as a string.
|
||||
*
|
||||
* @param exception The exception.
|
||||
*
|
||||
* @return Stack trace as a string.
|
||||
*/
|
||||
public static String getStackTrace(Exception exception) {
|
||||
|
||||
@@ -95,45 +95,30 @@ public final class Utils {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getCountryCode(String ip) {
|
||||
if (checkGeoIP()) {
|
||||
return lookupService.getCountry(ip).getCode();
|
||||
}
|
||||
return "--";
|
||||
}
|
||||
|
||||
public static String getCountryName(String ip) {
|
||||
if (checkGeoIP()) {
|
||||
return lookupService.getCountry(ip).getName();
|
||||
}
|
||||
return "N/A";
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the group of a player, by its AuthMe group type.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param group The group type.
|
||||
*
|
||||
* @return True if succeed, false otherwise.
|
||||
* False is also returned if groups aren't supported with the current permissions system.
|
||||
* @param group The group type.
|
||||
* @return True if succeed, false otherwise. False is also returned if groups aren't supported
|
||||
* with the current permissions system.
|
||||
*/
|
||||
public static boolean setGroup(Player player, GroupType group) {
|
||||
// Check whether the permissions check is enabled
|
||||
if(!Settings.isPermissionCheckEnabled)
|
||||
if (!Settings.isPermissionCheckEnabled)
|
||||
return false;
|
||||
|
||||
// Get the permissions manager, and make sure it's valid
|
||||
PermissionsManager permsMan = plugin.getPermissionsManager();
|
||||
if(permsMan == null)
|
||||
if (permsMan == null)
|
||||
ConsoleLogger.showError("Failed to access permissions manager instance, shutting down.");
|
||||
assert permsMan != null;
|
||||
|
||||
// Make sure group support is available
|
||||
if(!permsMan.hasGroupSupport())
|
||||
if (!permsMan.hasGroupSupport())
|
||||
ConsoleLogger.showError("The current permissions system doesn't have group support, unable to set group!");
|
||||
|
||||
switch(group) {
|
||||
switch (group) {
|
||||
case UNREGISTERED:
|
||||
// Remove the other group type groups, set the current group
|
||||
permsMan.removeGroups(player, Arrays.asList(Settings.getRegisteredGroup, Settings.getUnloggedinGroup));
|
||||
@@ -152,7 +137,7 @@ public final class Utils {
|
||||
case LOGGEDIN:
|
||||
// Get the limbo player data
|
||||
LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(player.getName().toLowerCase());
|
||||
if(limbo == null)
|
||||
if (limbo == null)
|
||||
return false;
|
||||
|
||||
// Get the players group
|
||||
@@ -169,21 +154,20 @@ public final class Utils {
|
||||
|
||||
/**
|
||||
* TODO: This method requires better explanation.
|
||||
*
|
||||
* <p>
|
||||
* Set the normal group of a player.
|
||||
*
|
||||
* @param player The player.
|
||||
* @param group The normal group.
|
||||
|
||||
* @param group The normal group.
|
||||
* @return True on success, false on failure.
|
||||
*/
|
||||
public static boolean addNormal(Player player, String group) {
|
||||
if(!Settings.isPermissionCheckEnabled)
|
||||
if (!Settings.isPermissionCheckEnabled)
|
||||
return false;
|
||||
|
||||
// Get the permissions manager, and make sure it's valid
|
||||
PermissionsManager permsMan = plugin.getPermissionsManager();
|
||||
if(permsMan == null)
|
||||
if (permsMan == null)
|
||||
ConsoleLogger.showError("Failed to access permissions manager instance, shutting down.");
|
||||
assert permsMan != null;
|
||||
|
||||
@@ -221,10 +205,11 @@ public final class Utils {
|
||||
|
||||
/**
|
||||
* Method packCoords.
|
||||
* @param x double
|
||||
* @param y double
|
||||
* @param z double
|
||||
* @param w String
|
||||
*
|
||||
* @param x double
|
||||
* @param y double
|
||||
* @param z double
|
||||
* @param w String
|
||||
* @param pl Player
|
||||
*/
|
||||
public static void packCoords(double x, double y, double z, String w,
|
||||
@@ -265,40 +250,35 @@ public final class Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a given directory and all his content.
|
||||
*
|
||||
* @param directory File
|
||||
*/
|
||||
public enum GroupType {
|
||||
UNREGISTERED,
|
||||
REGISTERED,
|
||||
NOTLOGGEDIN,
|
||||
LOGGEDIN
|
||||
}
|
||||
|
||||
public static void purgeDirectory(File file) {
|
||||
if (!file.isDirectory()) {
|
||||
public static void purgeDirectory(File directory) {
|
||||
if (!directory.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
File[] files = file.listFiles();
|
||||
File[] files = directory.listFiles();
|
||||
if (files == null) {
|
||||
return;
|
||||
}
|
||||
for (File target : files) {
|
||||
if (target.isDirectory()) {
|
||||
purgeDirectory(target);
|
||||
target.delete();
|
||||
} else {
|
||||
target.delete();
|
||||
}
|
||||
target.delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* 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="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")
|
||||
@@ -309,7 +289,7 @@ public final class Utils {
|
||||
try {
|
||||
// The lookup of a method via Reflections is rather expensive, so we keep a reference to it
|
||||
if (getOnlinePlayers == null) {
|
||||
getOnlinePlayers = Bukkit.class.getMethod("getOnlinePlayers");
|
||||
getOnlinePlayers = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
|
||||
}
|
||||
Object obj = getOnlinePlayers.invoke(null);
|
||||
if (obj instanceof Collection<?>) {
|
||||
@@ -328,8 +308,8 @@ public final class Utils {
|
||||
}
|
||||
|
||||
/**
|
||||
* Method run when the Utils class is loaded to verify whether or not the Bukkit
|
||||
* implementation returns the online players as a Collection.
|
||||
* 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()
|
||||
*/
|
||||
@@ -373,4 +353,13 @@ public final class Utils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public enum GroupType {
|
||||
UNREGISTERED,
|
||||
REGISTERED,
|
||||
NOTLOGGEDIN,
|
||||
LOGGEDIN
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user