Attempts to clean the migration code a bit
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
This commit is contained in:
@@ -65,13 +65,13 @@ public class StargateYamlConfiguration extends YamlConfiguration {
|
|||||||
* Loads the configuration of the given plugin
|
* Loads the configuration of the given plugin
|
||||||
*
|
*
|
||||||
* @param plugin <p>The plugin to load the configuration from</p>
|
* @param plugin <p>The plugin to load the configuration from</p>
|
||||||
|
* @param copyDefaults <p>Whether to copy default values when loading</p>
|
||||||
* @return <p>The loaded configuration, or null if unsuccessful</p>
|
* @return <p>The loaded configuration, or null if unsuccessful</p>
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
@Nullable
|
@Nullable
|
||||||
public static StargateYamlConfiguration loadConfiguration(@NotNull Plugin plugin) {
|
public static StargateYamlConfiguration loadConfiguration(@NotNull Plugin plugin, boolean copyDefaults) {
|
||||||
StargateYamlConfiguration configuration = new StargateYamlConfiguration();
|
StargateYamlConfiguration configuration = new StargateYamlConfiguration();
|
||||||
configuration.options().copyDefaults(true);
|
configuration.options().copyDefaults(copyDefaults);
|
||||||
try {
|
try {
|
||||||
configuration.load(new File(plugin.getDataFolder(), ConfigHelper.getConfigFile()));
|
configuration.load(new File(plugin.getDataFolder(), ConfigHelper.getConfigFile()));
|
||||||
return configuration;
|
return configuration;
|
||||||
@@ -88,7 +88,6 @@ public class StargateYamlConfiguration extends YamlConfiguration {
|
|||||||
* @param configuration <p>The configuration to save</p>
|
* @param configuration <p>The configuration to save</p>
|
||||||
* @return <p>True if the configuration was successfully saved</p>
|
* @return <p>True if the configuration was successfully saved</p>
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
|
||||||
public static boolean saveConfiguration(@NotNull Plugin plugin, @NotNull FileConfiguration configuration) {
|
public static boolean saveConfiguration(@NotNull Plugin plugin, @NotNull FileConfiguration configuration) {
|
||||||
try {
|
try {
|
||||||
configuration.save(new File(plugin.getDataFolder(), ConfigHelper.getConfigFile()));
|
configuration.save(new File(plugin.getDataFolder(), ConfigHelper.getConfigFile()));
|
||||||
|
@@ -3,12 +3,12 @@ package net.knarcraft.knarlib.util;
|
|||||||
import net.knarcraft.knarlib.config.StargateYamlConfiguration;
|
import net.knarcraft.knarlib.config.StargateYamlConfiguration;
|
||||||
import net.knarcraft.knarlib.property.ColorConversion;
|
import net.knarcraft.knarlib.property.ColorConversion;
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.InvalidConfigurationException;
|
|
||||||
import org.bukkit.configuration.MemorySection;
|
import org.bukkit.configuration.MemorySection;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -65,31 +65,22 @@ public final class ConfigHelper {
|
|||||||
File dataFolder = plugin.getDataFolder();
|
File dataFolder = plugin.getDataFolder();
|
||||||
File config = new File(dataFolder, CONFIG_FILE);
|
File config = new File(dataFolder, CONFIG_FILE);
|
||||||
|
|
||||||
|
// Load the old configuration as a normal configuration to prevent old comments from being retained
|
||||||
|
FileConfiguration oldConfiguration = StargateYamlConfiguration.loadConfiguration(plugin, false);
|
||||||
|
if (oldConfiguration == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//Save the old config just in case something goes wrong
|
//Save the old config just in case something goes wrong
|
||||||
try {
|
try {
|
||||||
StargateYamlConfiguration currentConfiguration = new StargateYamlConfiguration();
|
oldConfiguration.save(new File(dataFolder, BACKUP_CONFIG_FILE));
|
||||||
currentConfiguration.load(config);
|
} catch (IOException exception) {
|
||||||
currentConfiguration.save(new File(dataFolder, BACKUP_CONFIG_FILE));
|
|
||||||
} catch (IOException | InvalidConfigurationException exception) {
|
|
||||||
plugin.getLogger().log(Level.WARNING, "Unable to save old backup and do migration");
|
plugin.getLogger().log(Level.WARNING, "Unable to save old backup and do migration");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Load old and new configuration
|
InputStream configStream = loadInternalFile(plugin, CONFIG_FILE, "internal configuration");
|
||||||
plugin.reloadConfig();
|
|
||||||
FileConfiguration oldConfiguration;
|
|
||||||
try {
|
|
||||||
oldConfiguration = new YamlConfiguration();
|
|
||||||
oldConfiguration.load(config);
|
|
||||||
} catch (IOException | InvalidConfigurationException exception) {
|
|
||||||
plugin.getLogger().log(Level.WARNING, "Unable to load old configuration and do migration");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
InputStream configStream = FileHelper.getInputStreamForInternalFile("/" + CONFIG_FILE);
|
|
||||||
if (configStream == null) {
|
if (configStream == null) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "Could not migrate the configuration, as the internal " +
|
|
||||||
"configuration could not be read!");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
YamlConfiguration newConfiguration = StargateYamlConfiguration.loadConfiguration(
|
YamlConfiguration newConfiguration = StargateYamlConfiguration.loadConfiguration(
|
||||||
@@ -98,10 +89,8 @@ public final class ConfigHelper {
|
|||||||
//Read all available config migrations
|
//Read all available config migrations
|
||||||
Map<String, String> migrationFields;
|
Map<String, String> migrationFields;
|
||||||
try {
|
try {
|
||||||
InputStream migrationStream = FileHelper.getInputStreamForInternalFile("/" + MIGRATION_FILE);
|
InputStream migrationStream = loadInternalFile(plugin, MIGRATION_FILE, "internal migration paths");
|
||||||
if (migrationStream == null) {
|
if (migrationStream == null) {
|
||||||
plugin.getLogger().log(Level.SEVERE, "Could not migrate the configuration, as the internal " +
|
|
||||||
"migration paths could not be read!");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(migrationStream),
|
migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(migrationStream),
|
||||||
@@ -111,7 +100,7 @@ public final class ConfigHelper {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Replace old config names with the new ones
|
// Replace old config names with the new ones
|
||||||
for (String key : migrationFields.keySet()) {
|
for (String key : migrationFields.keySet()) {
|
||||||
if (oldConfiguration.contains(key)) {
|
if (oldConfiguration.contains(key)) {
|
||||||
migrateProperty(migrationFields, key, oldConfiguration);
|
migrateProperty(migrationFields, key, oldConfiguration);
|
||||||
@@ -126,10 +115,8 @@ public final class ConfigHelper {
|
|||||||
newConfiguration.set(key, oldConfiguration.get(key));
|
newConfiguration.set(key, oldConfiguration.get(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// Save merged configuration
|
||||||
newConfiguration.save(config);
|
if (!StargateYamlConfiguration.saveConfiguration(plugin, newConfiguration)) {
|
||||||
} catch (IOException exception) {
|
|
||||||
plugin.getLogger().log(Level.WARNING, "Unable to save migrated config");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,4 +156,25 @@ public final class ConfigHelper {
|
|||||||
oldConfiguration.set(key, null);
|
oldConfiguration.set(key, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a file from the internal plugin .jar
|
||||||
|
*
|
||||||
|
* @param plugin <p>The plugin to load the file from</p>
|
||||||
|
* @param fileName <p>The file to load</p>
|
||||||
|
* @param fileDescription <p>The file description to use in error messages</p>
|
||||||
|
* @return <p>An input stream for the file, or null if the file could not be read</p>
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
private static InputStream loadInternalFile(@NotNull Plugin plugin, @NotNull String fileName,
|
||||||
|
@NotNull String fileDescription) {
|
||||||
|
InputStream configStream = FileHelper.getInputStreamForInternalFile("/" + fileName);
|
||||||
|
if (configStream == null) {
|
||||||
|
plugin.getLogger().log(Level.SEVERE, "Could not migrate the configuration, as the " + fileDescription +
|
||||||
|
" could not be read!");
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return configStream;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user