Update to ConfigMe 1.4.0

This commit is contained in:
ljacqu 2023-08-23 06:29:11 +02:00
parent 80c1c2edac
commit 79309c3c05
5 changed files with 47 additions and 30 deletions

View File

@ -760,7 +760,7 @@
<dependency> <dependency>
<groupId>ch.jalu</groupId> <groupId>ch.jalu</groupId>
<artifactId>configme</artifactId> <artifactId>configme</artifactId>
<version>1.3.0</version> <version>1.4.0</version>
<optional>true</optional> <optional>true</optional>
<exclusions> <exclusions>
<exclusion> <exclusion>

View File

@ -5,6 +5,7 @@ import ch.jalu.configme.properties.Property;
import ch.jalu.configme.properties.convertresult.PropertyValue; import ch.jalu.configme.properties.convertresult.PropertyValue;
import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyReader;
import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.message.updater.MessageUpdater.MessageKeyProperty;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -39,15 +40,19 @@ public class MessageKeyConfigurationData extends ConfigurationDataImpl {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public List<Property<String>> getAllMessageProperties() { public List<MessageKeyProperty> getAllMessageProperties() {
return (List) getProperties(); return (List) getProperties();
} }
public String getMessage(MessageKey messageKey) { public String getMessage(MessageKey messageKey) {
return getValue(new MessageUpdater.MessageKeyProperty(messageKey)); return getValue(new MessageKeyProperty(messageKey));
}
public String getMessage(MessageKeyProperty property) {
return (String) getValues().get(property.getPath());
} }
public void setMessage(MessageKey messageKey, String message) { public void setMessage(MessageKey messageKey, String message) {
setValue(new MessageUpdater.MessageKeyProperty(messageKey), message); setValue(new MessageKeyProperty(messageKey), message);
} }
} }

View File

@ -4,13 +4,13 @@ import ch.jalu.configme.exception.ConfigMeException;
import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyReader;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; 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.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -36,8 +36,8 @@ final class MessageMigraterPropertyReader implements PropertyReader {
* @param file the file to load * @param file the file to load
* @return the created property reader * @return the created property reader
*/ */
public static MessageMigraterPropertyReader loadFromFile(File file) { public static MessageMigraterPropertyReader loadFromFile(Path file) {
try (InputStream is = new FileInputStream(file)) { try (InputStream is = Files.newInputStream(file)) {
return loadFromStream(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);

View File

@ -2,7 +2,6 @@ package fr.xephi.authme.message.updater;
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.StringProperty; import ch.jalu.configme.properties.StringProperty;
import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder; import ch.jalu.configme.properties.convertresult.ConvertErrorRecorder;
import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyReader;
@ -10,8 +9,8 @@ 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;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.message.MessageKey; import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.output.ConsoleLoggerFactory;
import fr.xephi.authme.util.FileUtils; import fr.xephi.authme.util.FileUtils;
import java.io.File; import java.io.File;
@ -102,9 +101,9 @@ public class MessageUpdater {
private boolean addMissingKeys(JarMessageSource jarMessageSource, MessageKeyConfigurationData configurationData) { private boolean addMissingKeys(JarMessageSource jarMessageSource, MessageKeyConfigurationData configurationData) {
List<String> addedKeys = new ArrayList<>(); List<String> addedKeys = new ArrayList<>();
for (Property<String> property : configurationData.getAllMessageProperties()) { for (MessageKeyProperty property : configurationData.getAllMessageProperties()) {
final String key = property.getPath(); final String key = property.getPath();
if (configurationData.getValue(property) == null) { if (configurationData.getMessage(property) == null) {
configurationData.setValue(property, jarMessageSource.getMessageFromJar(property)); configurationData.setValue(property, jarMessageSource.getMessageFromJar(property));
addedKeys.add(key); addedKeys.add(key);
} }

View File

@ -2,8 +2,14 @@ package fr.xephi.authme.message.updater;
import ch.jalu.configme.resource.PropertyReader; import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileResource; import ch.jalu.configme.resource.YamlFileResource;
import ch.jalu.configme.resource.yaml.SnakeYamlNodeBuilder;
import ch.jalu.configme.resource.yaml.SnakeYamlNodeBuilderImpl;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.Tag;
import java.io.File; import java.io.File;
@ -12,36 +18,43 @@ import java.io.File;
*/ */
public class MigraterYamlFileResource extends YamlFileResource { public class MigraterYamlFileResource extends YamlFileResource {
private Yaml singleQuoteYaml;
public MigraterYamlFileResource(File file) { public MigraterYamlFileResource(File file) {
super(file); super(file);
} }
@Override @Override
public PropertyReader createReader() { public PropertyReader createReader() {
return MessageMigraterPropertyReader.loadFromFile(getFile()); return MessageMigraterPropertyReader.loadFromFile(getPath());
} }
@Override @Override
protected Yaml createNewYaml() { protected Yaml createNewYaml() {
if (singleQuoteYaml == null) { DumperOptions options = new DumperOptions();
DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); options.setAllowUnicode(true);
options.setAllowUnicode(true); options.setProcessComments(true);
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.SINGLE_QUOTED); options.setIndent(4);
// Overridden setting: don't split lines // Overridden setting: don't split lines
options.setSplitLines(false); options.setSplitLines(false);
singleQuoteYaml = new Yaml(options); return new Yaml(options);
}
return singleQuoteYaml;
} }
// Because we set the YAML object to put strings in single quotes, this method by default uses that YAML object
// and also puts all paths as single quotes. Override to just always return the same string since we know those
// are only message names (so never any conflicting strings like "true" or "0").
@Override @Override
protected String escapePathElementIfNeeded(String path) { protected @NotNull SnakeYamlNodeBuilder createNodeBuilder() {
return path; return new MigraterYamlNodeBuilder();
}
/** Extended to represent all strings with single quotes in the YAML. */
private static final class MigraterYamlNodeBuilder extends SnakeYamlNodeBuilderImpl {
@Override
protected @NotNull Node createStringNode(@NotNull String value) {
return new ScalarNode(Tag.STR, value, null, null, DumperOptions.ScalarStyle.SINGLE_QUOTED);
}
@Override
public @NotNull Node createKeyNode(@NotNull String key) {
return super.createStringNode(key); // no single quotes
}
} }
} }