Clean up config file writing
This commit is contained in:
parent
6dbb359eb2
commit
be657aada5
@ -7,10 +7,10 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||||||
import org.bukkit.enchantments.Enchantment;
|
import org.bukkit.enchantments.Enchantment;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
@ -98,7 +98,7 @@ public class ConfigLoader
|
|||||||
};
|
};
|
||||||
String[] bStatsComment =
|
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 " +
|
"It has a negligible impact on performance and more users on stats keeps me more motivated " +
|
||||||
"to support this plugin!"
|
"to support this plugin!"
|
||||||
};
|
};
|
||||||
@ -132,7 +132,7 @@ public class ConfigLoader
|
|||||||
};
|
};
|
||||||
String[] permissionsComment =
|
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."
|
"Useful if permissions are unavailable."
|
||||||
};
|
};
|
||||||
String[] craftingInSmithingTableComment =
|
String[] craftingInSmithingTableComment =
|
||||||
@ -221,8 +221,9 @@ public class ConfigLoader
|
|||||||
Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!");
|
Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
|
//noinspection deprecation
|
||||||
Enchantment enchantment = Enchantment.getByKey(key);
|
final NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
|
||||||
|
final Enchantment enchantment = Enchantment.getByKey(key);
|
||||||
if (enchantment == null)
|
if (enchantment == null)
|
||||||
{
|
{
|
||||||
Bukkit.getLogger().warning("The enchantment \"" + fullKey + "\" could not be found!");
|
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.
|
// Write all the config options to the config.yml.
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
File dataFolder = plugin.getDataFolder();
|
final Path dataDir = plugin.getDataFolder().toPath();
|
||||||
if (!dataFolder.exists())
|
if (!Files.exists(dataDir))
|
||||||
dataFolder.mkdir();
|
Files.createDirectory(dataDir);
|
||||||
|
|
||||||
File saveTo = new File(plugin.getDataFolder(), "config.yml");
|
final Path configFile = dataDir.resolve("config.yml");
|
||||||
if (!saveTo.exists())
|
|
||||||
saveTo.createNewFile();
|
final StringBuilder sb = new StringBuilder(6000);
|
||||||
else
|
sb.append("# ").append(HEADER).append('\n');
|
||||||
|
for (final ConfigOption<?> configOption : configOptionsList)
|
||||||
{
|
{
|
||||||
saveTo.delete();
|
if (configOption.hasComment())
|
||||||
saveTo.createNewFile();
|
sb.append('\n');
|
||||||
|
sb.append(configOption).append('\n');
|
||||||
}
|
}
|
||||||
FileWriter fw = new FileWriter(saveTo, true);
|
|
||||||
PrintWriter pw = new PrintWriter(fw);
|
|
||||||
|
|
||||||
if (HEADER != null)
|
Files.write(configFile, sb.toString().getBytes(),
|
||||||
pw.println("# " + HEADER + "\n");
|
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
{
|
{
|
||||||
|
@ -3,6 +3,7 @@ package nl.pim16aap2.armoredElytra.util;
|
|||||||
import nl.pim16aap2.armoredElytra.ArmoredElytra;
|
import nl.pim16aap2.armoredElytra.ArmoredElytra;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/* This class represent a configuration option.
|
/* This class represent a configuration option.
|
||||||
@ -52,11 +53,16 @@ public class ConfigOption<V>
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String[] getComment()
|
public @Nullable String[] getComment()
|
||||||
{
|
{
|
||||||
return comment;
|
return comment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasComment()
|
||||||
|
{
|
||||||
|
return comment != null;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString()
|
public String toString()
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user