diff --git a/pom.xml b/pom.xml index 5bd9ed5ab..979a6cea5 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.gmail.goosius SiegeWar - 0.7.1 + 0.7.2 siegewar diff --git a/src/main/java/com/gmail/goosius/siegewar/settings/CommentedConfiguration.java b/src/main/java/com/gmail/goosius/siegewar/settings/CommentedConfiguration.java deleted file mode 100644 index da00f0c21..000000000 --- a/src/main/java/com/gmail/goosius/siegewar/settings/CommentedConfiguration.java +++ /dev/null @@ -1,233 +0,0 @@ -package com.gmail.goosius.siegewar.settings; - -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.YamlConfiguration; -import org.bukkit.configuration.file.YamlConstructor; -import org.bukkit.configuration.file.YamlRepresenter; -import org.yaml.snakeyaml.DumperOptions; -import org.yaml.snakeyaml.Yaml; -import org.yaml.snakeyaml.representer.Representer; - -import com.gmail.goosius.siegewar.utils.FileMgmt; - -import java.io.File; -import java.io.IOException; -import java.util.HashMap; - -/** - * @author dumptruckman & Articdive - */ -public class CommentedConfiguration extends YamlConfiguration { - private HashMap comments; - private File file; - - private final DumperOptions yamlOptions = new DumperOptions(); - private final Representer yamlRepresenter = new YamlRepresenter(); - private final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions); - - public CommentedConfiguration(File file) { - - super(); - comments = new HashMap<>(); - this.file = file; - } - - public boolean load() { - - boolean loaded = true; - - try { - this.load(file); - } catch (InvalidConfigurationException | IOException e) { - loaded = false; - } - - return loaded; - } - - public void save() { - - try { - // Spigot 1.18.1 added SnakeYaml's ability to use Comments in yaml. - // They have it enabled by default, we need to stop it happening. - yamlOptions.setProcessComments(false); - } catch (NoSuchMethodError ignored) {} - - boolean saved = true; - - // Save the config just like normal - try { - this.save(file); - - } catch (Exception e) { - saved = false; - } - - // 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 = FileMgmt.convertFileToString(file).split("[" + System.getProperty("line.separator") + "]"); - - // This will hold the newly formatted line - StringBuilder newContents = new StringBuilder(); - // This holds the current path the lines are at in the config - String currentPath = ""; - // 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 (String line : yamlContents) { - // 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.isEmpty()) { - currentPath = 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; - } - } - // 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 - currentPath += "." + line.substring(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 - int newDepth = whiteSpace / 2; - for (int i = 0; i < depth - newDepth; i++) { - currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - } - // 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 = ""; - } else { - // If there is a final period, replace everything after it with nothing - currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - currentPath += "."; - } - // Add the new node name to the path - currentPath += line.substring(whiteSpace, index); - // Reset the depth - depth = newDepth; - } 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 = ""; - } else { - // If there is a final period, replace everything after it with nothing - currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - currentPath += "."; - } - //currentPath = currentPath.replace(currentPath.substring(currentPath.lastIndexOf(".")), ""); - currentPath += line.substring(whiteSpace, index); - - } - - } - - } else { - node = false; - } - - if (node) { - // If there's a comment for the current path, retrieve it and flag that path as already commented - String comment = comments.get(currentPath); - - if (comment != null) { - // Add the comment to the beginning of the current line - line = comment + System.getProperty("line.separator") + line + System.getProperty("line.separator"); - } else { - // Add a new line as it is a node, but has no comment - line += System.getProperty("line.separator"); - } - } - // Add the (modified) line to the total config String - if (!node) { - newContents.append(line).append(System.getProperty("line.separator")); - } else { - newContents.append(line); - } - } - - /* - * Due to a Bukkit Bug with the Configuration - * we just need to remove any extra comments at the start of a file. - */ - while (newContents.toString().startsWith(" " + System.getProperty("line.separator"))) { - newContents = new StringBuilder(newContents.toString().replaceFirst(" " + System.getProperty("line.separator"), "")); - } - FileMgmt.stringToFile(newContents.toString(), file); - } - } - - /** - * 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. - */ - public void addComment(String path, String... commentLines) { - - StringBuilder commentstring = new StringBuilder(); - StringBuilder leadingSpaces = new StringBuilder(); - for (int n = 0; n < path.length(); n++) { - if (path.charAt(n) == '.') { - leadingSpaces.append(" "); - } - } - for (String line : commentLines) { - if (!line.isEmpty()) { - line = leadingSpaces + line; - } else { - line = " "; - } - if (commentstring.length() > 0) { - commentstring.append(System.getProperty("line.separator")); - } - commentstring.append(line); - } - comments.put(path, commentstring.toString()); - } - - @SuppressWarnings("deprecation") - @Override - public String saveToString() { - yamlOptions.setIndent(options().indent()); - yamlOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - yamlOptions.setWidth(10000); - yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); - - - String dump = yaml.dump(getValues(false)); - - - if (dump.equals(BLANK_CONFIG)) { - dump = ""; - } - - return dump; - } -} diff --git a/src/main/java/com/gmail/goosius/siegewar/settings/Settings.java b/src/main/java/com/gmail/goosius/siegewar/settings/Settings.java index f4c69ed29..8165f523f 100644 --- a/src/main/java/com/gmail/goosius/siegewar/settings/Settings.java +++ b/src/main/java/com/gmail/goosius/siegewar/settings/Settings.java @@ -7,6 +7,7 @@ import com.gmail.goosius.siegewar.utils.FileMgmt; import com.gmail.goosius.siegewar.utils.SiegeWarBattleSessionUtil; +import com.palmergames.bukkit.config.CommentedConfiguration; import com.palmergames.util.TimeTools; public class Settings { @@ -61,7 +62,7 @@ public static void loadConfig(String filepath, String version) throws Exception File file = new File(filepath); // read the config.yml into memory - config = new CommentedConfiguration(file); + config = new CommentedConfiguration(file.toPath()); if (!config.load()) throw new IOException("Failed to load Config!"); @@ -98,7 +99,7 @@ public static String getLastRunVersion(String currentVersion) { */ private static void setDefaults(String version, File file) { - newConfig = new CommentedConfiguration(file); + newConfig = new CommentedConfiguration(file.toPath()); newConfig.load(); for (ConfigNodes root : ConfigNodes.values()) { diff --git a/src/main/java/com/gmail/goosius/siegewar/settings/Translation.java b/src/main/java/com/gmail/goosius/siegewar/settings/Translation.java index 3f5cf1126..f8d848f20 100644 --- a/src/main/java/com/gmail/goosius/siegewar/settings/Translation.java +++ b/src/main/java/com/gmail/goosius/siegewar/settings/Translation.java @@ -1,6 +1,7 @@ package com.gmail.goosius.siegewar.settings; import com.gmail.goosius.siegewar.SiegeWar; +import com.palmergames.bukkit.config.CommentedConfiguration; import com.palmergames.bukkit.towny.command.HelpMenu; import com.palmergames.bukkit.util.Colors; import com.palmergames.util.StringMgmt; @@ -29,10 +30,10 @@ public static void loadLanguage(String filepath, String defaultRes) throws Excep File file = FileMgmt.unpackResourceFile(fullPath, res, defaultRes); // read the (language).yml into memory - language = new CommentedConfiguration(file); + language = new CommentedConfiguration(file.toPath()); language.load(); HelpMenu.loadMenus(); - CommentedConfiguration newLanguage = new CommentedConfiguration(file); + CommentedConfiguration newLanguage = new CommentedConfiguration(file.toPath()); try { newLanguage.loadFromString(FileMgmt.convertStreamToString("/" + res));