Update configme (#1631)
* Upgrade to ConfigMe 1.0.1 * Use ConfigMe reader whenever possible, minor simplifications
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package tools.messages;
|
||||
|
||||
import ch.jalu.configme.SettingsManager;
|
||||
import ch.jalu.configme.configurationdata.ConfigurationData;
|
||||
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 fr.xephi.authme.message.updater.MessageKeyConfigurationData;
|
||||
import fr.xephi.authme.message.updater.MessageUpdater;
|
||||
import fr.xephi.authme.message.updater.MigraterYamlFileResource;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@@ -47,12 +47,18 @@ public final class MessagesFileWriter {
|
||||
}
|
||||
|
||||
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
|
||||
List<String> initialComments = getInitialUserComments();
|
||||
List<String> initialComments = getInitialUserComments(configurationData);
|
||||
|
||||
// Create property resource with new defaults, save with ConfigMe for proper sections & comments
|
||||
PropertyResource resource = createPropertyResourceWithCommentEntries();
|
||||
new SettingsManager(resource, null, MessageUpdater.getConfigurationData()).save();
|
||||
addMissingMessagesWithCommentMarker(reader, configurationData);
|
||||
resource.exportProperties(configurationData);
|
||||
|
||||
// Go through the newly saved file and replace texts with comment marker to actual YAML comments
|
||||
// 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
|
||||
*/
|
||||
private List<String> getInitialUserComments() {
|
||||
private List<String> getInitialUserComments(ConfigurationData configurationData) {
|
||||
final List<String> initialComments = new ArrayList<>();
|
||||
final String firstCommentByConfigMe = getFirstCommentByConfigMe();
|
||||
final String firstCommentByConfigMe = getFirstCommentByConfigMe(configurationData);
|
||||
|
||||
for (String line : FileIoUtils.readLinesFromFile(file.toPath())) {
|
||||
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() {
|
||||
ConfigurationData configurationData = MessageUpdater.getConfigurationData();
|
||||
private static String getFirstCommentByConfigMe(ConfigurationData configurationData) {
|
||||
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
|
||||
* with the {@link #COMMENT_MARKER}
|
||||
* Adds a text with a {@link #COMMENT_MARKER} for all properties which are not yet present in the reader.
|
||||
*
|
||||
* @param reader the property reader
|
||||
* @param configurationData the configuration data
|
||||
*/
|
||||
private PropertyResource createPropertyResourceWithCommentEntries() {
|
||||
YamlFileResource resource = new MigraterYamlFileResource(file);
|
||||
for (Property<?> property : MessageUpdater.getConfigurationData().getProperties()) {
|
||||
String text = resource.getString(property.getPath());
|
||||
private void addMissingMessagesWithCommentMarker(PropertyReader reader,
|
||||
MessageKeyConfigurationData configurationData) {
|
||||
for (Property<String> property : configurationData.getAllMessageProperties()) {
|
||||
String text = reader.getString(property.getPath());
|
||||
if (text == null) {
|
||||
resource.setValue(property.getPath(), COMMENT_MARKER + defaultFile.getString(property.getPath()));
|
||||
configurationData.setValue(property, COMMENT_MARKER + defaultFile.getString(property.getPath()));
|
||||
}
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user