Improves configuration migration, and fixes some issues
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good
Fixes incorrect dropItems instead of dropItem key in config.yml Adds some missing dropper messages to config.yml Adds proper migration keys for all known configuration options Adds a modified version of Stargate's StargateYamlConfiguration to allow retaining comments during configuration migration. Fixes the annoying "[]" is not a valid repairable item Removes the use of data keys for the global configuration
This commit is contained in:
@ -1,10 +1,23 @@
|
||||
package net.knarcraft.blacksmith.util;
|
||||
|
||||
import net.knarcraft.blacksmith.BlacksmithPlugin;
|
||||
import net.knarcraft.blacksmith.config.StargateYamlConfiguration;
|
||||
import net.knarcraft.knarlib.property.ColorConversion;
|
||||
import net.knarcraft.knarlib.util.FileHelper;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.configuration.MemorySection;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/**
|
||||
* A helper class for getting an object value as the correct type
|
||||
@ -94,4 +107,103 @@ public final class ConfigHelper {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all configuration values from the old name to the new name
|
||||
*
|
||||
* @param currentConfiguration <p>The current config to back up</p>
|
||||
*/
|
||||
public static void migrateConfig(@NotNull String dataFolderPath, @NotNull FileConfiguration currentConfiguration) {
|
||||
BlacksmithPlugin instance = BlacksmithPlugin.getInstance();
|
||||
|
||||
//Save the old config just in case something goes wrong
|
||||
try {
|
||||
currentConfiguration.save(new File(dataFolderPath, "config.yml.old"));
|
||||
} catch (IOException exception) {
|
||||
instance.getLogger().log(Level.WARNING, "Unable to save old backup and do migration");
|
||||
return;
|
||||
}
|
||||
|
||||
//Load old and new configuration
|
||||
instance.reloadConfig();
|
||||
FileConfiguration oldConfiguration = instance.getConfig();
|
||||
InputStream configStream = FileHelper.getInputStreamForInternalFile("/config.yml");
|
||||
if (configStream == null) {
|
||||
instance.getLogger().log(Level.SEVERE, "Could not migrate the configuration, as the internal " +
|
||||
"configuration could not be read!");
|
||||
return;
|
||||
}
|
||||
YamlConfiguration newConfiguration = StargateYamlConfiguration.loadConfiguration(
|
||||
FileHelper.getBufferedReaderFromInputStream(configStream));
|
||||
|
||||
//Read all available config migrations
|
||||
Map<String, String> migrationFields;
|
||||
try {
|
||||
InputStream migrationStream = FileHelper.getInputStreamForInternalFile("/config-migrations.txt");
|
||||
if (migrationStream == null) {
|
||||
instance.getLogger().log(Level.SEVERE, "Could not migrate the configuration, as the internal migration paths could not be read!");
|
||||
return;
|
||||
}
|
||||
migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(migrationStream),
|
||||
"=", ColorConversion.NORMAL);
|
||||
} catch (IOException exception) {
|
||||
instance.getLogger().log(Level.WARNING, "Unable to load config migration file");
|
||||
return;
|
||||
}
|
||||
|
||||
//Replace old config names with the new ones
|
||||
for (String key : migrationFields.keySet()) {
|
||||
if (oldConfiguration.contains(key)) {
|
||||
migrateProperty(migrationFields, key, oldConfiguration);
|
||||
}
|
||||
}
|
||||
|
||||
// Copy all keys to the new config
|
||||
for (String key : StargateYamlConfiguration.getKeysWithoutComments(oldConfiguration, true)) {
|
||||
if (oldConfiguration.get(key) instanceof MemorySection) {
|
||||
continue;
|
||||
}
|
||||
newConfiguration.set(key, oldConfiguration.get(key));
|
||||
}
|
||||
|
||||
try {
|
||||
newConfiguration.save(new File(dataFolderPath, "config.yml"));
|
||||
} catch (IOException exception) {
|
||||
instance.getLogger().log(Level.WARNING, "Unable to save migrated config");
|
||||
}
|
||||
|
||||
instance.reloadConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Migrates one configuration property
|
||||
*
|
||||
* @param migrationFields <p>The configuration fields to be migrated</p>
|
||||
* @param key <p>The key/path of the property to migrate</p>
|
||||
* @param oldConfiguration <p>The original pre-migration configuration</p>
|
||||
*/
|
||||
private static void migrateProperty(@NotNull Map<String, String> migrationFields, @NotNull String key,
|
||||
@NotNull FileConfiguration oldConfiguration) {
|
||||
String newPath = migrationFields.get(key);
|
||||
Object oldValue = oldConfiguration.get(key);
|
||||
if (!newPath.trim().isEmpty()) {
|
||||
if (oldConfiguration.isConfigurationSection(key)) {
|
||||
// Copy each value of a configuration section
|
||||
ConfigurationSection sourceSection = oldConfiguration.getConfigurationSection(key);
|
||||
ConfigurationSection destinationSection = oldConfiguration.createSection(newPath);
|
||||
if (sourceSection == null) {
|
||||
return;
|
||||
}
|
||||
for (String path : StargateYamlConfiguration.getKeysWithoutComments(sourceSection, true)) {
|
||||
destinationSection.set(path, sourceSection.get(path));
|
||||
}
|
||||
} else {
|
||||
// Copy the value to the new path
|
||||
oldConfiguration.set(newPath, oldValue);
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the old path's value
|
||||
oldConfiguration.set(key, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -244,7 +244,7 @@ public final class ItemHelper {
|
||||
//Parse every material, and add to reforgeAble items
|
||||
for (String item : itemList) {
|
||||
//Ignore ",,"
|
||||
if (InputParsingHelper.isEmpty(item)) {
|
||||
if (InputParsingHelper.isEmpty(item) || item.matches("\\[]")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user