#347 Add NewSetting to command service

- Adjust NewSetting constructor to match needs in AuthMe
- Add NewSetting to the command service
- See CaptchaCommand for a sample replacement from Settings to NewSetting
This commit is contained in:
ljacqu
2016-01-09 12:45:58 +01:00
parent 88629702f5
commit 3845c1e0eb
12 changed files with 107 additions and 60 deletions
@@ -6,7 +6,7 @@ import fr.xephi.authme.settings.domain.Property;
import fr.xephi.authme.settings.propertymap.PropertyMap;
import fr.xephi.authme.util.CollectionUtils;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.FileConfiguration;
import java.io.File;
import java.io.FileWriter;
@@ -22,41 +22,43 @@ import java.util.Map;
public class NewSetting {
private File file;
private YamlConfiguration configuration;
private FileConfiguration configuration;
/**
* Constructor.
* Loads the file as YAML and checks its integrity.
*
* @param configuration The configuration to interact with
* @param file The configuration file
*/
public NewSetting(File file) {
this.configuration = YamlConfiguration.loadConfiguration(file);
public NewSetting(FileConfiguration configuration, File file) {
this.configuration = configuration;
this.file = file;
PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
if (!containsAllSettings(propertyMap)) {
save(propertyMap);
}
// TODO ljacqu 20160109: Ensure that save() works as desired (i.e. that it always produces valid YAML)
// and then uncomment the lines below. Once this is uncommented, the checks in the old Settings.java should
// be removed as we should check to rewrite the config.yml file only at one place
// --------
// PropertyMap propertyMap = SettingsFieldRetriever.getAllPropertyFields();
// if (!containsAllSettings(propertyMap)) {
// save(propertyMap);
// }
}
/**
* Simple constructor for testing purposes. Does not check for all properties and
* never saves to the file.
* Constructor for testing purposes, allowing more options.
*
* @param yamlConfiguration The YamlConfiguration object to use
* @param configuration The FileConfiguration object to use
* @param file The file to write to
* @param propertyMap The property map whose properties should be verified for presence, or null to skip this
*/
@VisibleForTesting
NewSetting(YamlConfiguration yamlConfiguration, File file, PropertyMap propertyMap) {
this.configuration = yamlConfiguration;
NewSetting(FileConfiguration configuration, File file, PropertyMap propertyMap) {
this.configuration = configuration;
this.file = file;
if (propertyMap != null) {
if (!containsAllSettings(propertyMap)) {
save(propertyMap);
}
if (propertyMap != null && !containsAllSettings(propertyMap)) {
save(propertyMap);
}
}
@@ -67,7 +69,7 @@ public class NewSetting {
* @param <T> The property's type
* @return The property's value
*/
public <T> T getOption(Property<T> property) {
public <T> T getProperty(Property<T> property) {
return property.getFromFile(configuration);
}
@@ -1,6 +1,6 @@
package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.List;
@@ -19,7 +19,7 @@ class EnumPropertyType<E extends Enum<E>> extends PropertyType<E> {
}
@Override
public E getFromFile(Property<E> property, YamlConfiguration configuration) {
public E getFromFile(Property<E> property, FileConfiguration configuration) {
String textValue = configuration.getString(property.getPath());
if (textValue == null) {
return property.getDefaultValue();
@@ -34,7 +34,7 @@ class EnumPropertyType<E extends Enum<E>> extends PropertyType<E> {
}
@Override
public boolean contains(Property<E> property, YamlConfiguration configuration) {
public boolean contains(Property<E> property, FileConfiguration configuration) {
return super.contains(property, configuration)
&& mapToEnum(configuration.getString(property.getPath())) != null;
}
@@ -1,6 +1,6 @@
package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.Arrays;
import java.util.List;
@@ -60,7 +60,7 @@ public class Property<T> {
* @param configuration The configuration to read the value from
* @return The value, or default if not present
*/
public T getFromFile(YamlConfiguration configuration) {
public T getFromFile(FileConfiguration configuration) {
return type.getFromFile(this, configuration);
}
@@ -70,7 +70,7 @@ public class Property<T> {
* @param configuration The configuration to read the value from
* @return The property value as YAML
*/
public List<String> formatValueAsYaml(YamlConfiguration configuration) {
public List<String> formatValueAsYaml(FileConfiguration configuration) {
return type.asYaml(this, configuration);
}
@@ -80,7 +80,7 @@ public class Property<T> {
* @param configuration The configuration file to verify
* @return True if the property is present, false otherwise
*/
public boolean isPresent(YamlConfiguration configuration) {
public boolean isPresent(FileConfiguration configuration) {
return type.contains(this, configuration);
}
@@ -1,6 +1,6 @@
package fr.xephi.authme.settings.domain;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.ArrayList;
import java.util.List;
@@ -28,7 +28,7 @@ public abstract class PropertyType<T> {
* @param configuration The YAML configuration to read from
* @return The read value, or the default value if absent
*/
public abstract T getFromFile(Property<T> property, YamlConfiguration configuration);
public abstract T getFromFile(Property<T> property, FileConfiguration configuration);
/**
* Return the property's value (or its default) as YAML.
@@ -37,7 +37,7 @@ public abstract class PropertyType<T> {
* @param configuration The YAML configuration to read from
* @return The read value or its default in YAML format
*/
public List<String> asYaml(Property<T> property, YamlConfiguration configuration) {
public List<String> asYaml(Property<T> property, FileConfiguration configuration) {
return asYaml(getFromFile(property, configuration));
}
@@ -48,7 +48,7 @@ public abstract class PropertyType<T> {
* @param configuration The configuration to verify
* @return True if the property is present, false otherwise
*/
public boolean contains(Property<T> property, YamlConfiguration configuration) {
public boolean contains(Property<T> property, FileConfiguration configuration) {
return configuration.contains(property.getPath());
}
@@ -66,7 +66,7 @@ public abstract class PropertyType<T> {
*/
private static final class BooleanProperty extends PropertyType<Boolean> {
@Override
public Boolean getFromFile(Property<Boolean> property, YamlConfiguration configuration) {
public Boolean getFromFile(Property<Boolean> property, FileConfiguration configuration) {
return configuration.getBoolean(property.getPath(), property.getDefaultValue());
}
@@ -81,7 +81,7 @@ public abstract class PropertyType<T> {
*/
private static final class DoubleProperty extends PropertyType<Double> {
@Override
public Double getFromFile(Property<Double> property, YamlConfiguration configuration) {
public Double getFromFile(Property<Double> property, FileConfiguration configuration) {
return configuration.getDouble(property.getPath(), property.getDefaultValue());
}
@@ -96,7 +96,7 @@ public abstract class PropertyType<T> {
*/
private static final class IntegerProperty extends PropertyType<Integer> {
@Override
public Integer getFromFile(Property<Integer> property, YamlConfiguration configuration) {
public Integer getFromFile(Property<Integer> property, FileConfiguration configuration) {
return configuration.getInt(property.getPath(), property.getDefaultValue());
}
@@ -111,7 +111,7 @@ public abstract class PropertyType<T> {
*/
private static final class StringProperty extends PropertyType<String> {
@Override
public String getFromFile(Property<String> property, YamlConfiguration configuration) {
public String getFromFile(Property<String> property, FileConfiguration configuration) {
return configuration.getString(property.getPath(), property.getDefaultValue());
}
@@ -131,7 +131,7 @@ public abstract class PropertyType<T> {
*/
private static final class StringListProperty extends PropertyType<List<String>> {
@Override
public List<String> getFromFile(Property<List<String>> property, YamlConfiguration configuration) {
public List<String> getFromFile(Property<List<String>> property, FileConfiguration configuration) {
if (!configuration.isList(property.getPath())) {
return property.getDefaultValue();
}
@@ -154,7 +154,7 @@ public abstract class PropertyType<T> {
}
@Override
public boolean contains(Property<List<String>> property, YamlConfiguration configuration) {
public boolean contains(Property<List<String>> property, FileConfiguration configuration) {
return configuration.contains(property.getPath()) && configuration.isList(property.getPath());
}
}