From 5ee87f292c7ff4c003c0df9aa74828297bf2e4e4 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Fri, 8 May 2020 00:03:43 -0400 Subject: [PATCH 01/14] revert 0b0e44b also removes `throws UnsupportedEncodingException` as it's unnecessary --- .../util/CommentedYamlConfiguration.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 4bfb6194..1bc8604c 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -38,8 +38,12 @@ public CommentedYamlConfiguration(File file, boolean doComments) { /** * Loads this Configuration object into memory. */ - public void load() throws UnsupportedEncodingException { - config = new EnhancedConfiguration(file); + public void load() { + try { + config = new EncodedConfiguration(file, "UTF-8"); + } catch (UnsupportedEncodingException e) { + config = new EnhancedConfiguration(file); + } } /** From 3fa714673b5a1da7b15d2f3ae65726a6c9dd2644 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Fri, 8 May 2020 22:21:56 -0400 Subject: [PATCH 02/14] tweak comments for configs --- .../InventoriesConfig.java | 8 +- .../YamlWorldGroupManager.java | 2 +- .../util/CommentedYamlConfiguration.java | 121 ++++++------------ 3 files changed, 43 insertions(+), 88 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java b/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java index 4258bba7..1fb820d4 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java +++ b/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java @@ -21,10 +21,6 @@ public final class InventoriesConfig { * Enum for easily keeping track of config paths, defaults and comments. */ public enum Path { - /** - * Add a comment to the top of file. - */ - SETTINGS("settings", null, "# ===[ Multiverse Inventories Config ]==="), /** * Locale name config path, default and comments. */ @@ -62,7 +58,7 @@ public enum Path { */ OPTIONAL_SHARES("shares.use_optionals", new ArrayList(), "# You must specify optional shares you wish to use here or they will be ignored.", - "# The only built in optional share is \"economy\""), + "# The only built in optional shares are \"economy\" and \"last_location\"."), /** * Whether or not to split data based on game modes. */ @@ -132,7 +128,7 @@ private List getComments() { // Sets defaults config values this.setDefaults(); - config.getConfig().options().header("# Multiverse-Inventories Settings"); + config.getConfig().options().header("Multiverse-Inventories Settings"); // Saves the configuration from memory to file config.save(); diff --git a/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java b/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java index 9dd1809f..bb90309e 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java +++ b/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java @@ -54,7 +54,7 @@ final class YamlWorldGroupManager extends AbstractWorldGroupManager { this.getConfig().createSection("groups"); } - groupsConfig.getConfig().options().header("# Multiverse-Inventories Groups"); + groupsConfig.getConfig().options().header("Multiverse-Inventories Groups"); // Saves the configuration from memory to file groupsConfig.save(); diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 1bc8604c..811c416c 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -29,7 +29,6 @@ public final class CommentedYamlConfiguration { private boolean doComments; public CommentedYamlConfiguration(File file, boolean doComments) { - super(); comments = new HashMap(); this.file = file; this.doComments = doComments; @@ -57,7 +56,7 @@ public FileConfiguration getConfig() { * Saves the file as per normal for YamlConfiguration and then parses the file and inserts * comments where necessary. * - * @return True if succesful. + * @return True if successful. */ public boolean save() { boolean saved = true; @@ -70,44 +69,36 @@ public boolean save() { if (!doComments) { return saved; } + // if there's comments to add and it saved fine, we need to add comments if (!comments.isEmpty() && saved) { - // String array of each line in the config file - String[] yamlContents = - this.convertFileToString(file).split("[" + System.getProperty("line.separator") + "]"); - // This will hold the entire newly formatted config + // convert config file to String + String stringConfig = this.convertFileToString(file); + + // detect which kind of line endings it uses + String lineEnding; + if (stringConfig.contains("\r\n")) lineEnding = "\r\n"; + else lineEnding = "\n"; + + // convert stringConfig to array, ignoring the header + String[] arrayConfig = stringConfig.substring(stringConfig.indexOf(lineEnding) + lineEnding.length()).split(lineEnding); + + // begin building the new config, starting with the header StringBuilder newContents = new StringBuilder(); - String initialContents = config.options().header(); - if (initialContents == null) { - initialContents = ""; - } - newContents.append(initialContents) - .append(System.getProperty("line.separator")) - .append(System.getProperty("line.separator")); + newContents.append("# ").append(config.options().header()).append(lineEnding); + // This holds the current path the lines are at in the config StringBuilder currentPath = new StringBuilder(); - // This tells if the specified path has already been commented - boolean commentedPath = false; + // This flags if the line is a node or unknown text. boolean node = false; // The depth of the path. (number of words separated by periods - 1) int depth = 0; - // TODO find a better solution here? - // This will cause the first line to be ignored. - boolean firstLine = true; // Loop through the config lines - for (final String line : yamlContents) { - if (firstLine) { - firstLine = false; - if (line.startsWith("#")) { - continue; - } - } + for (final String line : arrayConfig) { // If the line is a node (and not something like a list value) if (line.contains(": ") || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) { - // This is a new node so we need to mark it for commenting (if there are comments) - commentedPath = false; // This is a node so flag it as one node = true; @@ -132,7 +123,7 @@ public boolean save() { } // Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth if (whiteSpace / 2 > depth) { - // Path is deeper. Add a . and the node name + // Path is deeper. Add a dot and the node name currentPath.append(".").append(line.substring(whiteSpace, index)); depth++; } else if (whiteSpace / 2 < depth) { @@ -173,39 +164,21 @@ public boolean save() { } StringBuilder newLine = new StringBuilder(line); if (node) { - String comment = null; - if (!commentedPath) { - // If there's a comment for the current path, retrieve it and flag that path as already commented - comment = comments.get(currentPath.toString()); - } + // get the comment for the current node + String comment = comments.get(currentPath.toString()); if (comment != null && !comment.isEmpty()) { // Add the comment to the beginning of the current line newLine.insert(0, System.getProperty("line.separator")).insert(0, comment); - comment = null; - commentedPath = true; - } - /* Old code for removing uncommented lines. - * May need reworking. - if (comment != null || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) { - // Add the (modified) line to the total config String - // This modified version will not write the config if a comment is not present - newContents += line + System.getProperty("line.separator"); + if (newLine.charAt(newLine.length() - 1) != ':' && !line.equals(arrayConfig[arrayConfig.length - 1])) + newLine.append(System.getProperty("line.separator")); } - */ } + newLine.append(System.getProperty("line.separator")); // Add the (modified) line to the total config String newContents.append(newLine.toString()); } - /* - * Due to a bukkit bug we need to strip any extra new lines from the - * beginning of this file, else they will multiply. - */ - /* - while (newContents.startsWith(System.getProperty("line.separator"))) { - newContents = newContents.replaceFirst(System.getProperty("line.separator"), ""); - } - */ + try { // Write the string to the config file this.stringToFile(newContents.toString(), file); @@ -217,63 +190,52 @@ public boolean save() { } /** - * Adds a comment just before the specified path. The comment can be multiple lines. An empty string will indicate a blank line. + * Adds a comment just before the specified path. The comment can be multiple lines. An empty string will indicate + * a blank line. * * @param path Configuration path to add comment. - * @param commentLines Comments to add. One String per line. + * @param commentLines Comments to add. One String per line. */ public void addComment(String path, List commentLines) { - StringBuilder commentstring = new StringBuilder(); - String leadingSpaces = ""; + StringBuilder commentString = new StringBuilder(); + StringBuilder leadingSpaces = new StringBuilder(); for (int n = 0; n < path.length(); n++) { if (path.charAt(n) == '.') { - leadingSpaces += " "; + leadingSpaces.append(" "); } } for (String line : commentLines) { if (!line.isEmpty()) { - line = leadingSpaces + line; - } else { - line = " "; + line = leadingSpaces.toString() + line; } - if (commentstring.length() > 0) { - commentstring.append("\r\n"); + if (commentString.length() > 0) { + commentString.append(System.getProperty("line.separator")); } - commentstring.append(line); + commentString.append(line); } - comments.put(path, commentstring.toString()); + comments.put(path, commentString.toString()); } /** * Pass a file and it will return it's contents as a string. * * @param file File to read. - * @return Contents of file. String will be empty in case of any errors. + * @return Contents of file. String will be empty in case of any errors. */ private String convertFileToString(File file) { final int bufferSize = 1024; if (file != null && file.exists() && file.canRead() && !file.isDirectory()) { Writer writer = new StringWriter(); - InputStream is = null; - char[] buffer = new char[bufferSize]; - try { - is = new FileInputStream(file); - Reader reader = new BufferedReader( - new InputStreamReader(is, "UTF-8")); + + try (InputStream is = new FileInputStream(file)) { int n; + Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } } catch (IOException e) { e.printStackTrace(); - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException ignore) { - } - } } return writer.toString(); } else { @@ -293,9 +255,6 @@ private boolean stringToFile(String source, File file) throws IOException { OutputStreamWriter out = null; try { out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); - - source.replaceAll("\n", System.getProperty("line.separator")); - out.write(source); out.close(); return true; From 47671fae701f0c2227d07291b6b6d27fe2261ae3 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Wed, 7 Oct 2020 22:15:38 -0400 Subject: [PATCH 03/14] support multi-line headers --- .../util/CommentedYamlConfiguration.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 811c416c..39302479 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -17,21 +17,24 @@ import java.io.Writer; import java.util.HashMap; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * A Configuration wrapper class that allows for comments to be applied to the config paths. */ public final class CommentedYamlConfiguration { - private HashMap comments; - private File file; + private final File file; private FileConfiguration config = null; - private boolean doComments; + private final boolean doComments; + private final HashMap comments; + private final Pattern newlinePattern = Pattern.compile("\r?\n"); public CommentedYamlConfiguration(File file, boolean doComments) { - comments = new HashMap(); this.file = file; this.doComments = doComments; + comments = new HashMap(); } /** @@ -75,17 +78,20 @@ public boolean save() { // convert config file to String String stringConfig = this.convertFileToString(file); - // detect which kind of line endings it uses - String lineEnding; - if (stringConfig.contains("\r\n")) lineEnding = "\r\n"; - else lineEnding = "\n"; + // figure out where the header ends + int indexAfterHeader = 0; + Matcher newline = newlinePattern.matcher(stringConfig); + + while (newline.find() && stringConfig.charAt(newline.end()) == '#') { + indexAfterHeader = newline.end(); + } // convert stringConfig to array, ignoring the header - String[] arrayConfig = stringConfig.substring(stringConfig.indexOf(lineEnding) + lineEnding.length()).split(lineEnding); + String[] arrayConfig = stringConfig.substring(indexAfterHeader).split(newline.group()); // begin building the new config, starting with the header StringBuilder newContents = new StringBuilder(); - newContents.append("# ").append(config.options().header()).append(lineEnding); + newContents.append(stringConfig, 0, indexAfterHeader); // This holds the current path the lines are at in the config StringBuilder currentPath = new StringBuilder(); @@ -168,13 +174,13 @@ public boolean save() { String comment = comments.get(currentPath.toString()); if (comment != null && !comment.isEmpty()) { // Add the comment to the beginning of the current line - newLine.insert(0, System.getProperty("line.separator")).insert(0, comment); + newLine.insert(0, "\n").insert(0, comment); if (newLine.charAt(newLine.length() - 1) != ':' && !line.equals(arrayConfig[arrayConfig.length - 1])) - newLine.append(System.getProperty("line.separator")); + newLine.append("\n"); } } - newLine.append(System.getProperty("line.separator")); + newLine.append("\n"); // Add the (modified) line to the total config String newContents.append(newLine.toString()); } @@ -209,7 +215,7 @@ public void addComment(String path, List commentLines) { line = leadingSpaces.toString() + line; } if (commentString.length() > 0) { - commentString.append(System.getProperty("line.separator")); + commentString.append("\n"); } commentString.append(line); } From 4b994349f90acea54898264cfa1d20776fed516b Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Wed, 7 Oct 2020 22:31:27 -0400 Subject: [PATCH 04/14] clean up CommentedYamlConfiguration --- .../util/CommentedYamlConfiguration.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 39302479..0d6afedd 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -15,6 +15,7 @@ import java.io.StringWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.List; import java.util.regex.Matcher; @@ -97,7 +98,7 @@ public boolean save() { StringBuilder currentPath = new StringBuilder(); // This flags if the line is a node or unknown text. - boolean node = false; + boolean node; // The depth of the path. (number of words separated by periods - 1) int depth = 0; @@ -109,7 +110,7 @@ public boolean save() { node = true; // Grab the index of the end of the node name - int index = 0; + int index; index = line.indexOf(": "); if (index < 0) { index = line.length() - 1; @@ -130,7 +131,7 @@ public boolean save() { // Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth if (whiteSpace / 2 > depth) { // Path is deeper. Add a dot and the node name - currentPath.append(".").append(line.substring(whiteSpace, index)); + currentPath.append(".").append(line, whiteSpace, index); depth++; } else if (whiteSpace / 2 < depth) { // Path is shallower, calculate current depth from whitespace (whitespace / 2) and subtract that many levels from the currentPath @@ -148,7 +149,7 @@ public boolean save() { currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); } // Add the new node name to the path - currentPath.append(line.substring(whiteSpace, index)); + currentPath.append(line, whiteSpace, index); // Reset the depth depth = newDepth; } else { @@ -161,14 +162,16 @@ public boolean save() { // If there is a final period, replace everything after it with nothing currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); } - //currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - currentPath.append(line.substring(whiteSpace, index)); + + currentPath.append(line, whiteSpace, index); } } } else { node = false; } + StringBuilder newLine = new StringBuilder(line); + if (node) { // get the comment for the current node String comment = comments.get(currentPath.toString()); @@ -185,13 +188,9 @@ public boolean save() { newContents.append(newLine.toString()); } - try { - // Write the string to the config file - this.stringToFile(newContents.toString(), file); - } catch (IOException e) { - saved = false; - } + saved = this.stringToFile(newContents.toString(), file); } + return saved; } @@ -236,7 +235,7 @@ private String convertFileToString(File file) { try (InputStream is = new FileInputStream(file)) { int n; - Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); + Reader reader = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } @@ -255,12 +254,11 @@ private String convertFileToString(File file) { * @param source String to write. * @param file File to write to. * @return True on success. - * @throws java.io.IOException */ - private boolean stringToFile(String source, File file) throws IOException { + private boolean stringToFile(String source, File file) { OutputStreamWriter out = null; try { - out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); + out = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8); out.write(source); out.close(); return true; From 4659f2ff4f15c9aff3b642a0027c62780b3048b0 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Mon, 14 Dec 2020 15:25:03 -0500 Subject: [PATCH 05/14] remove Configuration-Library --- pom.xml | 15 ------- .../InventoriesConfig.java | 1 - .../YamlWorldGroupManager.java | 1 - .../util/CommentedYamlConfiguration.java | 19 +++----- .../util/EncodedConfiguration.java | 44 ------------------- 5 files changed, 6 insertions(+), 74 deletions(-) delete mode 100644 src/main/java/com/onarandombox/multiverseinventories/util/EncodedConfiguration.java diff --git a/pom.xml b/pom.xml index a6036cf4..794dab52 100644 --- a/pom.xml +++ b/pom.xml @@ -171,10 +171,6 @@ and adjust the build number accordingly --> - - com.feildmaster.lib.configuration - com.onarandombox.multiverseinventories.util - com.dumptruckman.minecraft.util.Logging com.onarandombox.multiverseinventories.utils.InvLogging @@ -229,17 +225,6 @@ and adjust the build number accordingly --> - - com.feildmaster.lib - EnhancedConfiguration - 1.1.3 - - - org.bukkit - bukkit - - - com.onarandombox.multiverseadventure Multiverse-Adventure diff --git a/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java b/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java index 1fb820d4..14a66cae 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java +++ b/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java @@ -123,7 +123,6 @@ private List getComments() { // Load the configuration file into memory config = new CommentedYamlConfiguration(configFile, true); - config.load(); // Sets defaults config values this.setDefaults(); diff --git a/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java b/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java index bb90309e..e70f4417 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java +++ b/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java @@ -43,7 +43,6 @@ final class YamlWorldGroupManager extends AbstractWorldGroupManager { } // Load the configuration file into memory groupsConfig = new CommentedYamlConfiguration(groupConfigFile, true); - groupsConfig.load(); if (migrateGroups) { migrateGroups(config); diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 0d6afedd..70cda57c 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -1,7 +1,7 @@ package com.onarandombox.multiverseinventories.util; -import com.feildmaster.lib.configuration.EnhancedConfiguration; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; import java.io.BufferedReader; import java.io.File; @@ -13,7 +13,6 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.StringWriter; -import java.io.UnsupportedEncodingException; import java.io.Writer; import java.nio.charset.StandardCharsets; import java.util.HashMap; @@ -27,7 +26,7 @@ public final class CommentedYamlConfiguration { private final File file; - private FileConfiguration config = null; + private final FileConfiguration config; private final boolean doComments; private final HashMap comments; private final Pattern newlinePattern = Pattern.compile("\r?\n"); @@ -35,18 +34,12 @@ public final class CommentedYamlConfiguration { public CommentedYamlConfiguration(File file, boolean doComments) { this.file = file; this.doComments = doComments; - comments = new HashMap(); - } + this.comments = new HashMap(); + this.config = new YamlConfiguration(); - /** - * Loads this Configuration object into memory. - */ - public void load() { try { - config = new EncodedConfiguration(file, "UTF-8"); - } catch (UnsupportedEncodingException e) { - config = new EnhancedConfiguration(file); - } + this.config.load(file); + } catch (Exception ignored) {} } /** diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/EncodedConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/EncodedConfiguration.java deleted file mode 100644 index e60fe7d7..00000000 --- a/src/main/java/com/onarandombox/multiverseinventories/util/EncodedConfiguration.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.onarandombox.multiverseinventories.util; - -import com.feildmaster.lib.configuration.EnhancedConfiguration; -import com.google.common.io.Files; -import org.apache.commons.lang.Validate; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.plugin.Plugin; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; -import java.nio.charset.IllegalCharsetNameException; - -public class EncodedConfiguration extends EnhancedConfiguration { - - private final Charset charset; - - public EncodedConfiguration(Plugin plugin, String charset) throws UnsupportedEncodingException, IllegalCharsetNameException { - super(plugin); - this.charset = Charset.forName(charset); - } - - public EncodedConfiguration(String file, Plugin plugin, String charset) throws UnsupportedEncodingException, IllegalCharsetNameException { - super(file, plugin); - this.charset = Charset.forName(charset); - } - - public EncodedConfiguration(File file, String charset) throws UnsupportedEncodingException, IllegalCharsetNameException { - super(file); - this.charset = Charset.forName(charset); - } - - public EncodedConfiguration(File file, Plugin plugin, String charset) throws UnsupportedEncodingException, IllegalCharsetNameException { - super(file, plugin); - this.charset = Charset.forName(charset); - } -} From 4167d3eda885ef356571db84bd0ead391068ba21 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Mon, 14 Dec 2020 15:25:25 -0500 Subject: [PATCH 06/14] always set first_run to false after first run --- .../multiverseinventories/AbstractWorldGroupManager.java | 1 - .../multiverseinventories/MultiverseInventories.java | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/AbstractWorldGroupManager.java b/src/main/java/com/onarandombox/multiverseinventories/AbstractWorldGroupManager.java index 51c9d71b..59ee14f5 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/AbstractWorldGroupManager.java +++ b/src/main/java/com/onarandombox/multiverseinventories/AbstractWorldGroupManager.java @@ -152,7 +152,6 @@ public void createDefaultGroup() { worlds.append(", ").append(defaultEnd.getName()); } updateGroup(worldGroup); - plugin.getMVIConfig().setFirstRun(false); plugin.getMVIConfig().save(); Logging.info("Created a default group for you containing all of your default worlds: " + worlds.toString()); } diff --git a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java index bf2949ef..83d0262a 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java +++ b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java @@ -369,6 +369,8 @@ public void run() { if (getGroupManager().getGroups().isEmpty()) { getGroupManager().createDefaultGroup(); } + + getMVIConfig().setFirstRun(false); } getGroupManager().checkForConflicts(null); } From 5d7657fc0857e984c560a30bf2dc0bbf936f8c20 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Fri, 14 May 2021 23:32:31 -0400 Subject: [PATCH 07/14] add unit test for CommentedYamlConfiguration --- .../TestCommentedYamlConfiguration.java | 105 ++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java diff --git a/src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java b/src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java new file mode 100644 index 00000000..408f4d63 --- /dev/null +++ b/src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java @@ -0,0 +1,105 @@ +package com.onarandombox.multiverseinventories; + +import com.onarandombox.multiverseinventories.util.CommentedYamlConfiguration; +import org.junit.Test; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; + +public class TestCommentedYamlConfiguration { + private static final char LINE_SEPARATOR = '\n'; + private static final String TEST_CONFIG_NAME = "testconfig.yml"; + + private static final String TEST_CONTENTS_1 = "# A Test Yaml File" + LINE_SEPARATOR + LINE_SEPARATOR + + "test: 123" + LINE_SEPARATOR + + "a_map:" + LINE_SEPARATOR + + " something: yep" + LINE_SEPARATOR + + " something_else: 42" + LINE_SEPARATOR + + " one_more_something_else: 24" + LINE_SEPARATOR + + " a_list:" + LINE_SEPARATOR + + " - 1" + LINE_SEPARATOR + + " - 2" + LINE_SEPARATOR + + " a_child_map:" + LINE_SEPARATOR + + " another_map:" + LINE_SEPARATOR + + " test: 123" + LINE_SEPARATOR + + " two_steps_back:" + LINE_SEPARATOR + + " test: true" + LINE_SEPARATOR + + "back_to_root: true" + LINE_SEPARATOR; + + private static final String COMMENTED_TEST_CONTENTS_1 = "# A Test Yaml File" + LINE_SEPARATOR + + LINE_SEPARATOR + + "# Yay" + LINE_SEPARATOR + + "test: 123" + LINE_SEPARATOR + + LINE_SEPARATOR + + "# They seem to be" + LINE_SEPARATOR + + "# Working" + LINE_SEPARATOR + + "a_map:" + LINE_SEPARATOR + + " # Yeah, they're working" + LINE_SEPARATOR + + " something: yep" + LINE_SEPARATOR + + " something_else: 42" + LINE_SEPARATOR + + " one_more_something_else: 24" + LINE_SEPARATOR + + LINE_SEPARATOR + + " # Aww yeah, comments on a list" + LINE_SEPARATOR + + " a_list:" + LINE_SEPARATOR + + " - 1" + LINE_SEPARATOR + + " - 2" + LINE_SEPARATOR + + " a_child_map:" + LINE_SEPARATOR + + " # Comments on a child child map" + LINE_SEPARATOR + + " another_map:" + LINE_SEPARATOR + + " test: 123" + LINE_SEPARATOR + + LINE_SEPARATOR + + " # Two steps back comments" + LINE_SEPARATOR + + " two_steps_back:" + LINE_SEPARATOR + + " test: true" + LINE_SEPARATOR + + LINE_SEPARATOR + + "# Back to root comments" + LINE_SEPARATOR + + "back_to_root: true" + LINE_SEPARATOR; + + private CommentedYamlConfiguration createConfig(boolean doComments) { + try (PrintWriter out = new PrintWriter(TEST_CONFIG_NAME)) { + out.println(TEST_CONTENTS_1); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + CommentedYamlConfiguration testConfig = new CommentedYamlConfiguration(new File(TEST_CONFIG_NAME), doComments); + + testConfig.getConfig().options().header("A Test Yaml File\n"); + + testConfig.addComment("test", Arrays.asList("# Yay")); + testConfig.addComment("a_map", Arrays.asList("# They seem to be", "# Working")); + testConfig.addComment("a_map.something", Arrays.asList("# Yeah, they're working")); + testConfig.addComment("a_map.a_list", Arrays.asList("# Aww yeah, comments on a list")); + testConfig.addComment("a_map.a_child_map.another_map", Arrays.asList("# Comments on a child child map")); + testConfig.addComment("a_map.two_steps_back", Arrays.asList("# Two steps back comments")); + testConfig.addComment("back_to_root", Arrays.asList("# Back to root comments")); + + return testConfig; + } + + @Test + public void testNoComments() throws Exception { + CommentedYamlConfiguration testConfig = createConfig(false); + testConfig.save(); + + String uncommentedConfigFile = new String(Files.readAllBytes(Paths.get(TEST_CONFIG_NAME)), StandardCharsets.UTF_8); + assertEquals(TEST_CONTENTS_1, uncommentedConfigFile); + } + + @Test + public void testWithComments() throws Exception { + CommentedYamlConfiguration testConfig = createConfig(true); + testConfig.save(); + + String commentedConfigFile = new String(Files.readAllBytes(Paths.get(TEST_CONFIG_NAME)), StandardCharsets.UTF_8); + assertEquals(COMMENTED_TEST_CONTENTS_1, commentedConfigFile); + } +} From 01105a945fa986e50ecfddd891b94d343685b14c Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Sat, 15 May 2021 00:10:03 -0400 Subject: [PATCH 08/14] add newlines before comments instead of after --- .../util/CommentedYamlConfiguration.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 70cda57c..4c72e33a 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -171,14 +171,16 @@ public boolean save() { if (comment != null && !comment.isEmpty()) { // Add the comment to the beginning of the current line newLine.insert(0, "\n").insert(0, comment); - if (newLine.charAt(newLine.length() - 1) != ':' && !line.equals(arrayConfig[arrayConfig.length - 1])) - newLine.append("\n"); + char previousChar = newContents.charAt(newContents.length() - 2); + if (previousChar != ':' && previousChar != '\n') { + newLine.insert(0, "\n"); + } } } newLine.append("\n"); // Add the (modified) line to the total config String - newContents.append(newLine.toString()); + newContents.append(newLine); } saved = this.stringToFile(newContents.toString(), file); From 258674a042bd93029f17bbef8ba3186c88c5c95d Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Sat, 15 May 2021 00:38:32 -0400 Subject: [PATCH 09/14] make newline pattern static --- .../util/CommentedYamlConfiguration.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 4c72e33a..f708e017 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -29,7 +29,8 @@ public final class CommentedYamlConfiguration { private final FileConfiguration config; private final boolean doComments; private final HashMap comments; - private final Pattern newlinePattern = Pattern.compile("\r?\n"); + + private static final Pattern NEW_LINE_PATTERN = Pattern.compile("\r?\n"); public CommentedYamlConfiguration(File file, boolean doComments) { this.file = file; @@ -74,7 +75,7 @@ public boolean save() { // figure out where the header ends int indexAfterHeader = 0; - Matcher newline = newlinePattern.matcher(stringConfig); + Matcher newline = NEW_LINE_PATTERN.matcher(stringConfig); while (newline.find() && stringConfig.charAt(newline.end()) == '#') { indexAfterHeader = newline.end(); From 0401d066554b11c3a34f56d9dd3d48f03aeccac5 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Sat, 15 May 2021 00:39:05 -0400 Subject: [PATCH 10/14] only do calculation once --- .../util/CommentedYamlConfiguration.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index f708e017..9453b1d8 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -122,15 +122,16 @@ public boolean save() { break; } } + + int whiteSpaceDividedByTwo = whiteSpace / 2; // Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth - if (whiteSpace / 2 > depth) { + if (whiteSpaceDividedByTwo > depth) { // Path is deeper. Add a dot and the node name currentPath.append(".").append(line, whiteSpace, index); depth++; - } else if (whiteSpace / 2 < depth) { + } else if (whiteSpaceDividedByTwo < depth) { // Path is shallower, calculate current depth from whitespace (whitespace / 2) and subtract that many levels from the currentPath - int newDepth = whiteSpace / 2; - for (int i = 0; i < depth - newDepth; i++) { + for (int i = 0; i < depth - whiteSpaceDividedByTwo; i++) { currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), ""); } // Grab the index of the final period @@ -145,7 +146,7 @@ public boolean save() { // Add the new node name to the path currentPath.append(line, whiteSpace, index); // Reset the depth - depth = newDepth; + depth = whiteSpaceDividedByTwo; } else { // Path is same depth, replace the last path node name to the current node name int lastIndex = currentPath.lastIndexOf("."); From f373a222ade76fe3631a8c42d00b733defc9402f Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Sat, 15 May 2021 00:39:28 -0400 Subject: [PATCH 11/14] don't use StringBuilder's insert --- .../util/CommentedYamlConfiguration.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 9453b1d8..20252f67 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -165,22 +165,28 @@ public boolean save() { node = false; } - StringBuilder newLine = new StringBuilder(line); + StringBuilder newLine = new StringBuilder(); if (node) { // get the comment for the current node String comment = comments.get(currentPath.toString()); if (comment != null && !comment.isEmpty()) { - // Add the comment to the beginning of the current line - newLine.insert(0, "\n").insert(0, comment); + // if the previous line doesn't end in a colon + // and there's not already a newline character, + // add a newline before we add the comment char previousChar = newContents.charAt(newContents.length() - 2); if (previousChar != ':' && previousChar != '\n') { - newLine.insert(0, "\n"); + newLine.append("\n"); } + + // add the comment + newLine.append(comment).append("\n"); } } - newLine.append("\n"); + // add the config line + newLine.append(line).append("\n"); + // Add the (modified) line to the total config String newContents.append(newLine); } From 62d578643429f75b37f3919a17e7cd6f15e52be9 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Sat, 15 May 2021 00:39:58 -0400 Subject: [PATCH 12/14] use StringBuilder instead of String concatenation --- .../util/CommentedYamlConfiguration.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 20252f67..48703df8 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -213,13 +213,12 @@ public void addComment(String path, List commentLines) { } } for (String line : commentLines) { - if (!line.isEmpty()) { - line = leadingSpaces.toString() + line; - } if (commentString.length() > 0) { commentString.append("\n"); } - commentString.append(line); + if (!line.isEmpty()) { + commentString.append(leadingSpaces).append(line); + } } comments.put(path, commentString.toString()); } From be039d8b3e887d8ad05f0f41f2af18a97b56adcc Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Mon, 10 Jan 2022 09:38:52 -0500 Subject: [PATCH 13/14] reduce nesting Co-authored-by: Ben Woo <30431861+benwoo1110@users.noreply.github.com> --- .../util/CommentedYamlConfiguration.java | 227 +++++++++--------- 1 file changed, 112 insertions(+), 115 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java index 48703df8..718b7da6 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java +++ b/src/main/java/com/onarandombox/multiverseinventories/util/CommentedYamlConfiguration.java @@ -57,144 +57,141 @@ public FileConfiguration getConfig() { * @return True if successful. */ public boolean save() { - boolean saved = true; - // Save the config just like normal + // try to save the config file, return false on failure try { config.save(file); } catch (Exception e) { - saved = false; - } - if (!doComments) { - return saved; + return false; } - // if there's comments to add and it saved fine, we need to add comments - if (!comments.isEmpty() && saved) { - // convert config file to String - String stringConfig = this.convertFileToString(file); + // if we're not supposed to add comments, or there aren't any to add, we're done + if (!doComments || comments.isEmpty()) { + return true; + } - // figure out where the header ends - int indexAfterHeader = 0; - Matcher newline = NEW_LINE_PATTERN.matcher(stringConfig); + // convert config file to String + String stringConfig = this.convertFileToString(file); - while (newline.find() && stringConfig.charAt(newline.end()) == '#') { - indexAfterHeader = newline.end(); - } + // figure out where the header ends + int indexAfterHeader = 0; + Matcher newline = NEW_LINE_PATTERN.matcher(stringConfig); - // convert stringConfig to array, ignoring the header - String[] arrayConfig = stringConfig.substring(indexAfterHeader).split(newline.group()); - - // begin building the new config, starting with the header - StringBuilder newContents = new StringBuilder(); - newContents.append(stringConfig, 0, indexAfterHeader); - - // This holds the current path the lines are at in the config - StringBuilder currentPath = new StringBuilder(); - - // This flags if the line is a node or unknown text. - boolean node; - // The depth of the path. (number of words separated by periods - 1) - int depth = 0; - - // Loop through the config lines - for (final String line : arrayConfig) { - // If the line is a node (and not something like a list value) - if (line.contains(": ") || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) { - // This is a node so flag it as one - node = true; - - // Grab the index of the end of the node name - int index; - index = line.indexOf(": "); - if (index < 0) { - index = line.length() - 1; - } - // If currentPath is empty, store the node name as the currentPath. (this is only on the first iteration, i think) - if (currentPath.toString().isEmpty()) { - currentPath = new StringBuilder(line.substring(0, index)); - } else { - // Calculate the whitespace preceding the node name - int whiteSpace = 0; - for (int n = 0; n < line.length(); n++) { - if (line.charAt(n) == ' ') { - whiteSpace++; - } else { - break; - } - } + while (newline.find() && stringConfig.charAt(newline.end()) == '#') { + indexAfterHeader = newline.end(); + } - int whiteSpaceDividedByTwo = whiteSpace / 2; - // Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth - if (whiteSpaceDividedByTwo > depth) { - // Path is deeper. Add a dot and the node name - currentPath.append(".").append(line, whiteSpace, index); - depth++; - } else if (whiteSpaceDividedByTwo < depth) { - // Path is shallower, calculate current depth from whitespace (whitespace / 2) and subtract that many levels from the currentPath - for (int i = 0; i < depth - whiteSpaceDividedByTwo; i++) { - currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), ""); - } - // Grab the index of the final period - int lastIndex = currentPath.lastIndexOf("."); - if (lastIndex < 0) { - // if there isn't a final period, set the current path to nothing because we're at root - currentPath = new StringBuilder(); - } else { - // If there is a final period, replace everything after it with nothing - currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); - } - // Add the new node name to the path - currentPath.append(line, whiteSpace, index); - // Reset the depth - depth = whiteSpaceDividedByTwo; + // convert stringConfig to array, ignoring the header + String[] arrayConfig = stringConfig.substring(indexAfterHeader).split(newline.group()); + + // begin building the new config, starting with the header + StringBuilder newContents = new StringBuilder(); + newContents.append(stringConfig, 0, indexAfterHeader); + + // This holds the current path the lines are at in the config + StringBuilder currentPath = new StringBuilder(); + + // This flags if the line is a node or unknown text. + boolean node; + // The depth of the path. (number of words separated by periods - 1) + int depth = 0; + + // Loop through the config lines + for (final String line : arrayConfig) { + // If the line is a node (and not something like a list value) + if (line.contains(": ") || (line.length() > 1 && line.charAt(line.length() - 1) == ':')) { + // This is a node so flag it as one + node = true; + + // Grab the index of the end of the node name + int index; + index = line.indexOf(": "); + if (index < 0) { + index = line.length() - 1; + } + // If currentPath is empty, store the node name as the currentPath. (this is only on the first iteration, i think) + if (currentPath.toString().isEmpty()) { + currentPath = new StringBuilder(line.substring(0, index)); + } else { + // Calculate the whitespace preceding the node name + int whiteSpace = 0; + for (int n = 0; n < line.length(); n++) { + if (line.charAt(n) == ' ') { + whiteSpace++; } else { - // Path is same depth, replace the last path node name to the current node name - int lastIndex = currentPath.lastIndexOf("."); - if (lastIndex < 0) { - // if there isn't a final period, set the current path to nothing because we're at root - currentPath = new StringBuilder(); - } else { - // If there is a final period, replace everything after it with nothing - currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); - } - - currentPath.append(line, whiteSpace, index); + break; } } - } else { - node = false; - } - StringBuilder newLine = new StringBuilder(); - - if (node) { - // get the comment for the current node - String comment = comments.get(currentPath.toString()); - if (comment != null && !comment.isEmpty()) { - // if the previous line doesn't end in a colon - // and there's not already a newline character, - // add a newline before we add the comment - char previousChar = newContents.charAt(newContents.length() - 2); - if (previousChar != ':' && previousChar != '\n') { - newLine.append("\n"); + int whiteSpaceDividedByTwo = whiteSpace / 2; + // Find out if the current depth (whitespace * 2) is greater/lesser/equal to the previous depth + if (whiteSpaceDividedByTwo > depth) { + // Path is deeper. Add a dot and the node name + currentPath.append(".").append(line, whiteSpace, index); + depth++; + } else if (whiteSpaceDividedByTwo < depth) { + // Path is shallower, calculate current depth from whitespace (whitespace / 2) and subtract that many levels from the currentPath + for (int i = 0; i < depth - whiteSpaceDividedByTwo; i++) { + currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), ""); + } + // Grab the index of the final period + int lastIndex = currentPath.lastIndexOf("."); + if (lastIndex < 0) { + // if there isn't a final period, set the current path to nothing because we're at root + currentPath = new StringBuilder(); + } else { + // If there is a final period, replace everything after it with nothing + currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); + } + // Add the new node name to the path + currentPath.append(line, whiteSpace, index); + // Reset the depth + depth = whiteSpaceDividedByTwo; + } else { + // Path is same depth, replace the last path node name to the current node name + int lastIndex = currentPath.lastIndexOf("."); + if (lastIndex < 0) { + // if there isn't a final period, set the current path to nothing because we're at root + currentPath = new StringBuilder(); + } else { + // If there is a final period, replace everything after it with nothing + currentPath.replace(currentPath.lastIndexOf("."), currentPath.length(), "").append("."); } - // add the comment - newLine.append(comment).append("\n"); + currentPath.append(line, whiteSpace, index); } } + } else { + node = false; + } - // add the config line - newLine.append(line).append("\n"); + StringBuilder newLine = new StringBuilder(); + + if (node) { + // get the comment for the current node + String comment = comments.get(currentPath.toString()); + if (comment != null && !comment.isEmpty()) { + // if the previous line doesn't end in a colon + // and there's not already a newline character, + // add a newline before we add the comment + char previousChar = newContents.charAt(newContents.length() - 2); + if (previousChar != ':' && previousChar != '\n') { + newLine.append("\n"); + } - // Add the (modified) line to the total config String - newContents.append(newLine); + // add the comment + newLine.append(comment).append("\n"); + } } - saved = this.stringToFile(newContents.toString(), file); + // add the config line + newLine.append(line).append("\n"); + + // Add the (modified) line to the total config String + newContents.append(newLine); } - return saved; + // try to save the config file, returning whether it saved successfully + return this.stringToFile(newContents.toString(), file); } /** From a7e274349b4631d5f64b1121455b332f56e932ad Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Mon, 10 Jan 2022 10:18:01 -0500 Subject: [PATCH 14/14] update test config location --- .../TestCommentedYamlConfiguration.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java b/src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java index 408f4d63..04b489f5 100644 --- a/src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java +++ b/src/test/java/com/onarandombox/multiverseinventories/TestCommentedYamlConfiguration.java @@ -6,17 +6,15 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.Paths; import java.util.Arrays; import static org.junit.Assert.assertEquals; public class TestCommentedYamlConfiguration { private static final char LINE_SEPARATOR = '\n'; - private static final String TEST_CONFIG_NAME = "testconfig.yml"; + private static final File TEST_CONFIG = new File("bin/test/testconfig.yml"); private static final String TEST_CONTENTS_1 = "# A Test Yaml File" + LINE_SEPARATOR + LINE_SEPARATOR + "test: 123" + LINE_SEPARATOR + @@ -64,13 +62,14 @@ public class TestCommentedYamlConfiguration { "back_to_root: true" + LINE_SEPARATOR; private CommentedYamlConfiguration createConfig(boolean doComments) { - try (PrintWriter out = new PrintWriter(TEST_CONFIG_NAME)) { + TEST_CONFIG.getParentFile().mkdirs(); + try (PrintWriter out = new PrintWriter(TEST_CONFIG)) { out.println(TEST_CONTENTS_1); } catch (FileNotFoundException e) { e.printStackTrace(); } - CommentedYamlConfiguration testConfig = new CommentedYamlConfiguration(new File(TEST_CONFIG_NAME), doComments); + CommentedYamlConfiguration testConfig = new CommentedYamlConfiguration(TEST_CONFIG, doComments); testConfig.getConfig().options().header("A Test Yaml File\n"); @@ -90,7 +89,7 @@ public void testNoComments() throws Exception { CommentedYamlConfiguration testConfig = createConfig(false); testConfig.save(); - String uncommentedConfigFile = new String(Files.readAllBytes(Paths.get(TEST_CONFIG_NAME)), StandardCharsets.UTF_8); + String uncommentedConfigFile = new String(Files.readAllBytes(TEST_CONFIG.getCanonicalFile().toPath()), StandardCharsets.UTF_8); assertEquals(TEST_CONTENTS_1, uncommentedConfigFile); } @@ -99,7 +98,7 @@ public void testWithComments() throws Exception { CommentedYamlConfiguration testConfig = createConfig(true); testConfig.save(); - String commentedConfigFile = new String(Files.readAllBytes(Paths.get(TEST_CONFIG_NAME)), StandardCharsets.UTF_8); + String commentedConfigFile = new String(Files.readAllBytes(TEST_CONFIG.getCanonicalFile().toPath()), StandardCharsets.UTF_8); assertEquals(COMMENTED_TEST_CONTENTS_1, commentedConfigFile); } }