Enhance mainclass

This commit is contained in:
DNx5 2015-09-13 23:41:28 +07:00
parent e85d8acc24
commit 7458224ada
3 changed files with 121 additions and 132 deletions

View File

@ -71,7 +71,7 @@ public class AuthMe extends JavaPlugin {
public Essentials ess; public Essentials ess;
public Location essentialsSpawn; public Location essentialsSpawn;
public MultiverseCore multiverse = null; public MultiverseCore multiverse = null;
public LookupService ls = null; public LookupService lookupService = null;
public CitizensCommunicator citizens; public CitizensCommunicator citizens;
public boolean isCitizensActive = false; public boolean isCitizensActive = false;
public boolean CombatTag = false; public boolean CombatTag = false;
@ -79,10 +79,10 @@ public class AuthMe extends JavaPlugin {
public boolean BungeeCord = false; public boolean BungeeCord = false;
public boolean antibotMod = false; public boolean antibotMod = false;
public boolean delayedAntiBot = true; public boolean delayedAntiBot = true;
public ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<String, BukkitTask>(); public ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<>();
public ConcurrentHashMap<String, Integer> captcha = new ConcurrentHashMap<String, Integer>(); public ConcurrentHashMap<String, Integer> captcha = new ConcurrentHashMap<>();
public ConcurrentHashMap<String, String> cap = new ConcurrentHashMap<String, String>(); public ConcurrentHashMap<String, String> cap = new ConcurrentHashMap<>();
public ConcurrentHashMap<String, String> realIp = new ConcurrentHashMap<String, String>(); public ConcurrentHashMap<String, String> realIp = new ConcurrentHashMap<>();
protected static String vgUrl = "http://monitor-1.verygames.net/api/?action=ipclean-real-ip&out=raw&ip=%IP%&port=%PORT%"; protected static String vgUrl = "http://monitor-1.verygames.net/api/?action=ipclean-real-ip&out=raw&ip=%IP%&port=%PORT%";
public static AuthMe getInstance() { public static AuthMe getInstance() {
@ -264,22 +264,19 @@ public class AuthMe extends JavaPlugin {
// Reload support hook // Reload support hook
if (Settings.reloadSupport) { if (Settings.reloadSupport) {
try { int playersOnline = Utils.getOnlinePlayers().size();
int playersOnline = Utils.getOnlinePlayers().length; if (database != null) {
if (database != null) { if (playersOnline < 1) {
if (playersOnline < 1) { database.purgeLogged();
database.purgeLogged(); } else {
} else { for (PlayerAuth auth : database.getLoggedPlayers()) {
for (PlayerAuth auth : database.getLoggedPlayers()) { if (auth == null)
if (auth == null) continue;
continue; auth.setLastLogin(new Date().getTime());
auth.setLastLogin(new Date().getTime()); database.updateSession(auth);
database.updateSession(auth); PlayerCache.getInstance().addPlayer(auth);
PlayerCache.getInstance().addPlayer(auth);
}
} }
} }
} catch (Exception ex) {
} }
} }
@ -318,7 +315,7 @@ public class AuthMe extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
// Save player data // Save player data
Player[] players = Utils.getOnlinePlayers(); Collection<? extends Player> players = Utils.getOnlinePlayers();
if (players != null) { if (players != null) {
for (Player player : players) { for (Player player : players) {
this.savePlayer(player); this.savePlayer(player);
@ -361,7 +358,7 @@ public class AuthMe extends JavaPlugin {
// Initialize and setup the database // Initialize and setup the database
public void setupDatabase() throws ClassNotFoundException, PoolInitializationException, SQLException { public void setupDatabase() throws ClassNotFoundException, PoolInitializationException, SQLException {
// Backend MYSQL - FILE - SQLITE - SQLITEHIKARI // Backend MYSQL - FILE - SQLITE - SQLITEHIKARI
int accounts = 0; int accounts;
switch (Settings.getDataSource) { switch (Settings.getDataSource) {
case FILE: case FILE:
database = new FlatFile(); database = new FlatFile();
@ -391,11 +388,7 @@ public class AuthMe extends JavaPlugin {
if (Settings.getDataSource == DataSource.DataSourceType.FILE) { if (Settings.getDataSource == DataSource.DataSourceType.FILE) {
Converter converter = new ForceFlatToSqlite(database, this); Converter converter = new ForceFlatToSqlite(database, this);
try { getServer().getScheduler().runTaskAsynchronously(this, converter);
Thread t = new Thread(converter);
t.start();
} catch (Exception e) {
}
ConsoleLogger.showError("FlatFile backend has been detected and is now deprecated, next time server starts up, it will be changed to SQLite... Conversion will be started Asynchronously, it will not drop down your performance !"); ConsoleLogger.showError("FlatFile backend has been detected and is now deprecated, next time server starts up, it will be changed to SQLite... Conversion will be started Asynchronously, it will not drop down your performance !");
ConsoleLogger.showError("If you want to keep FlatFile, set file again into config at backend, but this message and this change will appear again at the next restart"); ConsoleLogger.showError("If you want to keep FlatFile, set file again into config at backend, but this message and this change will appear again at the next restart");
} }
@ -431,29 +424,26 @@ public class AuthMe extends JavaPlugin {
// Check the version of the ChestShop plugin // Check the version of the ChestShop plugin
public void checkChestShop() { public void checkChestShop() {
if (Settings.legacyChestShop && server.getPluginManager().isPluginEnabled("ChestShop")) { if (Settings.legacyChestShop && server.getPluginManager().isPluginEnabled("ChestShop")) {
String rawver = com.Acrobot.ChestShop.ChestShop.getVersion();
double version;
try { try {
String rawver = com.Acrobot.ChestShop.ChestShop.getVersion(); version = Double.valueOf(rawver.split(" ")[0]);
double version = 0; } catch (NumberFormatException nfe) {
try { try {
version = Double.valueOf(rawver.split(" ")[0]); version = Double.valueOf(rawver.split("t")[0]);
} catch (NumberFormatException nfe) { } catch (NumberFormatException nfee) {
try { legacyChestShop = false;
version = Double.valueOf(rawver.split("t")[0]);
} catch (NumberFormatException nfee) {
legacyChestShop = false;
return;
}
}
if (version >= 3.813) {
return; return;
} }
if (version < 3.50) {
ConsoleLogger.showError("Please Update your ChestShop version! Bugs may occur!");
return;
}
legacyChestShop = true;
} catch (Exception e) {
} }
if (version >= 3.813) {
return;
}
if (version < 3.50) {
ConsoleLogger.showError("Please Update your ChestShop version! Bugs may occur!");
return;
}
legacyChestShop = true;
} else { } else {
legacyChestShop = false; legacyChestShop = false;
} }
@ -462,21 +452,18 @@ public class AuthMe extends JavaPlugin {
// Check PerWorldInventories version // Check PerWorldInventories version
public void checkPerWorldInventories() { public void checkPerWorldInventories() {
if (server.getPluginManager().isPluginEnabled("PerWorldInventories")) { if (server.getPluginManager().isPluginEnabled("PerWorldInventories")) {
double version = 0;
String ver = server.getPluginManager().getPlugin("PerWorldInventories").getDescription().getVersion();
try { try {
double version = 0; version = Double.valueOf(ver.split(" ")[0]);
String ver = server.getPluginManager().getPlugin("PerWorldInventories").getDescription().getVersion(); } catch (NumberFormatException nfe) {
try { try {
version = Double.valueOf(ver.split(" ")[0]); version = Double.valueOf(ver.split("t")[0]);
} catch (NumberFormatException nfe) { } catch (NumberFormatException ignore) {
try {
version = Double.valueOf(ver.split("t")[0]);
} catch (NumberFormatException nfee) {
}
} }
if (version < 1.57) { }
ConsoleLogger.showError("Please Update your PerWorldInventories version! INVENTORY WIPE may occur!"); if (version < 1.57) {
} ConsoleLogger.showError("Please Update your PerWorldInventories version! INVENTORY WIPE may occur!");
} catch (Exception e) {
} }
} }
} }
@ -554,34 +541,31 @@ public class AuthMe extends JavaPlugin {
if ((citizens.isNPC(player)) || (Utils.getInstance().isUnrestricted(player)) || (CombatTagComunicator.isNPC(player))) { if ((citizens.isNPC(player)) || (Utils.getInstance().isUnrestricted(player)) || (CombatTagComunicator.isNPC(player))) {
return; return;
} }
try { String name = player.getName().toLowerCase();
String name = player.getName().toLowerCase(); if (PlayerCache.getInstance().isAuthenticated(name) && !player.isDead() && Settings.isSaveQuitLocationEnabled) {
if (PlayerCache.getInstance().isAuthenticated(name) && !player.isDead() && Settings.isSaveQuitLocationEnabled) { final PlayerAuth auth = new PlayerAuth(player.getName().toLowerCase(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getWorld().getName(), player.getName());
final PlayerAuth auth = new PlayerAuth(player.getName().toLowerCase(), player.getLocation().getX(), player.getLocation().getY(), player.getLocation().getZ(), player.getWorld().getName(), player.getName()); database.updateQuitLoc(auth);
database.updateQuitLoc(auth);
}
if (LimboCache.getInstance().hasLimboPlayer(name)) {
LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name);
if (Settings.protectInventoryBeforeLogInEnabled) {
player.getInventory().setArmorContents(limbo.getArmour());
player.getInventory().setContents(limbo.getInventory());
}
if (!Settings.noTeleport) {
player.teleport(limbo.getLoc());
}
this.utils.addNormal(player, limbo.getGroup());
player.setOp(limbo.getOperator());
limbo.getTimeoutTaskId().cancel();
LimboCache.getInstance().deleteLimboPlayer(name);
if (this.playerBackup.doesCacheExist(player)) {
this.playerBackup.removeCache(player);
}
}
PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name);
player.saveData();
} catch (Exception ex) {
} }
if (LimboCache.getInstance().hasLimboPlayer(name)) {
LimboPlayer limbo = LimboCache.getInstance().getLimboPlayer(name);
if (Settings.protectInventoryBeforeLogInEnabled) {
player.getInventory().setArmorContents(limbo.getArmour());
player.getInventory().setContents(limbo.getInventory());
}
if (!Settings.noTeleport) {
player.teleport(limbo.getLoc());
}
this.utils.addNormal(player, limbo.getGroup());
player.setOp(limbo.getOperator());
limbo.getTimeoutTaskId().cancel();
LimboCache.getInstance().deleteLimboPlayer(name);
if (this.playerBackup.doesCacheExist(player)) {
this.playerBackup.removeCache(player);
}
}
PlayerCache.getInstance().removePlayer(name);
database.setUnlogged(name);
player.saveData();
} }
// Select the player to kick when a vip player join the server when full // Select the player to kick when a vip player join the server when full
@ -686,8 +670,12 @@ public class AuthMe extends JavaPlugin {
public void downloadGeoIp() { public void downloadGeoIp() {
ConsoleLogger.info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com"); ConsoleLogger.info("[LICENSE] This product uses data from the GeoLite API created by MaxMind, available at http://www.maxmind.com");
File file = new File(getDataFolder(), "GeoIP.dat"); File file = new File(getDataFolder(), "GeoIP.dat");
if (!file.exists()) { try {
try { if (file.exists()) {
if (lookupService == null) {
lookupService = new LookupService(file);
}
} else {
String url = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz"; String url = "http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz";
URL downloadUrl = new URL(url); URL downloadUrl = new URL(url);
URLConnection conn = downloadUrl.openConnection(); URLConnection conn = downloadUrl.openConnection();
@ -706,35 +694,26 @@ public class AuthMe extends JavaPlugin {
} }
output.close(); output.close();
input.close(); input.close();
} catch (Exception e) {
} }
} catch (Exception e) {
ConsoleLogger.writeStackTrace(e);
} }
} }
// TODO: Need to review the code below! // TODO: Need to review the code below!
public String getCountryCode(String ip) { public String getCountryCode(String ip) {
try { if (lookupService != null) {
if (ls == null) return lookupService.getCountry(ip).getCode();
ls = new LookupService(new File(getDataFolder(), "GeoIP.dat"));
String code = ls.getCountry(ip).getCode();
if (code != null && !code.isEmpty())
return code;
} catch (Exception e) {
} }
return null; return "--";
} }
public String getCountryName(String ip) { public String getCountryName(String ip) {
try { if (lookupService != null) {
if (ls == null) return lookupService.getCountry(ip).getName();
ls = new LookupService(new File(getDataFolder(), "GeoIP.dat"));
String code = ls.getCountry(ip).getName();
if (code != null && !code.isEmpty())
return code;
} catch (Exception e) {
} }
return null; return "N/A";
} }
public void switchAntiBotMod(boolean mode) { public void switchAntiBotMod(boolean mode) {
@ -765,20 +744,17 @@ public class AuthMe extends JavaPlugin {
} }
public String replaceAllInfos(String message, Player player) { public String replaceAllInfos(String message, Player player) {
try { int playersOnline = Utils.getOnlinePlayers().size();
int playersOnline = Utils.getOnlinePlayers().length; message = message.replace("&", "\u00a7");
message = message.replace("&", "\u00a7"); message = message.replace("{PLAYER}", player.getName());
message = message.replace("{PLAYER}", player.getName()); message = message.replace("{ONLINE}", "" + playersOnline);
message = message.replace("{ONLINE}", "" + playersOnline); message = message.replace("{MAXPLAYERS}", "" + this.getServer().getMaxPlayers());
message = message.replace("{MAXPLAYERS}", "" + this.getServer().getMaxPlayers()); message = message.replace("{IP}", getIP(player));
message = message.replace("{IP}", getIP(player)); message = message.replace("{LOGINS}", "" + PlayerCache.getInstance().getLogged());
message = message.replace("{LOGINS}", "" + PlayerCache.getInstance().getLogged()); message = message.replace("{WORLD}", player.getWorld().getName());
message = message.replace("{WORLD}", player.getWorld().getName()); message = message.replace("{SERVER}", this.getServer().getServerName());
message = message.replace("{SERVER}", this.getServer().getServerName()); message = message.replace("{VERSION}", this.getServer().getBukkitVersion());
message = message.replace("{VERSION}", this.getServer().getBukkitVersion()); message = message.replace("{COUNTRY}", this.getCountryName(getIP(player)));
message = message.replace("{COUNTRY}", this.getCountryName(getIP(player)));
} catch (Exception e) {
}
return message; return message;
} }
@ -831,7 +807,7 @@ public class AuthMe extends JavaPlugin {
if (inputLine != null && !inputLine.isEmpty() && !inputLine.equalsIgnoreCase("error") && !inputLine.contains("error")) { if (inputLine != null && !inputLine.isEmpty() && !inputLine.equalsIgnoreCase("error") && !inputLine.contains("error")) {
realIP = inputLine; realIP = inputLine;
} }
} catch (Exception e) { } catch (Exception ignored) {
} }
return realIP; return realIP;
} }

View File

@ -11,21 +11,32 @@ import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
public class Utils { public class Utils {
private static boolean getOnlinePlayersIsCollection;
private String currentGroup; private String currentGroup;
private static Utils singleton; private static Utils singleton;
private static Method getOnlinePlayers;
public final AuthMe plugin; public final AuthMe plugin;
public Utils(AuthMe plugin) { public Utils(AuthMe plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
static {
try {
Method m = Bukkit.class.getDeclaredMethod("getOnlinePlayers");
getOnlinePlayersIsCollection = m.getReturnType() == Collection.class;
} catch (Exception ignored) {
}
}
public void setGroup(Player player, groupType group) { public void setGroup(Player player, groupType group) {
setGroup(player.getName(), group); setGroup(player.getName(), group);
} }
@ -182,19 +193,21 @@ public class Utils {
} }
} }
public static Player[] getOnlinePlayers() { public static Collection<? extends Player> getOnlinePlayers() {
Player[] players; if (getOnlinePlayersIsCollection) {
try { return Bukkit.getOnlinePlayers();
Method m = Bukkit.class.getMethod("getOnlinePlayers");
if (m.getReturnType() == Collection.class) {
players = (Player[]) ((Collection<?>) m.invoke(null)).toArray();
} else {
players = ((Player[]) m.invoke(null));
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
// can never happen
players = null;
} }
return players; try {
if (getOnlinePlayers == null) {
getOnlinePlayers = Bukkit.class.getMethod("getOnlinePlayers");
}
Object obj = getOnlinePlayers.invoke(null);
if (obj instanceof Collection) {
return (Collection) obj;
}
return Arrays.asList((Player[]) obj);
} catch (Exception ignored) {
}
return Collections.emptyList();
} }
} }

View File

@ -572,7 +572,7 @@ public class AuthMePlayerListener implements Listener {
return; return;
} }
int playersOnline = Utils.getOnlinePlayers().length; int playersOnline = Utils.getOnlinePlayers().size();
if (playersOnline > plugin.getServer().getMaxPlayers()) { if (playersOnline > plugin.getServer().getMaxPlayers()) {
event.allow(); event.allow();
return; return;