Update configme (#1631)

* Upgrade to ConfigMe 1.0.1
* Use ConfigMe reader whenever possible, minor simplifications
This commit is contained in:
ljacqu 2018-09-09 15:45:00 +02:00 committed by GitHub
parent 6676a39447
commit ee764c0a6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
42 changed files with 516 additions and 555 deletions

View File

@ -543,7 +543,7 @@
<dependency> <dependency>
<groupId>ch.jalu</groupId> <groupId>ch.jalu</groupId>
<artifactId>configme</artifactId> <artifactId>configme</artifactId>
<version>0.4.1</version> <version>1.0.1</version>
<optional>true</optional> <optional>true</optional>
<exclusions> <exclusions>
<exclusion> <exclusion>

View File

@ -39,7 +39,7 @@ public class JarMessageSource {
} }
private static String getString(String path, PropertyReader reader) { private static String getString(String path, PropertyReader reader) {
return reader == null ? null : reader.getTypedObject(path, String.class); return reader == null ? null : reader.getString(path);
} }
private static MessageMigraterPropertyReader loadJarFile(String jarPath) { private static MessageMigraterPropertyReader loadJarFile(String jarPath) {

View File

@ -0,0 +1,43 @@
package fr.xephi.authme.message.updater;
import ch.jalu.configme.configurationdata.ConfigurationDataImpl;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyReader;
import fr.xephi.authme.message.MessageKey;
import java.util.List;
import java.util.Map;
public class MessageKeyConfigurationData extends ConfigurationDataImpl {
/**
* Constructor.
*
* @param propertyListBuilder property list builder for message key properties
* @param allComments registered comments
*/
public MessageKeyConfigurationData(MessageUpdater.MessageKeyPropertyListBuilder propertyListBuilder,
Map<String, List<String>> allComments) {
super(propertyListBuilder.getAllProperties(), allComments);
}
@Override
public void initializeValues(PropertyReader reader) {
getAllMessageProperties().stream()
.filter(prop -> prop.isPresent(reader))
.forEach(prop -> setValue(prop, prop.determineValue(reader)));
}
@SuppressWarnings("unchecked")
public List<Property<String>> getAllMessageProperties() {
return (List) getProperties();
}
public String getMessage(MessageKey messageKey) {
return getValue(new MessageUpdater.MessageKeyProperty(messageKey));
}
public void setMessage(MessageKey messageKey, String message) {
setValue(new MessageUpdater.MessageKeyProperty(messageKey), message);
}
}

View File

@ -12,20 +12,19 @@ import java.io.InputStreamReader;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Set;
/** /**
* Implementation of {@link PropertyReader} which can read a file or a stream with * Implementation of {@link PropertyReader} which can read a file or a stream with
* a specified charset. * a specified charset.
*/ */
public final class MessageMigraterPropertyReader implements PropertyReader { final class MessageMigraterPropertyReader implements PropertyReader {
public static final Charset CHARSET = StandardCharsets.UTF_8; private static final Charset CHARSET = StandardCharsets.UTF_8;
private Map<String, Object> root; private Map<String, Object> root;
/** See same field in {@link ch.jalu.configme.resource.YamlFileReader} for details. */
private boolean hasObjectAsRoot = false;
private MessageMigraterPropertyReader(Map<String, Object> valuesMap) { private MessageMigraterPropertyReader(Map<String, Object> valuesMap) {
root = valuesMap; root = valuesMap;
@ -38,14 +37,11 @@ public final class MessageMigraterPropertyReader implements PropertyReader {
* @return the created property reader * @return the created property reader
*/ */
public static MessageMigraterPropertyReader loadFromFile(File file) { public static MessageMigraterPropertyReader loadFromFile(File file) {
Map<String, Object> valuesMap;
try (InputStream is = new FileInputStream(file)) { try (InputStream is = new FileInputStream(file)) {
valuesMap = readStreamToMap(is); return loadFromStream(is);
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException("Error while reading file '" + file + "'", e); throw new IllegalStateException("Error while reading file '" + file + "'", e);
} }
return new MessageMigraterPropertyReader(valuesMap);
} }
public static MessageMigraterPropertyReader loadFromStream(InputStream inputStream) { public static MessageMigraterPropertyReader loadFromStream(InputStream inputStream) {
@ -53,10 +49,20 @@ public final class MessageMigraterPropertyReader implements PropertyReader {
return new MessageMigraterPropertyReader(valuesMap); return new MessageMigraterPropertyReader(valuesMap);
} }
@Override
public boolean contains(String path) {
return getObject(path) != null;
}
@Override
public Set<String> getKeys(boolean b) {
throw new UnsupportedOperationException();
}
@Override @Override
public Object getObject(String path) { public Object getObject(String path) {
if (path.isEmpty()) { if (path.isEmpty()) {
return hasObjectAsRoot ? root.get("") : root; return root.get("");
} }
Object node = root; Object node = root;
String[] keys = path.split("\\."); String[] keys = path.split("\\.");
@ -70,66 +76,29 @@ public final class MessageMigraterPropertyReader implements PropertyReader {
} }
@Override @Override
public <T> T getTypedObject(String path, Class<T> clazz) { public String getString(String path) {
Object value = getObject(path); Object o = getObject(path);
if (clazz.isInstance(value)) { return o instanceof String ? (String) o : null;
return clazz.cast(value);
}
return null;
} }
@Override @Override
public void set(String path, Object value) { public Integer getInt(String path) {
Objects.requireNonNull(path); throw new UnsupportedOperationException();
if (path.isEmpty()) {
root.clear();
root.put("", value);
hasObjectAsRoot = true;
} else if (hasObjectAsRoot) {
throw new ConfigMeException("The root path is a bean property; you cannot set values to any subpath. "
+ "Modify the bean at the root or set a new one instead.");
} else {
setValueInChildPath(path, value);
}
}
/**
* Sets the value at the given path. This method is used when the root is a map and not a specific object.
*
* @param path the path to set the value at
* @param value the value to set
*/
@SuppressWarnings("unchecked")
private void setValueInChildPath(String path, Object value) {
Map<String, Object> node = root;
String[] keys = path.split("\\.");
for (int i = 0; i < keys.length - 1; ++i) {
Object child = node.get(keys[i]);
if (child instanceof Map<?, ?>) {
node = (Map<String, Object>) child;
} else { // child is null or some other value - replace with map
Map<String, Object> newEntry = new HashMap<>();
node.put(keys[i], newEntry);
if (value == null) {
// For consistency, replace whatever value/null here with an empty map,
// but if the value is null our work here is done.
return;
}
node = newEntry;
}
}
// node now contains the parent map (existing or newly created)
if (value == null) {
node.remove(keys[keys.length - 1]);
} else {
node.put(keys[keys.length - 1], value);
}
} }
@Override @Override
public void reload() { public Double getDouble(String path) {
throw new UnsupportedOperationException("Reload not supported by this implementation"); throw new UnsupportedOperationException();
}
@Override
public Boolean getBoolean(String path) {
throw new UnsupportedOperationException();
}
@Override
public List<?> getList(String path) {
throw new UnsupportedOperationException();
} }
private static Map<String, Object> readStreamToMap(InputStream inputStream) { private static Map<String, Object> readStreamToMap(InputStream inputStream) {

View File

@ -1,10 +1,10 @@
package fr.xephi.authme.message.updater; package fr.xephi.authme.message.updater;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.PropertyListBuilder; import ch.jalu.configme.configurationdata.PropertyListBuilder;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.StringProperty; import ch.jalu.configme.properties.StringProperty;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyResource;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.io.Files; import com.google.common.io.Files;
@ -20,21 +20,15 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static java.util.Collections.singletonList;
/** /**
* Migrates the used messages file to a complete, up-to-date version when necessary. * Migrates the used messages file to a complete, up-to-date version when necessary.
*/ */
public class MessageUpdater { public class MessageUpdater {
/**
* Configuration data object for all message keys incl. comments associated to sections.
*/
private static final ConfigurationData CONFIGURATION_DATA = buildConfigurationData();
public static ConfigurationData getConfigurationData() {
return CONFIGURATION_DATA;
}
/** /**
* Applies any necessary migrations to the user's messages file and saves it if it has been modified. * Applies any necessary migrations to the user's messages file and saves it if it has been modified.
* *
@ -57,52 +51,57 @@ public class MessageUpdater {
*/ */
private boolean migrateAndSave(File userFile, JarMessageSource jarMessageSource) { private boolean migrateAndSave(File userFile, JarMessageSource jarMessageSource) {
// YamlConfiguration escapes all special characters when saving, making the file hard to use, so use ConfigMe // YamlConfiguration escapes all special characters when saving, making the file hard to use, so use ConfigMe
MessageKeyConfigurationData configurationData = createConfigurationData();
PropertyResource userResource = new MigraterYamlFileResource(userFile); PropertyResource userResource = new MigraterYamlFileResource(userFile);
PropertyReader reader = userResource.createReader();
configurationData.initializeValues(reader);
// Step 1: Migrate any old keys in the file to the new paths // Step 1: Migrate any old keys in the file to the new paths
boolean movedOldKeys = migrateOldKeys(userResource); boolean movedOldKeys = migrateOldKeys(reader, configurationData);
// Step 2: Perform newer migrations // Step 2: Perform newer migrations
boolean movedNewerKeys = migrateKeys(userResource); boolean movedNewerKeys = migrateKeys(reader, configurationData);
// Step 3: Take any missing messages from the message files shipped in the AuthMe JAR // Step 3: Take any missing messages from the message files shipped in the AuthMe JAR
boolean addedMissingKeys = addMissingKeys(jarMessageSource, userResource); boolean addedMissingKeys = addMissingKeys(jarMessageSource, configurationData);
if (movedOldKeys || movedNewerKeys || addedMissingKeys) { if (movedOldKeys || movedNewerKeys || addedMissingKeys) {
backupMessagesFile(userFile); backupMessagesFile(userFile);
SettingsManager settingsManager = new SettingsManager(userResource, null, CONFIGURATION_DATA); userResource.exportProperties(configurationData);
settingsManager.save();
ConsoleLogger.debug("Successfully saved {0}", userFile); ConsoleLogger.debug("Successfully saved {0}", userFile);
return true; return true;
} }
return false; return false;
} }
private boolean migrateKeys(PropertyResource userResource) { private boolean migrateKeys(PropertyReader propertyReader, MessageKeyConfigurationData configurationData) {
return moveIfApplicable(userResource, "misc.two_factor_create", MessageKey.TWO_FACTOR_CREATE.getKey()); return moveIfApplicable(propertyReader, configurationData,
"misc.two_factor_create", MessageKey.TWO_FACTOR_CREATE);
} }
private static boolean moveIfApplicable(PropertyResource resource, String oldPath, String newPath) { private static boolean moveIfApplicable(PropertyReader reader, MessageKeyConfigurationData configurationData,
if (resource.getString(newPath) == null && resource.getString(oldPath) != null) { String oldPath, MessageKey messageKey) {
resource.setValue(newPath, resource.getString(oldPath)); if (configurationData.getMessage(messageKey) == null && reader.getString(oldPath) != null) {
configurationData.setMessage(messageKey, reader.getString(oldPath));
return true; return true;
} }
return false; return false;
} }
private boolean migrateOldKeys(PropertyResource userResource) { private boolean migrateOldKeys(PropertyReader propertyReader, MessageKeyConfigurationData configurationData) {
boolean hasChange = OldMessageKeysMigrater.migrateOldPaths(userResource); boolean hasChange = OldMessageKeysMigrater.migrateOldPaths(propertyReader, configurationData);
if (hasChange) { if (hasChange) {
ConsoleLogger.info("Old keys have been moved to the new ones in your messages_xx.yml file"); ConsoleLogger.info("Old keys have been moved to the new ones in your messages_xx.yml file");
} }
return hasChange; return hasChange;
} }
private boolean addMissingKeys(JarMessageSource jarMessageSource, PropertyResource userResource) { private boolean addMissingKeys(JarMessageSource jarMessageSource, MessageKeyConfigurationData configurationData) {
List<String> addedKeys = new ArrayList<>(); List<String> addedKeys = new ArrayList<>();
for (Property<?> property : CONFIGURATION_DATA.getProperties()) { for (Property<String> property : configurationData.getAllMessageProperties()) {
final String key = property.getPath(); final String key = property.getPath();
if (userResource.getString(key) == null) { if (configurationData.getValue(property) == null) {
userResource.setValue(key, jarMessageSource.getMessageFromJar(property)); configurationData.setValue(property, jarMessageSource.getMessageFromJar(property));
addedKeys.add(key); addedKeys.add(key);
} }
} }
@ -129,40 +128,68 @@ public class MessageUpdater {
* *
* @return the configuration data to export with * @return the configuration data to export with
*/ */
private static ConfigurationData buildConfigurationData() { public static MessageKeyConfigurationData createConfigurationData() {
Map<String, String[]> comments = ImmutableMap.<String, String[]>builder() Map<String, String> comments = ImmutableMap.<String, String>builder()
.put("registration", new String[]{"Registration"}) .put("registration", "Registration")
.put("password", new String[]{"Password errors on registration"}) .put("password", "Password errors on registration")
.put("login", new String[]{"Login"}) .put("login", "Login")
.put("error", new String[]{"Errors"}) .put("error", "Errors")
.put("antibot", new String[]{"AntiBot"}) .put("antibot", "AntiBot")
.put("unregister", new String[]{"Unregister"}) .put("unregister", "Unregister")
.put("misc", new String[]{"Other messages"}) .put("misc", "Other messages")
.put("session", new String[]{"Session messages"}) .put("session", "Session messages")
.put("on_join_validation", new String[]{"Error messages when joining"}) .put("on_join_validation", "Error messages when joining")
.put("email", new String[]{"Email"}) .put("email", "Email")
.put("recovery", new String[]{"Password recovery by email"}) .put("recovery", "Password recovery by email")
.put("captcha", new String[]{"Captcha"}) .put("captcha", "Captcha")
.put("verification", new String[]{"Verification code"}) .put("verification", "Verification code")
.put("time", new String[]{"Time units"}) .put("time", "Time units")
.put("two_factor", new String[]{"Two-factor authentication"}) .put("two_factor", "Two-factor authentication")
.build(); .build();
Set<String> addedKeys = new HashSet<>(); Set<String> addedKeys = new HashSet<>();
PropertyListBuilder builder = new PropertyListBuilder(); MessageKeyPropertyListBuilder builder = new MessageKeyPropertyListBuilder();
// Add one key per section based on the comments map above so that the order is clear // Add one key per section based on the comments map above so that the order is clear
for (String path : comments.keySet()) { for (String path : comments.keySet()) {
MessageKey key = Arrays.stream(MessageKey.values()).filter(p -> p.getKey().startsWith(path + ".")) MessageKey key = Arrays.stream(MessageKey.values()).filter(p -> p.getKey().startsWith(path + "."))
.findFirst().orElseThrow(() -> new IllegalStateException(path)); .findFirst().orElseThrow(() -> new IllegalStateException(path));
builder.add(new StringProperty(key.getKey(), "")); builder.addMessageKey(key);
addedKeys.add(key.getKey()); addedKeys.add(key.getKey());
} }
// Add all remaining keys to the property list builder // Add all remaining keys to the property list builder
Arrays.stream(MessageKey.values()) Arrays.stream(MessageKey.values())
.filter(key -> !addedKeys.contains(key.getKey())) .filter(key -> !addedKeys.contains(key.getKey()))
.forEach(key -> builder.add(new StringProperty(key.getKey(), ""))); .forEach(builder::addMessageKey);
return new ConfigurationData(builder.create(), comments); // Create ConfigurationData instance
Map<String, List<String>> commentsMap = comments.entrySet().stream()
.collect(Collectors.toMap(e -> e.getKey(), e -> singletonList(e.getValue())));
return new MessageKeyConfigurationData(builder, commentsMap);
} }
static final class MessageKeyProperty extends StringProperty {
MessageKeyProperty(MessageKey messageKey) {
super(messageKey.getKey(), "");
}
@Override
protected String getFromReader(PropertyReader reader) {
return reader.getString(getPath());
}
}
static final class MessageKeyPropertyListBuilder {
private PropertyListBuilder propertyListBuilder = new PropertyListBuilder();
void addMessageKey(MessageKey key) {
propertyListBuilder.add(new MessageKeyProperty(key));
}
@SuppressWarnings("unchecked")
List<MessageKeyProperty> getAllProperties() {
return (List) propertyListBuilder.create();
}
}
} }

View File

@ -1,41 +1,30 @@
package fr.xephi.authme.message.updater; package fr.xephi.authme.message.updater;
import ch.jalu.configme.beanmapper.leafproperties.LeafPropertiesGenerator; import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.exception.ConfigMeException;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyPathTraverser;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.resource.YamlFileResource;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import static fr.xephi.authme.message.updater.MessageMigraterPropertyReader.CHARSET;
/** /**
* Extension of {@link YamlFileResource} to fine-tune the export style * Extension of {@link YamlFileResource} to fine-tune the export style.
* and to be able to specify the character encoding.
*/ */
public class MigraterYamlFileResource extends YamlFileResource { public class MigraterYamlFileResource extends YamlFileResource {
private static final String INDENTATION = " ";
private final File file;
private Yaml singleQuoteYaml; private Yaml singleQuoteYaml;
public MigraterYamlFileResource(File file) { public MigraterYamlFileResource(File file) {
super(file, MessageMigraterPropertyReader.loadFromFile(file), new LeafPropertiesGenerator()); super(file);
this.file = file;
} }
@Override @Override
protected Yaml getSingleQuoteYaml() { public PropertyReader createReader() {
return MessageMigraterPropertyReader.loadFromFile(getFile());
}
@Override
protected Yaml createNewYaml() {
if (singleQuoteYaml == null) { if (singleQuoteYaml == null) {
DumperOptions options = new DumperOptions(); DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
@ -47,57 +36,4 @@ public class MigraterYamlFileResource extends YamlFileResource {
} }
return singleQuoteYaml; return singleQuoteYaml;
} }
@Override
public void exportProperties(ConfigurationData configurationData) {
try (FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter writer = new OutputStreamWriter(fos, CHARSET)) {
PropertyPathTraverser pathTraverser = new PropertyPathTraverser(configurationData);
for (Property<?> property : convertPropertiesToExportableTypes(configurationData.getProperties())) {
List<PropertyPathTraverser.PathElement> pathElements = pathTraverser.getPathElements(property);
for (PropertyPathTraverser.PathElement pathElement : pathElements) {
writeComments(writer, pathElement.indentationLevel, pathElement.comments);
writer.append("\n")
.append(indent(pathElement.indentationLevel))
.append(pathElement.name)
.append(":");
}
writer.append(" ")
.append(toYaml(property, pathElements.get(pathElements.size() - 1).indentationLevel));
}
writer.flush();
writer.close();
} catch (IOException e) {
throw new ConfigMeException("Could not save config to '" + file.getPath() + "'", e);
} finally {
singleQuoteYaml = null;
}
}
private void writeComments(Writer writer, int indentation, String[] comments) throws IOException {
if (comments.length == 0) {
return;
}
String commentStart = "\n" + indent(indentation) + "# ";
for (String comment : comments) {
writer.append(commentStart).append(comment);
}
}
private <T> String toYaml(Property<T> property, int indent) {
Object value = property.getValue(this);
String representation = transformValue(property, value);
String[] lines = representation.split("\\n");
return String.join("\n" + indent(indent), lines);
}
private static String indent(int level) {
String result = "";
for (int i = 0; i < level; i++) {
result += INDENTATION;
}
return result;
}
} }

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.message.updater; package fr.xephi.authme.message.updater;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyReader;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.MessageKey;
@ -16,7 +16,6 @@ import static com.google.common.collect.ImmutableMap.of;
*/ */
final class OldMessageKeysMigrater { final class OldMessageKeysMigrater {
@VisibleForTesting @VisibleForTesting
static final Map<MessageKey, String> KEYS_TO_OLD_PATH = ImmutableMap.<MessageKey, String>builder() static final Map<MessageKey, String> KEYS_TO_OLD_PATH = ImmutableMap.<MessageKey, String>builder()
.put(MessageKey.LOGIN_SUCCESS, "login") .put(MessageKey.LOGIN_SUCCESS, "login")
@ -130,23 +129,26 @@ final class OldMessageKeysMigrater {
/** /**
* Migrates any existing old key paths to their new paths if no text has been defined for the new key. * Migrates any existing old key paths to their new paths if no text has been defined for the new key.
* *
* @param resource the resource to modify and read from * @param reader the property reader to get values from
* @param configurationData the configuration data to write to
* @return true if at least one message could be migrated, false otherwise * @return true if at least one message could be migrated, false otherwise
*/ */
static boolean migrateOldPaths(PropertyResource resource) { static boolean migrateOldPaths(PropertyReader reader, MessageKeyConfigurationData configurationData) {
boolean wasPropertyMoved = false; boolean wasPropertyMoved = false;
for (Map.Entry<MessageKey, String> migrationEntry : KEYS_TO_OLD_PATH.entrySet()) { for (Map.Entry<MessageKey, String> migrationEntry : KEYS_TO_OLD_PATH.entrySet()) {
wasPropertyMoved |= moveIfApplicable(resource, migrationEntry.getKey(), migrationEntry.getValue()); wasPropertyMoved |= moveIfApplicable(reader, configurationData,
migrationEntry.getKey(), migrationEntry.getValue());
} }
return wasPropertyMoved; return wasPropertyMoved;
} }
private static boolean moveIfApplicable(PropertyResource resource, MessageKey messageKey, String oldPath) { private static boolean moveIfApplicable(PropertyReader reader, MessageKeyConfigurationData configurationData,
if (resource.getString(messageKey.getKey()) == null) { MessageKey messageKey, String oldPath) {
String textAtOldPath = resource.getString(oldPath); if (configurationData.getMessage(messageKey) == null) {
String textAtOldPath = reader.getString(oldPath);
if (textAtOldPath != null) { if (textAtOldPath != null) {
textAtOldPath = replaceOldPlaceholders(messageKey, textAtOldPath); textAtOldPath = replaceOldPlaceholders(messageKey, textAtOldPath);
resource.setValue(messageKey.getKey(), textAtOldPath); configurationData.setMessage(messageKey, textAtOldPath);
return true; return true;
} }
} }

View File

@ -23,7 +23,6 @@ import org.bukkit.entity.Player;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -45,7 +44,6 @@ public class ValidationService implements Reloadable {
private GeoIpService geoIpService; private GeoIpService geoIpService;
private Pattern passwordRegex; private Pattern passwordRegex;
private Set<String> unrestrictedNames;
private Multimap<String, String> restrictedNames; private Multimap<String, String> restrictedNames;
ValidationService() { ValidationService() {
@ -55,8 +53,6 @@ public class ValidationService implements Reloadable {
@Override @Override
public void reload() { public void reload() {
passwordRegex = Utils.safePatternCompile(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)); passwordRegex = Utils.safePatternCompile(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX));
// Use Set for more efficient contains() lookup
unrestrictedNames = new HashSet<>(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES));
restrictedNames = settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS) restrictedNames = settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)
? loadNameRestrictions(settings.getProperty(RestrictionSettings.RESTRICTED_USERS)) ? loadNameRestrictions(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
: HashMultimap.create(); : HashMultimap.create();
@ -140,7 +136,7 @@ public class ValidationService implements Reloadable {
* @return true if unrestricted, false otherwise * @return true if unrestricted, false otherwise
*/ */
public boolean isUnrestricted(String name) { public boolean isUnrestricted(String name) {
return unrestrictedNames.contains(name.toLowerCase()); return settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES).contains(name.toLowerCase());
} }
/** /**
@ -208,7 +204,7 @@ public class ValidationService implements Reloadable {
* @param configuredRestrictions the restriction rules to convert to a map * @param configuredRestrictions the restriction rules to convert to a map
* @return map of allowed IPs/domain names by player name * @return map of allowed IPs/domain names by player name
*/ */
private Multimap<String, String> loadNameRestrictions(List<String> configuredRestrictions) { private Multimap<String, String> loadNameRestrictions(Set<String> configuredRestrictions) {
Multimap<String, String> restrictions = HashMultimap.create(); Multimap<String, String> restrictions = HashMultimap.create();
for (String restriction : configuredRestrictions) { for (String restriction : configuredRestrictions) {
if (isInsideString(';', restriction)) { if (isInsideString(';', restriction)) {

View File

@ -1,7 +1,8 @@
package fr.xephi.authme.service.yaml; package fr.xephi.authme.service.yaml;
import ch.jalu.configme.exception.ConfigMeException;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.resource.YamlFileResource;
import org.yaml.snakeyaml.parser.ParserException;
import java.io.File; import java.io.File;
@ -15,16 +16,32 @@ public final class YamlFileResourceProvider {
/** /**
* Creates a {@link YamlFileResource} instance for the given file. Wraps SnakeYAML's parse exception * Creates a {@link YamlFileResource} instance for the given file. Wraps SnakeYAML's parse exception
* into an AuthMe exception. * thrown when a reader is created into an AuthMe exception.
* *
* @param file the file to load * @param file the file to load
* @return the generated resource * @return the generated resource
*/ */
public static YamlFileResource loadFromFile(File file) { public static YamlFileResource loadFromFile(File file) {
try { return new AuthMeYamlFileResource(file);
return new YamlFileResource(file); }
} catch (ParserException e) {
throw new YamlParseException(file.getPath(), e); /**
* Extension of {@link YamlFileResource} which wraps SnakeYAML's parse exception into a custom
* exception when a reader is created.
*/
private static final class AuthMeYamlFileResource extends YamlFileResource {
AuthMeYamlFileResource(File file) {
super(file);
}
@Override
public PropertyReader createReader() {
try {
return super.createReader();
} catch (ConfigMeException e) {
throw new YamlParseException(getFile().getPath(), e);
}
} }
} }
} }

View File

@ -1,6 +1,8 @@
package fr.xephi.authme.service.yaml; package fr.xephi.authme.service.yaml;
import org.yaml.snakeyaml.parser.ParserException; import ch.jalu.configme.exception.ConfigMeException;
import static com.google.common.base.MoreObjects.firstNonNull;
/** /**
* Exception when a YAML file could not be parsed. * Exception when a YAML file could not be parsed.
@ -13,10 +15,10 @@ public class YamlParseException extends RuntimeException {
* Constructor. * Constructor.
* *
* @param file the file a parsing exception occurred with * @param file the file a parsing exception occurred with
* @param snakeYamlException the caught exception from SnakeYAML * @param configMeException the caught exception from ConfigMe
*/ */
public YamlParseException(String file, ParserException snakeYamlException) { public YamlParseException(String file, ConfigMeException configMeException) {
super(snakeYamlException); super(firstNonNull(configMeException.getCause(), configMeException));
this.file = file; this.file = file;
} }

View File

@ -1,7 +1,7 @@
package fr.xephi.authme.settings; package fr.xephi.authme.settings;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.BaseProperty;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyReader;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
@ -15,7 +15,7 @@ import static com.google.common.collect.Sets.newHashSet;
* *
* @param <E> the enum type * @param <E> the enum type
*/ */
public class EnumSetProperty<E extends Enum<E>> extends Property<Set<E>> { public class EnumSetProperty<E extends Enum<E>> extends BaseProperty<Set<E>> {
private final Class<E> enumClass; private final Class<E> enumClass;
@ -26,8 +26,8 @@ public class EnumSetProperty<E extends Enum<E>> extends Property<Set<E>> {
} }
@Override @Override
protected Set<E> getFromResource(PropertyResource resource) { protected Set<E> getFromReader(PropertyReader reader) {
Object entry = resource.getObject(getPath()); Object entry = reader.getObject(getPath());
if (entry instanceof Collection<?>) { if (entry instanceof Collection<?>) {
return ((Collection<?>) entry).stream() return ((Collection<?>) entry).stream()
.map(val -> toEnum(String.valueOf(val))) .map(val -> toEnum(String.valueOf(val)))
@ -45,4 +45,11 @@ public class EnumSetProperty<E extends Enum<E>> extends Property<Set<E>> {
} }
return null; return null;
} }
@Override
public Object toExportValue(Set<E> value) {
return value.stream()
.map(Enum::name)
.collect(Collectors.toList());
}
} }

View File

@ -1,6 +1,6 @@
package fr.xephi.authme.settings; package fr.xephi.authme.settings;
import ch.jalu.configme.SettingsManager; import ch.jalu.configme.SettingsManagerImpl;
import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.migration.MigrationService; import ch.jalu.configme.migration.MigrationService;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyResource;
@ -16,7 +16,7 @@ import static fr.xephi.authme.util.FileUtils.copyFileFromResource;
/** /**
* The AuthMe settings manager. * The AuthMe settings manager.
*/ */
public class Settings extends SettingsManager { public class Settings extends SettingsManagerImpl {
private final File pluginFolder; private final File pluginFolder;
private String passwordEmailMessage; private String passwordEmailMessage;
@ -33,7 +33,7 @@ public class Settings extends SettingsManager {
*/ */
public Settings(File pluginFolder, PropertyResource resource, MigrationService migrationService, public Settings(File pluginFolder, PropertyResource resource, MigrationService migrationService,
ConfigurationData configurationData) { ConfigurationData configurationData) {
super(resource, migrationService, configurationData); super(resource, configurationData, migrationService);
this.pluginFolder = pluginFolder; this.pluginFolder = pluginFolder;
loadSettingsFromFiles(); loadSettingsFromFiles();
} }

View File

@ -1,8 +1,9 @@
package fr.xephi.authme.settings; package fr.xephi.authme.settings;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.migration.PlainMigrationService; import ch.jalu.configme.migration.PlainMigrationService;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyReader;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.initialization.DataFolder;
@ -25,6 +26,7 @@ import java.util.Set;
import static ch.jalu.configme.properties.PropertyInitializer.newListProperty; import static ch.jalu.configme.properties.PropertyInitializer.newListProperty;
import static ch.jalu.configme.properties.PropertyInitializer.newProperty; import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
import static fr.xephi.authme.settings.properties.DatabaseSettings.MYSQL_POOL_SIZE;
import static fr.xephi.authme.settings.properties.RegistrationSettings.DELAY_JOIN_MESSAGE; 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_JOIN_MESSAGE;
import static fr.xephi.authme.settings.properties.RegistrationSettings.REMOVE_LEAVE_MESSAGE; import static fr.xephi.authme.settings.properties.RegistrationSettings.REMOVE_LEAVE_MESSAGE;
@ -53,33 +55,33 @@ public class SettingsMigrationService extends PlainMigrationService {
@Override @Override
@SuppressWarnings("checkstyle:BooleanExpressionComplexity") @SuppressWarnings("checkstyle:BooleanExpressionComplexity")
protected boolean performMigrations(PropertyResource resource, List<Property<?>> properties) { protected boolean performMigrations(PropertyReader reader, ConfigurationData configurationData) {
boolean changes = false; boolean changes = false;
if ("[a-zA-Z0-9_?]*".equals(resource.getString(ALLOWED_NICKNAME_CHARACTERS.getPath()))) { if ("[a-zA-Z0-9_?]*".equals(reader.getString(ALLOWED_NICKNAME_CHARACTERS.getPath()))) {
resource.setValue(ALLOWED_NICKNAME_CHARACTERS.getPath(), "[a-zA-Z0-9_]*"); configurationData.setValue(ALLOWED_NICKNAME_CHARACTERS, "[a-zA-Z0-9_]*");
changes = true; changes = true;
} }
setOldOtherAccountsCommandFieldsIfSet(resource); setOldOtherAccountsCommandFieldsIfSet(reader);
// Note ljacqu 20160211: Concatenating migration methods with | instead of the usual || // Note ljacqu 20160211: Concatenating migration methods with | instead of the usual ||
// ensures that all migrations will be performed // ensures that all migrations will be performed
return changes return changes
| performMailTextToFileMigration(resource) | performMailTextToFileMigration(reader)
| migrateJoinLeaveMessages(resource) | migrateJoinLeaveMessages(reader, configurationData)
| migrateForceSpawnSettings(resource) | migrateForceSpawnSettings(reader, configurationData)
| migratePoolSizeSetting(resource) | migratePoolSizeSetting(reader, configurationData)
| changeBooleanSettingToLogLevelProperty(resource) | changeBooleanSettingToLogLevelProperty(reader, configurationData)
| hasOldHelpHeaderProperty(resource) | hasOldHelpHeaderProperty(reader)
| hasSupportOldPasswordProperty(resource) | hasSupportOldPasswordProperty(reader)
| convertToRegistrationType(resource) | convertToRegistrationType(reader, configurationData)
| mergeAndMovePermissionGroupSettings(resource) | mergeAndMovePermissionGroupSettings(reader, configurationData)
| moveDeprecatedHashAlgorithmIntoLegacySection(resource) | moveDeprecatedHashAlgorithmIntoLegacySection(reader, configurationData)
| moveSaltColumnConfigWithOtherColumnConfigs(resource) | moveSaltColumnConfigWithOtherColumnConfigs(reader, configurationData)
|| hasDeprecatedProperties(resource); || hasDeprecatedProperties(reader);
} }
private static boolean hasDeprecatedProperties(PropertyResource resource) { private static boolean hasDeprecatedProperties(PropertyReader reader) {
String[] deprecatedProperties = { String[] deprecatedProperties = {
"Converter.Rakamak.newPasswordHash", "Hooks.chestshop", "Hooks.legacyChestshop", "Hooks.notifications", "Converter.Rakamak.newPasswordHash", "Hooks.chestshop", "Hooks.legacyChestshop", "Hooks.notifications",
"Passpartu", "Performances", "settings.restrictions.enablePasswordVerifier", "Xenoforo.predefinedSalt", "Passpartu", "Performances", "settings.restrictions.enablePasswordVerifier", "Xenoforo.predefinedSalt",
@ -90,7 +92,7 @@ public class SettingsMigrationService extends PlainMigrationService {
"settings.sessions.sessionExpireOnIpChange", "settings.restrictions.otherAccountsCmd", "settings.sessions.sessionExpireOnIpChange", "settings.restrictions.otherAccountsCmd",
"settings.restrictions.otherAccountsCmdThreshold"}; "settings.restrictions.otherAccountsCmdThreshold"};
for (String deprecatedPath : deprecatedProperties) { for (String deprecatedPath : deprecatedProperties) {
if (resource.contains(deprecatedPath)) { if (reader.contains(deprecatedPath)) {
return true; return true;
} }
} }
@ -119,12 +121,12 @@ public class SettingsMigrationService extends PlainMigrationService {
/** /**
* Check if {@code Email.mailText} is present and move it to the Email.html file if it doesn't exist yet. * Check if {@code Email.mailText} is present and move it to the Email.html file if it doesn't exist yet.
* *
* @param resource The property resource * @param reader The property reader
* @return True if a migration has been completed, false otherwise * @return True if a migration has been completed, false otherwise
*/ */
private boolean performMailTextToFileMigration(PropertyResource resource) { private boolean performMailTextToFileMigration(PropertyReader reader) {
final String oldSettingPath = "Email.mailText"; final String oldSettingPath = "Email.mailText";
final String oldMailText = resource.getString(oldSettingPath); final String oldMailText = reader.getString(oldSettingPath);
if (oldMailText == null) { if (oldMailText == null) {
return false; return false;
} }
@ -149,12 +151,13 @@ public class SettingsMigrationService extends PlainMigrationService {
* Detect deprecated {@code settings.delayJoinLeaveMessages} and inform user of new "remove join messages" * Detect deprecated {@code settings.delayJoinLeaveMessages} and inform user of new "remove join messages"
* and "remove leave messages" settings. * and "remove leave messages" settings.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean migrateJoinLeaveMessages(PropertyResource resource) { private static boolean migrateJoinLeaveMessages(PropertyReader reader, ConfigurationData configData) {
Property<Boolean> oldDelayJoinProperty = newProperty("settings.delayJoinLeaveMessages", false); Property<Boolean> oldDelayJoinProperty = newProperty("settings.delayJoinLeaveMessages", false);
boolean hasMigrated = moveProperty(oldDelayJoinProperty, DELAY_JOIN_MESSAGE, resource); boolean hasMigrated = moveProperty(oldDelayJoinProperty, DELAY_JOIN_MESSAGE, reader, configData);
if (hasMigrated) { if (hasMigrated) {
ConsoleLogger.info(String.format("Note that we now also have the settings %s and %s", ConsoleLogger.info(String.format("Note that we now also have the settings %s and %s",
@ -167,31 +170,33 @@ public class SettingsMigrationService extends PlainMigrationService {
* Detects old "force spawn loc on join" and "force spawn on these worlds" settings and moves them * Detects old "force spawn loc on join" and "force spawn on these worlds" settings and moves them
* to the new paths. * to the new paths.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean migrateForceSpawnSettings(PropertyResource resource) { private static boolean migrateForceSpawnSettings(PropertyReader reader, ConfigurationData configData) {
Property<Boolean> oldForceLocEnabled = newProperty( Property<Boolean> oldForceLocEnabled = newProperty(
"settings.restrictions.ForceSpawnLocOnJoinEnabled", false); "settings.restrictions.ForceSpawnLocOnJoinEnabled", false);
Property<List<String>> oldForceWorlds = newListProperty( Property<List<String>> oldForceWorlds = newListProperty(
"settings.restrictions.ForceSpawnOnTheseWorlds", "world", "world_nether", "world_the_ed"); "settings.restrictions.ForceSpawnOnTheseWorlds", "world", "world_nether", "world_the_ed");
return moveProperty(oldForceLocEnabled, FORCE_SPAWN_LOCATION_AFTER_LOGIN, resource) return moveProperty(oldForceLocEnabled, FORCE_SPAWN_LOCATION_AFTER_LOGIN, reader, configData)
| moveProperty(oldForceWorlds, FORCE_SPAWN_ON_WORLDS, resource); | moveProperty(oldForceWorlds, FORCE_SPAWN_ON_WORLDS, reader, configData);
} }
/** /**
* Detects the old auto poolSize value and replaces it with the default value. * Detects the old auto poolSize value and replaces it with the default value.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean migratePoolSizeSetting(PropertyResource resource) { private static boolean migratePoolSizeSetting(PropertyReader reader, ConfigurationData configData) {
Integer oldValue = resource.getInt("DataSource.poolSize"); Integer oldValue = reader.getInt(MYSQL_POOL_SIZE.getPath());
if (oldValue == null || oldValue > 0) { if (oldValue == null || oldValue > 0) {
return false; return false;
} }
resource.setValue("DataSource.poolSize", 10); configData.setValue(MYSQL_POOL_SIZE, 10);
return true; return true;
} }
@ -199,24 +204,26 @@ public class SettingsMigrationService extends PlainMigrationService {
* Changes the old boolean property "hide spam from console" to the new property specifying * Changes the old boolean property "hide spam from console" to the new property specifying
* the log level. * the log level.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean changeBooleanSettingToLogLevelProperty(PropertyResource resource) { private static boolean changeBooleanSettingToLogLevelProperty(PropertyReader reader,
ConfigurationData configData) {
final String oldPath = "Security.console.noConsoleSpam"; final String oldPath = "Security.console.noConsoleSpam";
final Property<LogLevel> newProperty = PluginSettings.LOG_LEVEL; final Property<LogLevel> newProperty = PluginSettings.LOG_LEVEL;
if (!newProperty.isPresent(resource) && resource.contains(oldPath)) { if (!newProperty.isPresent(reader) && reader.contains(oldPath)) {
ConsoleLogger.info("Moving '" + oldPath + "' to '" + newProperty.getPath() + "'"); ConsoleLogger.info("Moving '" + oldPath + "' to '" + newProperty.getPath() + "'");
boolean oldValue = MoreObjects.firstNonNull(resource.getBoolean(oldPath), false); boolean oldValue = MoreObjects.firstNonNull(reader.getBoolean(oldPath), false);
LogLevel level = oldValue ? LogLevel.INFO : LogLevel.FINE; LogLevel level = oldValue ? LogLevel.INFO : LogLevel.FINE;
resource.setValue(newProperty.getPath(), level.name()); configData.setValue(newProperty, level);
return true; return true;
} }
return false; return false;
} }
private static boolean hasOldHelpHeaderProperty(PropertyResource resource) { private static boolean hasOldHelpHeaderProperty(PropertyReader reader) {
if (resource.contains("settings.helpHeader")) { if (reader.contains("settings.helpHeader")) {
ConsoleLogger.warning("Help header setting is now in messages/help_xx.yml, " ConsoleLogger.warning("Help header setting is now in messages/help_xx.yml, "
+ "please check the file to set it again"); + "please check the file to set it again");
return true; return true;
@ -224,9 +231,9 @@ public class SettingsMigrationService extends PlainMigrationService {
return false; return false;
} }
private static boolean hasSupportOldPasswordProperty(PropertyResource resource) { private static boolean hasSupportOldPasswordProperty(PropertyReader reader) {
String path = "settings.security.supportOldPasswordHash"; String path = "settings.security.supportOldPasswordHash";
if (resource.contains(path)) { if (reader.contains(path)) {
ConsoleLogger.warning("Property '" + path + "' is no longer supported. " ConsoleLogger.warning("Property '" + path + "' is no longer supported. "
+ "Use '" + SecuritySettings.LEGACY_HASHES.getPath() + "' instead."); + "Use '" + SecuritySettings.LEGACY_HASHES.getPath() + "' instead.");
return true; return true;
@ -237,56 +244,58 @@ public class SettingsMigrationService extends PlainMigrationService {
/** /**
* Converts old boolean configurations for registration to the new enum properties, if applicable. * Converts old boolean configurations for registration to the new enum properties, if applicable.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean convertToRegistrationType(PropertyResource resource) { private static boolean convertToRegistrationType(PropertyReader reader, ConfigurationData configData) {
String oldEmailRegisterPath = "settings.registration.enableEmailRegistrationSystem"; String oldEmailRegisterPath = "settings.registration.enableEmailRegistrationSystem";
if (RegistrationSettings.REGISTRATION_TYPE.isPresent(resource) || !resource.contains(oldEmailRegisterPath)) { if (RegistrationSettings.REGISTRATION_TYPE.isPresent(reader) || !reader.contains(oldEmailRegisterPath)) {
return false; return false;
} }
boolean useEmail = newProperty(oldEmailRegisterPath, false).getValue(resource); boolean useEmail = newProperty(oldEmailRegisterPath, false).determineValue(reader);
RegistrationType registrationType = useEmail ? RegistrationType.EMAIL : RegistrationType.PASSWORD; RegistrationType registrationType = useEmail ? RegistrationType.EMAIL : RegistrationType.PASSWORD;
String useConfirmationPath = useEmail String useConfirmationPath = useEmail
? "settings.registration.doubleEmailCheck" ? "settings.registration.doubleEmailCheck"
: "settings.restrictions.enablePasswordConfirmation"; : "settings.restrictions.enablePasswordConfirmation";
boolean hasConfirmation = newProperty(useConfirmationPath, false).getValue(resource); boolean hasConfirmation = newProperty(useConfirmationPath, false).determineValue(reader);
RegisterSecondaryArgument secondaryArgument = hasConfirmation RegisterSecondaryArgument secondaryArgument = hasConfirmation
? RegisterSecondaryArgument.CONFIRMATION ? RegisterSecondaryArgument.CONFIRMATION
: RegisterSecondaryArgument.NONE; : RegisterSecondaryArgument.NONE;
ConsoleLogger.warning("Merging old registration settings into '" ConsoleLogger.warning("Merging old registration settings into '"
+ RegistrationSettings.REGISTRATION_TYPE.getPath() + "'"); + RegistrationSettings.REGISTRATION_TYPE.getPath() + "'");
resource.setValue(RegistrationSettings.REGISTRATION_TYPE.getPath(), registrationType); configData.setValue(RegistrationSettings.REGISTRATION_TYPE, registrationType);
resource.setValue(RegistrationSettings.REGISTER_SECOND_ARGUMENT.getPath(), secondaryArgument); configData.setValue(RegistrationSettings.REGISTER_SECOND_ARGUMENT, secondaryArgument);
return true; return true;
} }
/** /**
* Migrates old permission group settings to the new configurations. * Migrates old permission group settings to the new configurations.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean mergeAndMovePermissionGroupSettings(PropertyResource resource) { private static boolean mergeAndMovePermissionGroupSettings(PropertyReader reader, ConfigurationData configData) {
boolean performedChanges; boolean performedChanges;
// We have two old settings replaced by only one: move the first non-empty one // We have two old settings replaced by only one: move the first non-empty one
Property<String> oldUnloggedInGroup = newProperty("settings.security.unLoggedinGroup", ""); Property<String> oldUnloggedInGroup = newProperty("settings.security.unLoggedinGroup", "");
Property<String> oldRegisteredGroup = newProperty("GroupOptions.RegisteredPlayerGroup", ""); Property<String> oldRegisteredGroup = newProperty("GroupOptions.RegisteredPlayerGroup", "");
if (!oldUnloggedInGroup.getValue(resource).isEmpty()) { if (!oldUnloggedInGroup.determineValue(reader).isEmpty()) {
performedChanges = moveProperty(oldUnloggedInGroup, PluginSettings.REGISTERED_GROUP, resource); performedChanges = moveProperty(oldUnloggedInGroup, PluginSettings.REGISTERED_GROUP, reader, configData);
} else { } else {
performedChanges = moveProperty(oldRegisteredGroup, PluginSettings.REGISTERED_GROUP, resource); performedChanges = moveProperty(oldRegisteredGroup, PluginSettings.REGISTERED_GROUP, reader, configData);
} }
// Move paths of other old options // Move paths of other old options
performedChanges |= moveProperty(newProperty("GroupOptions.UnregisteredPlayerGroup", ""), performedChanges |= moveProperty(newProperty("GroupOptions.UnregisteredPlayerGroup", ""),
PluginSettings.UNREGISTERED_GROUP, resource); PluginSettings.UNREGISTERED_GROUP, reader, configData);
performedChanges |= moveProperty(newProperty("permission.EnablePermissionCheck", false), performedChanges |= moveProperty(newProperty("permission.EnablePermissionCheck", false),
PluginSettings.ENABLE_PERMISSION_CHECK, resource); PluginSettings.ENABLE_PERMISSION_CHECK, reader, configData);
return performedChanges; return performedChanges;
} }
@ -294,19 +303,21 @@ public class SettingsMigrationService extends PlainMigrationService {
* If a deprecated hash is used, it is added to the legacy hashes option and the active hash * If a deprecated hash is used, it is added to the legacy hashes option and the active hash
* is changed to SHA256. * is changed to SHA256.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean moveDeprecatedHashAlgorithmIntoLegacySection(PropertyResource resource) { private static boolean moveDeprecatedHashAlgorithmIntoLegacySection(PropertyReader reader,
HashAlgorithm currentHash = SecuritySettings.PASSWORD_HASH.getValue(resource); ConfigurationData configData) {
HashAlgorithm currentHash = SecuritySettings.PASSWORD_HASH.determineValue(reader);
// Skip CUSTOM (has no class) and PLAINTEXT (is force-migrated later on in the startup process) // Skip CUSTOM (has no class) and PLAINTEXT (is force-migrated later on in the startup process)
if (currentHash != HashAlgorithm.CUSTOM && currentHash != HashAlgorithm.PLAINTEXT) { if (currentHash != HashAlgorithm.CUSTOM && currentHash != HashAlgorithm.PLAINTEXT) {
Class<?> encryptionClass = currentHash.getClazz(); Class<?> encryptionClass = currentHash.getClazz();
if (encryptionClass.isAnnotationPresent(Deprecated.class)) { if (encryptionClass.isAnnotationPresent(Deprecated.class)) {
resource.setValue(SecuritySettings.PASSWORD_HASH.getPath(), HashAlgorithm.SHA256); configData.setValue(SecuritySettings.PASSWORD_HASH, HashAlgorithm.SHA256);
Set<HashAlgorithm> legacyHashes = SecuritySettings.LEGACY_HASHES.getValue(resource); Set<HashAlgorithm> legacyHashes = SecuritySettings.LEGACY_HASHES.determineValue(reader);
legacyHashes.add(currentHash); legacyHashes.add(currentHash);
resource.setValue(SecuritySettings.LEGACY_HASHES.getPath(), legacyHashes); configData.setValue(SecuritySettings.LEGACY_HASHES, legacyHashes);
ConsoleLogger.warning("The hash algorithm '" + currentHash ConsoleLogger.warning("The hash algorithm '" + currentHash
+ "' is no longer supported for active use. New hashes will be in SHA256."); + "' is no longer supported for active use. New hashes will be in SHA256.");
return true; return true;
@ -318,28 +329,30 @@ public class SettingsMigrationService extends PlainMigrationService {
/** /**
* Moves the property for the password salt column name to the same path as all other column name properties. * Moves the property for the password salt column name to the same path as all other column name properties.
* *
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @return True if the configuration has changed, false otherwise * @return True if the configuration has changed, false otherwise
*/ */
private static boolean moveSaltColumnConfigWithOtherColumnConfigs(PropertyResource resource) { private static boolean moveSaltColumnConfigWithOtherColumnConfigs(PropertyReader reader,
ConfigurationData configData) {
Property<String> oldProperty = newProperty("ExternalBoardOptions.mySQLColumnSalt", Property<String> oldProperty = newProperty("ExternalBoardOptions.mySQLColumnSalt",
DatabaseSettings.MYSQL_COL_SALT.getDefaultValue()); DatabaseSettings.MYSQL_COL_SALT.getDefaultValue());
return moveProperty(oldProperty, DatabaseSettings.MYSQL_COL_SALT, resource); return moveProperty(oldProperty, DatabaseSettings.MYSQL_COL_SALT, reader, configData);
} }
/** /**
* Retrieves the old config to run a command when alt accounts are detected and sets them to this instance * Retrieves the old config to run a command when alt accounts are detected and sets them to this instance
* for further processing. * for further processing.
* *
* @param resource The property resource * @param reader The property reader
*/ */
private void setOldOtherAccountsCommandFieldsIfSet(PropertyResource resource) { private void setOldOtherAccountsCommandFieldsIfSet(PropertyReader reader) {
Property<String> commandProperty = newProperty("settings.restrictions.otherAccountsCmd", ""); Property<String> commandProperty = newProperty("settings.restrictions.otherAccountsCmd", "");
Property<Integer> commandThresholdProperty = newProperty("settings.restrictions.otherAccountsCmdThreshold", 0); Property<Integer> commandThresholdProperty = newProperty("settings.restrictions.otherAccountsCmdThreshold", 0);
if (commandProperty.isPresent(resource) && commandThresholdProperty.getValue(resource) >= 2) { if (commandProperty.isPresent(reader) && commandThresholdProperty.determineValue(reader) >= 2) {
oldOtherAccountsCommand = commandProperty.getValue(resource); oldOtherAccountsCommand = commandProperty.determineValue(reader);
oldOtherAccountsCommandThreshold = commandThresholdProperty.getValue(resource); oldOtherAccountsCommandThreshold = commandThresholdProperty.determineValue(reader);
} }
} }
@ -348,19 +361,21 @@ public class SettingsMigrationService extends PlainMigrationService {
* *
* @param oldProperty The old property (create a temporary {@link Property} object with the path) * @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 newProperty The new property to move the value to
* @param resource The property resource * @param reader The property reader
* @param configData Configuration data
* @param <T> The type of the property * @param <T> The type of the property
* @return True if a migration has been done, false otherwise * @return True if a migration has been done, false otherwise
*/ */
private static <T> boolean moveProperty(Property<T> oldProperty, protected static <T> boolean moveProperty(Property<T> oldProperty,
Property<T> newProperty, Property<T> newProperty,
PropertyResource resource) { PropertyReader reader,
if (resource.contains(oldProperty.getPath())) { ConfigurationData configData) {
if (resource.contains(newProperty.getPath())) { if (reader.contains(oldProperty.getPath())) {
if (reader.contains(newProperty.getPath())) {
ConsoleLogger.info("Detected deprecated property " + oldProperty.getPath()); ConsoleLogger.info("Detected deprecated property " + oldProperty.getPath());
} else { } else {
ConsoleLogger.info("Renaming " + oldProperty.getPath() + " to " + newProperty.getPath()); ConsoleLogger.info("Renaming " + oldProperty.getPath() + " to " + newProperty.getPath());
resource.setValue(newProperty.getPath(), oldProperty.getValue(resource)); configData.setValue(newProperty, oldProperty.determineValue(reader));
} }
return true; return true;
} }

View File

@ -1,6 +1,7 @@
package fr.xephi.authme.settings.commandconfig; package fr.xephi.authme.settings.commandconfig;
import ch.jalu.configme.SettingsManager; import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.SettingsManagerBuilder;
import fr.xephi.authme.initialization.DataFolder; import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.initialization.Reloadable; import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.service.BukkitService; import fr.xephi.authme.service.BukkitService;
@ -148,8 +149,11 @@ public class CommandManager implements Reloadable {
File file = new File(dataFolder, "commands.yml"); File file = new File(dataFolder, "commands.yml");
FileUtils.copyFileFromResource(file, "commands.yml"); FileUtils.copyFileFromResource(file, "commands.yml");
SettingsManager settingsManager = new SettingsManager( SettingsManager settingsManager = SettingsManagerBuilder
YamlFileResourceProvider.loadFromFile(file), commandMigrationService, CommandSettingsHolder.class); .withResource(YamlFileResourceProvider.loadFromFile(file))
.configurationData(CommandSettingsHolder.class)
.migrationService(commandMigrationService)
.create();
CommandConfig commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS); CommandConfig commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
onJoinCommands = newReplacer(commandConfig.getOnJoin()); onJoinCommands = newReplacer(commandConfig.getOnJoin());
onLoginCommands = newOnLoginCmdReplacer(commandConfig.getOnLogin()); onLoginCommands = newOnLoginCmdReplacer(commandConfig.getOnLogin());

View File

@ -1,8 +1,8 @@
package fr.xephi.authme.settings.commandconfig; package fr.xephi.authme.settings.commandconfig;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.migration.MigrationService; import ch.jalu.configme.migration.MigrationService;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.PropertyResource;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import fr.xephi.authme.settings.SettingsMigrationService; import fr.xephi.authme.settings.SettingsMigrationService;
@ -30,10 +30,10 @@ class CommandMigrationService implements MigrationService {
} }
@Override @Override
public boolean checkAndMigrate(PropertyResource resource, List<Property<?>> properties) { public boolean checkAndMigrate(PropertyReader reader, ConfigurationData configurationData) {
final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getValue(resource); final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.determineValue(reader);
if (moveOtherAccountsConfig(commandConfig) || isFileEmpty(resource)) { if (moveOtherAccountsConfig(commandConfig) || isAnyCommandMissing(reader)) {
resource.setValue("", commandConfig); configurationData.setValue(CommandSettingsHolder.COMMANDS, commandConfig);
return true; return true;
} }
return false; return false;
@ -59,7 +59,7 @@ class CommandMigrationService implements MigrationService {
.replace("%playerip%", "%ip"); .replace("%playerip%", "%ip");
} }
private static boolean isFileEmpty(PropertyResource resource) { private static boolean isAnyCommandMissing(PropertyReader reader) {
return COMMAND_CONFIG_PROPERTIES.stream().anyMatch(property -> resource.getObject(property) == null); return COMMAND_CONFIG_PROPERTIES.stream().anyMatch(property -> reader.getObject(property) == null);
} }
} }

View File

@ -1,13 +1,10 @@
package fr.xephi.authme.settings.commandconfig; package fr.xephi.authme.settings.commandconfig;
import ch.jalu.configme.SectionComments;
import ch.jalu.configme.SettingsHolder; import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.configurationdata.CommentsConfiguration;
import ch.jalu.configme.properties.BeanProperty; import ch.jalu.configme.properties.BeanProperty;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import java.util.HashMap;
import java.util.Map;
/** /**
* Settings holder class for the commands.yml settings. * Settings holder class for the commands.yml settings.
*/ */
@ -16,12 +13,11 @@ public final class CommandSettingsHolder implements SettingsHolder {
public static final Property<CommandConfig> COMMANDS = public static final Property<CommandConfig> COMMANDS =
new BeanProperty<>(CommandConfig.class, "", new CommandConfig()); new BeanProperty<>(CommandConfig.class, "", new CommandConfig());
private CommandSettingsHolder() { private CommandSettingsHolder() {
} }
@SectionComments @Override
public static Map<String, String[]> sectionComments() { public void registerComments(CommentsConfiguration conf) {
String[] rootComments = { String[] rootComments = {
"This configuration file allows you to execute commands on various events.", "This configuration file allows you to execute commands on various events.",
"Supported placeholders in commands:", "Supported placeholders in commands:",
@ -60,21 +56,15 @@ public final class CommandSettingsHolder implements SettingsHolder {
" ifNumberOfAccountsAtLeast: 5" " ifNumberOfAccountsAtLeast: 5"
}; };
Map<String, String[]> commentMap = new HashMap<>(); conf.setComment("", rootComments);
commentMap.put("", rootComments); conf.setComment("onFirstLogin",
commentMap.put("onFirstLogin", new String[]{ "Commands to run for players logging in whose 'last login date' was empty");
"Commands to run for players logging in whose 'last login date' was empty" conf.setComment("onUnregister",
}); "Commands to run whenever a player is unregistered (by himself, or by an admin)");
commentMap.put("onUnregister", new String[]{ conf.setComment("onLogout",
"Commands to run whenever a player is unregistered (by himself, or by an admin)"
});
commentMap.put("onLogout", new String[]{
"These commands are called whenever a logged in player uses /logout or quits.", "These commands are called whenever a logged in player uses /logout or quits.",
"The commands are not run if a player that was not logged in quits the server.", "The commands are not run if a player that was not logged in quits the server.",
"Note: if your server crashes, these commands won't be run, so don't rely on them to undo", "Note: if your server crashes, these commands won't be run, so don't rely on them to undo",
"'onLogin' commands that would be dangerous for non-logged in players to have!" "'onLogin' commands that would be dangerous for non-logged in players to have!");
});
return commentMap;
} }
} }

View File

@ -19,7 +19,7 @@ public final class AuthMeSettingsRetriever {
* @return configuration data * @return configuration data
*/ */
public static ConfigurationData buildConfigurationData() { public static ConfigurationData buildConfigurationData() {
return ConfigurationDataBuilder.collectData( return ConfigurationDataBuilder.createConfiguration(
DatabaseSettings.class, PluginSettings.class, RestrictionSettings.class, DatabaseSettings.class, PluginSettings.class, RestrictionSettings.class,
EmailSettings.class, HooksSettings.class, ProtectionSettings.class, EmailSettings.class, HooksSettings.class, ProtectionSettings.class,
PurgeSettings.class, SecuritySettings.class, RegistrationSettings.class, PurgeSettings.class, SecuritySettings.class, RegistrationSettings.class,

View File

@ -1,12 +1,9 @@
package fr.xephi.authme.settings.properties; package fr.xephi.authme.settings.properties;
import ch.jalu.configme.Comment; import ch.jalu.configme.Comment;
import ch.jalu.configme.SectionComments;
import ch.jalu.configme.SettingsHolder; import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.configurationdata.CommentsConfiguration;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import static ch.jalu.configme.properties.PropertyInitializer.newProperty; import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
@ -51,9 +48,9 @@ public final class ConverterSettings implements SettingsHolder {
private ConverterSettings() { private ConverterSettings() {
} }
@SectionComments @Override
public static Map<String, String[]> buildSectionComments() { public void registerComments(CommentsConfiguration conf) {
return ImmutableMap.of("Converter", conf.setComment("Converter",
new String[]{"Converter settings: see https://github.com/AuthMe/AuthMeReloaded/wiki/Converters"}); "Converter settings: see https://github.com/AuthMe/AuthMeReloaded/wiki/Converters");
} }
} }

View File

@ -1,17 +1,14 @@
package fr.xephi.authme.settings.properties; package fr.xephi.authme.settings.properties;
import ch.jalu.configme.Comment; import ch.jalu.configme.Comment;
import ch.jalu.configme.SectionComments;
import ch.jalu.configme.SettingsHolder; import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.configurationdata.CommentsConfiguration;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import com.google.common.collect.ImmutableMap;
import fr.xephi.authme.data.limbo.AllowFlightRestoreType; import fr.xephi.authme.data.limbo.AllowFlightRestoreType;
import fr.xephi.authme.data.limbo.WalkFlySpeedRestoreType; import fr.xephi.authme.data.limbo.WalkFlySpeedRestoreType;
import fr.xephi.authme.data.limbo.persistence.LimboPersistenceType; import fr.xephi.authme.data.limbo.persistence.LimboPersistenceType;
import fr.xephi.authme.data.limbo.persistence.SegmentSize; import fr.xephi.authme.data.limbo.persistence.SegmentSize;
import java.util.Map;
import static ch.jalu.configme.properties.PropertyInitializer.newProperty; import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
/** /**
@ -72,8 +69,8 @@ public final class LimboSettings implements SettingsHolder {
private LimboSettings() { private LimboSettings() {
} }
@SectionComments @Override
public static Map<String, String[]> createSectionComments() { public void registerComments(CommentsConfiguration conf) {
String[] limboExplanation = { String[] limboExplanation = {
"Before a user logs in, various properties are temporarily removed from the player,", "Before a user logs in, various properties are temporarily removed from the player,",
"such as OP status, ability to fly, and walk/fly speed.", "such as OP status, ability to fly, and walk/fly speed.",
@ -81,6 +78,6 @@ public final class LimboSettings implements SettingsHolder {
"In this section, you may define how these properties should be handled.", "In this section, you may define how these properties should be handled.",
"Read more at https://github.com/AuthMe/AuthMeReloaded/wiki/Limbo-players" "Read more at https://github.com/AuthMe/AuthMeReloaded/wiki/Limbo-players"
}; };
return ImmutableMap.of("limbo", limboExplanation); conf.setComment("limbo", limboExplanation);
} }
} }

View File

@ -5,9 +5,10 @@ import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import java.util.List; import java.util.List;
import java.util.Set;
import static ch.jalu.configme.properties.PropertyInitializer.newListProperty; import static ch.jalu.configme.properties.PropertyInitializer.newListProperty;
import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseListProperty; import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseStringSetProperty;
import static ch.jalu.configme.properties.PropertyInitializer.newProperty; import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
public final class RestrictionSettings implements SettingsHolder { public final class RestrictionSettings implements SettingsHolder {
@ -24,8 +25,8 @@ public final class RestrictionSettings implements SettingsHolder {
newProperty("settings.restrictions.hideChat", false); newProperty("settings.restrictions.hideChat", false);
@Comment("Allowed commands for unauthenticated players") @Comment("Allowed commands for unauthenticated players")
public static final Property<List<String>> ALLOW_COMMANDS = public static final Property<Set<String>> ALLOW_COMMANDS =
newLowercaseListProperty("settings.restrictions.allowCommands", newLowercaseStringSetProperty("settings.restrictions.allowCommands",
"/login", "/register", "/l", "/reg", "/email", "/captcha", "/2fa", "/totp"); "/login", "/register", "/l", "/reg", "/email", "/captcha", "/2fa", "/totp");
@Comment({ @Comment({
@ -83,8 +84,8 @@ public final class RestrictionSettings implements SettingsHolder {
" AllowedRestrictedUser:", " AllowedRestrictedUser:",
" - playername;127.0.0.1", " - playername;127.0.0.1",
" - playername;regex:127\\.0\\.0\\..*"}) " - playername;regex:127\\.0\\.0\\..*"})
public static final Property<List<String>> RESTRICTED_USERS = public static final Property<Set<String>> RESTRICTED_USERS =
newLowercaseListProperty("settings.restrictions.AllowedRestrictedUser"); newLowercaseStringSetProperty("settings.restrictions.AllowedRestrictedUser");
@Comment("Ban unknown IPs trying to log in with a restricted username?") @Comment("Ban unknown IPs trying to log in with a restricted username?")
public static final Property<Boolean> BAN_UNKNOWN_IP = public static final Property<Boolean> BAN_UNKNOWN_IP =
@ -177,8 +178,8 @@ public final class RestrictionSettings implements SettingsHolder {
"- 'npcPlayer'", "- 'npcPlayer'",
"- 'npcPlayer2'" "- 'npcPlayer2'"
}) })
public static final Property<List<String>> UNRESTRICTED_NAMES = public static final Property<Set<String>> UNRESTRICTED_NAMES =
newLowercaseListProperty("settings.unrestrictions.UnrestrictedName"); newLowercaseStringSetProperty("settings.unrestrictions.UnrestrictedName");
private RestrictionSettings() { private RestrictionSettings() {
} }

View File

@ -6,10 +6,9 @@ import ch.jalu.configme.properties.Property;
import fr.xephi.authme.security.HashAlgorithm; import fr.xephi.authme.security.HashAlgorithm;
import fr.xephi.authme.settings.EnumSetProperty; import fr.xephi.authme.settings.EnumSetProperty;
import java.util.List;
import java.util.Set; import java.util.Set;
import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseListProperty; import static ch.jalu.configme.properties.PropertyInitializer.newLowercaseStringSetProperty;
import static ch.jalu.configme.properties.PropertyInitializer.newProperty; import static ch.jalu.configme.properties.PropertyInitializer.newProperty;
public final class SecuritySettings implements SettingsHolder { public final class SecuritySettings implements SettingsHolder {
@ -86,8 +85,8 @@ public final class SecuritySettings implements SettingsHolder {
"- '123456'", "- '123456'",
"- 'password'", "- 'password'",
"- 'help'"}) "- 'help'"})
public static final Property<List<String>> UNSAFE_PASSWORDS = public static final Property<Set<String>> UNSAFE_PASSWORDS =
newLowercaseListProperty("settings.security.unsafePasswords", newLowercaseStringSetProperty("settings.security.unsafePasswords",
"123456", "password", "qwerty", "12345", "54321", "123456789", "help"); "123456", "password", "qwerty", "12345", "54321", "123456789", "help");
@Comment("Tempban a user's IP address if they enter the wrong password too many times") @Comment("Tempban a user's IP address if they enter the wrong password too many times")

View File

@ -41,6 +41,7 @@ import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
@ -91,7 +92,7 @@ public class AuthMeInitializationTest {
public void shouldInitializeAllServices() { public void shouldInitializeAllServices() {
// given // given
Settings settings = Settings settings =
new Settings(dataFolder, mock(PropertyResource.class), null, buildConfigurationData()); new Settings(dataFolder, mock(PropertyResource.class, RETURNS_DEEP_STUBS), null, buildConfigurationData());
Injector injector = new InjectorBuilder() Injector injector = new InjectorBuilder()
.addDefaultHandlers("fr.xephi.authme") .addDefaultHandlers("fr.xephi.authme")

View File

@ -1,5 +1,7 @@
package fr.xephi.authme.command.help; package fr.xephi.authme.command.help;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.CommandDescription; import fr.xephi.authme.command.CommandDescription;
import fr.xephi.authme.command.CommandInitializer; import fr.xephi.authme.command.CommandInitializer;
@ -61,16 +63,16 @@ public class HelpMessagesConsistencyTest {
@Test @Test
public void shouldHaveEntryForEachHelpMessageKey() { public void shouldHaveEntryForEachHelpMessageKey() {
// given // given
FileConfiguration configuration = YamlConfiguration.loadConfiguration(DEFAULT_MESSAGES_FILE); PropertyReader reader = new YamlFileReader(DEFAULT_MESSAGES_FILE);
// when / then // when / then
for (HelpMessage message : HelpMessage.values()) { for (HelpMessage message : HelpMessage.values()) {
assertThat("Default configuration has entry for message '" + message + "'", assertThat("Default configuration should have entry for message '" + message + "'",
configuration.contains(message.getKey()), equalTo(true)); reader.contains(message.getKey()), equalTo(true));
} }
for (HelpSection section : HelpSection.values()) { for (HelpSection section : HelpSection.values()) {
assertThat("Default configuration has entry for section '" + section + "'", assertThat("Default configuration should have entry for section '" + section + "'",
configuration.contains(section.getKey()), equalTo(true)); reader.contains(section.getKey()), equalTo(true));
} }
} }

View File

@ -57,6 +57,7 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import static com.google.common.collect.Sets.newHashSet;
import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock; import static fr.xephi.authme.listener.EventCancelVerifier.withServiceMock;
import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay; import static fr.xephi.authme.service.BukkitServiceTestHelper.setBukkitServiceToScheduleSyncDelayedTaskWithDelay;
import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.contains;
@ -206,8 +207,7 @@ public class PlayerListenerTest {
public void shouldNotStopAllowedCommand() { public void shouldNotStopAllowedCommand() {
// given // given
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(true); given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(true);
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)) given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(newHashSet("/plugins", "/mail", "/msg"));
.willReturn(Arrays.asList("/plugins", "/mail", "/msg"));
PlayerCommandPreprocessEvent event = mockCommandEvent("/Mail send test Test"); PlayerCommandPreprocessEvent event = mockCommandEvent("/Mail send test Test");
// when // when
@ -222,7 +222,7 @@ public class PlayerListenerTest {
public void shouldNotCancelEventForAuthenticatedPlayer() { public void shouldNotCancelEventForAuthenticatedPlayer() {
// given // given
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false); given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.emptyList()); given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Collections.emptySet());
Player player = playerWithMockedServer(); Player player = playerWithMockedServer();
// PlayerCommandPreprocessEvent#getPlayer is final, so create a spy instead of a mock // PlayerCommandPreprocessEvent#getPlayer is final, so create a spy instead of a mock
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub")); PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
@ -243,7 +243,7 @@ public class PlayerListenerTest {
public void shouldCancelCommandEvent() { public void shouldCancelCommandEvent() {
// given // given
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false); given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Arrays.asList("/spawn", "/help")); given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(newHashSet("/spawn", "/help"));
Player player = playerWithMockedServer(); Player player = playerWithMockedServer();
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub")); PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
given(listenerService.shouldCancelEvent(player)).willReturn(true); given(listenerService.shouldCancelEvent(player)).willReturn(true);
@ -262,7 +262,7 @@ public class PlayerListenerTest {
public void shouldCancelFastCommandEvent() { public void shouldCancelFastCommandEvent() {
// given // given
given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false); given(settings.getProperty(HooksSettings.USE_ESSENTIALS_MOTD)).willReturn(false);
given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(Arrays.asList("/spawn", "/help")); given(settings.getProperty(RestrictionSettings.ALLOW_COMMANDS)).willReturn(newHashSet("/spawn", "/help"));
Player player = playerWithMockedServer(); Player player = playerWithMockedServer();
PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub")); PlayerCommandPreprocessEvent event = spy(new PlayerCommandPreprocessEvent(player, "/hub"));
given(quickCommandsProtectionManager.isAllowed(player.getName())).willReturn(false); given(quickCommandsProtectionManager.isAllowed(player.getName())).willReturn(false);

View File

@ -1,11 +1,11 @@
package fr.xephi.authme.message; package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.help.HelpMessage; import fr.xephi.authme.command.help.HelpMessage;
import fr.xephi.authme.command.help.HelpSection; import fr.xephi.authme.command.help.HelpSection;
import fr.xephi.authme.permission.DefaultPermission; import fr.xephi.authme.permission.DefaultPermission;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -49,27 +49,27 @@ public class HelpMessageConsistencyTest {
public void shouldHaveRequiredEntries() { public void shouldHaveRequiredEntries() {
for (File file : helpFiles) { for (File file : helpFiles) {
// given // given
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file); PropertyReader reader = new YamlFileReader(file);
// when / then // when / then
assertHasAllHelpSectionEntries(file.getName(), configuration); assertHasAllHelpSectionEntries(file.getName(), reader);
} }
} }
private void assertHasAllHelpSectionEntries(String filename, FileConfiguration configuration) { private void assertHasAllHelpSectionEntries(String filename, PropertyReader reader) {
for (HelpSection section : HelpSection.values()) { for (HelpSection section : HelpSection.values()) {
assertThat(filename + " should have entry for HelpSection '" + section + "'", assertThat(filename + " should have entry for HelpSection '" + section + "'",
configuration.getString(section.getKey()), notEmptyString()); reader.getString(section.getKey()), notEmptyString());
} }
for (HelpMessage message : HelpMessage.values()) { for (HelpMessage message : HelpMessage.values()) {
assertThat(filename + " should have entry for HelpMessage '" + message + "'", assertThat(filename + " should have entry for HelpMessage '" + message + "'",
configuration.getString(message.getKey()), notEmptyString()); reader.getString(message.getKey()), notEmptyString());
} }
for (DefaultPermission defaultPermission : DefaultPermission.values()) { for (DefaultPermission defaultPermission : DefaultPermission.values()) {
assertThat(filename + " should have entry for DefaultPermission '" + defaultPermission + "'", assertThat(filename + " should have entry for DefaultPermission '" + defaultPermission + "'",
configuration.getString(getPathForDefaultPermission(defaultPermission)), notEmptyString()); reader.getString(getPathForDefaultPermission(defaultPermission)), notEmptyString());
} }
} }

View File

@ -1,10 +1,10 @@
package fr.xephi.authme.message; package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -51,12 +51,12 @@ public class MessageFilePlaceholderTest {
@Test @Test
public void shouldHaveAllPlaceholders() { public void shouldHaveAllPlaceholders() {
// given // given
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile); PropertyReader reader = new YamlFileReader(messagesFile);
List<String> errors = new ArrayList<>(); List<String> errors = new ArrayList<>(0);
// when // when
for (MessageKey key : MessageKey.values()) { for (MessageKey key : MessageKey.values()) {
List<String> missingTags = findMissingTags(key, configuration); List<String> missingTags = findMissingTags(key, reader);
if (!missingTags.isEmpty()) { if (!missingTags.isEmpty()) {
errors.add("Message key '" + key + "' should have tags: " + String.join(", ", missingTags)); errors.add("Message key '" + key + "' should have tags: " + String.join(", ", missingTags));
} }
@ -68,9 +68,9 @@ public class MessageFilePlaceholderTest {
} }
} }
private List<String> findMissingTags(MessageKey key, FileConfiguration configuration) { private List<String> findMissingTags(MessageKey key, PropertyReader reader) {
if (key.getTags().length > 0 && configuration.contains(key.getKey())) { if (key.getTags().length > 0 && reader.contains(key.getKey())) {
String message = configuration.getString(key.getKey()); String message = reader.getString(key.getKey());
return Arrays.stream(key.getTags()) return Arrays.stream(key.getTags())
.filter(tag -> !EXCLUSIONS.get(key).contains(tag) && !message.contains(tag)) .filter(tag -> !EXCLUSIONS.get(key).contains(tag) && !message.contains(tag))
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -1,9 +1,9 @@
package fr.xephi.authme.message; package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.Test; import org.junit.Test;
import java.io.File; import java.io.File;
@ -24,10 +24,10 @@ public class MessagesFileConsistencyTest {
@Test @Test
public void shouldHaveAllMessages() { public void shouldHaveAllMessages() {
File file = TestHelper.getJarFile(MESSAGES_FILE); File file = TestHelper.getJarFile(MESSAGES_FILE);
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file); PropertyReader reader = new YamlFileReader(file);
List<String> errors = new ArrayList<>(); List<String> errors = new ArrayList<>();
for (MessageKey messageKey : MessageKey.values()) { for (MessageKey messageKey : MessageKey.values()) {
validateMessage(messageKey, configuration, errors); validateMessage(messageKey, reader, errors);
} }
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
@ -36,9 +36,9 @@ public class MessagesFileConsistencyTest {
} }
} }
private static void validateMessage(MessageKey messageKey, FileConfiguration configuration, List<String> errors) { private static void validateMessage(MessageKey messageKey, PropertyReader reader, List<String> errors) {
final String key = messageKey.getKey(); final String key = messageKey.getKey();
final String message = configuration.getString(key); final String message = reader.getString(key);
if (StringUtils.isEmpty(message)) { if (StringUtils.isEmpty(message)) {
errors.add("Messages file should have message for key '" + key + "'"); errors.add("Messages file should have message for key '" + key + "'");

View File

@ -1,10 +1,11 @@
package fr.xephi.authme.message; package fr.xephi.authme.message;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.command.help.HelpSection; import fr.xephi.authme.command.help.HelpSection;
import fr.xephi.authme.util.ExceptionUtils; import fr.xephi.authme.util.ExceptionUtils;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
@ -81,8 +82,8 @@ public class YamlTextFileCheckerTest {
*/ */
private void checkFile(File file, String mandatoryKey, List<String> errors) { private void checkFile(File file, String mandatoryKey, List<String> errors) {
try { try {
YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file); PropertyReader reader = new YamlFileReader(file);
if (StringUtils.isEmpty(configuration.getString(mandatoryKey))) { if (StringUtils.isEmpty(reader.getString(mandatoryKey))) {
errors.add("Message for '" + mandatoryKey + "' is empty"); errors.add("Message for '" + mandatoryKey + "' is empty");
} }
} catch (Exception e) { } catch (Exception e) {

View File

@ -1,13 +1,11 @@
package fr.xephi.authme.message.updater; package fr.xephi.authme.message.updater;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import com.google.common.io.Files; import com.google.common.io.Files;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.MessageKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -16,6 +14,7 @@ import org.junit.rules.TemporaryFolder;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -65,13 +64,13 @@ public class MessageUpdaterTest {
// then // then
assertThat(wasChanged, equalTo(true)); assertThat(wasChanged, equalTo(true));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile); PropertyReader reader = new YamlFileReader(messagesFile);
// Existing keys should not be overridden // Existing keys should not be overridden
assertThat(configuration.getString(MessageKey.LOGIN_SUCCESS.getKey()), equalTo("&cHere we have&bdefined some colors &dand some other &lthings")); assertThat(reader.getString(MessageKey.LOGIN_SUCCESS.getKey()), equalTo("&cHere we have&bdefined some colors &dand some other &lthings"));
assertThat(configuration.getString(MessageKey.EMAIL_ALREADY_USED_ERROR.getKey()), equalTo("")); assertThat(reader.getString(MessageKey.EMAIL_ALREADY_USED_ERROR.getKey()), equalTo(""));
// Check that new keys were added // Check that new keys were added
assertThat(configuration.getString(MessageKey.SECOND.getKey()), equalTo("second")); assertThat(reader.getString(MessageKey.SECOND.getKey()), equalTo("second"));
assertThat(configuration.getString(MessageKey.ERROR.getKey()), equalTo("&4An unexpected error occurred, please contact an administrator!")); assertThat(reader.getString(MessageKey.ERROR.getKey()), equalTo("&4An unexpected error occurred, please contact an administrator!"));
} }
@Test @Test
@ -85,18 +84,18 @@ public class MessageUpdaterTest {
// then // then
assertThat(wasChanged, equalTo(true)); assertThat(wasChanged, equalTo(true));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile); PropertyReader reader = new YamlFileReader(messagesFile);
assertThat(configuration.getString(MessageKey.PASSWORD_MATCH_ERROR.getKey()), assertThat(reader.getString(MessageKey.PASSWORD_MATCH_ERROR.getKey()),
equalTo("Password error message")); equalTo("Password error message"));
assertThat(configuration.getString(MessageKey.INVALID_NAME_CHARACTERS.getKey()), assertThat(reader.getString(MessageKey.INVALID_NAME_CHARACTERS.getKey()),
equalTo("not valid username: Allowed chars are %valid_chars")); equalTo("not valid username: Allowed chars are %valid_chars"));
assertThat(configuration.getString(MessageKey.INVALID_OLD_EMAIL.getKey()), assertThat(reader.getString(MessageKey.INVALID_OLD_EMAIL.getKey()),
equalTo("Email (old) is not valid!!")); equalTo("Email (old) is not valid!!"));
assertThat(configuration.getString(MessageKey.CAPTCHA_WRONG_ERROR.getKey()), assertThat(reader.getString(MessageKey.CAPTCHA_WRONG_ERROR.getKey()),
equalTo("The captcha code is %captcha_code for you")); equalTo("The captcha code is %captcha_code for you"));
assertThat(configuration.getString(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED.getKey()), assertThat(reader.getString(MessageKey.CAPTCHA_FOR_REGISTRATION_REQUIRED.getKey()),
equalTo("Now type /captcha %captcha_code")); equalTo("Now type /captcha %captcha_code"));
assertThat(configuration.getString(MessageKey.SECONDS.getKey()), assertThat(reader.getString(MessageKey.SECONDS.getKey()),
equalTo("seconds in plural")); equalTo("seconds in plural"));
} }
@ -111,10 +110,10 @@ public class MessageUpdaterTest {
// then // then
assertThat(wasChanged, equalTo(true)); assertThat(wasChanged, equalTo(true));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile); PropertyReader reader = new YamlFileReader(messagesFile);
assertThat(configuration.getString(MessageKey.TWO_FACTOR_CREATE.getKey()), equalTo("Old 2fa create text")); assertThat(reader.getString(MessageKey.TWO_FACTOR_CREATE.getKey()), equalTo("Old 2fa create text"));
assertThat(configuration.getString(MessageKey.WRONG_PASSWORD.getKey()), equalTo("test2 - wrong password")); // from pre-5.5 key assertThat(reader.getString(MessageKey.WRONG_PASSWORD.getKey()), equalTo("test2 - wrong password")); // from pre-5.5 key
assertThat(configuration.getString(MessageKey.SECOND.getKey()), equalTo("second")); // from messages_en.yml assertThat(reader.getString(MessageKey.SECOND.getKey()), equalTo("second")); // from messages_en.yml
} }
@Test @Test
@ -125,7 +124,7 @@ public class MessageUpdaterTest {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
// when // when
Set<String> messageKeysFromConfigData = MessageUpdater.getConfigurationData().getProperties().stream() Set<String> messageKeysFromConfigData = MessageUpdater.createConfigurationData().getProperties().stream()
.map(Property::getPath) .map(Property::getPath)
.collect(Collectors.toSet()); .collect(Collectors.toSet());
@ -141,8 +140,7 @@ public class MessageUpdaterTest {
.collect(Collectors.toSet()); .collect(Collectors.toSet());
// when // when
Map<String, String[]> comments = ReflectionTestUtils.getFieldValue( Map<String, List<String>> comments = MessageUpdater.createConfigurationData().getAllComments();
ConfigurationData.class, MessageUpdater.getConfigurationData(), "sectionComments");
// then // then
assertThat(comments.keySet(), equalTo(rootPaths)); assertThat(comments.keySet(), equalTo(rootPaths));

View File

@ -1,8 +1,10 @@
package fr.xephi.authme.message.updater; package fr.xephi.authme.message.updater;
import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.StringProperty; import ch.jalu.configme.properties.StringProperty;
import ch.jalu.configme.resource.PropertyReader;
import com.google.common.io.Files; import com.google.common.io.Files;
import fr.xephi.authme.TestHelper; import fr.xephi.authme.TestHelper;
import org.junit.Rule; import org.junit.Rule;
@ -31,14 +33,15 @@ public class MigraterYamlFileResourceTest {
public void shouldReadChineseFile() { public void shouldReadChineseFile() {
// given // given
File file = TestHelper.getJarFile(CHINESE_MESSAGES_FILE); File file = TestHelper.getJarFile(CHINESE_MESSAGES_FILE);
// when
MigraterYamlFileResource resource = new MigraterYamlFileResource(file); MigraterYamlFileResource resource = new MigraterYamlFileResource(file);
// when
PropertyReader reader = resource.createReader();
// then // then
assertThat(resource.getString("first"), equalTo("错误的密码")); assertThat(reader.getString("first"), equalTo("错误的密码"));
assertThat(resource.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!")); assertThat(reader.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!"));
assertThat(resource.getString("third"), equalTo("您已经可以在当前会话中执行任何敏感命令!")); assertThat(reader.getString("third"), equalTo("您已经可以在当前会话中执行任何敏感命令!"));
} }
@Test @Test
@ -47,24 +50,26 @@ public class MigraterYamlFileResourceTest {
File file = temporaryFolder.newFile(); File file = temporaryFolder.newFile();
Files.copy(TestHelper.getJarFile(CHINESE_MESSAGES_FILE), file); Files.copy(TestHelper.getJarFile(CHINESE_MESSAGES_FILE), file);
MigraterYamlFileResource resource = new MigraterYamlFileResource(file); MigraterYamlFileResource resource = new MigraterYamlFileResource(file);
ConfigurationData configurationData = buildConfigurationData();
configurationData.initializeValues(resource.createReader());
String newMessage = "您当前并没有任何邮箱与该账号绑定"; String newMessage = "您当前并没有任何邮箱与该账号绑定";
resource.setValue("third", newMessage); configurationData.setValue(new StringProperty("third", ""), newMessage);
// when // when
resource.exportProperties(buildConfigurationData()); resource.exportProperties(configurationData);
// then // then
resource = new MigraterYamlFileResource(file); PropertyReader reader = resource.createReader();
assertThat(resource.getString("first"), equalTo("错误的密码")); assertThat(reader.getString("first"), equalTo("错误的密码"));
assertThat(resource.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!")); assertThat(reader.getString("second"), equalTo("为了验证您的身份,您需要将一个电子邮件地址与您的帐户绑定!"));
assertThat(resource.getString("third"), equalTo(newMessage)); assertThat(reader.getString("third"), equalTo(newMessage));
} }
private static ConfigurationData buildConfigurationData() { private static ConfigurationData buildConfigurationData() {
List<Property<?>> properties = Arrays.asList( List<Property<String>> properties = Arrays.asList(
new StringProperty("first", "first"), new StringProperty("first", "first"),
new StringProperty("second", "second"), new StringProperty("second", "second"),
new StringProperty("third", "third")); new StringProperty("third", "third"));
return new ConfigurationData(properties); return ConfigurationDataBuilder.createConfiguration(properties);
} }
} }

View File

@ -22,10 +22,10 @@ import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.logging.Logger; import java.util.logging.Logger;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
@ -57,10 +57,9 @@ public class ValidationServiceTest {
given(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)).willReturn("[a-zA-Z]+"); given(settings.getProperty(RestrictionSettings.ALLOWED_PASSWORD_REGEX)).willReturn("[a-zA-Z]+");
given(settings.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).willReturn(3); given(settings.getProperty(SecuritySettings.MIN_PASSWORD_LENGTH)).willReturn(3);
given(settings.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).willReturn(20); given(settings.getProperty(SecuritySettings.MAX_PASSWORD_LENGTH)).willReturn(20);
given(settings.getProperty(SecuritySettings.UNSAFE_PASSWORDS)) given(settings.getProperty(SecuritySettings.UNSAFE_PASSWORDS)).willReturn(newHashSet("unsafe", "other-unsafe"));
.willReturn(asList("unsafe", "other-unsafe"));
given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3); given(settings.getProperty(EmailSettings.MAX_REG_PER_EMAIL)).willReturn(3);
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(asList("name01", "npc")); given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(newHashSet("name01", "npc"));
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(false); given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(false);
} }
@ -261,7 +260,7 @@ public class ValidationServiceTest {
assertThat(validationService.isUnrestricted("NAME01"), equalTo(true)); assertThat(validationService.isUnrestricted("NAME01"), equalTo(true));
// Check reloading // Check reloading
given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(asList("new", "names")); given(settings.getProperty(RestrictionSettings.UNRESTRICTED_NAMES)).willReturn(newHashSet("new", "names"));
validationService.reload(); validationService.reload();
assertThat(validationService.isUnrestricted("npc"), equalTo(false)); assertThat(validationService.isUnrestricted("npc"), equalTo(false));
assertThat(validationService.isUnrestricted("New"), equalTo(true)); assertThat(validationService.isUnrestricted("New"), equalTo(true));
@ -350,7 +349,7 @@ public class ValidationServiceTest {
// given // given
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true); given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS)) given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;32.24.16.8", "Gabriel;regex:93\\.23\\.44\\..*", "emanuel;94.65.24.*", "imyourisp;*.yourisp.net")); .willReturn(newHashSet("Bobby;127.0.0.4", "Tamara;32.24.16.8", "Gabriel;regex:93\\.23\\.44\\..*", "emanuel;94.65.24.*", "imyourisp;*.yourisp.net"));
validationService.reload(); validationService.reload();
Player bobby = mockPlayer("bobby", "127.0.0.4"); Player bobby = mockPlayer("bobby", "127.0.0.4");
@ -389,7 +388,7 @@ public class ValidationServiceTest {
Logger logger = TestHelper.setupLogger(); Logger logger = TestHelper.setupLogger();
given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true); given(settings.getProperty(RestrictionSettings.ENABLE_RESTRICTED_USERS)).willReturn(true);
given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS)) given(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
.willReturn(Arrays.asList("Bobby;127.0.0.4", "Tamara;")); .willReturn(newHashSet("Bobby;127.0.0.4", "Tamara;"));
// when // when
validationService.reload(); validationService.reload();

View File

@ -26,7 +26,7 @@ public class YamlFileResourceProviderTest {
YamlFileResource resource = YamlFileResourceProvider.loadFromFile(yamlFile); YamlFileResource resource = YamlFileResourceProvider.loadFromFile(yamlFile);
// then // then
assertThat(resource.getString("test.jkl"), equalTo("Test test")); assertThat(resource.createReader().getString("test.jkl"), equalTo("Test test"));
} }
@Test @Test
@ -36,7 +36,7 @@ public class YamlFileResourceProviderTest {
// when // when
try { try {
YamlFileResourceProvider.loadFromFile(yamlFile); YamlFileResourceProvider.loadFromFile(yamlFile).createReader();
// then // then
fail("Expected exception to be thrown"); fail("Expected exception to be thrown");

View File

@ -1,30 +1,22 @@
package fr.xephi.authme.settings; package fr.xephi.authme.settings;
import ch.jalu.configme.SectionComments;
import ch.jalu.configme.SettingsHolder;
import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.properties.EnumProperty; import ch.jalu.configme.properties.EnumProperty;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import fr.xephi.authme.ClassCollector;
import fr.xephi.authme.ReflectionTestUtils;
import fr.xephi.authme.TestHelper;
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever; import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
import fr.xephi.authme.settings.properties.SecuritySettings; import fr.xephi.authme.settings.properties.SecuritySettings;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
import static fr.xephi.authme.ReflectionTestUtils.getFieldValue; import static fr.xephi.authme.ReflectionTestUtils.getFieldValue;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
@ -58,7 +50,7 @@ public class SettingsConsistencyTest {
// when / then // when / then
for (Property<?> property : properties) { for (Property<?> property : properties) {
if (configurationData.getCommentsForSection(property.getPath()).length == 0) { if (configurationData.getCommentsForSection(property.getPath()).isEmpty()) {
fail("No comment defined for " + property); fail("No comment defined for " + property);
} }
} }
@ -67,83 +59,27 @@ public class SettingsConsistencyTest {
@Test @Test
public void shouldNotHaveVeryLongCommentLines() { public void shouldNotHaveVeryLongCommentLines() {
// given // given
List<Property<?>> properties = configurationData.getProperties(); Map<String, List<String>> commentEntries = configurationData.getAllComments();
List<Property<?>> badProperties = new ArrayList<>(); List<String> badPaths = new ArrayList<>(0);
// when // when
for (Property<?> property : properties) { for (Map.Entry<String, List<String>> commentEntry : commentEntries.entrySet()) {
for (String comment : configurationData.getCommentsForSection(property.getPath())) { for (String comment : commentEntry.getValue()) {
if (comment.length() > MAX_COMMENT_LENGTH) { if (comment.length() > MAX_COMMENT_LENGTH) {
badProperties.add(property); badPaths.add(commentEntry.getKey());
break; break;
} }
} }
} }
// then // then
if (!badProperties.isEmpty()) { if (!badPaths.isEmpty()) {
fail("Comment lines should not be longer than " + MAX_COMMENT_LENGTH + " chars, " fail("Comment lines should not be longer than " + MAX_COMMENT_LENGTH + " chars, "
+ "but found too long comments for:\n- " + "but found too long comments for paths:\n- "
+ badProperties.stream().map(Property::getPath).collect(Collectors.joining("\n- "))); + String.join("\n- ", badPaths));
} }
} }
@Test
public void shouldNotHaveVeryLongSectionCommentLines() {
// given
List<Method> sectionCommentMethods = getSectionCommentMethods();
Set<Method> badMethods = new HashSet<>();
// when
for (Method method : sectionCommentMethods) {
boolean hasTooLongLine = getSectionComments(method).stream()
.anyMatch(line -> line.length() > MAX_COMMENT_LENGTH);
if (hasTooLongLine) {
badMethods.add(method);
}
}
// then
if (!badMethods.isEmpty()) {
String methodList = badMethods.stream()
.map(m -> m.getName() + " in " + m.getDeclaringClass().getSimpleName())
.collect(Collectors.joining("\n- "));
fail("Found SectionComments methods with too long comments:\n- " + methodList);
}
}
/**
* Gets all {@link SectionComments} methods from {@link SettingsHolder} implementations.
*/
@SuppressWarnings("unchecked")
private List<Method> getSectionCommentMethods() {
// Find all SettingsHolder classes
List<Class<? extends SettingsHolder>> settingsClasses =
new ClassCollector(TestHelper.SOURCES_FOLDER, TestHelper.PROJECT_ROOT + "settings/properties/")
.collectClasses(SettingsHolder.class);
checkArgument(!settingsClasses.isEmpty(), "Could not find any SettingsHolder classes");
// Find all @SectionComments methods in these classes
return settingsClasses.stream()
.map(Class::getDeclaredMethods)
.flatMap(Arrays::stream)
.filter(method -> method.isAnnotationPresent(SectionComments.class))
.collect(Collectors.toList());
}
/**
* Returns all comments returned from the given SectionComments method, flattened into one list.
*
* @param sectionCommentsMethod the method whose comments should be retrieved
* @return flattened list of all comments provided by the method
*/
private static List<String> getSectionComments(Method sectionCommentsMethod) {
// @SectionComments methods are static
Map<String, String[]> comments = ReflectionTestUtils.invokeMethod(sectionCommentsMethod, null);
return comments.values().stream()
.flatMap(Arrays::stream)
.collect(Collectors.toList());
}
/** /**
* Checks that enum properties have all possible enum values listed in their comment * Checks that enum properties have all possible enum values listed in their comment

View File

@ -38,7 +38,7 @@ public class SettingsIntegrationTest {
private static final String INCOMPLETE_FILE = TestHelper.PROJECT_ROOT + "settings/config-incomplete-sample.yml"; private static final String INCOMPLETE_FILE = TestHelper.PROJECT_ROOT + "settings/config-incomplete-sample.yml";
private static ConfigurationData CONFIG_DATA = private static ConfigurationData CONFIG_DATA =
ConfigurationDataBuilder.collectData(TestConfiguration.class); ConfigurationDataBuilder.createConfiguration(TestConfiguration.class);
@Rule @Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder(); public TemporaryFolder temporaryFolder = new TemporaryFolder();

View File

@ -1,7 +1,7 @@
package fr.xephi.authme.settings; package fr.xephi.authme.settings;
import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.resource.YamlFileResource;
import com.google.common.io.Files; import com.google.common.io.Files;
@ -108,7 +108,7 @@ public class SettingsMigrationServiceTest {
SettingsMigrationService migrationService = new SettingsMigrationService(dataFolder); SettingsMigrationService migrationService = new SettingsMigrationService(dataFolder);
// when // when
migrationService.performMigrations(resource, AuthMeSettingsRetriever.buildConfigurationData().getProperties()); migrationService.performMigrations(resource.createReader(), AuthMeSettingsRetriever.buildConfigurationData());
// then // then
assertThat(migrationService.hasOldOtherAccountsCommand(), equalTo(true)); assertThat(migrationService.hasOldOtherAccountsCommand(), equalTo(true));
@ -146,8 +146,8 @@ public class SettingsMigrationServiceTest {
} }
@Override @Override
protected boolean performMigrations(PropertyResource resource, List<Property<?>> properties) { protected boolean performMigrations(PropertyReader reader, ConfigurationData configurationData) {
boolean result = super.performMigrations(resource, properties); boolean result = super.performMigrations(reader, configurationData);
returnedValues.add(result); returnedValues.add(result);
return result; return result;
} }

View File

@ -17,6 +17,7 @@ import java.nio.file.Files;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
/** /**
@ -25,7 +26,7 @@ import static org.mockito.Mockito.mock;
public class SettingsTest { public class SettingsTest {
private static final ConfigurationData CONFIG_DATA = private static final ConfigurationData CONFIG_DATA =
ConfigurationDataBuilder.collectData(TestConfiguration.class); ConfigurationDataBuilder.createConfiguration(TestConfiguration.class);
@Rule @Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder(); public TemporaryFolder temporaryFolder = new TemporaryFolder();
@ -49,7 +50,7 @@ public class SettingsTest {
createFile(emailFile); createFile(emailFile);
Files.write(emailFile.toPath(), emailMessage.getBytes()); Files.write(emailFile.toPath(), emailMessage.getBytes());
PropertyResource resource = mock(PropertyResource.class); PropertyResource resource = mock(PropertyResource.class, RETURNS_DEEP_STUBS);
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA); Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
// when // when
@ -67,7 +68,7 @@ public class SettingsTest {
createFile(emailFile); createFile(emailFile);
Files.write(emailFile.toPath(), emailMessage.getBytes()); Files.write(emailFile.toPath(), emailMessage.getBytes());
PropertyResource resource = mock(PropertyResource.class); PropertyResource resource = mock(PropertyResource.class, RETURNS_DEEP_STUBS);
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA); Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
// when // when
@ -85,7 +86,7 @@ public class SettingsTest {
createFile(emailFile); createFile(emailFile);
Files.write(emailFile.toPath(), emailMessage.getBytes()); Files.write(emailFile.toPath(), emailMessage.getBytes());
PropertyResource resource = mock(PropertyResource.class); PropertyResource resource = mock(PropertyResource.class, RETURNS_DEEP_STUBS);
Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA); Settings settings = new Settings(testPluginFolder, resource, null, CONFIG_DATA);
// when // when

View File

@ -1,7 +1,8 @@
package fr.xephi.authme.settings.commandconfig; package fr.xephi.authme.settings.commandconfig;
import ch.jalu.configme.beanmapper.BeanDescriptionFactory; import ch.jalu.configme.beanmapper.propertydescription.BeanDescriptionFactoryImpl;
import ch.jalu.configme.beanmapper.BeanPropertyDescription; import ch.jalu.configme.beanmapper.propertydescription.BeanPropertyDescription;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.configurationdata.ConfigurationDataBuilder; import ch.jalu.configme.configurationdata.ConfigurationDataBuilder;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.resource.YamlFileResource;
@ -52,7 +53,7 @@ public class CommandMigrationServiceTest {
// when // when
boolean result = commandMigrationService.checkAndMigrate( boolean result = commandMigrationService.checkAndMigrate(
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties()); resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
// then // then
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
@ -66,7 +67,7 @@ public class CommandMigrationServiceTest {
// when // when
boolean result = commandMigrationService.checkAndMigrate( boolean result = commandMigrationService.checkAndMigrate(
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties()); resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
// then // then
assertThat(result, equalTo(true)); assertThat(result, equalTo(true));
@ -80,7 +81,7 @@ public class CommandMigrationServiceTest {
// when // when
boolean result = commandMigrationService.checkAndMigrate( boolean result = commandMigrationService.checkAndMigrate(
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties()); resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
// then // then
assertThat(result, equalTo(false)); assertThat(result, equalTo(false));
@ -93,8 +94,8 @@ public class CommandMigrationServiceTest {
@Test @Test
public void shouldHaveAllPropertiesFromCommandConfig() { public void shouldHaveAllPropertiesFromCommandConfig() {
// given // given
String[] properties = new BeanDescriptionFactory() String[] properties = new BeanDescriptionFactoryImpl()
.collectWritableFields(CommandConfig.class) .getAllProperties(CommandConfig.class)
.stream() .stream()
.map(BeanPropertyDescription::getName) .map(BeanPropertyDescription::getName)
.toArray(String[]::new); .toArray(String[]::new);
@ -112,13 +113,14 @@ public class CommandMigrationServiceTest {
given(settingsMigrationService.getOldOtherAccountsCommandThreshold()).willReturn(3); given(settingsMigrationService.getOldOtherAccountsCommandThreshold()).willReturn(3);
File commandFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "settings/commandconfig/commands.complete.yml"); File commandFile = TestHelper.getJarFile(TestHelper.PROJECT_ROOT + "settings/commandconfig/commands.complete.yml");
PropertyResource resource = new YamlFileResource(commandFile); PropertyResource resource = new YamlFileResource(commandFile);
ConfigurationData configurationData = ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class);
// when // when
commandMigrationService.checkAndMigrate( commandMigrationService.checkAndMigrate(
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties()); resource.createReader(), configurationData);
// then // then
Map<String, OnLoginCommand> onLoginCommands = CommandSettingsHolder.COMMANDS.getValue(resource).getOnLogin(); Map<String, OnLoginCommand> onLoginCommands = configurationData.getValue(CommandSettingsHolder.COMMANDS).getOnLogin();
assertThat(onLoginCommands, aMapWithSize(6)); // 5 in the file + the newly migrated on assertThat(onLoginCommands, aMapWithSize(6)); // 5 in the file + the newly migrated on
OnLoginCommand newCommand = getUnknownOnLoginCommand(onLoginCommands); OnLoginCommand newCommand = getUnknownOnLoginCommand(onLoginCommands);
assertThat(newCommand.getCommand(), equalTo("helpop %p (%ip) has other accounts!")); assertThat(newCommand.getCommand(), equalTo("helpop %p (%ip) has other accounts!"));

View File

@ -42,7 +42,7 @@ public class CommandYmlConsistencyTest {
// when // when
boolean result = commandMigrationService.checkAndMigrate( boolean result = commandMigrationService.checkAndMigrate(
resource, ConfigurationDataBuilder.collectData(CommandSettingsHolder.class).getProperties()); resource.createReader(), ConfigurationDataBuilder.createConfiguration(CommandSettingsHolder.class));
// then // then
assertThat(result, equalTo(false)); assertThat(result, equalTo(false));

View File

@ -1,7 +1,7 @@
package tools.docs.config; package tools.docs.config;
import ch.jalu.configme.SettingsManager; import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.SettingsManagerBuilder;
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever; import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
import fr.xephi.authme.util.FileUtils; import fr.xephi.authme.util.FileUtils;
import tools.utils.AutoToolTask; import tools.utils.AutoToolTask;
@ -31,8 +31,9 @@ public class UpdateConfigPageTask implements AutoToolTask {
try { try {
// Create empty temporary .yml file and save the config to it // Create empty temporary .yml file and save the config to it
config = File.createTempFile("authme-config-", ".yml"); config = File.createTempFile("authme-config-", ".yml");
SettingsManager settingsManager = new SettingsManager( SettingsManager settingsManager = SettingsManagerBuilder.withYamlFile(config)
new YamlFileResource(config), null, AuthMeSettingsRetriever.buildConfigurationData()); .configurationData(AuthMeSettingsRetriever.buildConfigurationData())
.create();
settingsManager.save(); settingsManager.save();
// Get the contents and generate template file // Get the contents and generate template file

View File

@ -1,8 +1,8 @@
package tools.docs.translations; package tools.docs.translations;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.MessageKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import tools.utils.ToolsConstants; import tools.utils.ToolsConstants;
import java.io.File; import java.io.File;
@ -43,10 +43,10 @@ public class TranslationsGatherer {
} }
private void processMessagesFile(String code, File file) { private void processMessagesFile(String code, File file) {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file); PropertyReader reader = new YamlFileReader(file);
int availableMessages = 0; int availableMessages = 0;
for (MessageKey key : MessageKey.values()) { for (MessageKey key : MessageKey.values()) {
if (configuration.contains(key.getKey())) { if (reader.contains(key.getKey())) {
++availableMessages; ++availableMessages;
} }
} }

View File

@ -1,7 +1,7 @@
package tools.filegeneration; package tools.filegeneration;
import ch.jalu.configme.SettingsManager; import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.SettingsManagerBuilder;
import fr.xephi.authme.settings.commandconfig.CommandConfig; import fr.xephi.authme.settings.commandconfig.CommandConfig;
import fr.xephi.authme.settings.commandconfig.CommandSettingsHolder; import fr.xephi.authme.settings.commandconfig.CommandSettingsHolder;
import tools.utils.AutoToolTask; import tools.utils.AutoToolTask;
@ -24,8 +24,9 @@ public class GenerateCommandsYml implements AutoToolTask {
CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getDefaultValue(); CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getDefaultValue();
// Export the value to the file // Export the value to the file
SettingsManager settingsManager = new SettingsManager( SettingsManager settingsManager = SettingsManagerBuilder.withYamlFile(file)
new YamlFileResource(file), null, CommandSettingsHolder.class); .configurationData(CommandSettingsHolder.class)
.create();
settingsManager.setProperty(CommandSettingsHolder.COMMANDS, commandConfig); settingsManager.setProperty(CommandSettingsHolder.COMMANDS, commandConfig);
settingsManager.save(); settingsManager.save();

View File

@ -1,10 +1,10 @@
package tools.messages; package tools.messages;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.configurationdata.ConfigurationData; import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.properties.Property; import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyResource; import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.resource.YamlFileResource;
import fr.xephi.authme.message.updater.MessageKeyConfigurationData;
import fr.xephi.authme.message.updater.MessageUpdater; import fr.xephi.authme.message.updater.MessageUpdater;
import fr.xephi.authme.message.updater.MigraterYamlFileResource; import fr.xephi.authme.message.updater.MigraterYamlFileResource;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -47,12 +47,18 @@ public final class MessagesFileWriter {
} }
private void performWrite() { private void performWrite() {
// Initialize ConfigMe classes
MessageKeyConfigurationData configurationData = MessageUpdater.createConfigurationData();
YamlFileResource resource = new MigraterYamlFileResource(file);
PropertyReader reader = resource.createReader();
configurationData.initializeValues(reader);
// Store initial comments so we can add them back later // Store initial comments so we can add them back later
List<String> initialComments = getInitialUserComments(); List<String> initialComments = getInitialUserComments(configurationData);
// Create property resource with new defaults, save with ConfigMe for proper sections & comments // Create property resource with new defaults, save with ConfigMe for proper sections & comments
PropertyResource resource = createPropertyResourceWithCommentEntries(); addMissingMessagesWithCommentMarker(reader, configurationData);
new SettingsManager(resource, null, MessageUpdater.getConfigurationData()).save(); resource.exportProperties(configurationData);
// Go through the newly saved file and replace texts with comment marker to actual YAML comments // Go through the newly saved file and replace texts with comment marker to actual YAML comments
// and add initial comments back to the file // and add initial comments back to the file
@ -60,11 +66,14 @@ public final class MessagesFileWriter {
} }
/** /**
* Returns the comments at the top which are custom to the file.
*
* @param configurationData the configuration data
* @return any custom comments at the top of the file, for later usage * @return any custom comments at the top of the file, for later usage
*/ */
private List<String> getInitialUserComments() { private List<String> getInitialUserComments(ConfigurationData configurationData) {
final List<String> initialComments = new ArrayList<>(); final List<String> initialComments = new ArrayList<>();
final String firstCommentByConfigMe = getFirstCommentByConfigMe(); final String firstCommentByConfigMe = getFirstCommentByConfigMe(configurationData);
for (String line : FileIoUtils.readLinesFromFile(file.toPath())) { for (String line : FileIoUtils.readLinesFromFile(file.toPath())) {
if (line.isEmpty() || line.startsWith("#") && !line.equals(firstCommentByConfigMe)) { if (line.isEmpty() || line.startsWith("#") && !line.equals(firstCommentByConfigMe)) {
@ -86,27 +95,30 @@ public final class MessagesFileWriter {
} }
/** /**
* @return the first comment generated by ConfigMe (comment of the first root path) * Returns the first comment generated by ConfigMe (comment of the first root path).
*
* @param configurationData the configuration data
* @return first comment which is generated by ConfigMe
*/ */
private static String getFirstCommentByConfigMe() { private static String getFirstCommentByConfigMe(ConfigurationData configurationData) {
ConfigurationData configurationData = MessageUpdater.getConfigurationData();
String firstRootPath = configurationData.getProperties().get(0).getPath().split("\\.")[0]; String firstRootPath = configurationData.getProperties().get(0).getPath().split("\\.")[0];
return "# " + configurationData.getCommentsForSection(firstRootPath)[0]; return "# " + configurationData.getCommentsForSection(firstRootPath).get(0);
} }
/** /**
* @return generated {@link PropertyResource} with missing entries taken from the default file and marked * Adds a text with a {@link #COMMENT_MARKER} for all properties which are not yet present in the reader.
* with the {@link #COMMENT_MARKER} *
* @param reader the property reader
* @param configurationData the configuration data
*/ */
private PropertyResource createPropertyResourceWithCommentEntries() { private void addMissingMessagesWithCommentMarker(PropertyReader reader,
YamlFileResource resource = new MigraterYamlFileResource(file); MessageKeyConfigurationData configurationData) {
for (Property<?> property : MessageUpdater.getConfigurationData().getProperties()) { for (Property<String> property : configurationData.getAllMessageProperties()) {
String text = resource.getString(property.getPath()); String text = reader.getString(property.getPath());
if (text == null) { if (text == null) {
resource.setValue(property.getPath(), COMMENT_MARKER + defaultFile.getString(property.getPath())); configurationData.setValue(property, COMMENT_MARKER + defaultFile.getString(property.getPath()));
} }
} }
return resource;
} }
/** /**