changed settings load method.

This commit is contained in:
DNx5 2015-09-14 20:46:56 +07:00
parent 7c56dec476
commit 0b23074770
5 changed files with 93 additions and 179 deletions

View File

@ -53,7 +53,7 @@ import java.util.zip.GZIPInputStream;
public class AuthMe extends JavaPlugin {
private static Server server;
private final Server server = getServer();
private static Logger authmeLogger = Logger.getLogger("AuthMe");
private static AuthMe authme;
public Management management;
@ -76,7 +76,6 @@ public class AuthMe extends JavaPlugin {
public boolean isCitizensActive = false;
public boolean CombatTag = false;
public boolean legacyChestShop = false;
public boolean BungeeCord = false;
public boolean antibotMod = false;
public boolean delayedAntiBot = true;
public ConcurrentHashMap<String, BukkitTask> sessions = new ConcurrentHashMap<>();
@ -93,14 +92,6 @@ public class AuthMe extends JavaPlugin {
return settings;
}
public DataSource getAuthMeDatabase() {
return database;
}
public void setAuthMeDatabase(DataSource database) {
this.database = database;
}
public void setMessages(Messages m) {
this.m = m;
}
@ -120,8 +111,6 @@ public class AuthMe extends JavaPlugin {
// TODO: split the plugin in more modules
// TODO: remove vault as hard dependency
server = getServer();
PluginManager pm = server.getPluginManager();
// Setup the Logger
@ -131,9 +120,11 @@ public class AuthMe extends JavaPlugin {
// TODO: new configuration style (more files)
try {
settings = new Settings(this);
settings.reload();
} catch (Exception e) {
ConsoleLogger.writeStackTrace(e);
ConsoleLogger.showError("Can't load the configuration file... Something went wrong, to avoid security issues the server will shutdown!");
this.getServer().shutdown();
server.shutdown();
return;
}
@ -232,7 +223,7 @@ public class AuthMe extends JavaPlugin {
// Connect to the database and setup tables
try {
setupDatabase();
} catch (ClassNotFoundException | SQLException | PoolInitializationException ex) {
} catch (Exception ex) {
ConsoleLogger.writeStackTrace(ex);
ConsoleLogger.showError("Fatal error occurred during database connection! Authme initialization ABORTED!");
stopOrUnload();
@ -346,7 +337,7 @@ public class AuthMe extends JavaPlugin {
public void stopOrUnload() {
if (Settings.isStopEnabled) {
ConsoleLogger.showError("THE SERVER IS GOING TO SHUTDOWN AS DEFINED IN THE CONFIGURATION!");
AuthMe.getInstance().getServer().shutdown();
server.shutdown();
} else {
server.getPluginManager().disablePlugin(AuthMe.getInstance());
}
@ -391,7 +382,7 @@ public class AuthMe extends JavaPlugin {
if (Settings.getDataSource == DataSource.DataSourceType.FILE) {
Converter converter = new ForceFlatToSqlite(database, this);
getServer().getScheduler().runTaskAsynchronously(this, converter);
server.getScheduler().runTaskAsynchronously(this, converter);
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");
}
@ -512,12 +503,12 @@ public class AuthMe extends JavaPlugin {
// Check the presence of CombatTag
public void checkCombatTag() {
this.CombatTag = this.getServer().getPluginManager().isPluginEnabled("CombatTag");
this.CombatTag = server.getPluginManager().isPluginEnabled("CombatTag");
}
// Check if Citizens is active
public void checkCitizens() {
this.isCitizensActive = this.getServer().getPluginManager().isPluginEnabled("Citizens");
this.isCitizensActive = server.getPluginManager().isPluginEnabled("Citizens");
}
// Check if a player/command sender have a permission
@ -703,8 +694,6 @@ public class AuthMe extends JavaPlugin {
}
}
// TODO: Need to review the code below!
public String getCountryCode(String ip) {
if (lookupService != null) {
return lookupService.getCountry(ip).getCode();
@ -751,12 +740,12 @@ public class AuthMe extends JavaPlugin {
message = message.replace("&", "\u00a7");
message = message.replace("{PLAYER}", player.getName());
message = message.replace("{ONLINE}", "" + playersOnline);
message = message.replace("{MAXPLAYERS}", "" + this.getServer().getMaxPlayers());
message = message.replace("{MAXPLAYERS}", "" + server.getMaxPlayers());
message = message.replace("{IP}", getIP(player));
message = message.replace("{LOGINS}", "" + PlayerCache.getInstance().getLogged());
message = message.replace("{WORLD}", player.getWorld().getName());
message = message.replace("{SERVER}", this.getServer().getServerName());
message = message.replace("{VERSION}", this.getServer().getBukkitVersion());
message = message.replace("{SERVER}", server.getServerName());
message = message.replace("{VERSION}", server.getBukkitVersion());
message = message.replace("{COUNTRY}", this.getCountryName(getIP(player)));
return message;
}

View File

@ -1,5 +1,11 @@
package fr.xephi.authme;
import fr.xephi.authme.settings.Settings;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.List;
import java.util.concurrent.Callable;
@ -7,13 +13,6 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import fr.xephi.authme.settings.Settings;
import net.milkbowl.vault.permission.Permission;
public class DataManager {
public AuthMe plugin;
@ -150,7 +149,7 @@ public class DataManager {
}
public synchronized void purgePermissions(List<String> cleared,
Permission permission) {
Permission permission) {
int i = 0;
for (String name : cleared) {
try {
@ -170,24 +169,19 @@ public class DataManager {
return true;
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Boolean> result = executor.submit(new Callable<Boolean>() {
@Override
public synchronized Boolean call() throws Exception {
Boolean result = null;
try {
for (OfflinePlayer op : Utils.getOnlinePlayers())
if (op.getName().equalsIgnoreCase(name)) {
result = true;
break;
}
} catch (Exception e) {
}
return result;
for (OfflinePlayer op : Utils.getOnlinePlayers())
if (op.getName().equalsIgnoreCase(name)) {
return true;
}
return false;
}
});
try {
return result.get().booleanValue();
return result.get();
} catch (Exception e) {
return (false);
return false;
} finally {
executor.shutdown();
}

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.commands;
import com.zaxxer.hikari.pool.PoolInitializationException;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.Utils;
@ -28,7 +27,6 @@ import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@ -110,48 +108,23 @@ public class AdminCommand implements CommandExecutor {
return true;
}
} else if (args[0].equalsIgnoreCase("reload")) {
plugin.getSettings().reload();
m.reloadMessages();
plugin.database.close();
try {
plugin.getSettings().reload();
m.reloadMessages();
plugin.database.close();
plugin.setupDatabase();
} catch (ClassNotFoundException nfe) {
} catch (Exception e) {
ConsoleLogger.showError("Fatal error occurred! Authme instance ABORTED!");
if (Settings.isStopEnabled) {
AuthMe.getInstance().getServer().shutdown();
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
} else {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
}
return false;
} catch (SQLException sqle) {
ConsoleLogger.showError("Fatal error occurred! Authme instance ABORTED!");
if (Settings.isStopEnabled) {
AuthMe.getInstance().getServer().shutdown();
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
} else {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
}
return false;
} catch (PoolInitializationException pie) {
ConsoleLogger.showError("Fatal error occurred! Authme instance ABORTED!");
if (Settings.isStopEnabled) {
AuthMe.getInstance().getServer().shutdown();
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
} else {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance());
}
plugin.stopOrUnload();
return false;
}
m.send(sender, "reload");
} else if (args[0].equalsIgnoreCase("lastlogin")) {
if (args.length != 2) {
sender.sendMessage("Usage: /authme lastlogin <playername>");
return true;
}
PlayerAuth auth = null;
PlayerAuth auth;
try {
auth = plugin.database.getAuth(args[1].toLowerCase());
} catch (NullPointerException e) {
@ -177,31 +150,30 @@ public class AdminCommand implements CommandExecutor {
return true;
}
if (!args[1].contains(".")) {
final CommandSender fSender = sender;
final String[] arguments = args;
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
PlayerAuth auth = null;
PlayerAuth auth;
String message = "[AuthMe] ";
try {
auth = plugin.database.getAuth(arguments[1].toLowerCase());
} catch (NullPointerException npe) {
m.send(fSender, "unknown_user");
m.send(sender, "unknown_user");
return;
}
if (auth == null) {
m.send(fSender, "unknown_user");
m.send(sender, "unknown_user");
return;
}
List<String> accountList = plugin.database.getAllAuthsByName(auth);
if (accountList == null || accountList.isEmpty()) {
m.send(fSender, "user_unknown");
m.send(sender, "user_unknown");
return;
}
if (accountList.size() == 1) {
fSender.sendMessage("[AuthMe] " + arguments[1] + " is a single account player");
sender.sendMessage("[AuthMe] " + arguments[1] + " is a single account player");
return;
}
int i = 0;
@ -214,13 +186,12 @@ public class AdminCommand implements CommandExecutor {
message = message + ".";
}
}
fSender.sendMessage("[AuthMe] " + arguments[1] + " has " + String.valueOf(accountList.size()) + " accounts");
fSender.sendMessage(message);
sender.sendMessage("[AuthMe] " + arguments[1] + " has " + String.valueOf(accountList.size()) + " accounts");
sender.sendMessage(message);
}
});
return true;
} else {
final CommandSender fSender = sender;
final String[] arguments = args;
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@ -228,16 +199,16 @@ public class AdminCommand implements CommandExecutor {
public void run() {
String message = "[AuthMe] ";
if (arguments[1] == null) {
fSender.sendMessage("[AuthMe] Please put a valid IP");
sender.sendMessage("[AuthMe] Please put a valid IP");
return;
}
List<String> accountList = plugin.database.getAllAuthsByIp(arguments[1]);
if (accountList == null || accountList.isEmpty()) {
fSender.sendMessage("[AuthMe] This IP does not exist in the database");
sender.sendMessage("[AuthMe] This IP does not exist in the database");
return;
}
if (accountList.size() == 1) {
fSender.sendMessage("[AuthMe] " + arguments[1] + " is a single account player");
sender.sendMessage("[AuthMe] " + arguments[1] + " is a single account player");
return;
}
int i = 0;
@ -250,8 +221,8 @@ public class AdminCommand implements CommandExecutor {
message = message + ".";
}
}
fSender.sendMessage("[AuthMe] " + arguments[1] + " has " + String.valueOf(accountList.size()) + " accounts");
fSender.sendMessage(message);
sender.sendMessage("[AuthMe] " + arguments[1] + " has " + String.valueOf(accountList.size()) + " accounts");
sender.sendMessage(message);
}
});
return true;
@ -374,7 +345,7 @@ public class AdminCommand implements CommandExecutor {
}
return true;
} else if (args[0].equalsIgnoreCase("purgebannedplayers")) {
List<String> bannedPlayers = new ArrayList<String>();
List<String> bannedPlayers = new ArrayList<>();
for (OfflinePlayer off : plugin.getServer().getBannedPlayers()) {
bannedPlayers.add(off.getName().toLowerCase());
}

View File

@ -1,20 +1,19 @@
package fr.xephi.authme.datasource;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.Utils;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
import org.bukkit.entity.Player;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import fr.xephi.authme.Utils;
import org.bukkit.entity.Player;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.cache.auth.PlayerCache;
public class CacheDataSource implements DataSource {
private DataSource source;
public AuthMe plugin;
private ConcurrentHashMap<String, PlayerAuth> cache = new ConcurrentHashMap<String, PlayerAuth>();
private ConcurrentHashMap<String, PlayerAuth> cache = new ConcurrentHashMap<>();
public CacheDataSource(AuthMe plugin, DataSource source) {
this.plugin = plugin;
@ -24,8 +23,9 @@ public class CacheDataSource implements DataSource {
* load the server, but it will be much easier to check for an
* isAuthAvailable !
*/
for (PlayerAuth auth : source.getAllAuths())
for (PlayerAuth auth : source.getAllAuths()) {
cache.put(auth.getNickname().toLowerCase(), auth);
}
}
@Override
@ -144,12 +144,8 @@ public class CacheDataSource implements DataSource {
for (Player player : Utils.getOnlinePlayers()) {
String user = player.getName().toLowerCase();
if (PlayerCache.getInstance().isAuthenticated(user)) {
try {
PlayerAuth auth = source.getAuth(user);
cache.put(user, auth);
} catch (NullPointerException npe) {
}
PlayerAuth auth = source.getAuth(user);
cache.put(user, auth);
}
}
}

View File

@ -19,33 +19,34 @@ public final class Settings extends YamlConfiguration {
// This is not an option!
public static Boolean antiBotInAction = false;
public static final File PLUGIN_FOLDER;
public static final File CACHE_FOLDER;
public static final File AUTH_FILE;
public static final File SETTINGS_FILE;
public static final File PLUGIN_FOLDER = AuthMe.getInstance().getDataFolder();
public static final File CACHE_FOLDER = new File(PLUGIN_FOLDER, "cache");
public static final File AUTH_FILE = new File(PLUGIN_FOLDER, "auths.db");
public static final File SETTINGS_FILE = new File(PLUGIN_FOLDER, "config.yml");
public static File messageFile;
public static List<String> allowCommands = null;
public static List<String> getJoinPermissions = null;
public static List<String> getUnrestrictedName = null;
private static List<String> getRestrictedIp;
public static List<String> getMySQLOtherUsernameColumn = null;
public static List<String> getForcedWorlds = null;
public static List<String> countries = null;
public static List<String> countriesBlacklist = null;
public static List<String> forceCommands = null;
public static List<String> forceCommandsAsConsole = null;
public static List<String> forceRegisterCommands = null;
public static List<String> forceRegisterCommandsAsConsole = null;
public static List<String> allowCommands;
public static List<String> getJoinPermissions;
public static List<String> getUnrestrictedName;
public static List<String> getRestrictedIp;
public static List<String> getMySQLOtherUsernameColumn;
public static List<String> getForcedWorlds;
public static List<String> countries;
public static List<String> countriesBlacklist;
public static List<String> forceCommands;
public static List<String> forceCommandsAsConsole;
public static List<String> forceRegisterCommands;
public static List<String> forceRegisterCommandsAsConsole;
public static List<String> welcomeMsg;
public static List<String> unsafePasswords;
public static List<String> emailBlacklist;
public static List<String> emailWhitelist;
public static DataSourceType getDataSource;
public static HashAlgorithm getPasswordHash;
public static Boolean useLogging = false;
public static boolean useLogging = false;
public static int purgeDelay = 60;
public static List<String> welcomeMsg = null;
public static List<String> unsafePasswords;
public static List<String> emailBlacklist = null;
public static List<String> emailWhitelist = null;
public static Boolean isPermissionCheckEnabled, isRegistrationEnabled,
public static boolean isPermissionCheckEnabled, isRegistrationEnabled,
isForcedRegistrationEnabled, isTeleportToSpawnEnabled,
isSessionsEnabled, isChatAllowed, isAllowRestrictedIp,
isMovementAllowed, isKickNonRegisteredEnabled,
@ -91,35 +92,28 @@ public final class Settings extends YamlConfiguration {
protected static YamlConfiguration configFile;
static {
PLUGIN_FOLDER = AuthMe.getInstance().getDataFolder();
CACHE_FOLDER = new File(PLUGIN_FOLDER, "cache");
AUTH_FILE = new File(PLUGIN_FOLDER, "auths.db");
SETTINGS_FILE = new File(PLUGIN_FOLDER, "config.yml");
}
public Settings(AuthMe plugin) {
configFile = (YamlConfiguration) plugin.getConfig();
this.plugin = plugin;
boolean exist = exists();
}
public final void reload() throws Exception {
plugin.getLogger().info("Loading Configuration File...");
boolean exist = SETTINGS_FILE.exists();
if (!exist) {
plugin.saveDefaultConfig();
}
load();
loadConfigOptions(exist);
load(SETTINGS_FILE);
if (exist) {
mergeConfig();
}
loadVariables();
if (exist) {
saveDefaults();
}
messageFile = new File(PLUGIN_FOLDER, "messages" + File.separator + "messages_" + messagesLanguage + ".yml");
}
public void loadConfigOptions(boolean exist) {
plugin.getLogger().info("Loading Configuration File...");
if (exist)
mergeConfig();
loadVariables();
if (exist)
saveDefaults();
}
@SuppressWarnings("unchecked")
public static void loadVariables() {
@ -513,27 +507,6 @@ public final class Settings extends YamlConfiguration {
}
}
/**
* Loads the configuration from disk
*
* @return True if loaded successfully
*/
public final boolean load() {
try {
load(SETTINGS_FILE);
return true;
} catch (Exception ex) {
return false;
}
}
public final void reload() {
if (!exists()) {
plugin.saveDefaultConfig();
}
load();
}
/**
* Saves the configuration to disk
*
@ -548,15 +521,6 @@ public final class Settings extends YamlConfiguration {
}
}
/**
* Simple function for if the Configuration file exists
*
* @return True if configuration exists on disk
*/
public final boolean exists() {
return SETTINGS_FILE.exists();
}
/**
* Saves current configuration (plus defaults) to disk.
* <p>