#450 Port usages of Settings#set(), #setValue() to NewSettings

This commit is contained in:
ljacqu 2016-01-30 15:12:26 +01:00
parent 724296e02b
commit 059175f14e
5 changed files with 73 additions and 76 deletions

View File

@ -50,6 +50,8 @@ import fr.xephi.authme.settings.OtherAccounts;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.settings.Spawn; import fr.xephi.authme.settings.Spawn;
import fr.xephi.authme.settings.properties.DatabaseSettings; 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.CollectionUtils;
import fr.xephi.authme.util.GeoLiteAPI; import fr.xephi.authme.util.GeoLiteAPI;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
@ -628,7 +630,7 @@ public class AuthMe extends JavaPlugin {
if (Settings.getDataSource == DataSource.DataSourceType.FILE) { if (Settings.getDataSource == DataSource.DataSourceType.FILE) {
ConsoleLogger.showError("FlatFile backend has been detected and is now deprecated, it will be changed " + 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!"); "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(); DataSource source = converter.run();
if (source != null) { if (source != null) {
database = source; database = source;
@ -636,7 +638,7 @@ public class AuthMe extends JavaPlugin {
} }
// TODO: Move this to another place maybe ? // 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; " + 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"); "it will be changed and hashed now to the AuthMe default hashing method");
for (PlayerAuth auth : database.getAllAuths()) { for (PlayerAuth auth : database.getAllAuths()) {
@ -645,11 +647,11 @@ public class AuthMe extends JavaPlugin {
auth.setPassword(hashedPassword); auth.setPassword(hashedPassword);
database.updatePassword(auth); database.updatePassword(auth);
} }
Settings.setValue("settings.security.passwordHash", "SHA256"); newSettings.setProperty(SecuritySettings.PASSWORD_HASH, HashAlgorithm.SHA256);
Settings.reload(); newSettings.save();
} }
if (Settings.isCachingEnabled) { if (newSettings.getProperty(DatabaseSettings.USE_CACHING)) {
database = new CacheDataSource(database); database = new CacheDataSource(database);
} }
} }
@ -740,24 +742,20 @@ public class AuthMe extends JavaPlugin {
// Check the presence of the ProtocolLib plugin // Check the presence of the ProtocolLib plugin
public void checkProtocolLib() { public void checkProtocolLib() {
if (!server.getPluginManager().isPluginEnabled("ProtocolLib")) { if (!server.getPluginManager().isPluginEnabled("ProtocolLib")) {
if (Settings.protectInventoryBeforeLogInEnabled) { if (newSettings.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN)) {
ConsoleLogger.showError("WARNING!!! The protectInventory feature requires ProtocolLib! Disabling it..."); ConsoleLogger.showError("WARNING! The protectInventory feature requires ProtocolLib! Disabling it...");
Settings.protectInventoryBeforeLogInEnabled = false; Settings.protectInventoryBeforeLogInEnabled = false;
getSettings().set("settings.restrictions.ProtectInventoryBeforeLogIn", false); newSettings.setProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN, false);
} }
return; return;
} }
if (Settings.protectInventoryBeforeLogInEnabled) { if (newSettings.getProperty(RestrictionSettings.PROTECT_INVENTORY_BEFORE_LOGIN) && inventoryProtector == null) {
if (inventoryProtector == null) { inventoryProtector = new AuthMeInventoryPacketAdapter(this);
inventoryProtector = new AuthMeInventoryPacketAdapter(this); inventoryProtector.register();
inventoryProtector.register(); } else if (inventoryProtector != null) {
} inventoryProtector.unregister();
} else { inventoryProtector = null;
if (inventoryProtector != null) {
inventoryProtector.unregister();
inventoryProtector = null;
}
} }
if (tabComplete == null) { if (tabComplete == null) {
tabComplete = new AuthMeTabCompletePacketAdapter(this); tabComplete = new AuthMeTabCompletePacketAdapter(this);
@ -895,12 +893,12 @@ public class AuthMe extends JavaPlugin {
for (Player player : Utils.getOnlinePlayers()) { for (Player player : Utils.getOnlinePlayers()) {
if (player.isOnline()) { if (player.isOnline()) {
String name = player.getName().toLowerCase(); String name = player.getName().toLowerCase();
if (database.isAuthAvailable(name)) if (database.isAuthAvailable(name) && PlayerCache.getInstance().isAuthenticated(name)) {
if (PlayerCache.getInstance().isAuthenticated(name)) { String email = database.getAuth(name).getEmail();
String email = database.getAuth(name).getEmail(); if (email == null || email.isEmpty() || email.equalsIgnoreCase("your@email.com")) {
if (email == null || email.isEmpty() || email.equalsIgnoreCase("your@email.com")) messages.send(player, MessageKey.ADD_EMAIL_MESSAGE);
messages.send(player, MessageKey.ADD_EMAIL_MESSAGE);
} }
}
} }
} }
} }

View File

@ -4,32 +4,41 @@ import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth; import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.SQLite; 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 { public class ForceFlatToSqlite {
private final DataSource data; private final DataSource database;
private final NewSetting settings;
public ForceFlatToSqlite(DataSource data) { public ForceFlatToSqlite(DataSource database, NewSetting settings) {
this.data = data; this.database = database;
this.settings = settings;
} }
public DataSource run() { public DataSource run() {
DataSource sqlite = null;
try { try {
sqlite = new SQLite(); DataSource sqlite = new SQLite();
for (PlayerAuth auth : data.getAllAuths()) { for (PlayerAuth auth : database.getAllAuths()) {
auth.setRealName("Player"); auth.setRealName("Player");
sqlite.saveAuth(auth); sqlite.saveAuth(auth);
} }
Settings.setValue("DataSource.backend", "sqlite"); settings.setProperty(DatabaseSettings.BACKEND, DataSource.DataSourceType.SQLITE);
ConsoleLogger.info("Database successfully converted to sqlite !"); settings.save();
} catch (Exception e) { ConsoleLogger.info("Database successfully converted to sqlite!");
ConsoleLogger.showError("An error occurred while trying to convert flatfile to sqlite ..."); return sqlite;
return null; } 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;
} }
} }

View File

@ -36,17 +36,19 @@ public class FlatFile implements DataSource {
private final File source; private final File source;
public FlatFile() { public FlatFile() {
source = Settings.AUTH_FILE; AuthMe instance = AuthMe.getInstance();
source = new File(instance.getDataFolder(), "auths.db");
try { try {
source.createNewFile(); source.createNewFile();
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.showError(e.getMessage()); ConsoleLogger.showError(e.getMessage());
if (Settings.isStopEnabled) { if (Settings.isStopEnabled) {
ConsoleLogger.showError("Can't use FLAT FILE... SHUTDOWN..."); ConsoleLogger.showError("Can't use FLAT FILE... SHUTDOWN...");
AuthMe.getInstance().getServer().shutdown(); instance.getServer().shutdown();
} }
if (!Settings.isStopEnabled) { if (!Settings.isStopEnabled) {
AuthMe.getInstance().getServer().getPluginManager().disablePlugin(AuthMe.getInstance()); instance.getServer().getPluginManager().disablePlugin(instance);
} }
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -28,8 +28,7 @@ public class NewSetting {
private FileConfiguration configuration; private FileConfiguration configuration;
/** /**
* Constructor. * Constructor. Checks the given {@link FileConfiguration} object for completeness.
* Loads the file as YAML and checks its integrity.
* *
* @param configuration The configuration to interact with * @param configuration The configuration to interact with
* @param file The configuration file * @param file The configuration file
@ -74,7 +73,25 @@ public class NewSetting {
return property.getFromFile(configuration); 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 <T> The property's type
*/
public <T> void setProperty(Property<T> 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)) { try (FileWriter writer = new FileWriter(file)) {
Yaml simpleYaml = newYaml(false); Yaml simpleYaml = newYaml(false);
Yaml singleQuoteYaml = newYaml(true); Yaml singleQuoteYaml = newYaml(true);
@ -165,7 +182,7 @@ public class NewSetting {
} }
private static String indent(int level) { 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); StringBuilder sb = new StringBuilder(level * 4);
for (int i = 0; i < level; ++i) { for (int i = 0; i < level; ++i) {
sb.append(" "); sb.append(" ");

View File

@ -31,9 +31,8 @@ public final class Settings {
public static final File PLUGIN_FOLDER = Wrapper.getInstance().getDataFolder(); public static final File PLUGIN_FOLDER = Wrapper.getInstance().getDataFolder();
public static final File MODULE_FOLDER = new File(PLUGIN_FOLDER, "modules"); 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 CACHE_FOLDER = new File(PLUGIN_FOLDER, "cache");
public static final File AUTH_FILE = new File(PLUGIN_FOLDER, "auths.db"); private static final File EMAIL_FILE = new File(PLUGIN_FOLDER, "email.html");
public 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 SETTINGS_FILE = new File(PLUGIN_FOLDER, "config.yml");
public static final File LOG_FILE = new File(PLUGIN_FOLDER, "authme.log"); public static final File LOG_FILE = new File(PLUGIN_FOLDER, "authme.log");
// This is not an option! // This is not an option!
public static boolean antiBotInAction = false; 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. * Method getPasswordHash.
* *
@ -392,7 +382,7 @@ public final class Settings {
* *
* @return True if saved successfully * @return True if saved successfully
*/ */
public static boolean save() { private static boolean save() {
try { try {
configFile.save(SETTINGS_FILE); configFile.save(SETTINGS_FILE);
return true; return true;
@ -503,25 +493,6 @@ public final class Settings {
return correct; 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. * Saves current configuration (plus defaults) to disk.
* <p> * <p>
@ -529,7 +500,7 @@ public final class Settings {
* *
* @return True if saved successfully * @return True if saved successfully
*/ */
public final boolean saveDefaults() { private boolean saveDefaults() {
configFile.options() configFile.options()
.copyDefaults(true) .copyDefaults(true)
.copyHeader(true); .copyHeader(true);