From 478501eeabe52874fce1e7f514ccb8fc4c74ea08 Mon Sep 17 00:00:00 2001 From: Elier Date: Wed, 17 Nov 2021 21:52:29 -0500 Subject: [PATCH] Fix some missing comments (#257) --- .../java/me/elier/nachospigot/config/NachoConfig.java | 2 +- .../me/elier/nachospigot/config/NachoWorldConfig.java | 2 +- .../sugarcane/util/yaml/YamlCommenter.java | 11 +++++------ .../sugarcanemc/sugarcane/util/yaml/YamlUtils.java | 3 ++- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java b/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java index 7f620e4ee..7c9973144 100644 --- a/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java +++ b/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoConfig.java @@ -164,7 +164,7 @@ static void readConfig(Class clazz, Object instance) { try { config.save(CONFIG_FILE); - c.saveComments(CONFIG_FILE); + c.saveComments(CONFIG_FILE, instance != null); } catch (IOException ex) { LOGGER.log(Level.ERROR, "Could not save " + CONFIG_FILE, ex); } diff --git a/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoWorldConfig.java b/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoWorldConfig.java index ab85cf02d..b9bddf968 100644 --- a/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoWorldConfig.java +++ b/NachoSpigot-Server/src/main/java/me/elier/nachospigot/config/NachoWorldConfig.java @@ -80,7 +80,7 @@ static void loadComments() { addComment("entity.entity-activation", "Enables active ticks for entities"); addComment("entity.endermite-spawning", "Enables endermite spawning."); addComment("infinite-water-sources", "Enables infinite water sources"); - addComment("explosions.constant-explosions", "Changes the radius of explosions to be constant."); + addComment("explosions.constant-radius", "Changes the radius of explosions to be constant."); addComment("explosions.explode-protected-regions", "Toggles whether explosions should explode protected regions"); addComment("explosions.reduced-density-rays", "Toggles whether the server should use reduced rays when calculating density"); addComment("tick-enchantment-tables", "Toggles whether enchantment tables should be ticked"); diff --git a/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlCommenter.java b/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlCommenter.java index 2539c8f33..3e96745c6 100644 --- a/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlCommenter.java +++ b/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlCommenter.java @@ -38,20 +38,19 @@ public void setHeader(String header) { * Saves comments to config file * * @param file File to save to + * @param loadWorldConfig Whether to load world configuration comments * @throws IOException io */ - public void saveComments(File file) throws IOException { + public void saveComments(File file, boolean loadWorldConfig) throws IOException { ArrayList lines = (ArrayList) Files.readAllLines(file.toPath()); lines.removeIf(s -> s.trim().startsWith("#") || s.trim().length() <= 4); lines.add(0, "# " + Header.replace("\n", "\n# ") + "\n"); for (Map.Entry _comment : comments.entrySet()) { + if (!loadWorldConfig && _comment.getKey().startsWith("world-settings.")) continue; // World settings are initialized after NachoConfig, while NachoConfig loads their comments. + // This causes an issue on new configs, with comments not working for world settings, and causing an exception due to being unable to find the configuration option. int line = YamlUtils.findKey(lines, _comment.getKey()); if(line == -1) { - // TODO: This should be fixed soon. World settings are initialized after NachoConfig, while NachoConfig loads their comments. - // This causes an issue, with comments not working for world settings, and potentially causing a OOB exception. - if (_comment.getKey().startsWith("world-settings.")) continue; - throw new IllegalStateException(String.format( "You are trying to add a comment to key \"%s\" which does not exist!", _comment.getKey() @@ -76,4 +75,4 @@ private int getIndentation(String s){ while((s = s.replaceFirst(" ", "")).startsWith(" ")) i++; return i+1; } -} \ No newline at end of file +} diff --git a/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlUtils.java b/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlUtils.java index 9601cf75b..2d6c147c0 100644 --- a/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlUtils.java +++ b/NachoSpigot-Server/src/main/java/org/sugarcanemc/sugarcane/util/yaml/YamlUtils.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class YamlUtils { public static int findKey(List lines, String key) { @@ -11,7 +12,7 @@ public static int findKey(List lines, String key) { List _cpath = new ArrayList<>(); for (String part : parts) { _cpath.add(part); - for (int i = _line; i < lines.size(); i++) { + for (int i = 0; i < lines.size(); i++) { if (lines.get(i).trim().startsWith(part)) { _line = i; if (String.join(".", _cpath).equals(key)) {