From ac5868787ac126f5112d18e9fcedbdc383fb2c77 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Mon, 7 Sep 2020 21:33:00 +0200 Subject: [PATCH] Update to ConfigMe 1.2.0 --- pom.xml | 2 +- .../updater/MessageKeyConfigurationData.java | 16 ++++++++-- .../MessageMigraterPropertyReader.java | 5 ++++ .../message/updater/MessageUpdater.java | 3 +- .../authme/settings/EnumSetProperty.java | 3 +- .../settings/SettingsMigrationService.java | 29 +++++++++++-------- .../CommandMigrationService.java | 2 +- 7 files changed, 41 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index 7eabae590..1f2d7fb94 100644 --- a/pom.xml +++ b/pom.xml @@ -643,7 +643,7 @@ ch.jalu configme - 1.1.0 + 1.2.0 true diff --git a/src/main/java/fr/xephi/authme/message/updater/MessageKeyConfigurationData.java b/src/main/java/fr/xephi/authme/message/updater/MessageKeyConfigurationData.java index 50f948f4b..cf3c1c782 100644 --- a/src/main/java/fr/xephi/authme/message/updater/MessageKeyConfigurationData.java +++ b/src/main/java/fr/xephi/authme/message/updater/MessageKeyConfigurationData.java @@ -2,6 +2,7 @@ package fr.xephi.authme.message.updater; import ch.jalu.configme.configurationdata.ConfigurationDataImpl; import ch.jalu.configme.properties.Property; +import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; import fr.xephi.authme.message.MessageKey; @@ -23,9 +24,18 @@ public class MessageKeyConfigurationData extends ConfigurationDataImpl { @Override public void initializeValues(PropertyReader reader) { - getAllMessageProperties().stream() - .filter(prop -> prop.isPresent(reader)) - .forEach(prop -> setValue(prop, prop.determineValue(reader))); + for (Property property : getAllMessageProperties()) { + PropertyValue value = property.determineValue(reader); + if (value.isValidInResource()) { + setValue(property, value.getValue()); + } + } + } + + @Override + public T getValue(Property property) { + // Override to silently return null if property is unknown + return (T) getValues().get(property.getPath()); } @SuppressWarnings("unchecked") diff --git a/src/main/java/fr/xephi/authme/message/updater/MessageMigraterPropertyReader.java b/src/main/java/fr/xephi/authme/message/updater/MessageMigraterPropertyReader.java index a41b53409..a994df898 100644 --- a/src/main/java/fr/xephi/authme/message/updater/MessageMigraterPropertyReader.java +++ b/src/main/java/fr/xephi/authme/message/updater/MessageMigraterPropertyReader.java @@ -59,6 +59,11 @@ final class MessageMigraterPropertyReader implements PropertyReader { throw new UnsupportedOperationException(); } + @Override + public Set getChildKeys(String s) { + throw new UnsupportedOperationException(); + } + @Override public Object getObject(String path) { if (path.isEmpty()) { diff --git a/src/main/java/fr/xephi/authme/message/updater/MessageUpdater.java b/src/main/java/fr/xephi/authme/message/updater/MessageUpdater.java index a4f9767f8..77fc996b4 100644 --- a/src/main/java/fr/xephi/authme/message/updater/MessageUpdater.java +++ b/src/main/java/fr/xephi/authme/message/updater/MessageUpdater.java @@ -4,6 +4,7 @@ import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.PropertyListBuilder; import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.StringProperty; +import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyResource; import com.google.common.collect.ImmutableMap; @@ -177,7 +178,7 @@ public class MessageUpdater { } @Override - protected String getFromReader(PropertyReader reader) { + protected String getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { return reader.getString(getPath()); } } diff --git a/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java b/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java index ca611315f..583be0627 100644 --- a/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java +++ b/src/main/java/fr/xephi/authme/settings/EnumSetProperty.java @@ -1,6 +1,7 @@ package fr.xephi.authme.settings; import ch.jalu.configme.properties.BaseProperty; +import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.resource.PropertyReader; import java.util.Collection; @@ -26,7 +27,7 @@ public class EnumSetProperty> extends BaseProperty> { } @Override - protected Set getFromReader(PropertyReader reader) { + protected Set getFromReader(PropertyReader reader, ConvertErrorRecorder errorRecorder) { Object entry = reader.getObject(getPath()); if (entry instanceof Collection) { return ((Collection) entry).stream() diff --git a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java index 23168d530..29dfe7e5b 100644 --- a/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java +++ b/src/main/java/fr/xephi/authme/settings/SettingsMigrationService.java @@ -3,6 +3,7 @@ package fr.xephi.authme.settings; import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.migration.PlainMigrationService; import ch.jalu.configme.properties.Property; +import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.resource.PropertyReader; import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.initialization.DataFolder; @@ -214,7 +215,7 @@ public class SettingsMigrationService extends PlainMigrationService { ConfigurationData configData) { final String oldPath = "Security.console.noConsoleSpam"; final Property newProperty = PluginSettings.LOG_LEVEL; - if (!newProperty.isPresent(reader) && reader.contains(oldPath)) { + if (!newProperty.isValidInResource(reader) && reader.contains(oldPath)) { logger.info("Moving '" + oldPath + "' to '" + newProperty.getPath() + "'"); boolean oldValue = Optional.ofNullable(reader.getBoolean(oldPath)).orElse(false); LogLevel level = oldValue ? LogLevel.INFO : LogLevel.FINE; @@ -252,17 +253,18 @@ public class SettingsMigrationService extends PlainMigrationService { */ private static boolean convertToRegistrationType(PropertyReader reader, ConfigurationData configData) { String oldEmailRegisterPath = "settings.registration.enableEmailRegistrationSystem"; - if (RegistrationSettings.REGISTRATION_TYPE.isPresent(reader) || !reader.contains(oldEmailRegisterPath)) { + if (RegistrationSettings.REGISTRATION_TYPE.isValidInResource(reader) + || !reader.contains(oldEmailRegisterPath)) { return false; } - boolean useEmail = newProperty(oldEmailRegisterPath, false).determineValue(reader); + boolean useEmail = newProperty(oldEmailRegisterPath, false).determineValue(reader).getValue(); RegistrationType registrationType = useEmail ? RegistrationType.EMAIL : RegistrationType.PASSWORD; String useConfirmationPath = useEmail ? "settings.registration.doubleEmailCheck" : "settings.restrictions.enablePasswordConfirmation"; - boolean hasConfirmation = newProperty(useConfirmationPath, false).determineValue(reader); + boolean hasConfirmation = newProperty(useConfirmationPath, false).determineValue(reader).getValue(); RegisterSecondaryArgument secondaryArgument = hasConfirmation ? RegisterSecondaryArgument.CONFIRMATION : RegisterSecondaryArgument.NONE; @@ -287,7 +289,7 @@ public class SettingsMigrationService extends PlainMigrationService { // We have two old settings replaced by only one: move the first non-empty one Property oldUnloggedInGroup = newProperty("settings.security.unLoggedinGroup", ""); Property oldRegisteredGroup = newProperty("GroupOptions.RegisteredPlayerGroup", ""); - if (!oldUnloggedInGroup.determineValue(reader).isEmpty()) { + if (!oldUnloggedInGroup.determineValue(reader).getValue().isEmpty()) { performedChanges = moveProperty(oldUnloggedInGroup, PluginSettings.REGISTERED_GROUP, reader, configData); } else { performedChanges = moveProperty(oldRegisteredGroup, PluginSettings.REGISTERED_GROUP, reader, configData); @@ -311,13 +313,13 @@ public class SettingsMigrationService extends PlainMigrationService { */ private static boolean moveDeprecatedHashAlgorithmIntoLegacySection(PropertyReader reader, ConfigurationData configData) { - HashAlgorithm currentHash = SecuritySettings.PASSWORD_HASH.determineValue(reader); + HashAlgorithm currentHash = SecuritySettings.PASSWORD_HASH.determineValue(reader).getValue(); // Skip CUSTOM (has no class) and PLAINTEXT (is force-migrated later on in the startup process) if (currentHash != HashAlgorithm.CUSTOM && currentHash != HashAlgorithm.PLAINTEXT) { Class encryptionClass = currentHash.getClazz(); if (encryptionClass.isAnnotationPresent(Deprecated.class)) { configData.setValue(SecuritySettings.PASSWORD_HASH, HashAlgorithm.SHA256); - Set legacyHashes = SecuritySettings.LEGACY_HASHES.determineValue(reader); + Set legacyHashes = SecuritySettings.LEGACY_HASHES.determineValue(reader).getValue(); legacyHashes.add(currentHash); configData.setValue(SecuritySettings.LEGACY_HASHES, legacyHashes); logger.warning("The hash algorithm '" + currentHash @@ -352,9 +354,11 @@ public class SettingsMigrationService extends PlainMigrationService { Property commandProperty = newProperty("settings.restrictions.otherAccountsCmd", ""); Property commandThresholdProperty = newProperty("settings.restrictions.otherAccountsCmdThreshold", 0); - if (commandProperty.isPresent(reader) && commandThresholdProperty.determineValue(reader) >= 2) { - oldOtherAccountsCommand = commandProperty.determineValue(reader); - oldOtherAccountsCommandThreshold = commandThresholdProperty.determineValue(reader); + PropertyValue commandPropValue = commandProperty.determineValue(reader); + int commandThreshold = commandThresholdProperty.determineValue(reader).getValue(); + if (commandPropValue.isValidInResource() && commandThreshold >= 2) { + oldOtherAccountsCommand = commandPropValue.getValue(); + oldOtherAccountsCommandThreshold = commandThreshold; } } @@ -372,12 +376,13 @@ public class SettingsMigrationService extends PlainMigrationService { Property newProperty, PropertyReader reader, ConfigurationData configData) { - if (reader.contains(oldProperty.getPath())) { + PropertyValue oldPropertyValue = oldProperty.determineValue(reader); + if (oldPropertyValue.isValidInResource()) { if (reader.contains(newProperty.getPath())) { logger.info("Detected deprecated property " + oldProperty.getPath()); } else { logger.info("Renaming " + oldProperty.getPath() + " to " + newProperty.getPath()); - configData.setValue(newProperty, oldProperty.determineValue(reader)); + configData.setValue(newProperty, oldPropertyValue.getValue()); } return true; } diff --git a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java index 62925ff32..0822616d3 100644 --- a/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java +++ b/src/main/java/fr/xephi/authme/settings/commandconfig/CommandMigrationService.java @@ -31,7 +31,7 @@ class CommandMigrationService implements MigrationService { @Override public boolean checkAndMigrate(PropertyReader reader, ConfigurationData configurationData) { - final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.determineValue(reader); + final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.determineValue(reader).getValue(); if (moveOtherAccountsConfig(commandConfig) || isAnyCommandMissing(reader)) { configurationData.setValue(CommandSettingsHolder.COMMANDS, commandConfig); return true;