From cb3801bc7040b866a08300f86cbb5a1854b27f55 Mon Sep 17 00:00:00 2001 From: Kermina Awad Date: Mon, 10 Jan 2022 13:00:49 -0500 Subject: [PATCH] only add comments to config files when needed --- .../multiverseinventories/InventoriesConfig.java | 12 +++++++----- .../MultiverseInventories.java | 3 +-- .../YamlWorldGroupManager.java | 15 +++++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java b/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java index 14a66cae..e6b0e0fe 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java +++ b/src/main/java/com/onarandombox/multiverseinventories/InventoriesConfig.java @@ -104,8 +104,8 @@ private List getComments() { } } - private CommentedYamlConfiguration config; - private MultiverseInventories plugin; + private final CommentedYamlConfiguration config; + private final MultiverseInventories plugin; InventoriesConfig(MultiverseInventories plugin) throws IOException { this.plugin = plugin; @@ -114,15 +114,17 @@ private List getComments() { Logging.fine("Created data folder."); } - // Check if the config file exists. If not, create it. + // Check if the config file exists. If not, create it. File configFile = new File(plugin.getDataFolder(), "config.yml"); - if (!configFile.exists()) { + boolean configFileExists = configFile.exists(); + if (!configFileExists) { Logging.fine("Created config file."); configFile.createNewFile(); } // Load the configuration file into memory - config = new CommentedYamlConfiguration(configFile, true); + boolean supportsCommentsNatively = Integer.parseInt(this.plugin.getServer().getBukkitVersion().split("\\.")[1]) > 17; + config = new CommentedYamlConfiguration(configFile, !configFileExists || !supportsCommentsNatively); // Sets defaults config values this.setDefaults(); diff --git a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java index 83d0262a..d3d37d1c 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java +++ b/src/main/java/com/onarandombox/multiverseinventories/MultiverseInventories.java @@ -342,8 +342,7 @@ public InventoriesConfig getMVIConfig() { public void reloadConfig() { try { this.config = new InventoriesConfig(this); - this.worldGroupManager = new YamlWorldGroupManager(this, new File(getDataFolder(), "groups.yml"), - ((InventoriesConfig) config).getConfig()); + this.worldGroupManager = new YamlWorldGroupManager(this, this.config.getConfig()); this.worldProfileContainerStore = new WeakProfileContainerStore(this, ContainerType.WORLD); this.groupProfileContainerStore = new WeakProfileContainerStore(this, ContainerType.GROUP); diff --git a/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java b/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java index e70f4417..4ee11e2d 100644 --- a/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java +++ b/src/main/java/com/onarandombox/multiverseinventories/YamlWorldGroupManager.java @@ -29,20 +29,23 @@ final class YamlWorldGroupManager extends AbstractWorldGroupManager { }}); private final CommentedYamlConfiguration groupsConfig; + private final File groupsConfigFile; - YamlWorldGroupManager(final MultiverseInventories inventories, final File groupConfigFile, final Configuration config) - throws IOException { + YamlWorldGroupManager(final MultiverseInventories inventories, final Configuration config) throws IOException { super(inventories); + this.groupsConfigFile = new File(plugin.getDataFolder(), "groups.yml"); - // Check if the group config file exists. If not, create it and migrate group data. + // Check if the group config file exists. If not, create it and migrate group data. + boolean groupsConfigFileExists = this.groupsConfigFile.exists(); boolean migrateGroups = false; - if (!groupConfigFile.exists()) { + if (!groupsConfigFile.exists()) { Logging.fine("Created groups file."); - groupConfigFile.createNewFile(); + groupsConfigFile.createNewFile(); migrateGroups = true; } // Load the configuration file into memory - groupsConfig = new CommentedYamlConfiguration(groupConfigFile, true); + boolean supportsCommentsNatively = Integer.parseInt(this.plugin.getServer().getBukkitVersion().split("\\.")[1]) > 17; + groupsConfig = new CommentedYamlConfiguration(groupsConfigFile, !groupsConfigFileExists || !supportsCommentsNatively); if (migrateGroups) { migrateGroups(config);