From 059175f14e95066be57c7149efb0d42495529203 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 30 Jan 2016 15:12:26 +0100 Subject: [PATCH] #450 Port usages of Settings#set(), #setValue() to NewSettings --- src/main/java/fr/xephi/authme/AuthMe.java | 44 +++++++++---------- .../authme/converter/ForceFlatToSqlite.java | 35 +++++++++------ .../fr/xephi/authme/datasource/FlatFile.java | 8 ++-- .../fr/xephi/authme/settings/NewSetting.java | 25 +++++++++-- .../fr/xephi/authme/settings/Settings.java | 37 ++-------------- 5 files changed, 73 insertions(+), 76 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index e38ece18f..e89df701e 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -50,6 +50,8 @@ import fr.xephi.authme.settings.OtherAccounts; import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Spawn; import fr.xephi.authme.settings.properties.DatabaseSettings; +import fr.xephi.authme.settings.properties.RestrictionSettings; +import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.util.CollectionUtils; import fr.xephi.authme.util.GeoLiteAPI; import fr.xephi.authme.util.StringUtils; @@ -628,7 +630,7 @@ public class AuthMe extends JavaPlugin { if (Settings.getDataSource == DataSource.DataSourceType.FILE) { ConsoleLogger.showError("FlatFile backend has been detected and is now deprecated, it will be changed " + "to SQLite... Connection will be impossible until conversion is done!"); - ForceFlatToSqlite converter = new ForceFlatToSqlite(database); + ForceFlatToSqlite converter = new ForceFlatToSqlite(database, newSettings); DataSource source = converter.run(); if (source != null) { database = source; @@ -636,7 +638,7 @@ public class AuthMe extends JavaPlugin { } // TODO: Move this to another place maybe ? - if (Settings.getPasswordHash == HashAlgorithm.PLAINTEXT) { + if (HashAlgorithm.PLAINTEXT == newSettings.getProperty(SecuritySettings.PASSWORD_HASH)) { ConsoleLogger.showError("Your HashAlgorithm has been detected as plaintext and is now deprecated; " + "it will be changed and hashed now to the AuthMe default hashing method"); for (PlayerAuth auth : database.getAllAuths()) { @@ -645,11 +647,11 @@ public class AuthMe extends JavaPlugin { auth.setPassword(hashedPassword); database.updatePassword(auth); } - Settings.setValue("settings.security.passwordHash", "SHA256"); - Settings.reload(); + newSettings.setProperty(SecuritySettings.PASSWORD_HASH, HashAlgorithm.SHA256); + newSettings.save(); } - if (Settings.isCachingEnabled) { + if (newSettings.getProperty(DatabaseSettings.USE_CACHING)) { database = new CacheDataSource(database); } } @@ -740,24 +742,20 @@ public class AuthMe extends JavaPlugin { // Check the presence of the ProtocolLib plugin public void checkProtocolLib() { if (!server.getPluginManager().isPluginEnabled("ProtocolLib")) { - if (Settings.protectInventoryBeforeLogInEnabled) { - ConsoleLogger.showError("WARNING!!! The protectInventory feature requires ProtocolLib! Disabling it..."); + if (newSettings.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) { + ConsoleLogger.showError("WARNING! The protectInventory feature requires ProtocolLib! Disabling it..."); Settings.protectInventoryBeforeLogInEnabled = false; - getSettings().set("settings.restrictions.ProtectInventoryBeforeLogIn", false); + newSettings.setProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN, false); } return; } - if (Settings.protectInventoryBeforeLogInEnabled) { - if (inventoryProtector == null) { - inventoryProtector = new AuthMeInventoryPacketAdapter(this); - inventoryProtector.register(); - } - } else { - if (inventoryProtector != null) { - inventoryProtector.unregister(); - inventoryProtector = null; - } + if (newSettings.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN) && inventoryProtector == null) { + inventoryProtector = new AuthMeInventoryPacketAdapter(this); + inventoryProtector.register(); + } else if (inventoryProtector != null) { + inventoryProtector.unregister(); + inventoryProtector = null; } if (tabComplete == null) { tabComplete = new AuthMeTabCompletePacketAdapter(this); @@ -895,12 +893,12 @@ public class AuthMe extends JavaPlugin { for (Player player : Utils.getOnlinePlayers()) { if (player.isOnline()) { String name = player.getName().toLowerCase(); - if (database.isAuthAvailable(name)) - if (PlayerCache.getInstance().isAuthenticated(name)) { - String email = database.getAuth(name).getEmail(); - if (email == null || email.isEmpty() || email.equalsIgnoreCase("your@email.com")) - messages.send(player, MessageKey.ADD_EMAIL_MESSAGE); + if (database.isAuthAvailable(name) && PlayerCache.getInstance().isAuthenticated(name)) { + String email = database.getAuth(name).getEmail(); + if (email == null || email.isEmpty() || email.equalsIgnoreCase("your@email.com")) { + messages.send(player, MessageKey.ADD_EMAIL_MESSAGE); } + } } } } diff --git a/src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java b/src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java index b0ea61c0b..d873aa506 100644 --- a/src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java +++ b/src/main/java/fr/xephi/authme/converter/ForceFlatToSqlite.java @@ -4,32 +4,41 @@ import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.SQLite; -import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.settings.NewSetting; +import fr.xephi.authme.settings.properties.DatabaseSettings; +import fr.xephi.authme.util.StringUtils; + +import java.sql.SQLException; /** + * Mandatory migration from the deprecated flat file datasource to SQLite. */ public class ForceFlatToSqlite { - private final DataSource data; + private final DataSource database; + private final NewSetting settings; - public ForceFlatToSqlite(DataSource data) { - this.data = data; + public ForceFlatToSqlite(DataSource database, NewSetting settings) { + this.database = database; + this.settings = settings; } public DataSource run() { - DataSource sqlite = null; try { - sqlite = new SQLite(); - for (PlayerAuth auth : data.getAllAuths()) { + DataSource sqlite = new SQLite(); + for (PlayerAuth auth : database.getAllAuths()) { auth.setRealName("Player"); sqlite.saveAuth(auth); } - Settings.setValue("DataSource.backend", "sqlite"); - ConsoleLogger.info("Database successfully converted to sqlite !"); - } catch (Exception e) { - ConsoleLogger.showError("An error occurred while trying to convert flatfile to sqlite ..."); - return null; + settings.setProperty(DatabaseSettings.BACKEND, DataSource.DataSourceType.SQLITE); + settings.save(); + ConsoleLogger.info("Database successfully converted to sqlite!"); + return sqlite; + } catch (SQLException | ClassNotFoundException e) { + ConsoleLogger.showError("An error occurred while trying to convert flatfile to sqlite: " + + StringUtils.formatException(e)); + ConsoleLogger.writeStackTrace(e); } - return sqlite; + return null; } } diff --git a/src/main/java/fr/xephi/authme/datasource/FlatFile.java b/src/main/java/fr/xephi/authme/datasource/FlatFile.java index 4d7b4b38e..88bad0774 100644 --- a/src/main/java/fr/xephi/authme/datasource/FlatFile.java +++ b/src/main/java/fr/xephi/authme/datasource/FlatFile.java @@ -36,17 +36,19 @@ public class FlatFile implements DataSource { private final File source; public FlatFile() { - source = Settings.AUTH_FILE; + AuthMe instance = AuthMe.getInstance(); + + source = new File(instance.getDataFolder(), "auths.db"); try { source.createNewFile(); } catch (IOException e) { ConsoleLogger.showError(e.getMessage()); if (Settings.isStopEnabled) { ConsoleLogger.showError("Can't use FLAT FILE... SHUTDOWN..."); - AuthMe.getInstance().getServer().shutdown(); + instance.getServer().shutdown(); } if (!Settings.isStopEnabled) { - AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance()); + instance.getServer().getPluginManager().disablePlugin(instance); } e.printStackTrace(); } diff --git a/src/main/java/fr/xephi/authme/settings/NewSetting.java b/src/main/java/fr/xephi/authme/settings/NewSetting.java index 8aa46b84f..232b37b44 100644 --- a/src/main/java/fr/xephi/authme/settings/NewSetting.java +++ b/src/main/java/fr/xephi/authme/settings/NewSetting.java @@ -28,8 +28,7 @@ public class NewSetting { private FileConfiguration configuration; /** - * Constructor. - * Loads the file as YAML and checks its integrity. + * Constructor. Checks the given {@link FileConfiguration} object for completeness. * * @param configuration The configuration to interact with * @param file The configuration file @@ -74,7 +73,25 @@ public class NewSetting { return property.getFromFile(configuration); } - public void save(PropertyMap propertyMap) { + /** + * Set a new value for the given property. + * + * @param property The property to modify + * @param value The new value to assign to the property + * @param The property's type + */ + public void setProperty(Property property, T value) { + configuration.set(property.getPath(), value); + } + + /** + * Save the config file. Use after migrating one or more settings. + */ + public void save() { + save(SettingsFieldRetriever.getAllPropertyFields()); + } + + private void save(PropertyMap propertyMap) { try (FileWriter writer = new FileWriter(file)) { Yaml simpleYaml = newYaml(false); Yaml singleQuoteYaml = newYaml(true); @@ -165,7 +182,7 @@ public class NewSetting { } private static String indent(int level) { - // YAML uses indentation of 4 spaces + // We use an indentation of 4 spaces StringBuilder sb = new StringBuilder(level * 4); for (int i = 0; i < level; ++i) { sb.append(" "); diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 5cd3f592e..14794965d 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -31,9 +31,8 @@ public final class Settings { public static final File PLUGIN_FOLDER = Wrapper.getInstance().getDataFolder(); public static final File MODULE_FOLDER = new File(PLUGIN_FOLDER, "modules"); 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 EMAIL_FILE = new File(PLUGIN_FOLDER, "email.html"); - public static final File SETTINGS_FILE = new File(PLUGIN_FOLDER, "config.yml"); + private static final File EMAIL_FILE = new File(PLUGIN_FOLDER, "email.html"); + private static final File SETTINGS_FILE = new File(PLUGIN_FOLDER, "config.yml"); public static final File LOG_FILE = new File(PLUGIN_FOLDER, "authme.log"); // This is not an option! public static boolean antiBotInAction = false; @@ -311,15 +310,6 @@ public final class Settings { } } - /** - * @param key the key to set - * @param value the value to set - */ - public static void setValue(String key, Object value) { - instance.set(key, value); - save(); - } - /** * Method getPasswordHash. * @@ -392,7 +382,7 @@ public final class Settings { * * @return True if saved successfully */ - public static boolean save() { + private static boolean save() { try { configFile.save(SETTINGS_FILE); return true; @@ -503,25 +493,6 @@ public final class Settings { return correct; } - /** - * @param path - * - * @return - */ - private static boolean contains(String path) { - return configFile.contains(path); - } - - // public because it's used in AuthMe at one place - - /** - * @param path String - * @param value String - */ - public void set(String path, Object value) { - configFile.set(path, value); - } - /** * Saves current configuration (plus defaults) to disk. *

@@ -529,7 +500,7 @@ public final class Settings { * * @return True if saved successfully */ - public final boolean saveDefaults() { + private boolean saveDefaults() { configFile.options() .copyDefaults(true) .copyHeader(true);