#256 Add migration for moved properties

This commit is contained in:
ljacqu 2016-05-08 22:30:53 +02:00
parent f2e49dbf1e
commit b728b297b8
2 changed files with 56 additions and 15 deletions

View File

@ -8,11 +8,14 @@ import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import static fr.xephi.authme.settings.properties.RegistrationSettings.DELAY_JOIN_MESSAGE;
import static fr.xephi.authme.settings.properties.RegistrationSettings.REMOVE_JOIN_MESSAGE;
import static fr.xephi.authme.settings.properties.RegistrationSettings.REMOVE_LEAVE_MESSAGE;
import static fr.xephi.authme.settings.properties.RestrictionSettings.ALLOWED_NICKNAME_CHARACTERS;
import static fr.xephi.authme.settings.properties.RestrictionSettings.FORCE_SPAWN_LOCATION_AFTER_LOGIN;
import static fr.xephi.authme.settings.properties.RestrictionSettings.FORCE_SPAWN_ON_WORLDS;
/**
* Service for verifying that the configuration is up-to-date.
@ -44,7 +47,8 @@ public class SettingsMigrationService {
// ensures that all migrations will be performed
return changes
| performMailTextToFileMigration(configuration, pluginFolder)
| migrateJoinLeaveMessages(configuration);
| migrateJoinLeaveMessages(configuration)
| migrateForceSpawnSettings(configuration);
}
public boolean containsAllSettings(FileConfiguration configuration, PropertyMap propertyMap) {
@ -59,7 +63,8 @@ public class SettingsMigrationService {
private static boolean hasDeprecatedProperties(FileConfiguration configuration) {
String[] deprecatedProperties = {
"Converter.Rakamak.newPasswordHash", "Hooks.chestshop", "Hooks.legacyChestshop", "Hooks.notifications",
"Passpartu", "Performances", "settings.restrictions.enablePasswordVerifier", "Xenoforo.predefinedSalt", "VeryGames"};
"Passpartu", "Performances", "settings.restrictions.enablePasswordVerifier", "Xenoforo.predefinedSalt",
"VeryGames"};
for (String deprecatedPath : deprecatedProperties) {
if (configuration.contains(deprecatedPath)) {
return true;
@ -109,14 +114,50 @@ public class SettingsMigrationService {
* @return True if the configuration has changed, false otherwise
*/
private static boolean migrateJoinLeaveMessages(FileConfiguration configuration) {
final String oldDelayJoinPath = "settings.delayJoinLeaveMessages";
if (configuration.contains(oldDelayJoinPath)) {
ConsoleLogger.info("Detected deprecated property " + oldDelayJoinPath);
Property<Boolean> oldDelayJoinProperty = Property.newProperty("settings.delayJoinLeaveMessages", false);
boolean hasMigrated = moveProperty(oldDelayJoinProperty, DELAY_JOIN_MESSAGE, configuration);
if (hasMigrated) {
ConsoleLogger.info(String.format("Note that we now also have the settings %s and %s",
REMOVE_JOIN_MESSAGE.getPath(), REMOVE_LEAVE_MESSAGE.getPath()));
if (!configuration.contains(DELAY_JOIN_MESSAGE.getPath())) {
configuration.set(DELAY_JOIN_MESSAGE.getPath(), true);
ConsoleLogger.info("Renamed " + oldDelayJoinPath + " to " + DELAY_JOIN_MESSAGE.getPath());
}
return hasMigrated;
}
/**
* Detect old "force spawn loc on join" and "force spawn on these worlds" settings and moves them
* to the new paths.
*
* @param configuration The file configuration
* @return True if the configuration has changed, false otherwise
*/
private static boolean migrateForceSpawnSettings(FileConfiguration configuration) {
Property<Boolean> oldForceLocEnabled = Property.newProperty(
"settings.restrictions.ForceSpawnLocOnJoinEnabled", false);
Property<List<String>> oldForceWorlds = Property.newListProperty(
"settings.restrictions.ForceSpawnOnTheseWorlds", "world", "world_nether", "world_the_ed");
return moveProperty(oldForceLocEnabled, FORCE_SPAWN_LOCATION_AFTER_LOGIN, configuration)
| moveProperty(oldForceWorlds, FORCE_SPAWN_ON_WORLDS, configuration);
}
/**
* Checks for an old property path and moves it to a new path if present.
*
* @param oldProperty The old property (create a temporary {@link Property} object with the path)
* @param newProperty The new property to move the value to
* @param configuration The file configuration
* @param <T> The type of the property
* @return True if a migration has been done, false otherwise
*/
private static <T> boolean moveProperty(Property<T> oldProperty,
Property<T> newProperty,
FileConfiguration configuration) {
if (configuration.contains(oldProperty.getPath())) {
ConsoleLogger.info("Detected deprecated property " + oldProperty.getPath());
if (!configuration.contains(newProperty.getPath())) {
ConsoleLogger.info("Renamed " + oldProperty.getPath() + " to " + newProperty.getPath());
configuration.set(newProperty.getPath(), oldProperty.getFromFile(configuration));
}
return true;
}

View File

@ -63,6 +63,13 @@ public class RestrictionSettings implements SettingsClass {
public static final Property<Boolean> FORCE_SPAWN_LOCATION_AFTER_LOGIN =
newProperty("settings.restrictions.ForceSpawnLocOnJoin.enabled", false);
@Comment({
"WorldNames where we need to force the spawn location",
"Case-sensitive!"})
public static final Property<List<String>> FORCE_SPAWN_ON_WORLDS =
newListProperty("settings.restrictions.ForceSpawnLocOnJoin.worlds",
"world", "world_nether", "world_the_end");
@Comment("This option will save the quit location of the players.")
public static final Property<Boolean> SAVE_QUIT_LOCATION =
newProperty("settings.restrictions.SaveQuitLocation", false);
@ -149,13 +156,6 @@ public class RestrictionSettings implements SettingsClass {
public static final Property<Boolean> DISPLAY_OTHER_ACCOUNTS =
newProperty("settings.restrictions.displayOtherAccounts", true);
@Comment({
"WorldNames where we need to force the spawn location",
"Case-sensitive!"})
public static final Property<List<String>> FORCE_SPAWN_ON_WORLDS =
newListProperty("settings.restrictions.ForceSpawnLocOnJoin.worlds",
"world", "world_nether", "world_the_end");
@Comment("Ban ip when the ip is not the ip registered in database")
public static final Property<Boolean> BAN_UNKNOWN_IP =
newProperty("settings.restrictions.banUnsafedIP", false);