Adds code for migrating old configuration values
This commit is contained in:
@ -22,6 +22,8 @@ import net.knarcraft.knarlib.formatting.StringFormatter;
|
||||
import net.knarcraft.knarlib.formatting.TranslatableMessage;
|
||||
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
|
||||
import net.knarcraft.knarlib.formatting.Translator;
|
||||
import net.knarcraft.knarlib.property.ColorConversion;
|
||||
import net.knarcraft.knarlib.util.FileHelper;
|
||||
import net.knarcraft.knarlib.util.UpdateChecker;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
@ -35,6 +37,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
@ -142,8 +147,12 @@ public class BlacksmithPlugin extends JavaPlugin {
|
||||
|
||||
//Copy default config to disk
|
||||
FileConfiguration fileConfiguration = this.getConfig();
|
||||
fileConfiguration.options().copyDefaults(true);
|
||||
this.saveDefaultConfig();
|
||||
if (fileConfiguration.getString("defaults.dropItem") != null) {
|
||||
migrateConfig(fileConfiguration);
|
||||
this.saveConfig();
|
||||
}
|
||||
fileConfiguration.options().copyDefaults(true);
|
||||
this.reloadConfig();
|
||||
this.saveConfig();
|
||||
|
||||
@ -253,4 +262,48 @@ public class BlacksmithPlugin extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all configuration values from the old name to the new name
|
||||
*
|
||||
* @param fileConfiguration <p>The config to read from and write to</p>
|
||||
*/
|
||||
private void migrateConfig(FileConfiguration fileConfiguration) {
|
||||
//Save the old config just in case something goes wrong
|
||||
try {
|
||||
fileConfiguration.save(getDataFolder() + "/config.yml.old");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
//Read all available config migrations
|
||||
Map<String, String> migrationFields = null;
|
||||
try {
|
||||
InputStream inputStream = FileHelper.getInputStreamForInternalFile("/config-migrations.txt");
|
||||
if (inputStream != null) {
|
||||
migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(inputStream),
|
||||
"=", ColorConversion.NORMAL);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
if (migrationFields == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
//Replace old config names with the new ones
|
||||
for (String key : migrationFields.keySet()) {
|
||||
if (fileConfiguration.contains(key)) {
|
||||
String newPath = migrationFields.get(key);
|
||||
Object oldValue = fileConfiguration.get(key);
|
||||
if (!newPath.trim().isEmpty()) {
|
||||
fileConfiguration.set(newPath, oldValue);
|
||||
}
|
||||
fileConfiguration.set(key, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user