Clean up config file writing

This commit is contained in:
Pim van der Loos 2021-11-04 13:20:47 +01:00
parent 6dbb359eb2
commit be657aada5
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
2 changed files with 28 additions and 30 deletions

View File

@ -7,10 +7,10 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.enchantments.Enchantment;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
@ -98,7 +98,7 @@ public class ConfigLoader
};
String[] bStatsComment =
{
"Allow this plugin to send (anonymised) stats using bStats. Please consider keeping it enabled.",
"Allow this plugin to send (anonymous) stats using bStats. Please consider keeping it enabled.",
"It has a negligible impact on performance and more users on stats keeps me more motivated " +
"to support this plugin!"
};
@ -132,7 +132,7 @@ public class ConfigLoader
};
String[] permissionsComment =
{
"Globally bypass permissions for wearing and/or crafting amored elytras.",
"Globally bypass permissions for wearing and/or crafting armored elytras.",
"Useful if permissions are unavailable."
};
String[] craftingInSmithingTableComment =
@ -221,8 +221,9 @@ public class ConfigLoader
Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!");
return;
}
NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
Enchantment enchantment = Enchantment.getByKey(key);
//noinspection deprecation
final NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
final Enchantment enchantment = Enchantment.getByKey(key);
if (enchantment == null)
{
Bukkit.getLogger().warning("The enchantment \"" + fullKey + "\" could not be found!");
@ -264,32 +265,23 @@ public class ConfigLoader
// Write all the config options to the config.yml.
try
{
File dataFolder = plugin.getDataFolder();
if (!dataFolder.exists())
dataFolder.mkdir();
final Path dataDir = plugin.getDataFolder().toPath();
if (!Files.exists(dataDir))
Files.createDirectory(dataDir);
File saveTo = new File(plugin.getDataFolder(), "config.yml");
if (!saveTo.exists())
saveTo.createNewFile();
else
final Path configFile = dataDir.resolve("config.yml");
final StringBuilder sb = new StringBuilder(6000);
sb.append("# ").append(HEADER).append('\n');
for (final ConfigOption<?> configOption : configOptionsList)
{
saveTo.delete();
saveTo.createNewFile();
if (configOption.hasComment())
sb.append('\n');
sb.append(configOption).append('\n');
}
FileWriter fw = new FileWriter(saveTo, true);
PrintWriter pw = new PrintWriter(fw);
if (HEADER != null)
pw.println("# " + HEADER + "\n");
for (int idx = 0; idx < configOptionsList.size(); ++idx)
pw.println(configOptionsList.get(idx).toString() +
// Only print an additional newLine if the next config option has a comment.
(idx < configOptionsList.size() - 1 &&
configOptionsList.get(idx + 1).getComment() == null ? "" : "\n"));
pw.flush();
pw.close();
Files.write(configFile, sb.toString().getBytes(),
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
}
catch (IOException e)
{

View File

@ -3,6 +3,7 @@ package nl.pim16aap2.armoredElytra.util;
import nl.pim16aap2.armoredElytra.ArmoredElytra;
import org.bukkit.configuration.file.FileConfiguration;
import javax.annotation.Nullable;
import java.util.List;
/* This class represent a configuration option.
@ -52,11 +53,16 @@ public class ConfigOption<V>
return value;
}
public String[] getComment()
public @Nullable String[] getComment()
{
return comment;
}
public boolean hasComment()
{
return comment != null;
}
@Override
public String toString()
{