#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.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);
}
}
}
}
}

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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 <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)) {
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(" ");

View File

@ -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.
* <p>
@ -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);