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
42 changed files with 516 additions and 555 deletions
@@ -23,7 +23,6 @@ import org.bukkit.entity.Player;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
@@ -45,7 +44,6 @@ public class ValidationService implements Reloadable {
private GeoIpService geoIpService;
private Pattern passwordRegex;
private Set<String> unrestrictedNames;
private Multimap<String, String> restrictedNames;
ValidationService() {
@@ -55,8 +53,6 @@ public class ValidationService implements Reloadable {
@Override
public void reload() {
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)
? loadNameRestrictions(settings.getProperty(RestrictionSettings.RESTRICTED_USERS))
: HashMultimap.create();
@@ -140,7 +136,7 @@ public class ValidationService implements Reloadable {
* @return true if unrestricted, false otherwise
*/
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
* @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();
for (String restriction : configuredRestrictions) {
if (isInsideString(';', restriction)) {
@@ -1,7 +1,8 @@
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 org.yaml.snakeyaml.parser.ParserException;
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
* into an AuthMe exception.
* thrown when a reader is created into an AuthMe exception.
*
* @param file the file to load
* @return the generated resource
*/
public static YamlFileResource loadFromFile(File file) {
try {
return new YamlFileResource(file);
} catch (ParserException e) {
throw new YamlParseException(file.getPath(), e);
return new AuthMeYamlFileResource(file);
}
/**
* 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);
}
}
}
}
@@ -1,6 +1,8 @@
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.
@@ -13,10 +15,10 @@ public class YamlParseException extends RuntimeException {
* Constructor.
*
* @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) {
super(snakeYamlException);
public YamlParseException(String file, ConfigMeException configMeException) {
super(firstNonNull(configMeException.getCause(), configMeException));
this.file = file;
}