Adds code for migrating old configuration values
This commit is contained in:
parent
72d33ed7a2
commit
ac6fc430ff
@ -22,6 +22,8 @@ import net.knarcraft.knarlib.formatting.StringFormatter;
|
|||||||
import net.knarcraft.knarlib.formatting.TranslatableMessage;
|
import net.knarcraft.knarlib.formatting.TranslatableMessage;
|
||||||
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
|
import net.knarcraft.knarlib.formatting.TranslatableTimeUnit;
|
||||||
import net.knarcraft.knarlib.formatting.Translator;
|
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 net.knarcraft.knarlib.util.UpdateChecker;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.PluginCommand;
|
import org.bukkit.command.PluginCommand;
|
||||||
@ -35,6 +37,9 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -142,8 +147,12 @@ public class BlacksmithPlugin extends JavaPlugin {
|
|||||||
|
|
||||||
//Copy default config to disk
|
//Copy default config to disk
|
||||||
FileConfiguration fileConfiguration = this.getConfig();
|
FileConfiguration fileConfiguration = this.getConfig();
|
||||||
fileConfiguration.options().copyDefaults(true);
|
|
||||||
this.saveDefaultConfig();
|
this.saveDefaultConfig();
|
||||||
|
if (fileConfiguration.getString("defaults.dropItem") != null) {
|
||||||
|
migrateConfig(fileConfiguration);
|
||||||
|
this.saveConfig();
|
||||||
|
}
|
||||||
|
fileConfiguration.options().copyDefaults(true);
|
||||||
this.reloadConfig();
|
this.reloadConfig();
|
||||||
this.saveConfig();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -103,18 +103,6 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
|||||||
return ConfigHelper.asBoolean(getValue(setting));
|
return ConfigHelper.asBoolean(getValue(setting));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the given value as a double
|
|
||||||
*
|
|
||||||
* <p>This will throw an exception if used for a non-double setting</p>
|
|
||||||
*
|
|
||||||
* @param setting <p>The setting to get the value of</p>
|
|
||||||
* @return <p>The value of the given setting as a double</p>
|
|
||||||
*/
|
|
||||||
public double asDouble(ScrapperSetting setting) {
|
|
||||||
return ConfigHelper.asDouble(getValue(setting));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the value of a setting, using the default if not set
|
* Gets the value of a setting, using the default if not set
|
||||||
*
|
*
|
||||||
@ -137,12 +125,12 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
|||||||
*/
|
*/
|
||||||
private void loadSettings(DataKey root) {
|
private void loadSettings(DataKey root) {
|
||||||
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
||||||
if (!root.keyExists(setting.getChildPath())) {
|
if (!root.keyExists(setting.getPath())) {
|
||||||
//If the setting does not exist in the config file, add it
|
//If the setting does not exist in the config file, add it
|
||||||
root.setRaw(setting.getChildPath(), setting.getDefaultValue());
|
root.setRaw(setting.getPath(), setting.getDefaultValue());
|
||||||
} else {
|
} else {
|
||||||
//Set the setting to the value found in the path
|
//Set the setting to the value found in the path
|
||||||
settings.put(setting, root.getRaw(setting.getChildPath()));
|
settings.put(setting, root.getRaw(setting.getPath()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
src/main/resources/config-migrations.txt
Normal file
2
src/main/resources/config-migrations.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
defaults=blacksmith.defaults
|
||||||
|
global=blacksmith.global
|
Loading…
Reference in New Issue
Block a user