#603 Pass settings migration service as constructor parameter (work in progress)
This commit is contained in:
@@ -33,6 +33,7 @@ public class NewSetting {
|
||||
private final File pluginFolder;
|
||||
private final File configFile;
|
||||
private final PropertyMap propertyMap;
|
||||
private final SettingsMigrationService migrationService;
|
||||
private FileConfiguration configuration;
|
||||
/** The file with the localized messages based on {@link PluginSettings#MESSAGES_LANGUAGE}. */
|
||||
private File messagesFile;
|
||||
@@ -44,12 +45,16 @@ public class NewSetting {
|
||||
*
|
||||
* @param configFile The configuration file
|
||||
* @param pluginFolder The AuthMe plugin folder
|
||||
* @param propertyMap Collection of all available settings
|
||||
* @param migrationService Migration service to check the settings file with
|
||||
*/
|
||||
public NewSetting(File configFile, File pluginFolder, PropertyMap propertyMap) {
|
||||
public NewSetting(File configFile, File pluginFolder, PropertyMap propertyMap,
|
||||
SettingsMigrationService migrationService) {
|
||||
this.configuration = YamlConfiguration.loadConfiguration(configFile);
|
||||
this.configFile = configFile;
|
||||
this.pluginFolder = pluginFolder;
|
||||
this.propertyMap = propertyMap;
|
||||
this.migrationService = migrationService;
|
||||
validateAndLoadOptions();
|
||||
}
|
||||
|
||||
@@ -59,15 +64,18 @@ public class NewSetting {
|
||||
* @param configuration The FileConfiguration object to use
|
||||
* @param configFile The file to write to
|
||||
* @param propertyMap The property map whose properties should be verified for presence, or null to skip this
|
||||
* @param migrationService Migration service, or null to skip migration checks
|
||||
*/
|
||||
@VisibleForTesting
|
||||
NewSetting(FileConfiguration configuration, File configFile, PropertyMap propertyMap) {
|
||||
NewSetting(FileConfiguration configuration, File configFile, PropertyMap propertyMap,
|
||||
SettingsMigrationService migrationService) {
|
||||
this.configuration = configuration;
|
||||
this.configFile = configFile;
|
||||
this.pluginFolder = new File("");
|
||||
this.propertyMap = propertyMap;
|
||||
this.migrationService = migrationService;
|
||||
|
||||
if (propertyMap != null) {
|
||||
if (propertyMap != null && migrationService != null) {
|
||||
validateAndLoadOptions();
|
||||
}
|
||||
}
|
||||
@@ -184,7 +192,7 @@ public class NewSetting {
|
||||
}
|
||||
|
||||
private void validateAndLoadOptions() {
|
||||
if (SettingsMigrationService.checkAndMigrate(configuration, propertyMap, pluginFolder)) {
|
||||
if (migrationService.checkAndMigrate(configuration, propertyMap, pluginFolder)) {
|
||||
ConsoleLogger.info("Merged new config options");
|
||||
ConsoleLogger.info("Please check your config.yml file for new settings!");
|
||||
save();
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package fr.xephi.authme.settings;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import fr.xephi.authme.ConsoleLogger;
|
||||
import fr.xephi.authme.settings.domain.Property;
|
||||
import fr.xephi.authme.settings.propertymap.PropertyMap;
|
||||
@@ -18,25 +17,23 @@ import static fr.xephi.authme.settings.properties.RestrictionSettings.ALLOWED_NI
|
||||
/**
|
||||
* Service for verifying that the configuration is up-to-date.
|
||||
*/
|
||||
public final class SettingsMigrationService {
|
||||
|
||||
private SettingsMigrationService() {
|
||||
}
|
||||
public class SettingsMigrationService {
|
||||
|
||||
/**
|
||||
* Checks the config file and does any necessary migrations.
|
||||
* Checks the config file and performs any necessary migrations.
|
||||
*
|
||||
* @param configuration The file configuration to check and migrate
|
||||
* @param propertyMap The property map of all existing properties
|
||||
* @param pluginFolder The plugin folder
|
||||
* @return True if there is a change and the config must be saved, false if the config is up-to-date
|
||||
*/
|
||||
public static boolean checkAndMigrate(FileConfiguration configuration, PropertyMap propertyMap, File pluginFolder) {
|
||||
return performMigrations(configuration, pluginFolder) || hasDeprecatedProperties(configuration)
|
||||
public boolean checkAndMigrate(FileConfiguration configuration, PropertyMap propertyMap, File pluginFolder) {
|
||||
return performMigrations(configuration, pluginFolder)
|
||||
|| hasDeprecatedProperties(configuration)
|
||||
|| !containsAllSettings(configuration, propertyMap);
|
||||
}
|
||||
|
||||
private static boolean performMigrations(FileConfiguration configuration, File pluginFolder) {
|
||||
private boolean performMigrations(FileConfiguration configuration, File pluginFolder) {
|
||||
boolean changes = false;
|
||||
if ("[a-zA-Z0-9_?]*".equals(configuration.getString(ALLOWED_NICKNAME_CHARACTERS.getPath()))) {
|
||||
configuration.set(ALLOWED_NICKNAME_CHARACTERS.getPath(), "[a-zA-Z0-9_]*");
|
||||
@@ -50,8 +47,7 @@ public final class SettingsMigrationService {
|
||||
| migrateJoinLeaveMessages(configuration);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static boolean containsAllSettings(FileConfiguration configuration, PropertyMap propertyMap) {
|
||||
public boolean containsAllSettings(FileConfiguration configuration, PropertyMap propertyMap) {
|
||||
for (Property<?> property : propertyMap.keySet()) {
|
||||
if (!property.isPresent(configuration)) {
|
||||
return false;
|
||||
@@ -80,16 +76,16 @@ public final class SettingsMigrationService {
|
||||
* Check if {@code Email.mailText} is present and move it to the Email.html file if it doesn't exist yet.
|
||||
*
|
||||
* @param configuration The file configuration to verify
|
||||
* @param dataFolder The plugin data folder
|
||||
* @param pluginFolder The plugin data folder
|
||||
* @return True if a migration has been completed, false otherwise
|
||||
*/
|
||||
private static boolean performMailTextToFileMigration(FileConfiguration configuration, File dataFolder) {
|
||||
private static boolean performMailTextToFileMigration(FileConfiguration configuration, File pluginFolder) {
|
||||
final String oldSettingPath = "Email.mailText";
|
||||
if (!configuration.contains(oldSettingPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final File emailFile = new File(dataFolder, "email.html");
|
||||
final File emailFile = new File(pluginFolder, "email.html");
|
||||
final String mailText = configuration.getString(oldSettingPath)
|
||||
.replace("<playername>", "<playername />")
|
||||
.replace("<servername>", "<servername />")
|
||||
|
||||
Reference in New Issue
Block a user