Skip to content
This repository has been archived by the owner on May 29, 2022. It is now read-only.

Commit

Permalink
Fix some missing comments (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elier authored Nov 18, 2021
1 parent 0bdbcde commit 478501e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> lines = (ArrayList<String>) 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<String, String> _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()
Expand All @@ -76,4 +75,4 @@ private int getIndentation(String s){
while((s = s.replaceFirst(" ", "")).startsWith(" ")) i++;
return i+1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class YamlUtils {
public static int findKey(List<String> lines, String key) {
Expand All @@ -11,7 +12,7 @@ public static int findKey(List<String> lines, String key) {
List<String> _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)) {
Expand Down

0 comments on commit 478501e

Please sign in to comment.