diff --git a/src/main/java/fr/xephi/authme/settings/custom/DatabaseSettings.java b/src/main/java/fr/xephi/authme/settings/custom/DatabaseSettings.java index 26e36310..d8c5d991 100644 --- a/src/main/java/fr/xephi/authme/settings/custom/DatabaseSettings.java +++ b/src/main/java/fr/xephi/authme/settings/custom/DatabaseSettings.java @@ -1,5 +1,6 @@ package fr.xephi.authme.settings.custom; +import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.settings.custom.domain.Comment; import fr.xephi.authme.settings.custom.domain.Property; import fr.xephi.authme.settings.custom.domain.SettingsClass; @@ -12,8 +13,8 @@ public class DatabaseSettings implements SettingsClass { @Comment({"What type of database do you want to use?", "Valid values: sqlite, mysql"}) - public static final Property BACKEND = - newProperty(STRING, "DataSource.backend", "sqlite"); + public static final Property BACKEND = + newProperty(DataSource.DataSourceType.class, "DataSource.backend", DataSource.DataSourceType.SQLITE); @Comment("Enable database caching, should improve database performance") public static final Property USE_CACHING = diff --git a/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java b/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java index b64d2f2e..1b56cd0f 100644 --- a/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java +++ b/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java @@ -28,8 +28,6 @@ public class NewSetting { ConverterSettings.class, DatabaseSettings.class, EmailSettings.class, HooksSettings.class, ProtectionSettings.class, PurgeSettings.class, SecuritySettings.class); - private static final int YAML_INDENTATION = 4; - private File file; private YamlConfiguration configuration; @@ -75,7 +73,7 @@ public class NewSetting { if (newPathParts.size() > 1) { for (String path : newPathParts.subList(0, newPathParts.size() - 1)) { writer.append("\n") - .append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION)) + .append(indent(indentationLevel)) .append(path) .append(": "); ++indentationLevel; @@ -83,12 +81,12 @@ public class NewSetting { } for (String comment : entry.getValue()) { writer.append("\n") - .append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION)) + .append(indent(indentationLevel)) .append("# ") .append(comment); } writer.append("\n") - .append(StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION)) + .append(indent(indentationLevel)) .append(CollectionUtils.getRange(newPathParts, newPathParts.size() - 1).get(0)) .append(": "); @@ -96,7 +94,7 @@ public class NewSetting { String delim = ""; for (String yamlLine : yamlLines) { writer.append(delim).append(yamlLine); - delim = "\n" + StringUtils.repeat(" ", indentationLevel * YAML_INDENTATION); + delim = "\n" + indent(indentationLevel); } currentPath = propertyPath.subList(0, propertyPath.size() - 1); @@ -109,6 +107,15 @@ public class NewSetting { } } + private static String indent(int level) { + // YAML uses indentation of 4 spaces + StringBuilder sb = new StringBuilder(level * 4); + for (int i = 0; i < level; ++i) { + sb.append(" "); + } + return sb.toString(); + } + private static PropertyMap getAllPropertyFields() { PropertyMap properties = new PropertyMap(); for (Class clazz : CONFIGURATION_CLASSES) { diff --git a/src/main/java/fr/xephi/authme/settings/custom/domain/Property.java b/src/main/java/fr/xephi/authme/settings/custom/domain/Property.java index e777f6ec..89b9f942 100644 --- a/src/main/java/fr/xephi/authme/settings/custom/domain/Property.java +++ b/src/main/java/fr/xephi/authme/settings/custom/domain/Property.java @@ -31,6 +31,10 @@ public class Property { return new Property<>(type, path, Arrays.asList(defaultValues)); } + public static > Property newProperty(Class clazz, String path, E defaultValue) { + return new Property<>(new PropertyType.EnumProperty<>(clazz), path, defaultValue); + } + // ----- // Overloaded convenience methods for specific types // ----- diff --git a/src/main/java/fr/xephi/authme/settings/custom/domain/PropertyType.java b/src/main/java/fr/xephi/authme/settings/custom/domain/PropertyType.java index 3680de3f..ac6d3d69 100644 --- a/src/main/java/fr/xephi/authme/settings/custom/domain/PropertyType.java +++ b/src/main/java/fr/xephi/authme/settings/custom/domain/PropertyType.java @@ -143,4 +143,40 @@ public abstract class PropertyType { } } + /** + * Enum property. + * @param The enum class + */ + static final class EnumProperty> extends PropertyType { + private Class clazz; + + public EnumProperty(Class clazz) { + this.clazz = clazz; + } + + @Override + public E getFromFile(Property property, YamlConfiguration configuration) { + String textValue = configuration.getString(property.getPath()); + if (textValue == null) { + return property.getDefaultValue(); + } + E mappedValue = mapToEnum(textValue); + return mappedValue != null ? mappedValue : property.getDefaultValue(); + } + + @Override + protected List asYaml(E value) { + return asList("'" + value + "'"); + } + + private E mapToEnum(String value) { + for (E entry : clazz.getEnumConstants()) { + if (entry.name().equalsIgnoreCase(value)) { + return entry; + } + } + return null; + } + } + } diff --git a/src/main/java/fr/xephi/authme/util/CollectionUtils.java b/src/main/java/fr/xephi/authme/util/CollectionUtils.java index 51995165..13077547 100644 --- a/src/main/java/fr/xephi/authme/util/CollectionUtils.java +++ b/src/main/java/fr/xephi/authme/util/CollectionUtils.java @@ -60,15 +60,13 @@ public final class CollectionUtils { return coll == null || coll.isEmpty(); } - public static List filterCommonStart(List coll1, List coll2) { + public static List filterCommonStart(List list1, List list2) { List commonStart = new ArrayList<>(); - int minSize = Math.min(coll1.size(), coll2.size()); - for (int i = 0; i < minSize; ++i) { - if (Objects.equals(coll1.get(i), coll2.get(i))) { - commonStart.add(coll1.get(i)); - } else { - break; - } + int minSize = Math.min(list1.size(), list2.size()); + int i = 0; + while (i < minSize && Objects.equals(list1.get(i), list2.get(i))) { + commonStart.add(list1.get(i)); + ++i; } return commonStart; } diff --git a/src/main/java/fr/xephi/authme/util/StringUtils.java b/src/main/java/fr/xephi/authme/util/StringUtils.java index 2ee24381..9ae0ec0f 100644 --- a/src/main/java/fr/xephi/authme/util/StringUtils.java +++ b/src/main/java/fr/xephi/authme/util/StringUtils.java @@ -117,12 +117,4 @@ public final class StringUtils { return "[" + th.getClass().getSimpleName() + "]: " + th.getMessage(); } - public static String repeat(String str, int times) { - StringBuilder sb = new StringBuilder(str.length() * times); - for (int i = 0; i < times; ++i) { - sb.append(str); - } - return sb.toString(); - } - } diff --git a/src/test/java/fr/xephi/authme/settings/custom/NewSettingTest.java b/src/test/java/fr/xephi/authme/settings/custom/NewSettingTest.java index 39a5062c..585f128e 100644 --- a/src/test/java/fr/xephi/authme/settings/custom/NewSettingTest.java +++ b/src/test/java/fr/xephi/authme/settings/custom/NewSettingTest.java @@ -1,5 +1,6 @@ package fr.xephi.authme.settings.custom; +import fr.xephi.authme.datasource.DataSource; import fr.xephi.authme.settings.custom.domain.Property; import fr.xephi.authme.settings.custom.domain.PropertyType; import org.bukkit.configuration.file.YamlConfiguration; @@ -83,12 +84,14 @@ public class NewSettingTest { String helpHeader = settings.getOption(newProperty("settings.helpHeader", "")); List unsafePasswords = settings.getOption( newProperty(PropertyType.STRING_LIST, "Security.unsafePasswords")); + DataSource.DataSourceType dataSourceType = settings.getOption(DatabaseSettings.BACKEND); // then assertThat(result, equalTo(22)); assertThat(systemName, equalTo(TestConfiguration.SYSTEM_NAME.getDefaultValue())); assertThat(helpHeader, equalTo("AuthMeReloaded")); assertThat(unsafePasswords, contains("123456", "qwerty", "54321")); + assertThat(dataSourceType, equalTo(DataSource.DataSourceType.MYSQL)); } private File getConfigFile() { diff --git a/src/test/resources/437-config-test.yml b/src/test/resources/437-config-test.yml index f0d9f11b..5057bea9 100644 --- a/src/test/resources/437-config-test.yml +++ b/src/test/resources/437-config-test.yml @@ -3,7 +3,7 @@ test: DataSource: # What type of database do you want to use? # Valid values: sqlite, mysql - backend: sqlite + backend: mysql # Enable database caching, should improve database performance caching: true settings: